MathPHP\LinearAlgebra\Matrix::multiply PHP Method

multiply() public method

Matrix multiplication https://en.wikipedia.org/wiki/Matrix_multiplication#Matrix_product_.28two_matrices.29
public multiply ( Matrix/Vector $B ) : Matrix
$B Matrix/Vector
return Matrix
    public function multiply($B) : Matrix
    {
        if (!$B instanceof Matrix && !$B instanceof Vector) {
            throw new Exception\IncorrectTypeException('Can only do matrix multiplication with a Matrix or Vector');
        }
        if ($B instanceof Vector) {
            $B = $B->asColumnMatrix();
        }
        if ($B->getM() !== $this->n) {
            throw new Exception\MatrixException("Matrix dimensions do not match");
        }
        $n = $B->getN();
        $m = $this->m;
        $R = [];
        for ($i = 0; $i < $m; $i++) {
            for ($j = 0; $j < $n; $j++) {
                $VA = new Vector($this->getRow($i));
                $VB = new Vector($B->getColumn($j));
                $R[$i][$j] = $VA->dotProduct($VB);
            }
        }
        return MatrixFactory::create($R);
    }