Skip to content

Commit 84568f9

Browse files
committed
Added constants file
1 parent 1e70370 commit 84568f9

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
"psr-4": {
2929
"Rubix\\Tensor\\": "src/",
3030
"JAMA\\": "lib/JAMA"
31-
}
31+
},
32+
"files": [
33+
"src/Constants.php"
34+
]
3235
},
3336
"autoload-dev": {
3437
"psr-4": {

src/Constants.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rubix\Tensor
4+
{
5+
/**
6+
* A small number used in substitution of 0.
7+
*/
8+
const EPSILON = 1e-8;
9+
10+
/**
11+
* Two multiplied by pi.
12+
*/
13+
const TWO_PI = 2. * M_PI;
14+
}

src/Matrix.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Exception;
1111
use Closure;
1212

13+
use const Rubix\Tensor\EPSILON;
14+
use const Rubix\Tensor\TWO_PI;
15+
1316
/**
1417
* Matrix
1518
*
@@ -262,8 +265,8 @@ public static function gaussian(int $m, int $n) : self
262265

263266
$r = sqrt(-2. * log($r1));
264267

265-
$row[] = $r * sin($r2 * self::TWO_PI);
266-
$row[] = $r * cos($r2 * self::TWO_PI);
268+
$row[] = $r * sin($r2 * TWO_PI);
269+
$row[] = $r * cos($r2 * TWO_PI);
267270
}
268271

269272
if (count($row) > $n) {
@@ -1134,7 +1137,7 @@ public function lu() : array
11341137
}
11351138

11361139
$l[$j][$i] = ($pa[$j][$i] - $sigma)
1137-
/ ($u[$i][$i] ?: self::EPSILON);
1140+
/ ($u[$i][$i] ?: EPSILON);
11381141
}
11391142
}
11401143

@@ -1166,8 +1169,7 @@ public function eig(bool $normalize = true) : array
11661169
$eigenvalues = $decomp->getRealEigenvalues();
11671170
$eigenvectors = $decomp->getV()->getArray();
11681171

1169-
$eigenvectors = self::quick($eigenvectors)
1170-
->transpose();
1172+
$eigenvectors = self::quick($eigenvectors)->transpose();
11711173

11721174
if ($normalize === true) {
11731175
$norm = $eigenvectors->transpose()
@@ -1198,7 +1200,7 @@ public function solve(Vector $b) : ColumnVector
11981200

11991201
$pb = $p->dot($b);
12001202

1201-
$y = [$pb[0] / ($l[0][0] ?: self::EPSILON)];
1203+
$y = [$pb[0] / ($l[0][0] ?: EPSILON)];
12021204

12031205
for ($i = 1; $i < $this->m; $i++) {
12041206
$sigma = 0;
@@ -1212,7 +1214,7 @@ public function solve(Vector $b) : ColumnVector
12121214

12131215
$x = [];
12141216

1215-
$x = [$k => $y[$k] / ($l[$k][$k] ?: self::EPSILON)];
1217+
$x = [$k => $y[$k] / ($l[$k][$k] ?: EPSILON)];
12161218

12171219
for ($i = $this->m - 2; $i >= 0; $i--) {
12181220
$sigma = 0;
@@ -3888,7 +3890,7 @@ public function offsetUnset($index) : void
38883890
*/
38893891
public function offsetGet($index) : array
38903892
{
3891-
if (!isset($this->a[$index])) {
3893+
if (empty($this->a[$index])) {
38923894
throw new InvalidArgumentException('Element not found at'
38933895
. " offset $index.");
38943896
}

src/Tensor.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88

99
interface Tensor extends ArrayAccess, IteratorAggregate, Countable
1010
{
11-
const TWO_PI = 2. * M_PI;
12-
13-
const EPSILON = 1e-8;
14-
1511
/**
1612
* Return a tuple with the dimensionality of the tensor.
1713
*

src/Vector.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Exception;
1010
use Closure;
1111

12+
use const Rubix\Tensor\EPSILON;
13+
use const Rubix\Tensor\TWO_PI;
14+
1215
/**
1316
* Vector
1417
*
@@ -160,8 +163,8 @@ public static function gaussian(int $n) : self
160163

161164
$r = sqrt(-2. * log($r1));
162165

163-
$a[] = $r * sin($r2 * self::TWO_PI);
164-
$a[] = $r * cos($r2 * self::TWO_PI);
166+
$a[] = $r * sin($r2 * TWO_PI);
167+
$a[] = $r * cos($r2 * TWO_PI);
165168
}
166169

167170
if (count($a) > $n) {
@@ -231,7 +234,7 @@ public static function linspace(float $start, float $end, int $n) : self
231234

232235
$range = abs($end - $start);
233236

234-
$interval = ($range / ($n - 1)) - (self::EPSILON * $range);
237+
$interval = ($range / ($n - 1)) - (EPSILON * $range);
235238

236239
return static::range($start, $end, $interval);
237240
}

0 commit comments

Comments
 (0)