3 require_once
"../Matrix.php";
13 $columnwise = array(1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.);
14 $rowwise = array(1.,4.,7.,10.,2.,5.,8.,11.,3.,6.,9.,12.);
15 $avals = array(array(1.,4.,7.,10.),array(2.,5.,8.,11.),array(3.,6.,9.,12.));
17 $tvals = array(array(1.,2.,3.),array(4.,5.,6.),array(7.,8.,9.),array(10.,11.,12.));
18 $subavals = array(array(5.,8.,11.),array(6.,9.,12.));
19 $rvals = array(array(1.,4.,7.),array(2.,5.,8.,11.),array(3.,6.,9.,12.));
20 $pvals = array(array(1.,1.,1.),array(1.,2.,3.),array(1.,3.,6.));
21 $ivals = array(array(1.,0.,0.,0.),array(0.,1.,0.,0.),array(0.,0.,1.,0.));
22 $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.));
23 $square = array(array(166.,188.,210.),array(188.,214.,240.),array(210.,240.,270.));
24 $sqSolution = array(array(13.),array(15.));
25 $condmat = array(array(1.,3.),array(7.,9.));
37 $rowindexset = array(1,2);
38 $badrowindexset = array(1,3);
39 $columnindexset = array(1,2,3);
40 $badcolumnindexset = array(1,2,4);
62 echo
"<p>Testing constructors and constructor-like methods...</p>";
64 $A =
new Matrix($columnwise, 3);
65 if($A instanceof Matrix) {
68 $errorCount = $this->
try_failure($errorCount,
"Column-packed constructor...",
"Unable to construct Matrix");
70 $T =
new Matrix($tvals);
71 if($T instanceof Matrix)
74 $errorCount = $this->
try_failure($errorCount,
"2D array constructor...",
"Unable to construct Matrix");
76 $A =
new Matrix($columnwise, $validID);
77 $B =
new Matrix($avals);
82 $B = Matrix::constructWithCopy($avals);
86 if ( ( $tmp - $B->get(0,0) ) != 0.0 )
87 $errorCount = $this->
try_failure($errorCount,
"constructWithCopy... ",
"copy not effected... data visible outside");
91 $I =
new Matrix($ivals);
95 $errorCount = $this->
try_failure($errorCount,
"identity... ",
"identity Matrix not successfully created");
117 print
"<p>Testing access methods...</p>";
119 $B =
new Matrix($avals);
120 if($B->getRowDimension() == $rows)
123 $errorCount = $this->
try_failure($errorCount,
"getRowDimension...");
125 if($B->getColumnDimension() == $cols)
128 $errorCount = $this->
try_failure($errorCount,
"getColumnDimension...");
130 $barray = $B->getArray();
134 $errorCount = $this->
try_failure($errorCount,
"getArray...");
136 $bpacked = $B->getColumnPackedCopy();
140 $errorCount = $this->
try_failure($errorCount,
"getColumnPackedCopy...");
142 $bpacked = $B->getRowPackedCopy();
146 $errorCount = $this->
try_failure($errorCount,
"getRowPackedCopy...");
162 print
"<p>Testing array-like methods...</p>";
172 print
"<p>Testing I/O methods...</p>";
177 echo
"<p>Testing linear algebra methods...<p>";
179 $A =
new Matrix($columnwise, 3);
183 $errorCount = $this->
try_failure($errorCount,
"Transpose check...",
"Matrices are not equal");
188 $errorCount = $this->
try_failure($errorCount,
"Maximum column sum...",
"Incorrect: " . $A->norm1() .
" != " . $columnsummax);
193 $errorCount = $this->
try_failure($errorCount,
"Maximum row sum...",
"Incorrect: " . $A->normInf() .
" != " . $rowsummax );
195 if($this->
checkScalars($A->normF(), sqrt($sumofsquares)))
198 $errorCount = $this->
try_failure($errorCount,
"Frobenius norm...",
"Incorrect:" . $A->normF() .
" != " . sqrt($sumofsquares));
203 $errorCount = $this->
try_failure($errorCount,
"Matrix trace...",
"Incorrect: " . $A->trace() .
" != " . $sumofdiagonals);
205 $B = $A->getMatrix(0, $A->getRowDimension(), 0, $A->getRowDimension());
209 $errorCount = $this->
try_failure($errorCount,
"Matrix determinant...",
"Incorrect: " . $B->det() .
" != " . 0);
211 $A =
new Matrix($columnwise,3);
212 $SQ =
new Matrix($square);
216 $errorCount = $this->
try_failure($errorCount,
"times(Matrix)...",
"Unable to multiply matrices");
221 $A =
new Matrix($columnwise, 4);
229 $errorCount = $this->
try_failure($errorCount,
"QRDecomposition...",
"incorrect qr decomposition calculation");
231 $A =
new Matrix($columnwise, 4);
236 if ($this->
checkMatrices($A, $U->times($S->times($V->transpose()))))
237 $this->
try_success(
"SingularValueDecomposition...",
"");
239 $errorCount = $this->
try_failure($errorCount,
"SingularValueDecomposition...",
"incorrect singular value decomposition calculation");
241 $n = $A->getColumnDimension();
242 $A = $A->getMatrix(0,$n-1,0,$n-1);
247 if ( $this->
checkMatrices($A->getMatrix($LU->getPivot(),0,$n-1), $L->times($LU->getU())) )
250 $errorCount = $this->
try_failure($errorCount,
"LUDecomposition...",
"incorrect LU decomposition calculation");
256 $errorCount = $this->
try_failure($errorCount,
"inverse()...",
"incorrect inverse calculation");
258 $DEF =
new Matrix($rankdef);
259 if($this->
checkScalars($DEF->rank(), min($DEF->getRowDimension(), $DEF->getColumnDimension())-1))
262 $this->
try_failure(
"Rank...",
"incorrect rank calculation");
264 $B =
new Matrix($condmat);
266 $singularvalues = $SVD->getSingularValues();
267 if($this->
checkScalars($B->cond(), $singularvalues[0]/$singularvalues[min($B->getRowDimension(), $B->getColumnDimension())-1]))
270 $this->
try_failure(
"Condition number...",
"incorrect condition number calculation");
272 $SUB =
new Matrix($subavals);
273 $O =
new Matrix($SUB->getRowDimension(),1,1.0);
274 $SOL =
new Matrix($sqSolution);
275 $SQ = $SUB->getMatrix(0,$SUB->getRowDimension()-1,0,$SUB->getRowDimension()-1);
279 $errorCount = $this->
try_failure($errorCount,
"solve()...",
"incorrect lu solve calculation");
281 $A =
new Matrix($pvals);
287 $errorCount = $this->
try_failure($errorCount,
"CholeskyDecomposition...",
"incorrect Cholesky decomposition calculation");
289 $X = $Chol->solve(Matrix::identity(3,3));
291 $this->
try_success(
"CholeskyDecomposition solve()...",
"");
293 $errorCount = $this->
try_failure($errorCount,
"CholeskyDecomposition solve()...",
"incorrect Choleskydecomposition solve calculation");
299 $this->
try_success(
"EigenvalueDecomposition (symmetric)...",
"");
301 $errorCount = $this->
try_failure($errorCount,
"EigenvalueDecomposition (symmetric)...",
"incorrect symmetric Eigenvalue decomposition calculation");
303 $A =
new Matrix($evals);
308 $this->
try_success(
"EigenvalueDecomposition (nonsymmetric)...",
"");
310 $errorCount = $this->
try_failure($errorCount,
"EigenvalueDecomposition (nonsymmetric)...",
"incorrect nonsymmetric Eigenvalue decomposition calculation");
312 print(
"<b>{$errorCount} total errors</b>.");
321 print
"> ". $s .
"success<br />";
323 print
"> Message: ". $e .
"<br />";
334 print
"> ". $s .
"*** failure ***<br />> Message: ". $e .
"<br />";
346 print
"> ". $s .
"*** warning ***<br />> Message: ". $e .
"<br />";
356 $eps = pow(2.0,-52.0);
357 if (
$x == 0 & abs(
$y) < 10*$eps)
return;
358 if (
$y == 0 & abs(
$x) < 10*$eps)
return;
359 if (abs(
$x-
$y) > 10 * $eps * max(abs(
$x),abs(
$y)))
374 for($i=0; $i < $nx; ++$i)
377 die(
"Attempt to compare vectors of different lengths");
397 if(
$X == null ||
$Y == null )
400 $eps = pow(2.0,-52.0);
401 if (
$X->norm1() == 0. &
$Y->norm1() < 10*$eps)
return true;
402 if (
$Y->norm1() == 0. &
$X->norm1() < 10*$eps)
return true;
406 if ($A->norm1() > 1000 * $eps * max(
$X->norm1(),
$Y->norm1()))
407 die(
"The norm of (X-Y) is too large: ".$A->norm1());