Exemplo n.º 1
0
 public function evaluate(array $params)
 {
     $m = $this->m;
     $n = $this->n;
     $R = [];
     for ($i = 0; $i < $m; $i++) {
         for ($j = 0; $j < $n; $j++) {
             $func = $this->A[$i][$j];
             $R[$i][$j] = $func($params);
         }
     }
     return MatrixFactory::create($R);
 }
Exemplo n.º 2
0
    public function testMatrixFromString()
    {
        $str = '
			1, 2, 4;
			5,6,8;3, 1,

			1
		';
        $expected = array(array(1, 2, 4), array(5, 6, 8), array(3, 1, 1));
        $this->assertEquals($expected, MatrixFactory::fromString($str)->getArray());
        //
        $str = '  1;5; 3';
        $expected = array(array(1), array(5), array(3));
        $this->assertEquals($expected, MatrixFactory::fromString($str)->getArray());
    }
Exemplo n.º 3
0
    public function testMap()
    {
        $callback = function ($elem) {
            return $elem * 2;
        };
        $c = MatrixFactory::fromString('2, 4;
			 6, 8');
        $this->assertEquals($c->getArray(), $this->B->map($callback)->getArray());
    }
Exemplo n.º 4
0
 /**
  * @dataProvider dataProviderForFrobeniusNorm
  */
 public function testFrobeniusNorm(array $A, $norm)
 {
     $A = MatrixFactory::create($A);
     $this->assertEquals($norm, $A->frobeniusNorm(), '', 0.0001);
 }
Exemplo n.º 5
0
 public function testDiv()
 {
     $a = $this->B;
     $this->assertEquals(MatrixFactory::identityMatrix(2)->getArray(), $a->div($a)->getArray());
 }
 /**
  * @dataProvider dataProviderForRowExclude
  */
 public function testRowExclude(array $A, int $mᵢ, array $R)
 {
     $A = MatrixFactory::create($A);
     $R = MatrixFactory::create($R);
     $this->assertEquals($R, $A->rowExclude($mᵢ));
 }
Exemplo n.º 7
0
 /**
  * @dataProvider dataProviderForJsonSerialize
  */
 public function testJsonSerialize(array $A, string $json)
 {
     $A = MatrixFactory::create($A);
     $this->assertEquals($json, json_encode($A));
 }
 /**
  * @dataProvider dataProviderForSolve
  * Compute the RREF before trying to solve.
  */
 public function testSolveRREF(array $A, array $b, array $expected)
 {
     $A = MatrixFactory::create($A);
     $b = new Vector($b);
     $expected = new Vector($expected);
     $A->rref();
     $x = $A->solve($b);
     $this->assertEquals($expected, $x, '', 0.001);
 }
Exemplo n.º 9
0
    public function testTranspose()
    {
        $trans_A = MatrixFactory::fromString('1, 2, 5;
			 5, 2, 5;
			 4, 2, 1');
        $this->assertEquals($trans_A->getArray(), MatrixAlgorithms::transpose($this->A)->getArray());
    }
Exemplo n.º 10
0
 /**
  * Axiom: det(A ⊗ B) = det(A)ᵐ det(B)ⁿ
  * Determinant of Kronecker product - where A is nxn matrix, and b is nxn matrix
  * @dataProvider dataProviderForKroneckerProductDeterminant
  */
 public function testKroneckerProductDeterminant(array $A, array $B)
 {
     $A = MatrixFactory::create($A);
     $B = MatrixFactory::create($B);
     $det⟮A⟯ᵐ = $A->det() ** $B->getM();
     $det⟮B⟯ⁿ = $B->det() ** $A->getN();
     $det⟮A⊗B⟯ = $A->kroneckerProduct($B)->det();
     $this->assertEquals($det⟮A⊗B⟯, $det⟮A⟯ᵐ * $det⟮B⟯ⁿ, '', 0.0001);
 }
Exemplo n.º 11
0
 /**
  * Return product of matrices
  *
  * @param Matrix|number $B
  * @return Matrix
  * @throws InvalidArgumentException
  */
 public function prod($B)
 {
     // Умножение на число
     if (is_numeric($B)) {
         list($rows, $cols) = $this->getSize();
         $new_matrix = MatrixFactory::zeroMatrix($rows, $cols);
         for ($row = 0; $row < $rows; $row++) {
             for ($col = 0; $col < $cols; $col++) {
                 $temp = $this->getElem($row, $col) * $B;
                 $new_matrix->setElem($row, $col, $temp);
             }
         }
         return $new_matrix;
     }
     if (!Matrix::isMatrix($B)) {
         throw new InvalidArgumentException("Argument must be Matrix or number");
     }
     list($rows1, $cols1) = $this->getSize();
     list($rows2, $cols2) = $B->getSize();
     if ($cols1 !== $rows2) {
         throw new InvalidArgumentException("Invalid size of matrices");
     }
     $new_matrix = MatrixFactory::zeroMatrix($rows1, $cols2);
     for ($row = 0; $row < $rows1; $row++) {
         for ($col = 0; $col < $cols2; $col++) {
             $sum = 0;
             for ($k = 0; $k < $cols1; $k++) {
                 $sum += $this->getElem($row, $k) * $B->getElem($k, $col);
             }
             $new_matrix->setElem($row, $col, $sum);
         }
     }
     return $new_matrix;
 }
Exemplo n.º 12
0
 public function testCofactorMatrixExceptionNotSquare()
 {
     $A = MatrixFactory::create([[1, 2, 3, 4], [2, 3, 4, 4], [3, 4, 5, 4]]);
     $this->setExpectedException('MathPHP\\Exception\\MatrixException');
     $A->cofactorMatrix();
 }
Exemplo n.º 13
0
 /**
  * @dataProvider dataProviderForEyeExceptions
  */
 public function testEyeExceptions(int $m, int $n, int $k, int $x)
 {
     $this->setExpectedException('MathPHP\\Exception\\OutOfBoundsException');
     $A = MatrixFactory::eye($m, $n, $k, $x);
 }
Exemplo n.º 14
0
 /**
  * Return transposed matrix
  *
  * @static
  * @param Matrix $matrix
  * @return Matrix
  */
 public static function transpose($matrix)
 {
     list($rows, $cols) = $matrix->getSize();
     $T = MatrixFactory::zeroMatrix($cols, $rows);
     for ($i = 0; $i < $rows; $i++) {
         for ($j = 0; $j < $cols; $j++) {
             $T->setElem($j, $i, $matrix->getElem($i, $j));
         }
     }
     return $T;
 }
Exemplo n.º 15
0
 public function testInsertRow()
 {
     $value = MatrixFactory::fromString('1, 2, 3; 7, 8, 9');
     $insertion = MatrixFactory::fromString('4, 5, 6');
     $expect = MatrixFactory::fromString('1, 2, 3; 4, 5, 6; 7, 8, 9');
     $this->assertEquals($expect->getArray(), $value->insertRow(1, $insertion)->getArray());
 }
 /**
  * @dataProvider dataProviderForColumnExclude
  */
 public function testColumnExclude(array $A, int $nᵢ, array $R)
 {
     $A = MatrixFactory::create($A);
     $R = MatrixFactory::create($R);
     $this->assertEquals($R, $A->columnExclude($nᵢ));
 }