@@ -133,6 +133,28 @@ public function multiplyByScalar($value): self
133
133
return new static ($ result );
134
134
}
135
135
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
+
136
158
/**
137
159
* Performs addition of two matrices
138
160
*
@@ -239,6 +261,11 @@ public static function __doOperation(int $opCode, $left, $right): Matrix
239
261
return $ left ->divideByScalar ($ right );
240
262
}
241
263
break ;
264
+ case OpCode::POW :
265
+ if ($ isLeftMatrix && $ isRightNumeric ) {
266
+ return $ left ->powByScalar ($ right );
267
+ }
268
+ break ;
242
269
}
243
270
244
271
throw new \LogicException ('Unsupported ' . OpCode::name ($ opCode ). ' operation or invalid arguments ' );
0 commit comments