Finished up matrix tests and fixed a few bugs

This commit is contained in:
Jeff Moser 2010-09-03 07:25:55 -04:00
parent 2c5c00f88b
commit 4a76cc34cc
2 changed files with 75 additions and 3 deletions

@ -159,7 +159,7 @@ class Matrix
// The idea is that it's the transpose of the cofactors // The idea is that it's the transpose of the cofactors
$result = array(); $result = array();
for ($currentColumn = 0; $currentColumn < $this->_columns; $currentColumn++) for ($currentColumn = 0; $currentColumn < $this->_columnCount; $currentColumn++)
{ {
for ($currentRow = 0; $currentRow < $this->_rowCount; $currentRow++) for ($currentRow = 0; $currentRow < $this->_rowCount; $currentRow++)
{ {
@ -185,7 +185,7 @@ class Matrix
return self::scalarMultiply($determinantInverse, $adjugate); return self::scalarMultiply($determinantInverse, $adjugate);
} }
public static function scalarMultiply($scalar, $matrix) public static function scalarMultiply($scalarValue, $matrix)
{ {
$rows = $matrix->getRowCount(); $rows = $matrix->getRowCount();
$columns = $matrix->getColumnCount(); $columns = $matrix->getColumnCount();
@ -393,6 +393,20 @@ class DiagonalMatrix extends Matrix
parent::__construct($rowCount, $colCount); parent::__construct($rowCount, $colCount);
for($currentRow = 0; $currentRow < $rowCount; $currentRow++)
{
for($currentCol = 0; $currentCol < $colCount; $currentCol++)
{
if($currentRow == $currentCol)
{
$this->setValue($currentRow, $currentCol, $diagonalValues[$currentRow]);
}
else
{
$this->setValue($currentRow, $currentCol, 0);
}
}
}
for($i = 0; $i < $diagonalCount; $i++) for($i = 0; $i < $diagonalCount; $i++)
{ {
$this->setValue($i, $i, $diagonalValues[$i]); $this->setValue($i, $i, $diagonalValues[$i]);

@ -6,6 +6,7 @@ require_once(dirname(__FILE__) . '/../../PHPSkills/Numerics/Matrix.php');
use \PHPUnit_Framework_TestCase; use \PHPUnit_Framework_TestCase;
use Moserware\Numerics\Matrix; use Moserware\Numerics\Matrix;
use Moserware\Numerics\IdentityMatrix;
use Moserware\Numerics\SquareMatrix; use Moserware\Numerics\SquareMatrix;
class MatrixTest extends PHPUnit_Framework_TestCase class MatrixTest extends PHPUnit_Framework_TestCase
@ -129,10 +130,67 @@ class MatrixTest extends PHPUnit_Framework_TestCase
$this->assertTrue($g->equals($h)); $this->assertTrue($g->equals($h));
} }
public function testAdjugate()
{
// From Wikipedia: http://en.wikipedia.org/wiki/Adjugate_matrix
$a = new SquareMatrix(1, 2,
3, 4);
$b = new SquareMatrix( 4, -2,
-3, 1);
$this->assertTrue($b->equals($a->getAdjugate()));
$c = new SquareMatrix(-3, 2, -5,
-1, 0, -2,
3, -4, 1);
$d = new SquareMatrix(-8, 18, -4,
-5, 12, -1,
4, -6, 2);
$this->assertTrue($d->equals($c->getAdjugate()));
}
public function testInverse()
{
// see http://www.mathwords.com/i/inverse_of_a_matrix.htm
$a = new SquareMatrix(4, 3,
3, 2);
$b = new SquareMatrix(-2, 3,
3, -4);
$aInverse = $a->getInverse();
$this->assertTrue($b->equals($aInverse));
$identity2x2 = new IdentityMatrix(2);
$aaInverse = Matrix::multiply($a, $aInverse);
$this->assertTrue($identity2x2->equals($aaInverse));
$c = new SquareMatrix(1, 2, 3,
0, 4, 5,
1, 0, 6);
$cInverse = $c->getInverse();
$d = Matrix::scalarMultiply((1.0 / 22), new SquareMatrix(24, -12, -2,
5, 3, -5,
-4, 2, 4));
$this->assertTrue($d->equals($cInverse));
$identity3x3 = new IdentityMatrix(3);
$ccInverse = Matrix::multiply($c, $cInverse);
$this->assertTrue($identity3x3->equals($ccInverse));
}
} }
$testSuite = new \PHPUnit_Framework_TestSuite(); $testSuite = new \PHPUnit_Framework_TestSuite();
$testSuite->addTest( new MatrixTest("testEquals")); $testSuite->addTest( new MatrixTest("testInverse"));
\PHPUnit_TextUI_TestRunner::run($testSuite); \PHPUnit_TextUI_TestRunner::run($testSuite);
?> ?>