Skip to content

Commit f53eb1b

Browse files
committed
Refactoring Vector classes
1 parent 1eb00e9 commit f53eb1b

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

src/Math/Linear/Vector.php

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ public function __construct(array $components) {
3333
* @psalm-suppress MissingClosureReturnType
3434
*/
3535
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');
4037
$result = array_map(function ($a, $b) {
4138
return $a + $b;
4239
}, $this->components, $other->components);
@@ -54,10 +51,7 @@ public function add(Vector $other): Vector {
5451
* @psalm-suppress MissingClosureReturnType
5552
*/
5653
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');
6155
$result = array_map(function ($a, $b) {
6256
return $a - $b;
6357
}, $this->components, $other->components);
@@ -91,10 +85,7 @@ public function scalarMultiply($scalar): Vector {
9185
* @psalm-suppress MixedOperand
9286
*/
9387
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');
9889
return array_sum(array_map(function ($a, $b) {
9990
return $a * $b;
10091
}, $this->components, $other->components));
@@ -197,6 +188,7 @@ public function getComponent(int $index): float|int {
197188
* @throws Exception If either vector has zero magnitude or dimensions don't match
198189
*/
199190
public function angleBetween(Vector $other): float {
191+
$this->assertSameDimension($other, 'angle calculation');
200192
$mag1 = $this->magnitude();
201193
$mag2 = $other->magnitude();
202194
if ($mag1 == 0 || $mag2 == 0) {
@@ -294,15 +286,13 @@ public function projectOnto(Vector $other): Vector {
294286
* @throws Exception If vectors have different dimensions
295287
*/
296288
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');
300290
$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+
*/
306296
function ($a, $b): float {
307297
return (float)$a * (float)$b;
308298
},
@@ -320,9 +310,7 @@ function ($a, $b): float {
320310
* @throws Exception If vectors have different dimensions
321311
*/
322312
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');
326314
$sum = 0;
327315
/** @psalm-suppress MixedAssignment */
328316
foreach ($this->components as $i => $value) {
@@ -340,9 +328,7 @@ public function euclideanDistance(Vector $other): float {
340328
* @throws Exception If vectors have different dimensions
341329
*/
342330
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');
346332
$sum = 0;
347333
foreach ($this->components as $i => $value) {
348334
$sum += abs((float)$value - (float)$other->components[$i]);
@@ -370,9 +356,21 @@ public function angleBetweenDegrees(Vector $other): float {
370356
* @throws Exception If vectors have different dimensions
371357
*/
372358
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 {
373372
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.");
375374
}
376-
return abs($this->dotProduct($other)) < $epsilon;
377375
}
378376
}

0 commit comments

Comments
 (0)