Skip to content

Commit 54e82d7

Browse files
committed
Add addVector() and subtractVector() for convenience.
1 parent e5c18d3 commit 54e82d7

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/Vector.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ public function getSize(): int
4040
return $this->getColumnCount();
4141
}
4242

43+
/**
44+
* @param Vector $other
45+
* @return Vector
46+
*/
47+
public function addVector(self $other): self
48+
{
49+
return self::fromMatrix($this->addMatrix($other));
50+
}
51+
52+
/**
53+
* @param Vector $other
54+
* @return Vector
55+
*/
56+
public function subtractVector(self $other): self
57+
{
58+
return self::fromMatrix($this->subtractMatrix($other));
59+
}
60+
4361
/**
4462
* @return float
4563
*/

tests/VectorTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,32 @@ public function testGetSize()
5050
$this->assertEquals(4, $vector->getSize());
5151
}
5252

53+
public function testAddVector()
54+
{
55+
$vector1 = new Vector([1, 2, 3, 4]);
56+
$vector2 = new Vector([-1, 3, 5, 2]);
57+
58+
$added = $vector1->addVector($vector2);
59+
60+
static::assertEquals(0, $added->get(0, 0));
61+
static::assertEquals(5, $added->get(0, 1));
62+
static::assertEquals(8, $added->get(0, 2));
63+
static::assertEquals(6, $added->get(0, 3));
64+
}
65+
66+
public function testSubtractVector()
67+
{
68+
$vector1 = new Vector([1, 2, 3, 4]);
69+
$vector2 = new Vector([-1, 3, 5, 2]);
70+
71+
$added = $vector1->subtractVector($vector2);
72+
73+
static::assertEquals(2, $added->get(0, 0));
74+
static::assertEquals(-1, $added->get(0, 1));
75+
static::assertEquals(-2, $added->get(0, 2));
76+
static::assertEquals(2, $added->get(0, 3));
77+
}
78+
5379
public function testSum()
5480
{
5581
$vector = self::buildVector();

0 commit comments

Comments
 (0)