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);
}