@@ -33,10 +33,7 @@ public function __construct(array $components) {
33
33
* @psalm-suppress MissingClosureReturnType
34
34
*/
35
35
public function add (Vector $ other ): Vector {
36
- if (count ($ this ->components ) !== count ($ other ->components )) {
37
- throw new Exception ('Vectors must have the same dimension for addition. ' );
38
- }
39
-
36
+ $ this ->assertSameDimension ($ other , 'addition ' );
40
37
$ result = array_map (function ($ a , $ b ) {
41
38
return $ a + $ b ;
42
39
}, $ this ->components , $ other ->components );
@@ -54,10 +51,7 @@ public function add(Vector $other): Vector {
54
51
* @psalm-suppress MissingClosureReturnType
55
52
*/
56
53
public function subtract (Vector $ other ): Vector {
57
- if (count ($ this ->components ) !== count ($ other ->components )) {
58
- throw new Exception ('Vectors must have the same dimension for subtraction. ' );
59
- }
60
-
54
+ $ this ->assertSameDimension ($ other , 'subtraction ' );
61
55
$ result = array_map (function ($ a , $ b ) {
62
56
return $ a - $ b ;
63
57
}, $ this ->components , $ other ->components );
@@ -91,10 +85,7 @@ public function scalarMultiply($scalar): Vector {
91
85
* @psalm-suppress MixedOperand
92
86
*/
93
87
public function dotProduct (Vector $ other ): float {
94
- if (count ($ this ->components ) !== count ($ other ->components )) {
95
- throw new Exception ('Vectors must have the same dimension for dot product. ' );
96
- }
97
-
88
+ $ this ->assertSameDimension ($ other , 'dot product ' );
98
89
return array_sum (array_map (function ($ a , $ b ) {
99
90
return $ a * $ b ;
100
91
}, $ this ->components , $ other ->components ));
@@ -197,6 +188,7 @@ public function getComponent(int $index): float|int {
197
188
* @throws Exception If either vector has zero magnitude or dimensions don't match
198
189
*/
199
190
public function angleBetween (Vector $ other ): float {
191
+ $ this ->assertSameDimension ($ other , 'angle calculation ' );
200
192
$ mag1 = $ this ->magnitude ();
201
193
$ mag2 = $ other ->magnitude ();
202
194
if ($ mag1 == 0 || $ mag2 == 0 ) {
@@ -294,15 +286,13 @@ public function projectOnto(Vector $other): Vector {
294
286
* @throws Exception If vectors have different dimensions
295
287
*/
296
288
public function hadamardProduct (Vector $ other ): Vector {
297
- if ($ this ->getDimension () !== $ other ->getDimension ()) {
298
- throw new Exception ('Vectors must have the same dimension for Hadamard product. ' );
299
- }
289
+ $ this ->assertSameDimension ($ other , 'Hadamard product ' );
300
290
$ result = array_map (
301
- /**
302
- * @param float|int $a
303
- * @param float|int $b
304
- * @return float
305
- */
291
+ /**
292
+ * @param float|int $a
293
+ * @param float|int $b
294
+ * @return float
295
+ */
306
296
function ($ a , $ b ): float {
307
297
return (float )$ a * (float )$ b ;
308
298
},
@@ -320,9 +310,7 @@ function ($a, $b): float {
320
310
* @throws Exception If vectors have different dimensions
321
311
*/
322
312
public function euclideanDistance (Vector $ other ): float {
323
- if ($ this ->getDimension () !== $ other ->getDimension ()) {
324
- throw new Exception ('Vectors must have the same dimension for distance calculation. ' );
325
- }
313
+ $ this ->assertSameDimension ($ other , 'distance calculation ' );
326
314
$ sum = 0 ;
327
315
/** @psalm-suppress MixedAssignment */
328
316
foreach ($ this ->components as $ i => $ value ) {
@@ -340,9 +328,7 @@ public function euclideanDistance(Vector $other): float {
340
328
* @throws Exception If vectors have different dimensions
341
329
*/
342
330
public function manhattanDistance (Vector $ other ): float {
343
- if ($ this ->getDimension () !== $ other ->getDimension ()) {
344
- throw new Exception ('Vectors must have the same dimension for distance calculation. ' );
345
- }
331
+ $ this ->assertSameDimension ($ other , 'distance calculation ' );
346
332
$ sum = 0 ;
347
333
foreach ($ this ->components as $ i => $ value ) {
348
334
$ sum += abs ((float )$ value - (float )$ other ->components [$ i ]);
@@ -370,9 +356,21 @@ public function angleBetweenDegrees(Vector $other): float {
370
356
* @throws Exception If vectors have different dimensions
371
357
*/
372
358
public function isOrthogonalTo (Vector $ other , float $ epsilon = 1e-9 ): bool {
359
+ $ this ->assertSameDimension ($ other , 'orthogonality check ' );
360
+ return abs ($ this ->dotProduct ($ other )) < $ epsilon ;
361
+ }
362
+
363
+ /**
364
+ * Ensures that this vector and the other vector have the same dimension.
365
+ *
366
+ * @param Vector $other The other vector
367
+ * @param string $context The context for the exception message
368
+ * @return void
369
+ * @throws Exception If dimensions do not match
370
+ */
371
+ private function assertSameDimension (Vector $ other , string $ context = 'operation ' ): void {
373
372
if ($ this ->getDimension () !== $ other ->getDimension ()) {
374
- throw new Exception (' Vectors must have the same dimension to check orthogonality. ' );
373
+ throw new Exception (" Vectors must have the same dimension for $ context . " );
375
374
}
376
- return abs ($ this ->dotProduct ($ other )) < $ epsilon ;
377
375
}
378
376
}
0 commit comments