Skip to content

Commit 08b12e6

Browse files
committed
Split subtract function into scalar and matrix versions.
1 parent 9353bed commit 08b12e6

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

src/Matrix.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -609,22 +609,42 @@ public function submatrix($row = null, $column = null)
609609
* @param Matrix|int|float $value Matrix or scalar to subtract from this matrix
610610
* @return Matrix
611611
* @throws MatrixException
612+
* @deprecated
612613
*/
613614
public function subtract($value)
614615
{
615616
if ($value instanceof Matrix) {
616-
if ($this->rows != $value->rows || $this->columns != $value->columns) {
617-
throw new MatrixException('Cannot subtract two matrices of different size.');
618-
}
617+
return $this->subtractMatrix($value);
618+
}
619619

620-
return $this->map(function ($element, $i, $j) use ($value) {
621-
return $element - $value->get($i, $j);
622-
});
623-
} else {
624-
return $this->map(function ($element) use ($value) {
625-
return $element - $value;
626-
});
620+
return $this->subtractScalar($value);
621+
}
622+
623+
/**
624+
* @param Matrix $value
625+
* @return Matrix
626+
* @throws MatrixException
627+
*/
628+
public function subtractMatrix(Matrix $value)
629+
{
630+
if ($this->rowCount != $value->rowCount || $this->columnCount != $value->columnCount) {
631+
throw new MatrixException('Cannot subtract two matrices of different size.');
627632
}
633+
634+
return $this->map(function ($element, $i, $j) use ($value) {
635+
return $element - $value->get($i, $j);
636+
});
637+
}
638+
639+
/**
640+
* @param float $value
641+
* @return Matrix
642+
*/
643+
public function subtractScalar($value)
644+
{
645+
return $this->map(function ($element) use ($value) {
646+
return $element - $value;
647+
});
628648
}
629649

630650
/**

0 commit comments

Comments
 (0)