Ejemplo n.º 1
0
 public function testCholesky()
 {
     $m1 = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
     $matrix1 = Matrix::matrixFromDoubles($m1);
     $m2 = [[17, 18, 19, 20], [21, 22, 23, 24], [25, 27, 28, 29], [37, 33, 31, 30]];
     $matrix2 = Matrix::matrixFromDoubles($m2);
     $c = new CholeskyDecomposition($matrix1);
     $c->solve($matrix2);
     $mx = $c->getL();
     $this->assertEquals(1.0, $mx->get(0, 0));
     $this->assertEquals(1.0, $mx->get(1, 1));
     $this->assertEquals(1.0, $mx->get(2, 2));
     $this->assertEquals(4, $mx->getRows());
     $this->assertEquals(4, $mx->getCols());
 }
 public function testBipolar2double()
 {
     // test a 1x4
     $booleanData1 = [true, false, true, false];
     $checkData1 = [1, -1, 1, -1];
     $matrix1 = Matrix::createRowMatrix(BiPolarUtil\bipolar2double($booleanData1));
     $checkMatrix1 = Matrix::createRowMatrix($checkData1);
     $this->assertTrue($matrix1->equals($checkMatrix1));
     // test a 2x2
     $booleanData2 = [[true, false], [false, true]];
     $checkData2 = [[1, -1], [-1, 1]];
     $matrix2 = Matrix::matrixFromDoubles(BiPolarUtil\bipolar2double($booleanData2));
     $checkMatrix2 = Matrix::matrixFromDoubles($checkData2);
     $this->assertTrue($matrix2->equals($checkMatrix2));
 }
 /**
  * Update the Hopfield weights after training.
  *
  * @param
  *        	Matrix delta
  *        	The amount to change the weights by.
  */
 private function convertHopfieldMatrix(Matrix $delta)
 {
     // add the new weight matrix to what is there already
     for ($row = 0; $row < $delta->getRows(); ++$row) {
         for ($col = 0; $col < $delta->getRows(); ++$col) {
             $this->addWeight($row, $col, $delta->get($row, $col));
         }
     }
 }
 public function testCopy()
 {
     $data = [[1.0, 2.0], [3.0, 4.0]];
     $source = Matrix::matrixFromDoubles($data);
     $target = new Matrix(2, 2);
     MatrixMath\copy($source, $target);
     $this->assertTrue($source->equals($target));
 }
 /**
  * Randomize the matrix based on an array, modify the array.
  * Previous values
  * may be used, or they may be discarded, depending on the randomizer.
  *
  * @param
  *        	Matrix m
  *        	A matrix to randomize.
  */
 public function randomizeMatrix(Matrix &$m)
 {
     $d = $m->getData();
     for ($r = 0; $r < $m->getRows(); ++$r) {
         for ($c = 0; $c < $m->getCols(); ++$c) {
             $d[$r][$c] = $this->randomizeDouble($d[$r][$c]);
         }
     }
     $m->setData($d);
 }
Ejemplo n.º 6
0
/**
 * Calculate the length of a vector.
 *
 * @param
 *        	input
 *        	The matrix to calculate the length of.
 *        	
 * @return Vector length.
 */
function vectorLength(Matrix $input)
{
    if (!$input->isVector()) {
        throw new MatrixError("Can only take the vector length of a vector.");
    }
    $v = $input->toPackedArray();
    $rtn = 0.0;
    foreach ($v as $element) {
        $rtn += pow($element, 2);
    }
    return sqrt($rtn);
}
 public function inverseCholesky()
 {
     $li = lowerTriangularInverse($l);
     $ic = array();
     for ($r = 0; $r < $this->n; ++$r) {
         for ($c = 0; $c < $this->n; ++$c) {
             for ($i = 0; $i < $this->n; ++$i) {
                 $ic[$r][$c] += $li[$i][$r] * $li[$i][$c];
             }
         }
     }
     return Matrix::matrixFromDoubles($ic);
 }
Ejemplo n.º 8
0
 public function testVectorLength()
 {
     $vectorData = [1.0, 2.0, 3.0, 4.0];
     $vector = Matrix::createRowMatrix($vectorData);
     $this->assertEquals(5, intval(MatrixMath\vectorLength($vector)));
     $nonVector = new Matrix(2, 2);
     try {
         MatrixMath\vectorLength($nonVector);
         $this->assertTrue(false);
     } catch (MatrixError $e) {
     }
 }