cvs: pear /Math_Matrix Matrix.php MatrixOp.php

From: Date: Fri, 24 May 2002 18:35:14 +0000
Subject: cvs: pear /Math_Matrix Matrix.php MatrixOp.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-4074@lists.php.net to get a copy of this message
jmcastagnetto Fri May 24 14:35:14 2002 EDT Modified files: /pear/Math_Matrix Matrix.php MatrixOp.php Log: Added new methods to main classes

Index: pear/Math_Matrix/Matrix.php diff -u pear/Math_Matrix/Matrix.php:1.1 pear/Math_Matrix/Matrix.php:1.2 --- pear/Math_Matrix/Matrix.php:1.1 Thu May 23 05:43:50 2002 +++ pear/Math_Matrix/Matrix.php Fri May 24 14:35:14 2002 @@ -18,7 +18,7 @@ // // Matrix definition and manipulation package // -// $Id: Matrix.php,v 1.1 2002/05/23 09:43:50 jmcastagnetto Exp $ +// $Id: Matrix.php,v 1.2 2002/05/24 18:35:14 jmcastagnetto Exp $ // require_once 'PEAR.php'; @@ -164,7 +164,7 @@ }/*}}}*/ function isEmpty() {/*{{{*/ - return ( $this->_data == null ); + return ( empty($this->_data) || is_null($this->_data) ); }/*}}}*/ @@ -176,14 +176,14 @@ * @return mixed an array of integers on success, a PEAR_Error object otherwise */ function getSize() { - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); else return array($this->_num_rows, $this->_num_cols); } function setElement($row, $col, $value) { - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); if ($row >= $this->_num_rows && $col >= $this->_num_cols) return PEAR::raiseError('Incorrect row and column values'); @@ -194,7 +194,7 @@ } function getElement($row, $col) { - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); if ($row >= $this->_num_rows && $col >= $this->_num_cols) return PEAR::raiseError('Incorrect row and column values'); @@ -202,7 +202,7 @@ } function getRow ($row) {/*{{{*/ - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); if ($row >= $this->_num_rows) return PEAR::raiseError('Incorrect row value'); @@ -210,7 +210,7 @@ }/*}}}*/ function getCol ($col) {/*{{{*/ - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); if ($col >= $this->_num_cols) return PEAR::raiseError('Incorrect column value'); @@ -220,7 +220,7 @@ }/*}}}*/ function setRow ($row, $arr) {/*{{{*/ - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); if ($row >= $this->_num_rows) return PEAR::raiseError('Incorrect row value'); @@ -234,7 +234,7 @@ }/*}}}*/ function setCol ($col, $arr) {/*{{{*/ - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); if ($col >= $this->_num_cols) return PEAR::raiseError('Incorrect column value'); @@ -254,35 +254,35 @@ }/*}}}*/ function getData () {/*{{{*/ - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); else return $this->_data; }/*}}}*/ function getMin () {/*{{{*/ - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); else return $this->_min; }/*}}}*/ function getMax () {/*{{{*/ - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); else return $this->_max; }/*}}}*/ function getMinMax () {/*{{{*/ - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); else return array ($this->_min, $this->_max); }/*}}}*/ function getValueIndex ($val) {/*{{{*/ - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); for ($i=0; $i < $this->_num_rows; $i++) for ($j=0; $j < $this->_num_cols; $j++) @@ -292,33 +292,33 @@ }/*}}}*/ function getMinIndex () {/*{{{*/ - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); return $this->getValueIndex($this->_min); }/*}}}*/ function getMaxIndex () {/*{{{*/ - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); return $this->getValueIndex($this->_max); }/*}}}*/ function getMinMaxIndex () {/*{{{*/ - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); else return array_merge($this->getMinIndex(), $this->getMaxIndex()); }/*}}}*/ function isSquare () {/*{{{*/ - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); else return $this->_square; }/*}}}*/ function toString ($format="%4s") {/*{{{*/ - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); $out = ""; for ($i=0; $i < $this->_num_rows; $i++) { @@ -330,7 +330,7 @@ }/*}}}*/ function toHTML() {/*{{{*/ - if (is_null($this->_data)) + if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); $out = "<table border>\n\t<caption align=\"top\"><b>Matrix</b>"; $out .= "</caption>\n\t<tr align=\"center\">\n\t\t<th>"; @@ -347,7 +347,7 @@ return $out."\n</table>\n"; }/*}}}*/ -} // end of Math_Matrix class/*}}}*/ +} // end of Math_Matrix class /*}}}*/ // vim: ts=4:sw=4:et: // vim6: fdl=1: Index: pear/Math_Matrix/MatrixOp.php diff -u pear/Math_Matrix/MatrixOp.php:1.1 pear/Math_Matrix/MatrixOp.php:1.2 --- pear/Math_Matrix/MatrixOp.php:1.1 Thu May 23 05:43:50 2002 +++ pear/Math_Matrix/MatrixOp.php Fri May 24 14:35:14 2002 @@ -18,9 +18,10 @@ // // Matrix manipulation class // -// $Id: MatrixOp.php,v 1.1 2002/05/23 09:43:50 jmcastagnetto Exp $ +// $Id: MatrixOp.php,v 1.2 2002/05/24 18:35:14 jmcastagnetto Exp $ // +require_once "Math/Vector/VectorOp.php"; /** * Matrix operations class @@ -72,6 +73,14 @@ return (get_class($matrix) == "math_matrix"); }/*}}}*/ + function isMatrix2x2 (&$matrix) {/*{{{*/ + return (get_class($matrix) == "math_matrix2x2"); + }/*}}}*/ + + function isMatrix3x3 (&$matrix) {/*{{{*/ + return (get_class($matrix) == "math_matrix3x3"); + }/*}}}*/ + function &makeMatrix ($nrows, $ncols, $value) {/*{{{*/ for ($i=0; $i<$nrows; $i++) $m[$i] = explode(":",substr(str_repeat($value.":",$ncols),0,-1)); @@ -127,6 +136,55 @@ return new Math_Matrix($out); }/*}}}*/ + function &multiply(&$m1, &$m2) {/*{{{*/ + if (!Math_MatrixOp::isMatrix($m1) || !Math_MatrixOp::isMatrix($m2)) + return PEAR::raiseError ("Wrong parameters, expected 2 Math_Matrix objects"); + list($nr1, $nc1) = $m1->getSize(); + list($nr2, $nc2) = $m2->getSize(); + if ($nc1 != $nr2) + return PEAR::raiseError("Incompatible sizes columns in m1 must be the same as rows in m2"); + for ($i=0; $i < $nr1; $i++) { + $row = $m1->getRow($i); + for ($j=0; $j < $nc2; $j++) { + $col = $m2->getCol($j); + $n = count($col); + $sum = 0; + for ($k=0; $k < $n; $k++) + $sum += $row[$k] * $col[$k]; + $res[$i][$j] = $sum; + } + } + $Class = get_class($m1); + return new $Class($res); + }/*}}}*/ + + function &vectorMultiply(&$m1, &$v1) {/*{{{*/ + // check that the vector classes are defined + $classes = get_declared_classes(); + if (!in_array("math_vector", $classes) || !in_array("math_vectopop", $classes)) + return PEAR::raiseError ("Classes Math_Vector and Math_VectorOp undefined". + " add \"require_once 'Math/Vector/Vector.php'\" to your script"); + if (!Math_MatrixOp::isMatrix($m1) || !Math_VectorOp::isVector($v1)) + return PEAR::raiseError ("Wrong parameters, expected a Math_Matrix object". + " and a Math_Vector object"); + list($nr1, $nc1) = $m1->getSize(); + $nv = $v1->length(); + if ($nc1 != $nv) + return PEAR::raiseError("Incompatible sizes columns in matrix must ". + "be the same as the number of elements in the vector"); + for ($i=0; $i < $nr1; $i++) { + $row = $m1->getRow($i); + for ($j=0; $j < $nv; $j++) { + $e = $v1->get($j); + $sum = 0; + for ($k=0; $k < $nc1; $k++) + $sum += $row[$k] * $e; + $res[$j] = $sum; + } + } + return new Math_Vector($res); + }/*}}}*/ + function &getSubMatrix (&$m1, $tlrow, $tlcol, $nrows, $ncols) {/*{{{*/ if (!Math_MatrixOp::isMatrix($m1) || !is_numeric($tlrow) || !is_numeric($tlcol) || !is_numeric($nrows) || !is_numeric($ncols)) @@ -144,6 +202,34 @@ for ($i=0; $i < $nc; $i++) $data[$i] = $m1->getCol($i); return new Math_Matrix($data); + }/*}}}*/ + + function &inverse(&$m) {/*{{{*/ + if (!Math_MatrixOp::isMatrix3x3($m)) + return PEAR::raiseError("Inverse implemented only for Math_Matrix3x3 objects"); + $d = $m->determinant(); + if ($d == 0) + return PEAR::raiseError("Matrix cannot be inverted, determinant = 0"); + $data[0][0] = ($m->getElement(1,1) * $m->getElement(2,2) - + $m->getElement(1,2) * $m->getElement(2,1)) / $d; + $data[0][1] = -1 * ($m->getElement(0,1) * $m->getElement(2,2) - + $m->getElement(0,2) * $m->getElement(2,0)) / $d; + $data[0][2] = ($m->getElement(0,1) * $m->getElement(1,2) - + $m->getElement(0,2) * $m->getElement(1,1)) / $d; + $data[1][0] = -1 *($m->getElement(1,0) * $m->getElement(2,2) - + $m->getElement(1,2) * $m->getElement(2,0)) / $d; + $data[1][1] = ($m->getElement(0,0) * $m->getElement(2,2) - + $m->getElement(0,2) * $m->getElement(2,0)) / $d; + $data[1][2] = -1 *($m->getElement(0,0) * $m->getElement(1,2) - + $m->getElement(0,2) * $m->getElement(1,0)) / $d; + $data[2][0] = ($m->getElement(1,0) * $m->getElement(2,1) - + $m->getElement(1,1) * $m->getElement(2,0)) / $d; + $data[2][1] = -1 *($m->getElement(0,0) * $m->getElement(2,1) - + $m->getElement(0,1) * $m->getElement(2,0)) / $d; + $data[2][2] = ($m->getElement(0,0) * $m->getElement(1,1) - + $m->getElement(0,1) * $m->getElement(1,0)) / $d; + return new Matrix3x3($data); + }/*}}}*/ function &swapRows ($m1, $row1, $row2) {/*{{{*/
« previous php.pear.cvs (#4074) next »