Source for file Matrix.class.php
Documentation is available at Matrix.class.php
* For including this file you have to define the constant "CLASSPATH".
* Because every include in the framework depends on the CLASSPATH definition.
* The CLASSPATH means the relative path to the folder that contains the
* @package data_structures
echo "<h3>You have to define the constant CLASSPATH!</h3>\r\n";
echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\r\n";
require_once( CLASSPATH. "core/Arrays.class.php" );
* @package data_structures
* @author Daniel Plücken <daniel@debakel.net>
* @license http://www.gnu.org/copyleft/lesser.html
* GNU Lesser General Public License
* @copyright Copyright (C) 2004 Daniel Plücken <daniel@debakel.net>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
* The number of rows in this matrix.
* The number of cols in this matrix.
* The stored matrix data.
* @author Daniel Plücken <daniel@debakel.net>
function Matrix( $rows = 0, $cols = 0 )
* Uses a arra to generate this matrix.
* @author Daniel Plücken <daniel@debakel.net>
* Summates two matrices and returns an object of the sum matrix.
* @author Daniel Plücken <daniel@debakel.net>
function summate( &$a_obj, &$b_obj )
|| count( $a_obj->dbl_m_arr ) != count( $b_obj->dbl_m_arr )
|| count( $a_obj->dbl_m_arr[0] ) != count( $b_obj->dbl_m_arr[0] )
count( $a_obj->dbl_m_arr ),
count( $a_obj->dbl_m_arr[0] )
for ( $i = 0; $i < count( $a_obj->dbl_m_arr ); $i++ )
for ( $k = 0; $k < count( $b_obj->dbl_m_arr[$i] ); $k++ )
$out_obj->dbl_m_arr[$i][$k]
= $a_obj->dbl_m_arr[$i][$k] + $b_obj->dbl_m_arr[$i][$k];
* Scales this matrix by the given value.
* @author Daniel Plücken <daniel@debakel.net>
* @param double $dbl_factor
function scale( $dbl_factor )
* Swaps two rows in this matrix. The row count begins at 1 (one)! This is an
* elemantary matrix row operation.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $int_row_a
* @param integer $int_row_b
function swapRows( $int_row_a, $int_row_b )
if ( $int_row_a == $int_row_b )
$dbl_help_arr = $this->dbl_m_arr[ $int_row_a- 1 ];
$this->dbl_m_arr[ $int_row_b- 1 ] = $dbl_help_arr;
* Scales a row in this matrix. The row count begins at 1 (one)! This is an
* elemantary matrix row operation.
* @author Daniel Plücken <daniel@debakel.net>
* @param double $dbl_factor
* @param integer $int_row
function scaleRow( $dbl_factor, $int_row )
foreach ( $this->dbl_m_arr[ $int_row- 1 ] as $key => $val )
$this->dbl_m_arr[ $int_row- 1 ][ $key ] *= $dbl_factor;
* Scales a row virtually and adds it to another given row in this matrix.
* The row count begins at 1 (one)! This is an elemantary matrix row
* @author Daniel Plücken <daniel@debakel.net>
* @param double $dbl_factor The scale factor.
* @param integer $int_srow Row to scale.
* @param integer $int_row Row which the scaled row will add with.
if ( $int_srow == $int_row )
foreach ( $this->dbl_m_arr[ $int_srow- 1 ] as $key => $val )
$tmp_arr[ $key ] = $val * $dbl_factor;
foreach ( $tmp_arr as $key => $val )
$this->dbl_m_arr[ $int_row- 1 ][ $key ] += $val;
* Converts this matrix into a reduced row echelon form using the Gaussian
* @author Daniel Plücken <daniel@debakel.net>
if ( !($bool_continue = $this->dbl_m_arr[$i][$k] == 0) )
// The left site til column $k+1 of the matrix is NOT in
// reduced row echelon form.
// The left site til column $k+1 of the matrix IS in reduced row
// echelon form, so we can continue with the next column.
if ( $this->dbl_m_arr[$int_pivo_row][$k] == 0 )
$this->swapRows( $int_pivo_row+ 1, $int_mem+ 1 );
$dbl_factor = 1/ $this->dbl_m_arr[$int_pivo_row][$k];
$this->scaleRow( $dbl_factor, $int_pivo_row+ 1 );
if ( $i != $int_pivo_row )
* Multiplies two matrices and returns an object of the product matrix.
* @author Daniel Plücken <daniel@debakel.net>
|| count( $a_obj->dbl_m_arr[0] ) != count( $b_obj->dbl_m_arr )
count( $a_obj->dbl_m_arr ),
count( $b_obj->dbl_m_arr[0] )
for ( $i = 0; $i < count( $a_obj->dbl_m_arr ); $i++ )
for ( $k = 0; $k < count( $b_obj->dbl_m_arr[0] ); $k++ )
$out_obj->dbl_m_arr[$i][$k] = 0;
for ( $l = 0; $l < count( $b_obj->dbl_m_arr ); $l++ )
// OR for ( $l = 0; $l < count( $a_obj->dbl_m_arr[$i] ); $l++ )
$out_obj->dbl_m_arr[$i][$k]
+= $a_obj->dbl_m_arr[$i][$l] * $b_obj->dbl_m_arr[$l][$k];
* Returns an object of this class which all elements on the diagonal have
* the value 1 (one) and the other elements have the value 0 (zero).
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $rows_cols
$out_obj = new Matrix( $rows_cols, $rows_cols );
for ( $i = 0; $i < $rows_cols; $i++ )
for ( $k = 0; $k < $rows_cols; $k++ )
$out_obj->dbl_m_arr[$i][$k] = 1;
$out_obj->dbl_m_arr[$i][$k] = 0;
|