Beispiel #1
0
 public function testZeroMatrix()
 {
     $expected = array(array(0.0, 0.0, 0.0), array(0.0, 0.0, 0.0), array(0.0, 0.0, 0.0));
     $this->assertEquals($expected, MatrixFactory::zeroMatrix(3)->getArray());
     //
     $expected = array(array(0.0, 0.0, 0.0), array(0.0, 0.0, 0.0));
     $this->assertEquals($expected, MatrixFactory::zeroMatrix(2, 3)->getArray());
 }
Beispiel #2
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;
 }
Beispiel #3
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;
 }