public function dotProduct(Vector $B) { if ($B->getN() !== $this->n) { throw new Exception\VectorException('Vectors have different number of items'); } return array_sum(array_map(function ($a, $b) { return $a * $b; }, $this->A, $B->getVector())); }
/** * Matrix multiplication * https://en.wikipedia.org/wiki/Matrix_multiplication#Matrix_product_.28two_matrices.29 * * @param Matrix/Vector $B Matrix or Vector to multiply * * @return Matrix * * @throws IncorrectTypeException if parameter B is not a Matrix or Vector * @throws MatrixException if matrix dimensions do not match */ 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); }