Skip to content

Commit aee0871

Browse files
authored
Merge pull request #1 from berezuev/exponent
Add pow by scalar on matrix function
2 parents c3c5167 + a96a74a commit aee0871

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/Matrix.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,28 @@ public function multiplyByScalar($value): self
133133
return new static($result);
134134
}
135135

136+
/**
137+
* Performs exponential expression by scalar value
138+
*
139+
* @param int|float $value Exponent
140+
*
141+
* @return $this
142+
*/
143+
public function powByScalar($value): self
144+
{
145+
if (!is_numeric($value)) {
146+
throw new \InvalidArgumentException("Exponent accepts only numeric values");
147+
}
148+
$result = [];
149+
for ($i = 0; $i < $this->rows; ++$i) {
150+
for ($j = 0; $j < $this->columns; ++$j) {
151+
$result[$i][$j] = $this->matrix[$i][$j] ** $value;
152+
}
153+
}
154+
155+
return new static($result);
156+
}
157+
136158
/**
137159
* Performs addition of two matrices
138160
*
@@ -239,6 +261,11 @@ public static function __doOperation(int $opCode, $left, $right): Matrix
239261
return $left->divideByScalar($right);
240262
}
241263
break;
264+
case OpCode::POW:
265+
if ($isLeftMatrix && $isRightNumeric) {
266+
return $left->powByScalar($right);
267+
}
268+
break;
242269
}
243270

244271
throw new \LogicException('Unsupported ' . OpCode::name($opCode). ' operation or invalid arguments');

0 commit comments

Comments
 (0)