ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
Matrix Class Reference
+ Collaboration diagram for Matrix:

Public Member Functions

 __construct ()
 Polymorphic constructor.
 getArray ()
 getArray
 getArrayCopy ()
 getArrayCopy
 constructWithCopy ($A)
 constructWithCopy Construct a matrix from a copy of a 2-D array.
 getColumnPackedCopy ()
 getColumnPackedCopy
 getRowPackedCopy ()
 getRowPackedCopy
 getRowDimension ()
 getRowDimension
 getColumnDimension ()
 getColumnDimension
 get ($i=null, $j=null)
 get
 getMatrix ()
 getMatrix
 setMatrix ()
 setMatrix
 checkMatrixDimensions ($B=null)
 checkMatrixDimensions
 set ($i=null, $j=null, $c=null)
 set
 identity ($m=null, $n=null)
 identity
 diagonal ($m=null, $n=null, $c=1)
 diagonal
 filled ($m=null, $n=null, $c=0)
 filled
 random ($m=null, $n=null, $a=RAND_MIN, $b=RAND_MAX)
 random
 packed ()
 packed
 getMatrixByRow ($i0=null, $iF=null)
 getMatrixByRow
 getMatrixByCol ($j0=null, $jF=null)
 getMatrixByCol
 transpose ()
 transpose
 norm1 ()
 norm1
 norm2 ()
 norm2
 normInf ()
 normInf
 normF ()
 normF
 rank ()
 Matrix rank.
 cond ()
 Matrix condition (2 norm)
 trace ()
 trace
 uminus ()
 uminus
 plus ()
 plus
 plusEquals ()
 plusEquals
 minus ()
 minus
 minusEquals ()
 minusEquals
 arrayTimes ()
 arrayTimes
 arrayTimesEquals ()
 arrayTimesEquals
 arrayRightDivide ()
 arrayRightDivide
 arrayRightDivideEquals ()
 arrayRightDivideEquals
 arrayLeftDivide ()
 arrayLeftDivide
 arrayLeftDivideEquals ()
 arrayLeftDivideEquals
 times ()
 times
 power ()
 power
 concat ()
 concat
 chol ()
 chol
 lu ()
 lu
 qr ()
 qr
 eig ()
 eig
 svd ()
 svd
 solve ($B)
 Solve A*X = B.
 inverse ()
 Matrix inverse or pseudoinverse.
 det ()
 det
 mprint ($A, $format="%01.2f", $width=2)
 Older debugging utility for backwards compatability.
 toHTML ($width=2)
 Debugging utility.

Data Fields

 $A = array()

Private Attributes

 $m
 $n

Detailed Description

Definition at line 36 of file Matrix.php.

Constructor & Destructor Documentation

Matrix::__construct ( )

Polymorphic constructor.

As PHP has no support for polymorphic constructors, we hack our own sort of polymorphism using func_num_args, func_get_arg, and gettype. In essence, we're just implementing a simple RTTI filter and calling the appropriate constructor.

Definition at line 68 of file Matrix.php.

References $m, $n, ArrayLengthException, JAMAError(), n, and PolymorphicArgumentException.

{
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
switch($match) {
//Square matrix - n x n
case 'integer':
$this->m = $args[0];
$this->n = $args[0];
$this->A = array_fill(0, $this->m, array_fill(0, $this->n, 0));
break;
//Rectangular matrix - m x n
case 'integer,integer':
$this->m = $args[0];
$this->n = $args[1];
$this->A = array_fill(0, $this->m, array_fill(0, $this->n, 0));
break;
//Rectangular matrix constant-filled - m x n filled with c
case 'integer,integer,integer':
$this->m = $args[0];
$this->n = $args[1];
$this->A = array_fill(0, $this->m, array_fill(0, $this->n, $args[2]));
break;
//Rectangular matrix constant-filled - m x n filled with c
case 'integer,integer,double':
$this->m = $args[0];
$this->n = $args[1];
$this->A = array_fill(0, $this->m, array_fill(0, $this->n, $args[2]));
break;
//Rectangular matrix - m x n initialized from 2D array
case 'array':
$this->m = count($args[0]);
$this->n = count($args[0][0]);
$this->A = $args[0];
break;
//Rectangular matrix - m x n initialized from 2D array
case 'array,integer,integer':
$this->m = $args[1];
$this->n = $args[2];
$this->A = $args[0];
break;
//Rectangular matrix - m x n initialized from packed array
case 'array,integer':
$this->m = $args[1];
if ($this->m != 0) {
$this->n = count($args[0]) / $this->m;
} else {
$this->n = 0;
}
if (($this->m * $this->n) == count($args[0])) {
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
$this->A[$i][$j] = $args[0][$i + $j * $this->m];
}
}
} else {
}
break;
default:
break;
}
} else {
}
} // function __construct()

+ Here is the call graph for this function:

Member Function Documentation

Matrix::arrayLeftDivide ( )

arrayLeftDivide

Element-by-element Left division A / B

Parameters
Matrix$BMatrix B
Returns
Matrix Division result

Definition at line 1001 of file Matrix.php.

References $m, $n, ArgumentTypeException, checkMatrixDimensions(), JAMAError(), and PolymorphicArgumentException.

{
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
break;
case 'array':
$M = new Matrix($args[0]);
break;
default:
break;
}
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
$M->set($i, $j, $M->get($i, $j) / $this->A[$i][$j]);
}
}
return $M;
} else {
}
} // function arrayLeftDivide()

+ Here is the call graph for this function:

Matrix::arrayLeftDivideEquals ( )

arrayLeftDivideEquals

Element-by-element Left division Aij = Aij / Bij

Parameters
mixed$BMatrix/Array
Returns
Matrix Matrix Aij

Definition at line 1038 of file Matrix.php.

References $m, $n, ArgumentTypeException, checkMatrixDimensions(), JAMAError(), and PolymorphicArgumentException.

{
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
break;
case 'array':
$M = new Matrix($args[0]);
break;
default:
break;
}
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
$this->A[$i][$j] = $M->get($i, $j) / $this->A[$i][$j];
}
}
return $M;
} else {
}
} // function arrayLeftDivideEquals()

+ Here is the call graph for this function:

Matrix::arrayRightDivide ( )

arrayRightDivide

Element-by-element right division A / B

Parameters
Matrix$BMatrix B
Returns
Matrix Division result

Definition at line 927 of file Matrix.php.

References $m, $n, ArgumentTypeException, checkMatrixDimensions(), JAMAError(), and PolymorphicArgumentException.

{
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
break;
case 'array':
$M = new Matrix($args[0]);
break;
default:
break;
}
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
$M->set($i, $j, $this->A[$i][$j] / $M->get($i, $j));
}
}
return $M;
} else {
}
} // function arrayRightDivide()

+ Here is the call graph for this function:

Matrix::arrayRightDivideEquals ( )

arrayRightDivideEquals

Element-by-element right division Aij = Aij / Bij

Parameters
mixed$BMatrix/Array
Returns
Matrix Matrix Aij

Definition at line 964 of file Matrix.php.

References $m, $n, ArgumentTypeException, checkMatrixDimensions(), JAMAError(), and PolymorphicArgumentException.

{
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
break;
case 'array':
$M = new Matrix($args[0]);
break;
default:
break;
}
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
$this->A[$i][$j] = $this->A[$i][$j] / $M->get($i, $j);
}
}
return $M;
} else {
}
} // function arrayRightDivideEquals()

+ Here is the call graph for this function:

Matrix::arrayTimes ( )

arrayTimes

Element-by-element multiplication Cij = Aij * Bij

Parameters
mixed$BMatrix/Array
Returns
Matrix Matrix Cij

Definition at line 853 of file Matrix.php.

References $m, $n, ArgumentTypeException, checkMatrixDimensions(), JAMAError(), and PolymorphicArgumentException.

{
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
break;
case 'array':
$M = new Matrix($args[0]);
break;
default:
break;
}
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
$M->set($i, $j, $M->get($i, $j) * $this->A[$i][$j]);
}
}
return $M;
} else {
}
} // function arrayTimes()

+ Here is the call graph for this function:

Matrix::arrayTimesEquals ( )

arrayTimesEquals

Element-by-element multiplication Aij = Aij * Bij

Parameters
mixed$BMatrix/Array
Returns
Matrix Matrix Aij

Definition at line 890 of file Matrix.php.

References $m, $n, ArgumentTypeException, checkMatrixDimensions(), JAMAError(), and PolymorphicArgumentException.

{
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
break;
case 'array':
$M = new Matrix($args[0]);
break;
default:
break;
}
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
$this->A[$i][$j] *= $M->get($i, $j);
}
}
return $this;
} else {
}
} // function arrayTimesEquals()

+ Here is the call graph for this function:

Matrix::checkMatrixDimensions (   $B = null)

checkMatrixDimensions

Is matrix B the same size?

Parameters
Matrix$BMatrix B
Returns
boolean

Definition at line 405 of file Matrix.php.

References ArgumentTypeException, JAMAError(), MatrixDimensionException, and n.

Referenced by arrayLeftDivide(), arrayLeftDivideEquals(), arrayRightDivide(), arrayRightDivideEquals(), arrayTimes(), arrayTimesEquals(), concat(), minus(), minusEquals(), plus(), plusEquals(), and power().

{
if ($B instanceof Matrix) {
if (($this->m == $B->getRowDimension()) && ($this->n == $B->getColumnDimension())) {
return true;
} else {
}
} else {
}
} // function checkMatrixDimensions()

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Matrix::chol ( )

chol

Cholesky decomposition

Returns
Matrix Cholesky decomposition

Definition at line 1236 of file Matrix.php.

{
return new CholeskyDecomposition($this);
} // function chol()
Matrix::concat ( )

concat

A = A & B

Parameters
mixed$BMatrix/Array
Returns
Matrix Sum

Definition at line 1201 of file Matrix.php.

References $m, $n, ArgumentTypeException, checkMatrixDimensions(), JAMAError(), and PolymorphicArgumentException.

{
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
case 'array':
$M = new Matrix($args[0]);
break;
default:
break;
}
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
// $this->A[$i][$j] = '"'.trim($this->A[$i][$j],'"').trim($M->get($i, $j),'"').'"';
$this->A[$i][$j] = trim($this->A[$i][$j],'"').trim($M->get($i, $j),'"');
}
}
return $this;
} else {
}
} // function concat()

+ Here is the call graph for this function:

Matrix::cond ( )

Matrix condition (2 norm)

Returns
ratio of largest to smallest singular value.

Definition at line 669 of file Matrix.php.

{
$svd = new SingularValueDecomposition($this);
return $svd->cond();
} // function cond ()
Matrix::constructWithCopy (   $A)

constructWithCopy Construct a matrix from a copy of a 2-D array.

Parameters
doubleA[][] Two-dimensional array of doubles.
Exceptions
IllegalArgumentExceptionAll rows must have the same length

Definition at line 165 of file Matrix.php.

References $A, $m, $n, JAMAError(), n, and RowLengthException.

Referenced by TestMatrix\TestMatrix().

{
$this->m = count($A);
$this->n = count($A[0]);
$newCopyMatrix = new Matrix($this->m, $this->n);
for ($i = 0; $i < $this->m; ++$i) {
if (count($A[$i]) != $this->n) {
}
for ($j = 0; $j < $this->n; ++$j) {
$newCopyMatrix->A[$i][$j] = $A[$i][$j];
}
}
return $newCopyMatrix;
} // function constructWithCopy()

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Matrix::det ( )

det

Calculate determinant

Returns
float Determinant

Definition at line 1318 of file Matrix.php.

{
$L = new LUDecomposition($this);
return $L->det();
} // function det()
Matrix::diagonal (   $m = null,
  $n = null,
  $c = 1 
)

diagonal

Generate a diagonal matrix

Parameters
int$mRow dimension
int$nColumn dimension
mixed$cDiagonal value
Returns
Matrix Diagonal matrix

Definition at line 468 of file Matrix.php.

References $m, and $n.

Referenced by identity().

{
$R = new Matrix($m, $n);
for($i = 0; $i < $m; ++$i) {
$R->set($i, $i, $c);
}
return $R;
} // function diagonal()

+ Here is the caller graph for this function:

Matrix::eig ( )

eig

Eigenvalue decomposition

Returns
Matrix Eigenvalue decomposition

Definition at line 1269 of file Matrix.php.

{
return new EigenvalueDecomposition($this);
} // function eig()
Matrix::filled (   $m = null,
  $n = null,
  $c = 0 
)

filled

Generate a filled matrix

Parameters
int$mRow dimension
int$nColumn dimension
int$cFill constant
Returns
Matrix Filled matrix

Definition at line 486 of file Matrix.php.

References $m, $n, ArgumentTypeException, and JAMAError().

{
if (is_int($m) && is_int($n) && is_numeric($c)) {
$R = new Matrix($m, $n, $c);
return $R;
} else {
}
} // function filled()

+ Here is the call graph for this function:

Matrix::get (   $i = null,
  $j = null 
)

get

Get the i,j-th element of the matrix.

Parameters
int$iRow position
int$jColumn position
Returns
mixed Element (int/float/double)

Definition at line 243 of file Matrix.php.

{
return $this->A[$i][$j];
} // function get()
Matrix::getArray ( )

getArray

Returns
array Matrix array

Definition at line 143 of file Matrix.php.

References $A.

{
return $this->A;
} // function getArray()
Matrix::getArrayCopy ( )

getArrayCopy

Returns
array Matrix array copy

Definition at line 153 of file Matrix.php.

References $A.

{
return $this->A;
} // function getArrayCopy()
Matrix::getColumnDimension ( )

getColumnDimension

Returns
int Column dimension

Definition at line 230 of file Matrix.php.

References $n.

{
return $this->n;
} // function getColumnDimension()
Matrix::getColumnPackedCopy ( )

getColumnPackedCopy

Get a column-packed array

Returns
array Column-packed matrix array

Definition at line 187 of file Matrix.php.

References $m, and $n.

{
$P = array();
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
array_push($P, $this->A[$j][$i]);
}
}
return $P;
} // function getColumnPackedCopy()
Matrix::getMatrix ( )

getMatrix

Get a submatrix

Parameters
int$i0Initial row index
int$iFFinal row index
int$j0Initial column index
int$jFFinal column index
Returns
Matrix Submatrix

Definition at line 258 of file Matrix.php.

References $m, $n, ArgumentBoundsException, JAMAError(), n, and PolymorphicArgumentException.

Referenced by getMatrixByCol(), and getMatrixByRow().

{
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
switch($match) {
//A($i0...; $j0...)
case 'integer,integer':
list($i0, $j0) = $args;
if ($i0 >= 0) { $m = $this->m - $i0; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
if ($j0 >= 0) { $n = $this->n - $j0; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
$R = new Matrix($m, $n);
for($i = $i0; $i < $this->m; ++$i) {
for($j = $j0; $j < $this->n; ++$j) {
$R->set($i, $j, $this->A[$i][$j]);
}
}
return $R;
break;
//A($i0...$iF; $j0...$jF)
case 'integer,integer,integer,integer':
list($i0, $iF, $j0, $jF) = $args;
if (($iF > $i0) && ($this->m >= $iF) && ($i0 >= 0)) { $m = $iF - $i0; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
if (($jF > $j0) && ($this->n >= $jF) && ($j0 >= 0)) { $n = $jF - $j0; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
$R = new Matrix($m+1, $n+1);
for($i = $i0; $i <= $iF; ++$i) {
for($j = $j0; $j <= $jF; ++$j) {
$R->set($i - $i0, $j - $j0, $this->A[$i][$j]);
}
}
return $R;
break;
//$R = array of row indices; $C = array of column indices
case 'array,array':
list($RL, $CL) = $args;
if (count($RL) > 0) { $m = count($RL); } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
if (count($CL) > 0) { $n = count($CL); } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
$R = new Matrix($m, $n);
for($i = 0; $i < $m; ++$i) {
for($j = 0; $j < $n; ++$j) {
$R->set($i - $i0, $j - $j0, $this->A[$RL[$i]][$CL[$j]]);
}
}
return $R;
break;
//$RL = array of row indices; $CL = array of column indices
case 'array,array':
list($RL, $CL) = $args;
if (count($RL) > 0) { $m = count($RL); } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
if (count($CL) > 0) { $n = count($CL); } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
$R = new Matrix($m, $n);
for($i = 0; $i < $m; ++$i) {
for($j = 0; $j < $n; ++$j) {
$R->set($i, $j, $this->A[$RL[$i]][$CL[$j]]);
}
}
return $R;
break;
//A($i0...$iF); $CL = array of column indices
case 'integer,integer,array':
list($i0, $iF, $CL) = $args;
if (($iF > $i0) && ($this->m >= $iF) && ($i0 >= 0)) { $m = $iF - $i0; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
if (count($CL) > 0) { $n = count($CL); } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
$R = new Matrix($m, $n);
for($i = $i0; $i < $iF; ++$i) {
for($j = 0; $j < $n; ++$j) {
$R->set($i - $i0, $j, $this->A[$RL[$i]][$j]);
}
}
return $R;
break;
//$RL = array of row indices
case 'array,integer,integer':
list($RL, $j0, $jF) = $args;
if (count($RL) > 0) { $m = count($RL); } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
if (($jF >= $j0) && ($this->n >= $jF) && ($j0 >= 0)) { $n = $jF - $j0; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
$R = new Matrix($m, $n+1);
for($i = 0; $i < $m; ++$i) {
for($j = $j0; $j <= $jF; ++$j) {
$R->set($i, $j - $j0, $this->A[$RL[$i]][$j]);
}
}
return $R;
break;
default:
break;
}
} else {
}
} // function getMatrix()

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Matrix::getMatrixByCol (   $j0 = null,
  $jF = null 
)

getMatrixByCol

Get a submatrix by column index/range

Parameters
int$i0Initial column index
int$iFFinal column index
Returns
Matrix Submatrix

Definition at line 558 of file Matrix.php.

References ArgumentTypeException, getMatrix(), and JAMAError().

{
if (is_int($j0)) {
if (is_int($jF)) {
return $this->getMatrix(0, $j0, $this->m, $jF + 1);
} else {
return $this->getMatrix(0, $j0, $this->m, $j0 + 1);
}
} else {
}
} // function getMatrixByCol()

+ Here is the call graph for this function:

Matrix::getMatrixByRow (   $i0 = null,
  $iF = null 
)

getMatrixByRow

Get a submatrix by row index/range

Parameters
int$i0Initial row index
int$iFFinal row index
Returns
Matrix Submatrix

Definition at line 537 of file Matrix.php.

References ArgumentTypeException, getMatrix(), JAMAError(), and n.

{
if (is_int($i0)) {
if (is_int($iF)) {
return $this->getMatrix($i0, 0, $iF + 1, $this->n);
} else {
return $this->getMatrix($i0, 0, $i0 + 1, $this->n);
}
} else {
}
} // function getMatrixByRow()

+ Here is the call graph for this function:

Matrix::getRowDimension ( )

getRowDimension

Returns
int Row dimension

Definition at line 220 of file Matrix.php.

References $m.

{
return $this->m;
} // function getRowDimension()
Matrix::getRowPackedCopy ( )

getRowPackedCopy

Get a row-packed array

Returns
array Row-packed matrix array

Definition at line 204 of file Matrix.php.

References $m, and $n.

{
$P = array();
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
array_push($P, $this->A[$i][$j]);
}
}
return $P;
} // function getRowPackedCopy()
Matrix::identity (   $m = null,
  $n = null 
)

identity

Generate an identity matrix.

Parameters
int$mRow dimension
int$nColumn dimension
Returns
Matrix Identity matrix

Definition at line 454 of file Matrix.php.

References $m, $n, and diagonal().

Referenced by inverse(), and TestMatrix\TestMatrix().

{
return $this->diagonal($m, $n, 1);
} // function identity()

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Matrix::inverse ( )

Matrix inverse or pseudoinverse.

Returns
Matrix ... Inverse(A) if A is square, pseudoinverse otherwise.

Definition at line 1307 of file Matrix.php.

References identity(), and solve().

{
return $this->solve($this->identity($this->m, $this->m));
} // function inverse()

+ Here is the call graph for this function:

Matrix::lu ( )

lu

LU decomposition

Returns
Matrix LU decomposition

Definition at line 1247 of file Matrix.php.

{
return new LUDecomposition($this);
} // function lu()
Matrix::minus ( )

minus

A - B

Parameters
mixed$BMatrix/Array
Returns
Matrix Sum

Definition at line 780 of file Matrix.php.

References $m, $n, ArgumentTypeException, checkMatrixDimensions(), JAMAError(), and PolymorphicArgumentException.

{
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
break;
case 'array':
$M = new Matrix($args[0]);
break;
default:
break;
}
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
$M->set($i, $j, $M->get($i, $j) - $this->A[$i][$j]);
}
}
return $M;
} else {
}
} // function minus()

+ Here is the call graph for this function:

Matrix::minusEquals ( )

minusEquals

A = A - B

Parameters
mixed$BMatrix/Array
Returns
Matrix Sum

Definition at line 816 of file Matrix.php.

References $m, $n, ArgumentTypeException, checkMatrixDimensions(), JAMAError(), and PolymorphicArgumentException.

{
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
break;
case 'array':
$M = new Matrix($args[0]);
break;
default:
break;
}
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
$this->A[$i][$j] -= $M->get($i, $j);
}
}
return $this;
} else {
}
} // function minusEquals()

+ Here is the call graph for this function:

Matrix::mprint (   $A,
  $format = "%01.2f",
  $width = 2 
)

Older debugging utility for backwards compatability.

Returns
html version of matrix

Definition at line 1329 of file Matrix.php.

References $A, $m, and $n.

{
$m = count($A);
$n = count($A[0]);
$spacing = str_repeat('&nbsp;',$width);
for ($i = 0; $i < $m; ++$i) {
for ($j = 0; $j < $n; ++$j) {
$formatted = sprintf($format, $A[$i][$j]);
echo $formatted.$spacing;
}
echo "<br />";
}
} // function mprint()
Matrix::norm1 ( )

norm1

One norm

Returns
float Maximum column sum

Definition at line 594 of file Matrix.php.

References $m, and $n.

{
$r = 0;
for($j = 0; $j < $this->n; ++$j) {
$s = 0;
for($i = 0; $i < $this->m; ++$i) {
$s += abs($this->A[$i][$j]);
}
$r = ($r > $s) ? $r : $s;
}
return $r;
} // function norm1()
Matrix::norm2 ( )

norm2

Maximum singular value

Returns
float Maximum singular value

Definition at line 613 of file Matrix.php.

{
} // function norm2()
Matrix::normF ( )

normF

Frobenius norm

Returns
float Square root of the sum of all elements squared

Definition at line 642 of file Matrix.php.

References $f, $m, $n, and hypo().

{
$f = 0;
for ($i = 0; $i < $this->m; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
$f = hypo($f,$this->A[$i][$j]);
}
}
return $f;
} // function normF()

+ Here is the call graph for this function:

Matrix::normInf ( )

normInf

Infinite norm

Returns
float Maximum row sum

Definition at line 623 of file Matrix.php.

References $m, and $n.

{
$r = 0;
for($i = 0; $i < $this->m; ++$i) {
$s = 0;
for($j = 0; $j < $this->n; ++$j) {
$s += abs($this->A[$i][$j]);
}
$r = ($r > $s) ? $r : $s;
}
return $r;
} // function normInf()
Matrix::packed ( )

packed

Alias for getRowPacked

Returns
array Packed array

Definition at line 524 of file Matrix.php.

{
return $this->getRowPacked();
} // function packed()
Matrix::plus ( )

plus

A + B

Parameters
mixed$BMatrix/Array
Returns
Matrix Sum

Definition at line 708 of file Matrix.php.

References $m, $n, ArgumentTypeException, checkMatrixDimensions(), JAMAError(), and PolymorphicArgumentException.

{
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
break;
case 'array':
$M = new Matrix($args[0]);
break;
default:
break;
}
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
$M->set($i, $j, $M->get($i, $j) + $this->A[$i][$j]);
}
}
return $M;
} else {
}
} // function plus()

+ Here is the call graph for this function:

Matrix::plusEquals ( )

plusEquals

A = A + B

Parameters
mixed$BMatrix/Array
Returns
Matrix Sum

Definition at line 744 of file Matrix.php.

References $m, $n, ArgumentTypeException, checkMatrixDimensions(), JAMAError(), and PolymorphicArgumentException.

{
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
break;
case 'array':
$M = new Matrix($args[0]);
break;
default:
break;
}
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
$this->A[$i][$j] += $M->get($i, $j);
}
}
return $this;
} else {
}
} // function plusEquals()

+ Here is the call graph for this function:

Matrix::power ( )

power

A = A ^ B

Parameters
mixed$BMatrix/Array
Returns
Matrix Sum

Definition at line 1165 of file Matrix.php.

References $m, $n, ArgumentTypeException, checkMatrixDimensions(), JAMAError(), and PolymorphicArgumentException.

{
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
break;
case 'array':
$M = new Matrix($args[0]);
break;
default:
break;
}
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
$this->A[$i][$j] = pow($this->A[$i][$j],$M->get($i, $j));
}
}
return $this;
} else {
}
} // function power()

+ Here is the call graph for this function:

Matrix::qr ( )

qr

QR decomposition

Returns
Matrix QR decomposition

Definition at line 1258 of file Matrix.php.

{
return new QRDecomposition($this);
} // function qr()
Matrix::random (   $m = null,
  $n = null,
  $a = RAND_MIN,
  $b = RAND_MAX 
)

random

Generate a random matrix

Parameters
int$mRow dimension
int$nColumn dimension
Returns
Matrix Random matrix

Definition at line 503 of file Matrix.php.

References $m, $n, ArgumentTypeException, and JAMAError().

Referenced by Benchmark\runCholesky(), Benchmark\runEig(), Benchmark\runLU(), Benchmark\runQR(), and Benchmark\runSVD().

{
if (is_int($m) && is_int($n) && is_numeric($a) && is_numeric($b)) {
$R = new Matrix($m, $n);
for($i = 0; $i < $m; ++$i) {
for($j = 0; $j < $n; ++$j) {
$R->set($i, $j, mt_rand($a, $b));
}
}
return $R;
} else {
}
} // function random()

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Matrix::rank ( )

Matrix rank.

Returns
effective numerical rank, obtained from SVD.

Definition at line 658 of file Matrix.php.

{
$svd = new SingularValueDecomposition($this);
return $svd->rank();
} // function rank ()
Matrix::set (   $i = null,
  $j = null,
  $c = null 
)

set

Set the i,j-th element of the matrix.

Parameters
int$iRow position
int$jColumn position
mixed$cInt/float/double value
Returns
mixed Element (int/float/double)

Definition at line 428 of file Matrix.php.

{
// Optimized set version just has this
$this->A[$i][$j] = $c;
/*
if (is_int($i) && is_int($j) && is_numeric($c)) {
if (($i < $this->m) && ($j < $this->n)) {
$this->A[$i][$j] = $c;
} else {
echo "A[$i][$j] = $c<br />";
throw new Exception(JAMAError(ArgumentBoundsException));
}
} else {
throw new Exception(JAMAError(ArgumentTypeException));
}
*/
} // function set()
Matrix::setMatrix ( )

setMatrix

Set a submatrix

Parameters
int$i0Initial row index
int$j0Initial column index
mixed$SMatrix/Array submatrix ($i0, $j0, $S) $S = Matrix ($i0, $j0, $S) $S = Array

Definition at line 362 of file Matrix.php.

References ArgumentBoundsException, ArgumentTypeException, JAMAError(), and PolymorphicArgumentException.

{
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
switch($match) {
case 'integer,integer,object':
if ($args[2] instanceof Matrix) { $M = $args[2]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
if (($args[0] + $M->m) <= $this->m) { $i0 = $args[0]; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
if (($args[1] + $M->n) <= $this->n) { $j0 = $args[1]; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
for($i = $i0; $i < $i0 + $M->m; ++$i) {
for($j = $j0; $j < $j0 + $M->n; ++$j) {
$this->A[$i][$j] = $M->get($i - $i0, $j - $j0);
}
}
break;
case 'integer,integer,array':
$M = new Matrix($args[2]);
if (($args[0] + $M->m) <= $this->m) { $i0 = $args[0]; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
if (($args[1] + $M->n) <= $this->n) { $j0 = $args[1]; } else { throw new Exception(JAMAError(ArgumentBoundsException)); }
for($i = $i0; $i < $i0 + $M->m; ++$i) {
for($j = $j0; $j < $j0 + $M->n; ++$j) {
$this->A[$i][$j] = $M->get($i - $i0, $j - $j0);
}
}
break;
default:
break;
}
} else {
}
} // function setMatrix()

+ Here is the call graph for this function:

Matrix::solve (   $B)

Solve A*X = B.

Parameters
Matrix$BRight hand side
Returns
Matrix ... Solution if A is square, least squares solution otherwise

Definition at line 1291 of file Matrix.php.

References n.

Referenced by inverse().

{
if ($this->m == $this->n) {
$LU = new LUDecomposition($this);
return $LU->solve($B);
} else {
$QR = new QRDecomposition($this);
return $QR->solve($B);
}
} // function solve()

+ Here is the caller graph for this function:

Matrix::svd ( )

svd

Singular value decomposition

Returns
Singular value decomposition

Definition at line 1280 of file Matrix.php.

{
return new SingularValueDecomposition($this);
} // function svd()
Matrix::times ( )

times

Matrix multiplication

Parameters
mixed$nMatrix/Array/Scalar
Returns
Matrix Product

Definition at line 1074 of file Matrix.php.

References $m, $n, ArgumentTypeException, JAMAError(), n, and PolymorphicArgumentException.

{
if (func_num_args() > 0) {
$args = func_get_args();
$match = implode(",", array_map('gettype', $args));
switch($match) {
case 'object':
if ($args[0] instanceof Matrix) { $B = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); }
if ($this->n == $B->m) {
$C = new Matrix($this->m, $B->n);
for($j = 0; $j < $B->n; ++$j) {
for ($k = 0; $k < $this->n; ++$k) {
$Bcolj[$k] = $B->A[$k][$j];
}
for($i = 0; $i < $this->m; ++$i) {
$Arowi = $this->A[$i];
$s = 0;
for($k = 0; $k < $this->n; ++$k) {
$s += $Arowi[$k] * $Bcolj[$k];
}
$C->A[$i][$j] = $s;
}
}
return $C;
} else {
throw new Exception(JAMAError(MatrixDimensionMismatch));
}
break;
case 'array':
$B = new Matrix($args[0]);
if ($this->n == $B->m) {
$C = new Matrix($this->m, $B->n);
for($i = 0; $i < $C->m; ++$i) {
for($j = 0; $j < $C->n; ++$j) {
$s = "0";
for($k = 0; $k < $C->n; ++$k) {
$s += $this->A[$i][$k] * $B->A[$k][$j];
}
$C->A[$i][$j] = $s;
}
}
return $C;
} else {
throw new Exception(JAMAError(MatrixDimensionMismatch));
}
return $M;
break;
case 'integer':
$C = new Matrix($this->A);
for($i = 0; $i < $C->m; ++$i) {
for($j = 0; $j < $C->n; ++$j) {
$C->A[$i][$j] *= $args[0];
}
}
return $C;
break;
case 'double':
$C = new Matrix($this->m, $this->n);
for($i = 0; $i < $C->m; ++$i) {
for($j = 0; $j < $C->n; ++$j) {
$C->A[$i][$j] = $args[0] * $this->A[$i][$j];
}
}
return $C;
break;
case 'float':
$C = new Matrix($this->A);
for($i = 0; $i < $C->m; ++$i) {
for($j = 0; $j < $C->n; ++$j) {
$C->A[$i][$j] *= $args[0];
}
}
return $C;
break;
default:
break;
}
} else {
}
} // function times()

+ Here is the call graph for this function:

Matrix::toHTML (   $width = 2)

Debugging utility.

Returns
Output HTML representation of matrix

Definition at line 1349 of file Matrix.php.

References $m, and $n.

{
print('<table style="background-color:#eee;">');
for($i = 0; $i < $this->m; ++$i) {
print('<tr>');
for($j = 0; $j < $this->n; ++$j) {
print('<td style="background-color:#fff;border:1px solid #000;padding:2px;text-align:center;vertical-align:middle;">' . $this->A[$i][$j] . '</td>');
}
print('</tr>');
}
print('</table>');
} // function toHTML()
Matrix::trace ( )

trace

Sum of diagonal elements

Returns
float Sum of diagonal elements

Definition at line 681 of file Matrix.php.

References $n, and n.

{
$s = 0;
$n = min($this->m, $this->n);
for($i = 0; $i < $n; ++$i) {
$s += $this->A[$i][$i];
}
return $s;
} // function trace()
Matrix::transpose ( )

transpose

Tranpose matrix

Returns
Matrix Transposed matrix

Definition at line 577 of file Matrix.php.

References $m, $n, and n.

{
$R = new Matrix($this->n, $this->m);
for($i = 0; $i < $this->m; ++$i) {
for($j = 0; $j < $this->n; ++$j) {
$R->set($j, $i, $this->A[$i][$j]);
}
}
return $R;
} // function transpose()
Matrix::uminus ( )

uminus

Unary minus matrix -A

Returns
Matrix Unary minus matrix

Definition at line 697 of file Matrix.php.

{
} // function uminus()

Field Documentation

Matrix::$A = array()

Definition at line 44 of file Matrix.php.

Referenced by constructWithCopy(), getArray(), getArrayCopy(), and mprint().


The documentation for this class was generated from the following file: