Skip to content

Commit 86853be

Browse files
committed
Factor out additional copy of the internal representation.
1 parent 309edc4 commit 86853be

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

src/Vector.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,9 @@
66

77
final class Vector extends Matrix
88
{
9-
/**
10-
* @var array
11-
*/
12-
private $vector;
13-
149
public function __construct(array $literal)
1510
{
1611
parent::__construct($literal);
17-
18-
$this->vector = $literal[0];
1912
}
2013

2114
/**************************************************************************
@@ -30,7 +23,7 @@ public function __construct(array $literal)
3023
*/
3124
public function getVector(): array
3225
{
33-
return $this->vector;
26+
return $this->internal[0];
3427
}
3528

3629
/**
@@ -56,7 +49,7 @@ public function getSize(): int
5649
*/
5750
public function sum()
5851
{
59-
return array_sum($this->vector);
52+
return array_sum($this->getVector());
6053
}
6154

6255
/**
@@ -89,7 +82,7 @@ public function dotProduct(self $other)
8982
function ($a, $b) {
9083
return $a * $b;
9184
},
92-
$this->vector,
85+
$this->getVector(),
9386
$other->getVector()
9487
));
9588
}
@@ -135,7 +128,7 @@ public function outerProduct(Vector $other): Matrix
135128
$literal = array();
136129
for ($i = 0; $i < $this->getSize(); $i++) {
137130
for ($j = 0; $j < $other->getSize(); $j++) {
138-
$literal[$i][$j] = $this->vector[$i] * $other->getVector()[$j];
131+
$literal[$i][$j] = $this->getVector()[$i] * $other->getVector()[$j];
139132
}
140133
}
141134

@@ -160,9 +153,9 @@ public function crossProduct(Vector $other): self
160153
throw new VectorException('Vectors have to have 3 size');
161154
}
162155

163-
$x = ($this->vector[1] * $other->vector[2]) - ($this->vector[2] * $other->vector[1]);
164-
$y = -(($this->vector[0] * $other->vector[2]) - ($this->vector[2] * $other->vector[0]));
165-
$z = ($this->vector[0] * $other->vector[1]) - ($this->vector[1] * $other->vector[0]);
156+
$x = ($this->getVector()[1] * $other->getVector()[2]) - ($this->getVector()[2] * $other->getVector()[1]);
157+
$y = -(($this->getVector()[0] * $other->getVector()[2]) - ($this->getVector()[2] * $other->getVector()[0]));
158+
$z = ($this->getVector()[0] * $other->getVector()[1]) - ($this->getVector()[1] * $other->getVector()[0]);
166159

167160
return new Vector([[$x, $y, $z]]);
168161
}
@@ -224,7 +217,8 @@ public function projection(self $other): self
224217
public function l1Norm()
225218
{
226219
$sum = 0;
227-
foreach($this->vector as $value) {
220+
221+
foreach ($this->getVector() as $value) {
228222
$sum += abs($value);
229223
}
230224

@@ -245,9 +239,10 @@ public function l1Norm()
245239
*/
246240
public function l2Norm(): float
247241
{
248-
$literal = array();
242+
$literal = [];
243+
249244
for ($i = 0, $rows = $this->getSize(); $i < $rows; $i++) {
250-
$literal[] = $this->vector[$i]**2;
245+
$literal[] = $this->getVector()[$i] ** 2;
251246
}
252247

253248
return sqrt(array_sum($literal));
@@ -262,8 +257,9 @@ public function l2Norm(): float
262257
*/
263258
public function maxNorm()
264259
{
265-
$max = abs($this->vector[0]);
266-
foreach($this->vector as $value) {
260+
$max = abs($this->getVector()[0]);
261+
262+
foreach($this->getVector() as $value) {
267263
if(abs($value) > $max) {
268264
$max = abs($value);
269265
}

0 commit comments

Comments
 (0)