Skip to content

Commit e2e5ad1

Browse files
committed
Switch to using a single array (not nested) for the constructor, since a vector should never have more than one row. Add helper factory method to cast from a matrix to a vector.
1 parent dff0f45 commit e2e5ad1

File tree

2 files changed

+39
-54
lines changed

2 files changed

+39
-54
lines changed

src/Vector.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,22 @@
66

77
final class Vector extends Matrix
88
{
9+
/**
10+
* @param array $literal
11+
*/
912
public function __construct(array $literal)
1013
{
11-
parent::__construct($literal);
14+
parent::__construct([$literal]);
15+
}
16+
17+
/**
18+
* @param Matrix $matrix
19+
* @param int $row
20+
* @return Vector
21+
*/
22+
public static function fromMatrix(Matrix $matrix, int $row = 0): self
23+
{
24+
return new self($matrix->toArray()[$row]);
1225
}
1326

1427
/**************************************************************************
@@ -156,7 +169,7 @@ public function crossProduct(Vector $other): self
156169
$y = -(($this->toArray()[0] * $other->toArray()[2]) - ($this->toArray()[2] * $other->toArray()[0]));
157170
$z = ($this->toArray()[0] * $other->toArray()[1]) - ($this->toArray()[1] * $other->toArray()[0]);
158171

159-
return new Vector([[$x, $y, $z]]);
172+
return new self([$x, $y, $z]);
160173
}
161174

162175
/**
@@ -176,7 +189,7 @@ public function normalize(): self
176189
{
177190
$norm = $this->l2norm();
178191

179-
return $this->divideScalar($norm);
192+
return self::fromMatrix($this->divideScalar($norm));
180193
}
181194

182195
/**
@@ -193,7 +206,7 @@ public function normalize(): self
193206
*/
194207
public function projection(self $other): self
195208
{
196-
return $other->multiplyScalar($this->dotProduct($other) / (($other->l2norm())**2));
209+
return self::fromMatrix($other->multiplyScalar($this->dotProduct($other) / ($other->l2norm() ** 2)));
197210
}
198211

199212
/**************************************************************************

tests/VectorTest.php

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ class VectorTest extends \PHPUnit_Framework_TestCase
99
{
1010
private function buildVector()
1111
{
12-
return new Vector([
13-
[1, 2, 3, 4],
14-
]);
12+
return new Vector([1, 2, 3, 4]);
1513
}
1614

1715
public function testEmptyCount()
@@ -42,66 +40,46 @@ public function testSum()
4240

4341
public function testLength()
4442
{
45-
$vector = new Vector([
46-
[3, 4]
47-
]);
43+
$vector = new Vector([3, 4]);
4844

4945
$this->assertEquals(5, $vector->length());
5046
}
5147

5248
public function testDotProduct()
5349
{
54-
$vector1 = new Vector([
55-
[1,2,3,4]
56-
]);
57-
$vector2 = new Vector([
58-
[-1,3,5,2]
59-
]);
50+
$vector1 = new Vector([1, 2, 3, 4]);
51+
$vector2 = new Vector([-1, 3, 5, 2]);
6052

6153
$this->assertEquals((1 * -1) + (2 * 3) + (3 * 5) + (4 * 2), $vector1->dotProduct($vector2));
6254
}
6355

6456
public function testOuterProduct()
6557
{
66-
$vector1 = new Vector([
67-
[1,2]
68-
]);
69-
$vector2 = new Vector([
70-
[3,4,5]
71-
]);
58+
$vector1 = new Vector([1, 2]);
59+
$vector2 = new Vector([3, 4, 5]);
7260

73-
$this->assertEquals(new Matrix([[3,4,5],[6,8,10]]), $vector1->outerProduct($vector2));
61+
$this->assertEquals(new Matrix([[3, 4, 5], [6, 8, 10]]), $vector1->outerProduct($vector2));
7462

7563
//this test case appear A ⨂ B != B ⨂ A
76-
$vector1 = new Vector([
77-
[3,4,5]
78-
]);
79-
$vector2 = new Vector([
80-
[1,2]
81-
]);
82-
83-
$this->assertEquals(new Matrix([[3,6],[4,8],[5,10]]), $vector1->outerProduct($vector2));
64+
$vector1 = new Vector([3, 4, 5]);
65+
$vector2 = new Vector([1, 2]);
66+
67+
$this->assertEquals(new Matrix([[3, 6], [4, 8], [5, 10]]), $vector1->outerProduct($vector2));
8468
}
8569

8670
public function testCrossProduct()
8771
{
88-
$vector1 = new Vector([
89-
[2,3,4]
90-
]);
91-
$vector2 = new Vector([
92-
[5,6,7]
93-
]);
94-
95-
$this->assertEquals(new Vector([[-3,6,-3]]), $vector1->crossProduct($vector2));
72+
$vector1 = new Vector([2, 3, 4]);
73+
$vector2 = new Vector([5, 6, 7]);
74+
75+
$this->assertEquals(new Vector([-3, 6, -3]), $vector1->crossProduct($vector2));
9676
}
9777

9878
public function testNormalize()
9979
{
100-
$vector = new Vector([
101-
[3,4]
102-
]);
80+
$vector = new Vector([3, 4]);
10381

104-
$this->assertEquals(new Vector([[3/5, 4/5]]), $vector->normalize());
82+
$this->assertEquals(new Vector([3 / 5, 4 / 5]), $vector->normalize());
10583
}
10684

10785
public function testL1Norm()
@@ -111,22 +89,16 @@ public function testL1Norm()
11189

11290
public function testL2Norm()
11391
{
114-
$vector = new Vector([
115-
[3, 4]
116-
]);
92+
$vector = new Vector([3, 4]);
11793

11894
$this->assertEquals(5, $vector->l2Norm());
11995
}
12096

12197
public function testProjection()
12298
{
123-
$vector1 = new Vector([
124-
[1,1]
125-
]);
126-
$vector2 = new Vector([
127-
[3,1]
128-
]);
129-
130-
$this->assertEquals(new Vector([[12/10, 4/10]]), $vector1->projection($vector2));
99+
$vector1 = new Vector([1, 1]);
100+
$vector2 = new Vector([3, 1]);
101+
102+
$this->assertEquals(new Vector([12 / 10, 4 / 10]), $vector1->projection($vector2));
131103
}
132104
}

0 commit comments

Comments
 (0)