data_structures
[ class tree: data_structures ] [ index: data_structures ] [ all elements ]

Source for file Matrix.class.php

Documentation is available at Matrix.class.php

  1. <?php
  2. /**
  3.  * For including this file you have to define the constant "CLASSPATH".
  4.  * Because every include in the framework depends on the CLASSPATH definition.
  5.  * The CLASSPATH means the relative path to the folder that contains the
  6.  * framework GilliGan.
  7.  *
  8.  * @package data_structures
  9.  */
  10. if!defined"CLASSPATH" ) )
  11. {
  12.   echo "<h3>You have to define the constant CLASSPATH!</h3>\r\n";
  13.   echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\r\n";
  14.   exit();
  15. }
  16.  
  17. /**
  18.  *
  19.  */
  20. require_onceCLASSPATH."core/Arrays.class.php" );
  21.  
  22. /**
  23.  * @package data_structures
  24.  *
  25.  * @version   0.1.0
  26.  * @since     0.9.1.8
  27.  * @author    Daniel Plücken <daniel@debakel.net>
  28.  * @license   http://www.gnu.org/copyleft/lesser.html
  29.  *             GNU Lesser General Public License
  30.  * @copyright Copyright (C) 2004 Daniel Plücken <daniel@debakel.net>
  31.  *
  32.  *  This library is free software; you can redistribute it and/or
  33.  *  modify it under the terms of the GNU Lesser General Public
  34.  *  License as published by the Free Software Foundation; either
  35.  *  version 2.1 of the License.
  36.  *
  37.  *  This library is distributed in the hope that it will be useful,
  38.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  39.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40.  *  GNU Lesser General Public License for more details.
  41.  *
  42.  *  You should have received a copy of the GNU Lesser General
  43.  *  Public License along with this library; if not, write to the
  44.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  45.  *  Boston, MA 02111-1307 USA
  46.  */
  47. class Matrix
  48. {
  49.     /**
  50.      * The number of rows in this matrix.
  51.      * 
  52.      * @access public
  53.      * @var    integer $int_rows 
  54.      */
  55.     var $int_rows = 0;
  56.     /**
  57.      * The number of cols in this matrix.
  58.      * 
  59.      * @access public
  60.      * @var    integer $int_cols 
  61.      */
  62.     var $int_cols = 0;
  63.     /**
  64.      * The stored matrix data.
  65.      * 
  66.      * @access public
  67.      * @var    array  $dbl_m_arr 
  68.      */
  69.     var $dbl_m_arr = array();
  70.  
  71.  
  72.  
  73.    /**
  74.     * Constructor
  75.     *
  76.     * @version 1.0
  77.     * @since   0.1.0
  78.     * @author  Daniel Plücken <daniel@debakel.net>
  79.     * @access  public
  80.     * @param   integer $rows 
  81.     * @param   integer $cols 
  82.     * @return  void 
  83.     */
  84.    function Matrix$rows 0$cols )
  85.    {
  86.       $this->int_rows = $rows;
  87.       $this->int_cols = $cols;
  88.    }
  89.  
  90.  
  91.  
  92.  
  93.    /**
  94.     * Uses a arra to generate this matrix.
  95.     *
  96.     * @version 1.0
  97.     * @since   0.1.0
  98.     * @static
  99.     * @author  Daniel Plücken <daniel@debakel.net>
  100.     * @access  public
  101.     * @param   array $data_arr 
  102.     * @return  boolean 
  103.     */
  104.    function inputArray&$data_arr )
  105.    {
  106.       if !is_array$data_arr[0) )
  107.          return false;
  108.       
  109.       $this->int_rows = count$data_arr );
  110.       $this->int_cols = count$data_arr[0);
  111.       
  112.       $this->dbl_m_arr =$data_arr;
  113.       
  114.       return true;
  115.    }
  116.  
  117.  
  118.  
  119.    /**
  120.     * Summates two matrices and returns an object of the sum matrix.
  121.     *
  122.     * @version 1.0
  123.     * @since   0.1.0
  124.     * @static
  125.     * @author  Daniel Plücken <daniel@debakel.net>
  126.     * @access  public
  127.     * @param   Matrix $a_obj 
  128.     * @param   Matrix $b_obj 
  129.     * @return  false|Matrix
  130.     */
  131.    function summate&$a_obj&$b_obj )
  132.    {
  133.        if (
  134.             !is_array$a_obj->dbl_m_arr )
  135.          || !is_array$b_obj->dbl_m_arr )
  136.          || !is_array$a_obj->dbl_m_arr[0)
  137.          || !is_array$b_obj->dbl_m_arr[0)
  138.          || count$a_obj->dbl_m_arr != count$b_obj->dbl_m_arr )
  139.          || count$a_obj->dbl_m_arr[0!= count$b_obj->dbl_m_arr[0)
  140.           )
  141.           return false;
  142.        
  143.        $out_obj new Matrix(
  144.                               count$a_obj->dbl_m_arr ),
  145.                               count$a_obj->dbl_m_arr[0)
  146.                             );
  147.        
  148.        for $i 0$i count$a_obj->dbl_m_arr )$i++ )
  149.            for $k 0$k count$b_obj->dbl_m_arr[$i)$k++ )
  150.                $out_obj->dbl_m_arr[$i][$k]
  151.              = $a_obj->dbl_m_arr[$i][$k$b_obj->dbl_m_arr[$i][$k];
  152.        
  153.        return $out_obj;
  154.    }
  155.  
  156.  
  157.  
  158.    /**
  159.     * Scales this matrix by the given value.
  160.     *
  161.     * @version 1.0
  162.     * @since   0.1.0
  163.     * @author  Daniel Plücken <daniel@debakel.net>
  164.     * @access  public
  165.     * @param   double $dbl_factor 
  166.     * @return  void 
  167.     */
  168.    function scale$dbl_factor )
  169.    {
  170.        for $i 0$i count$this->dbl_m_arr )$i++ )
  171.            for $k 0$k count$this->dbl_m_arr[$i)$k++ )
  172.                $this->dbl_m_arr[$i][$k*= $dbl_factor;
  173.    }
  174.  
  175.  
  176.  
  177.    /**
  178.     * Swaps two rows in this matrix. The row count begins at 1 (one)! This is an
  179.     * elemantary matrix row operation.
  180.     *
  181.     * @version 1.0
  182.     * @since   0.1.0
  183.     * @author  Daniel Plücken <daniel@debakel.net>
  184.     * @access  public
  185.     * @param   integer $int_row_a 
  186.     * @param   integer $int_row_b 
  187.     * @return  void 
  188.     */
  189.    function swapRows$int_row_a$int_row_b )
  190.    {
  191.        if $int_row_a == $int_row_b )
  192.           return false;
  193.        
  194.        $dbl_help_arr $this->dbl_m_arr$int_row_a-];
  195.        $this->dbl_m_arr$int_row_a-$this->dbl_m_arr$int_row_b-];
  196.        $this->dbl_m_arr$int_row_b-$dbl_help_arr;
  197.        
  198.        return true;
  199.    }
  200.  
  201.  
  202.  
  203.    /**
  204.     * Scales a row in this matrix. The row count begins at 1 (one)! This is an
  205.     * elemantary matrix row operation.
  206.     *
  207.     * @version 1.0
  208.     * @since   0.1.0
  209.     * @author  Daniel Plücken <daniel@debakel.net>
  210.     * @access  public
  211.     * @param   double  $dbl_factor 
  212.     * @param   integer $int_row 
  213.     * @return  boolean 
  214.     */
  215.    function scaleRow$dbl_factor$int_row )
  216.    {
  217.        if (
  218.             !is_numeric $dbl_factor )
  219.          || $dbl_factor == 0
  220.          || $dbl_factor == 0.0
  221.           )
  222.           return false;
  223.        
  224.        foreach $this->dbl_m_arr$int_row-as $key => $val )
  225.           $this->dbl_m_arr$int_row-]$key *= $dbl_factor;
  226.        
  227.        return true;
  228.    }
  229.  
  230.  
  231.  
  232.    /**
  233.     * Scales a row virtually and adds it to another given row in this matrix.
  234.     * The row count begins at 1 (one)! This is an elemantary matrix row
  235.     * operation.
  236.     *
  237.     * @version 1.0
  238.     * @since   0.1.0
  239.     * @author  Daniel Plücken <daniel@debakel.net>
  240.     * @access  public
  241.     * @param   double  $dbl_factor The scale factor.
  242.     * @param   integer $int_srow   Row to scale.
  243.     * @param   integer $int_row    Row which the scaled row will add with.
  244.     * @return  boolean 
  245.     */
  246.    function addScaledRowToAnother$dbl_factor$int_srow$int_row )
  247.    {
  248.        if $int_srow == $int_row )
  249.           return false;
  250.        
  251.        $tmp_arr array();
  252.        foreach $this->dbl_m_arr$int_srow-as $key => $val )
  253.           $tmp_arr$key $val $dbl_factor;
  254.        
  255.        foreach $tmp_arr as $key => $val )
  256.           $this->dbl_m_arr$int_row-]$key += $val;
  257.        
  258.        return true;
  259.    }
  260.  
  261.  
  262.  
  263.  
  264.    /**
  265.     * Converts this matrix into a reduced row echelon form using the Gaussian
  266.     * elimination.
  267.     *
  268.     * @todo    implementing
  269.     * @version 1.0
  270.     * @since   0.1.0
  271.     * @author  Daniel Plücken <daniel@debakel.net>
  272.     * @access  public
  273.     * @param   Matrix $a_obj 
  274.     * @param   Matrix $b_obj 
  275.     * @return  false|Matrix
  276.     */
  277.    {
  278.       $int_pivo_row 0;
  279.       for (
  280.             $k 0;
  281.             $k count$this->dbl_m_arr[0)
  282.          && $int_pivo_row count$this->dbl_m_arr );
  283.             $k++
  284.           )
  285.       {
  286.           # proof case one
  287.           $int_mem = -1;
  288.           for $i $int_pivo_row$i count$this->dbl_m_arr )$i++ )
  289.               if !($bool_continue $this->dbl_m_arr[$i][$k== 0) )
  290.               {
  291.                  $int_mem $i;
  292.                  // The left site til column $k+1 of the matrix is NOT in
  293.                  // reduced row echelon form.
  294.                  break;
  295.               }
  296.           
  297.           if $bool_continue )
  298.              // The left site til column $k+1 of the matrix IS in reduced row
  299.              // echelon form, so we can continue with the next column.
  300.              continue;
  301.           
  302.           # proof case two
  303.           if $this->dbl_m_arr[$int_pivo_row][$k== )
  304.              $this->swapRows$int_pivo_row+1$int_mem+);
  305.           
  306.           # handle case three
  307.           $dbl_factor 1/$this->dbl_m_arr[$int_pivo_row][$k];
  308.           $this->scaleRow$dbl_factor$int_pivo_row+);
  309.           for $i 0$i count$this->dbl_m_arr )$i++ )
  310.               if $i != $int_pivo_row )
  311.                  $this->addScaledRowToAnother(
  312.                                    -$this->dbl_m_arr[$i][$k],
  313.                                     $int_pivo_row+1$i+1
  314.                                              );
  315.           
  316.           $int_pivo_row++;
  317.       }
  318.    }
  319.  
  320.  
  321.  
  322.  
  323.    /**
  324.     * Multiplies two matrices and returns an object of the product matrix.
  325.     *
  326.     * @version 1.0
  327.     * @since   0.1.0
  328.     * @static
  329.     * @author  Daniel Plücken <daniel@debakel.net>
  330.     * @access  public
  331.     * @param   Matrix $a_obj 
  332.     * @param   Matrix $b_obj 
  333.     * @return  false|Matrix
  334.     */
  335.    function multiply&$a_obj&$b_obj )
  336.    {
  337.        if (
  338.             !is_array$a_obj->dbl_m_arr[0)
  339.          || !is_array$b_obj->dbl_m_arr )
  340.          || count$a_obj->dbl_m_arr[0!= count$b_obj->dbl_m_arr )
  341.           )
  342.           return false;
  343.        
  344.        $out_obj new Matrix(
  345.                               count$a_obj->dbl_m_arr ),
  346.                               count$b_obj->dbl_m_arr[0)
  347.                             );
  348.        
  349.        for $i 0$i count$a_obj->dbl_m_arr )$i++ )
  350.            for $k 0$k count$b_obj->dbl_m_arr[0)$k++ )
  351.            {
  352.                $out_obj->dbl_m_arr[$i][$k0;
  353.                for $l 0$l count$b_obj->dbl_m_arr )$l++ )
  354.        // OR   for ( $l = 0; $l < count( $a_obj->dbl_m_arr[$i] ); $l++ )
  355.                        $out_obj->dbl_m_arr[$i][$k]
  356.                     += $a_obj->dbl_m_arr[$i][$l$b_obj->dbl_m_arr[$l][$k];
  357.            }
  358.                    
  359.        
  360.        return $out_obj;
  361.    }
  362.  
  363.  
  364.  
  365.    /**
  366.     * Returns an object of this class which all elements on the diagonal have
  367.     * the value 1 (one) and the other elements have the value 0 (zero).
  368.     *
  369.     * @version 1.0
  370.     * @since   0.1.0
  371.     * @static
  372.     * @author  Daniel Plücken <daniel@debakel.net>
  373.     * @access  public
  374.     * @param   integer $rows_cols 
  375.     * @return  Matrix 
  376.     */
  377.     function getIdentityMatrix$rows_cols )
  378.     {
  379.        $out_obj new Matrix$rows_cols$rows_cols );
  380.        for $i 0$i $rows_cols$i++ )
  381.            for $k 0$k $rows_cols$k++ )
  382.                if $i == $k )
  383.                   $out_obj->dbl_m_arr[$i][$k1;
  384.                else
  385.                   $out_obj->dbl_m_arr[$i][$k0;
  386.        
  387.        return $out_obj;
  388.     }
  389. // End of class Matrix
  390. ?>

Documentation generated on Thu, 05 Jun 2008 19:13:44 +0200 by phpDocumentor 1.4.1