Skip to content

Commit 97c7012

Browse files
committed
A few blanket optimizations
1 parent 9e9ae49 commit 97c7012

File tree

4 files changed

+69
-38
lines changed

4 files changed

+69
-38
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
- Unreleased
2+
3+
- 1.0.1
24
- Added Column Vector
35
- Implemented Eigenvalue decomposition
46
- Added solve system of linear equations

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"homepage": "https://github.com/RubixML/Tensor",
66
"license": "MIT",
77
"keywords": [
8-
"math", "linear-algebra", "matrix", "vector", "tensor", "statistics"
8+
"math", "linear-algebra", "matrix", "vector", "tensor", "statistics", "php"
99
],
1010
"authors": [
1111
{

src/ColumnVector.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ protected function multiplyMatrix(Matrix $b) : Matrix
7474
$c = [];
7575

7676
foreach ($b as $i => $row) {
77+
$valueA = $this->a[$i];
78+
7779
$temp = [];
7880

79-
foreach ($row as $value) {
80-
$temp[] = $this->a[$i] * $value;
81+
foreach ($row as $valueB) {
82+
$temp[] = $valueA * $valueB;
8183
}
8284

8385
$c[] = $temp;
@@ -103,10 +105,12 @@ protected function divideMatrix(Matrix $b) : Matrix
103105
$c = [];
104106

105107
foreach ($b as $i => $row) {
108+
$valueA = $this->a[$i];
109+
106110
$temp = [];
107111

108-
foreach ($row as $value) {
109-
$temp[] = $this->a[$i] / $value;
112+
foreach ($row as $valueB) {
113+
$temp[] = $valueA / $valueB;
110114
}
111115

112116
$c[] = $temp;
@@ -132,10 +136,12 @@ protected function addMatrix(Matrix $b) : Matrix
132136
$c = [];
133137

134138
foreach ($b as $i => $row) {
139+
$valueA = $this->a[$i];
140+
135141
$temp = [];
136142

137-
foreach ($row as $value) {
138-
$temp[] = $this->a[$i] + $value;
143+
foreach ($row as $valueB) {
144+
$temp[] = $valueA + $valueB;
139145
}
140146

141147
$c[] = $temp;
@@ -162,10 +168,12 @@ protected function subtractMatrix(Matrix $b) : Matrix
162168
$c = [];
163169

164170
foreach ($b as $i => $row) {
171+
$valueA = $this->a[$i];
172+
165173
$temp = [];
166174

167-
foreach ($row as $value) {
168-
$temp[] = $this->a[$i] - $value;
175+
foreach ($row as $valueB) {
176+
$temp[] = $valueA - $valueB;
169177
}
170178

171179
$c[] = $temp;
@@ -192,10 +200,12 @@ protected function powMatrix(Matrix $b) : Matrix
192200
$c = [];
193201

194202
foreach ($b as $i => $row) {
203+
$valueA = $this->a[$i];
204+
195205
$temp = [];
196206

197-
foreach ($row as $value) {
198-
$temp[] = $this->a[$i] ** $value;
207+
foreach ($row as $valueB) {
208+
$temp[] = $valueA ** $valueB;
199209
}
200210

201211
$c[] = $temp;
@@ -220,10 +230,12 @@ protected function modMatrix(Matrix $b) : Matrix
220230
$c = [];
221231

222232
foreach ($b as $i => $row) {
233+
$valueA = $this->a[$i];
234+
223235
$temp = [];
224236

225-
foreach ($row as $value) {
226-
$temp[] = $this->a[$i] % $value;
237+
foreach ($row as $valueB) {
238+
$temp[] = $valueA % $valueB;
227239
}
228240

229241
$c[] = $temp;

src/Matrix.php

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,8 @@ public static function stack(array $vectors) : self
361361

362362
foreach ($vectors as $vector) {
363363
if (!$vector instanceof Vector) {
364-
throw new InvalidArgumentException("Cannot build matrix"
365-
. " from a non vector, " . gettype($vector)
366-
. " found.");
364+
throw new InvalidArgumentException("Cannot stack non"
365+
. " vectors, " . gettype($vector) . " found.");
367366
}
368367

369368
$a[] = $vector->asArray();
@@ -571,13 +570,7 @@ public function asColumnVectors() : array
571570
*/
572571
public function flatten() : Vector
573572
{
574-
$b = [];
575-
576-
foreach ($this->a as $row) {
577-
foreach ($row as $value) {
578-
$b[] = $value;
579-
}
580-
}
573+
$b = array_reduce($this->a, 'array_merge', []);
581574

582575
return Vector::quick($b);
583576
}
@@ -1616,7 +1609,7 @@ public function round(int $precision = 0) : self
16161609

16171610
$b = [];
16181611

1619-
foreach ($this->a as $i => $row) {
1612+
foreach ($this->a as $row) {
16201613
$temp = [];
16211614

16221615
foreach ($row as $value) {
@@ -1667,7 +1660,7 @@ public function clip(float $min, float $max) : self
16671660

16681661
$b = [];
16691662

1670-
foreach ($this->a as $i => $row) {
1663+
foreach ($this->a as $row) {
16711664
$temp = [];
16721665

16731666
foreach ($row as $value) {
@@ -1701,7 +1694,7 @@ public function sign() : self
17011694
{
17021695
$b = [];
17031696

1704-
foreach ($this->a as $i => $row) {
1697+
foreach ($this->a as $row) {
17051698
$temp = [];
17061699

17071700
foreach ($row as $value) {
@@ -2290,10 +2283,12 @@ protected function multiplyColumnVector(ColumnVector $b) : self
22902283
$c = [];
22912284

22922285
foreach ($this->a as $i => $row) {
2286+
$valueB = $b[$i];
2287+
22932288
$temp = [];
22942289

2295-
foreach ($row as $value) {
2296-
$temp[] = $value * $b[$i];
2290+
foreach ($row as $valueA) {
2291+
$temp[] = $valueA * $valueB;
22972292
}
22982293

22992294
$c[] = $temp;
@@ -2319,10 +2314,12 @@ protected function divideColumnVector(ColumnVector $b) : self
23192314
$c = [];
23202315

23212316
foreach ($this->a as $i => $row) {
2317+
$valueB = $b[$i];
2318+
23222319
$temp = [];
23232320

2324-
foreach ($row as $value) {
2325-
$temp[] = $value / $b[$i];
2321+
foreach ($row as $valueA) {
2322+
$temp[] = $valueA / $valueB;
23262323
}
23272324

23282325
$c[] = $temp;
@@ -2348,10 +2345,12 @@ protected function addColumnVector(ColumnVector $b) : self
23482345
$c = [];
23492346

23502347
foreach ($this->a as $i => $row) {
2348+
$valueB = $b[$i];
2349+
23512350
$temp = [];
23522351

2353-
foreach ($row as $value) {
2354-
$temp[] = $value + $b[$i];
2352+
foreach ($row as $valueA) {
2353+
$temp[] = $valueA + $valueB;
23552354
}
23562355

23572356
$c[] = $temp;
@@ -2377,10 +2376,12 @@ protected function subtractColumnVector(ColumnVector $b) : self
23772376
$c = [];
23782377

23792378
foreach ($this->a as $i => $row) {
2379+
$valueB = $b[$i];
2380+
23802381
$temp = [];
23812382

2382-
foreach ($row as $value) {
2383-
$temp[] = $value - $b[$i];
2383+
foreach ($row as $valueA) {
2384+
$temp[] = $valueA - $valueB;
23842385
}
23852386

23862387
$c[]= $temp;
@@ -2406,10 +2407,12 @@ protected function powColumnVector(ColumnVector $b) : self
24062407
$c = [];
24072408

24082409
foreach ($this->a as $i => $row) {
2410+
$valueB = $b[$i];
2411+
24092412
$temp = [];
24102413

2411-
foreach ($row as $value) {
2412-
$temp[] = $value ** $b[$i];
2414+
foreach ($row as $valueA) {
2415+
$temp[] = $valueA ** $valueB;
24132416
}
24142417

24152418
$c[] = $temp;
@@ -2435,10 +2438,12 @@ protected function modColumnVector(ColumnVector $b) : self
24352438
$c = [];
24362439

24372440
foreach ($this->a as $i => $row) {
2441+
$valueB = $b[$i];
2442+
24382443
$temp = [];
24392444

2440-
foreach ($row as $value) {
2441-
$temp[] = $value % $b[$i];
2445+
foreach ($row as $valueA) {
2446+
$temp[] = $valueA % $valueB;
24422447
}
24432448

24442449
$c[] = $temp;
@@ -2461,6 +2466,10 @@ protected function multiplyScalar($scalar) : self
24612466
. " or float, " . gettype($scalar) . " found.");
24622467
}
24632468

2469+
if ($scalar == 0) {
2470+
return self::zeros(...$this->shape());
2471+
}
2472+
24642473
$b = [];
24652474

24662475
foreach ($this->a as $row) {
@@ -2519,6 +2528,10 @@ protected function addScalar($scalar) : self
25192528
. " or float, " . gettype($scalar) . " found.");
25202529
}
25212530

2531+
if ($scalar == 0) {
2532+
return clone $this;
2533+
}
2534+
25222535
$b = [];
25232536

25242537
foreach ($this->a as $i => $row) {
@@ -2548,6 +2561,10 @@ protected function subtractScalar($scalar) : self
25482561
. " or float, " . gettype($scalar) . " found.");
25492562
}
25502563

2564+
if ($scalar == 0) {
2565+
return clone $this;
2566+
}
2567+
25512568
$b = [];
25522569

25532570
foreach ($this->a as $i => $row) {

0 commit comments

Comments
 (0)