Source for file TestMatrix.php
Documentation is available at TestMatrix.php
require_once "../Matrix.php";
$columnwise = array(1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.);
$rowwise = array(1.,4.,7.,10.,2.,5.,8.,11.,3.,6.,9.,12.);
$avals = array(array(1.,4.,7.,10.),array(2.,5.,8.,11.),array(3.,6.,9.,12.));
$tvals = array(array(1.,2.,3.),array(4.,5.,6.),array(7.,8.,9.),array(10.,11.,12.));
$subavals = array(array(5.,8.,11.),array(6.,9.,12.));
$rvals = array(array(1.,4.,7.),array(2.,5.,8.,11.),array(3.,6.,9.,12.));
$pvals = array(array(1.,1.,1.),array(1.,2.,3.),array(1.,3.,6.));
$ivals = array(array(1.,0.,0.,0.),array(0.,1.,0.,0.),array(0.,0.,1.,0.));
$evals = array(array(0.,1.,0.,0.),array(1.,0.,2.e-7,0.),array(0.,- 2.e-7,0.,1.),array(0.,0.,1.,0.));
$square = array(array(166.,188.,210.),array(188.,214.,240.),array(210.,240.,270.));
$sqSolution = array(array(13.),array(15.));
$condmat = array(array(1.,3.),array(7.,9.));
$invalidID = 5; /* should trigger bad shape for construction with val */
$raggedr = 0; /* (raggedr,raggedc) should be out of bounds in ragged array */
$validID = 3; /* leading dimension of intended test Matrices */
$nonconformld = 4; /* leading dimension which is valid, but nonconforming */
$ib = 1; /* index ranges for sub Matrix */
$rowindexset = array(1,2);
$badrowindexset = array(1,3);
$columnindexset = array(1,2,3);
$badcolumnindexset = array(1,2,4);
* Constructors and constructor-like methods:
* Matrix(int, int, double)
* Matrix(int, int, double[][])
* constructWithCopy(double[][])
echo "<p>Testing constructors and constructor-like methods...</p>";
$A = new Matrix($columnwise, 3);
if($A instanceof Matrix) {
$errorCount = $this->try_failure($errorCount, "Column-packed constructor...", "Unable to construct Matrix");
$errorCount = $this->try_failure($errorCount, "2D array constructor...", "Unable to construct Matrix");
$A = new Matrix($columnwise, $validID);
$B = Matrix::constructWithCopy($avals);
/** check that constructWithCopy behaves properly **/
if ( ( $tmp - $B->get(0,0) ) != 0.0 )
$errorCount = $this->try_failure($errorCount,"constructWithCopy... ","copy not effected... data visible outside");
$errorCount = $this->try_failure($errorCount,"identity... ","identity Matrix not successfully created");
* getMatrix(int,int,int,int)
* getMatrix(int,int,int[])
* getMatrix(int[],int,int)
* setMatrix(int,int,int,int,Matrix)
* setMatrix(int,int,int[],Matrix)
* setMatrix(int[],int,int,Matrix)
* setMatrix(int[],int[],Matrix)
print "<p>Testing access methods...</p>";
if($B->getRowDimension() == $rows)
$errorCount = $this->try_failure($errorCount, "getRowDimension...");
if($B->getColumnDimension() == $cols)
$errorCount = $this->try_failure($errorCount, "getColumnDimension...");
$barray = $B->getArray();
$errorCount = $this->try_failure($errorCount, "getArray...");
$bpacked = $B->getColumnPackedCopy();
$errorCount = $this->try_failure($errorCount, "getColumnPackedCopy...");
$bpacked = $B->getRowPackedCopy();
$errorCount = $this->try_failure($errorCount, "getRowPackedCopy...");
print "<p>Testing array-like methods...</p>";
print "<p>Testing I/O methods...</p>";
* Test linear algebra methods
echo "<p>Testing linear algebra methods...<p>";
$A = new Matrix($columnwise, 3);
$errorCount = $this->try_failure($errorCount, "Transpose check...", "Matrices are not equal");
$errorCount = $this->try_failure($errorCount, "Maximum column sum...", "Incorrect: " . $A->norm1() . " != " . $columnsummax);
$errorCount = $this->try_failure($errorCount, "Maximum row sum...", "Incorrect: " . $A->normInf() . " != " . $rowsummax );
$errorCount = $this->try_failure($errorCount, "Frobenius norm...", "Incorrect:" . $A->normF() . " != " . sqrt($sumofsquares));
$errorCount = $this->try_failure($errorCount, "Matrix trace...", "Incorrect: " . $A->trace() . " != " . $sumofdiagonals);
$B = $A->getMatrix(0, $A->getRowDimension(), 0, $A->getRowDimension());
$errorCount = $this->try_failure($errorCount, "Matrix determinant...", "Incorrect: " . $B->det() . " != " . 0);
$A = new Matrix($columnwise,3);
$SQ = new Matrix($square);
$errorCount = $this->try_failure($errorCount, "times(Matrix)...", "Unable to multiply matrices");
$A = new Matrix($columnwise, 4);
$errorCount = $this->try_failure($errorCount,"QRDecomposition...","incorrect qr decomposition calculation");
$A = new Matrix($columnwise, 4);
if ($this->checkMatrices($A, $U->times($S->times($V->transpose()))))
$this->try_success("SingularValueDecomposition...","");
$errorCount = $this->try_failure($errorCount,"SingularValueDecomposition...","incorrect singular value decomposition calculation");
$n = $A->getColumnDimension();
$A = $A->getMatrix(0,$n- 1,0,$n- 1);
if ( $this->checkMatrices($A->getMatrix($LU->getPivot(),0,$n- 1), $L->times($LU->getU())) )
$errorCount = $this->try_failure($errorCount,"LUDecomposition...","incorrect LU decomposition calculation");
$errorCount = $this->try_failure($errorCount, "inverse()...","incorrect inverse calculation");
$DEF = new Matrix($rankdef);
if($this->checkScalars($DEF->rank(), min($DEF->getRowDimension(), $DEF->getColumnDimension())- 1))
$this->try_failure("Rank...", "incorrect rank calculation");
$B = new Matrix($condmat);
$singularvalues = $SVD->getSingularValues();
if($this->checkScalars($B->cond(), $singularvalues[0]/ $singularvalues[min($B->getRowDimension(), $B->getColumnDimension())- 1]))
$this->try_failure("Condition number...", "incorrect condition number calculation");
$SUB = new Matrix($subavals);
$O = new Matrix($SUB->getRowDimension(),1,1.0);
$SOL = new Matrix($sqSolution);
$SQ = $SUB->getMatrix(0,$SUB->getRowDimension()- 1,0,$SUB->getRowDimension()- 1);
$errorCount = $this->try_failure($errorCount,"solve()...","incorrect lu solve calculation");
$errorCount = $this->try_failure($errorCount,"CholeskyDecomposition...","incorrect Cholesky decomposition calculation");
$X = $Chol->solve(Matrix::identity(3,3));
if ( $this->checkMatrices($A->times($X), Matrix::identity(3,3)) )
$this->try_success("CholeskyDecomposition solve()...","");
$errorCount = $this->try_failure($errorCount,"CholeskyDecomposition solve()...","incorrect Choleskydecomposition solve calculation");
$this->try_success("EigenvalueDecomposition (symmetric)...","");
$errorCount = $this->try_failure($errorCount,"EigenvalueDecomposition (symmetric)...","incorrect symmetric Eigenvalue decomposition calculation");
$this->try_success("EigenvalueDecomposition (nonsymmetric)...","");
$errorCount = $this->try_failure($errorCount,"EigenvalueDecomposition (nonsymmetric)...","incorrect nonsymmetric Eigenvalue decomposition calculation");
print ("<b>{$errorCount} total errors</b>.");
* Print appropriate messages for successful outcome try
print "> ". $s . "success<br />";
print "> Message: ". $e . "<br />";
* Print appropriate messages for unsuccessful outcome try
* @return int incremented counter
print "> ". $s . "*** failure ***<br />> Message: ". $e . "<br />";
* Print appropriate messages for unsuccessful outcome try
* @return int incremented counter
print "> ". $s . "*** warning ***<br />> Message: ". $e . "<br />";
* Check magnitude of difference of "scalars".
if ($x == 0 & abs($y) < 10* $eps) return;
if ($y == 0 & abs($x) < 10* $eps) return;
* Check norm of difference of "vectors".
for($i= 0; $i < $nx; ++ $i)
die("Attempt to compare vectors of different lengths");
* Check norm of difference of "arrays".
* Check norm of difference of "matrices".
if( $X == null || $Y == null )
if ($X->norm1() == 0. & $Y->norm1() < 10* $eps) return true;
if ($Y->norm1() == 0. & $X->norm1() < 10* $eps) return true;
if ($A->norm1() > 1000 * $eps * max($X->norm1(),$Y->norm1()))
die("The norm of (X-Y) is too large: ". $A->norm1());
|