MCordingley\LinearAlgebra\Matrix::multiplyMatrix PHP Метод

multiplyMatrix() публичный Метод

public multiplyMatrix ( self $value ) : self
$value self
Результат self
    public function multiplyMatrix(self $value) : self
    {
        if ($this->getColumnCount() !== $value->getRowCount()) {
            throw new MatrixException('Cannot multiply matrices of these sizes.');
        }
        $literal = [];
        for ($i = 0, $rows = $this->getRowCount(); $i < $rows; $i++) {
            $row = [];
            for ($j = 0, $valueColumns = $value->getColumnCount(); $j < $valueColumns; $j++) {
                $sum = 0;
                for ($k = 0, $columns = $this->getColumnCount(); $k < $columns; $k++) {
                    $sum += $this->get($i, $k) * $value->get($k, $j);
                }
                $row[] = $sum;
            }
            $literal[] = $row;
        }
        return new static($literal);
    }

Usage Example

Пример #1
0
 public function testMultiplyMatrix()
 {
     $matrix1 = new Matrix([[1, 2, 3], [4, 5, 6]]);
     $matrix2 = new Matrix([[7, 8], [9, 10], [11, 12]]);
     $multiplied = $matrix1->multiplyMatrix($matrix2);
     static::assertEquals(58, $multiplied->get(0, 0));
     static::assertEquals(64, $multiplied->get(0, 1));
     static::assertEquals(139, $multiplied->get(1, 0));
     static::assertEquals(154, $multiplied->get(1, 1));
 }