Ejemplo n.º 1
0
 public function testRowsAndCols()
 {
     $matrix = new Matrix(6, 3);
     $this->assertEquals($matrix->getRows(), 6);
     $this->assertEquals($matrix->getCols(), 3);
     $matrix->set(1, 2, 1.5);
     $this->assertEquals($matrix->get(1, 2), 1.5);
 }
 /**
  * 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));
         }
     }
 }
 /**
  * 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.º 4
0
/**
 * Return the transposition of a matrix.
 *
 * @param $input Matrix
 *        	The matrix to transpose.
 * @return Matrix The matrix transposed.
 */
function transpose(Matrix $input)
{
    $transposeMatrix = array();
    $d = $input->getData();
    for ($r = 0; $r < $input->getRows(); ++$r) {
        for ($c = 0; $c < $input->getCols(); ++$c) {
            $transposeMatrix[$c][$r] = $d[$r][$c];
        }
    }
    return Matrix::matrixFromDoubles($transposeMatrix);
}
 /**
  * Solve A*X = B.
  *
  * @param
  *        	b
  *        	A Matrix with as many rows as A and any number of columns.
  * @return X so that L*L'*X = b.
  */
 public function solve(Matrix $b)
 {
     if ($b->getRows() != $this->n) {
         throw new MatrixError("Matrix row dimensions must agree.");
     }
     if (!$this->isspd) {
         throw new RuntimeException("Matrix is not symmetric positive definite.");
     }
     // Copy right hand side.
     $x = $b->getArrayCopy();
     $nx = $b->getCols();
     // Solve L*Y = B;
     for ($k = 0; $k < $this->n; ++$k) {
         for ($j = 0; $j < $nx; ++$j) {
             for ($i = 0; $i < $k; ++$i) {
                 $x[$k][$j] -= $x[$i][$j] * $this->l[$k][$i];
             }
             $x[$k][$j] /= $this->l[$k][$k];
         }
     }
     // Solve L'*X = Y;
     for ($k = $this->n - 1; $k >= 0; $k--) {
         for ($j = 0; $j < $nx; ++$j) {
             for ($i = $k + 1; $i < $this->n; ++$i) {
                 $x[$k][$j] -= $x[$i][$j] * $this->l[$i][$k];
             }
             $x[$k][$j] /= $this->l[$k][$k];
         }
     }
     return Matrix::matrixFromDoubles($x);
 }