Skip to content

Commit e63a3f1

Browse files
committed
Split add into scalar and matrix versions. Deprecate the old function.
1 parent b607688 commit e63a3f1

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/Matrix.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,39 @@ public function __toString()
9797
* @param Matrix|int|float $value Matrix or scalar to add to this matrix
9898
* @return Matrix
9999
* @throws MatrixException
100+
* @deprecated
100101
*/
101102
public function add($value)
102103
{
103104
if ($value instanceof Matrix) {
104-
if ($this->rows != $value->rows || $this->columns != $value->columns) {
105-
throw new MatrixException('Cannot add two matrices of different size.');
106-
}
105+
return $this->addMatrix($value);
106+
}
107107

108-
return $this->map(function ($element, $i, $j) use ($value) {
109-
return $element + $value->get($i, $j);
110-
});
108+
return $this->addScalar($value);
109+
}
110+
111+
/**
112+
* @param Matrix $value
113+
* @return Matrix
114+
* @throws MatrixException
115+
*/
116+
public function addMatrix(Matrix $value)
117+
{
118+
if ($this->rows != $value->rows || $this->columns != $value->columns) {
119+
throw new MatrixException('Cannot add two matrices of different size.');
111120
}
112121

122+
return $this->map(function ($element, $i, $j) use ($value) {
123+
return $element + $value->get($i, $j);
124+
});
125+
}
126+
127+
/**
128+
* @param float $value
129+
* @return Matrix
130+
*/
131+
public function addScalar($value)
132+
{
113133
return $this->map(function ($element) use ($value) {
114134
return $element + $value;
115135
});

0 commit comments

Comments
 (0)