Skip to content

Commit 4e2ce24

Browse files
committed
Added is square method to matrix
1 parent 58a6287 commit 4e2ce24

File tree

4 files changed

+32
-58
lines changed

4 files changed

+32
-58
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
- 1.0.3
22
- Added clip upper and lower bounds
3+
- Added isSquare method to Matrix
34

45
- 1.0.2
56
- Added ref using row elimination method

src/Matrix.php

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

13-
use function gettype;
14-
use function array_search;
15-
use function array_values;
16-
use function array_map;
17-
use function array_reduce;
18-
use function array_slice;
19-
use function array_merge;
20-
use function array_fill;
21-
use function array_pop;
22-
use function is_array;
23-
use function is_int;
24-
use function is_float;
25-
use function intdiv;
26-
use function round;
27-
use function min;
28-
use function max;
29-
use function rand;
30-
use function sqrt;
31-
use function log;
32-
use function sin;
33-
use function cos;
34-
3513
/**
3614
* Matrix
3715
*
@@ -462,6 +440,16 @@ public function shape() : array
462440
return [$this->m, $this->n];
463441
}
464442

443+
/**
444+
* Is this a square matrix?
445+
*
446+
* @return bool
447+
*/
448+
public function isSquare() : bool
449+
{
450+
return $this->m === $this->n;
451+
}
452+
465453
/**
466454
* Return the number of elements in the tensor.
467455
*
@@ -544,7 +532,7 @@ public function columnAsVector(int $index) : ColumnVector
544532
*/
545533
public function diagonalAsVector() : Vector
546534
{
547-
if ($this->m !== $this->n) {
535+
if (!$this->isSquare()) {
548536
throw new RuntimeException('Cannot trace diagonal of a'
549537
. ' non square matrix.');
550538
}
@@ -607,9 +595,7 @@ public function asColumnVectors() : array
607595
*/
608596
public function flatten() : Vector
609597
{
610-
$b = array_reduce($this->a, 'array_merge', []);
611-
612-
return Vector::quick($b);
598+
return Vector::quick(array_reduce($this->a, 'array_merge', []));
613599
}
614600

615601
/**
@@ -739,9 +725,9 @@ public function inverse() : self
739725
*/
740726
public function determinant()
741727
{
742-
if ($this->m !== $this->n) {
743-
throw new RuntimeException('Determinant is undefined for a'
744-
. ' non square matrix.');
728+
if (!$this->isSquare()) {
729+
throw new RuntimeException('Determinant is undefined'
730+
. ' for a non square matrix.');
745731
}
746732

747733
[$b, $swaps] = $this->ref();
@@ -760,9 +746,9 @@ public function determinant()
760746
*/
761747
public function trace()
762748
{
763-
if ($this->m !== $this->n) {
764-
throw new InvalidArgumentException('Trace is undefined for a'
765-
. ' non square matrix.');
749+
if (!$this->isSquare()) {
750+
throw new InvalidArgumentException('Trace is undefined'
751+
. ' for a non square matrix.');
766752
}
767753

768754
return $this->diagonalAsVector()->sum();
@@ -1098,9 +1084,9 @@ public function rref() : self
10981084
*/
10991085
public function lu() : array
11001086
{
1101-
if ($this->m !== $this->n) {
1102-
throw new RuntimeException('Cannot decompose a non square'
1103-
. ' matrix.');
1087+
if (!$this->isSquare()) {
1088+
throw new RuntimeException('Cannot decompose a non'
1089+
. ' square matrix.');
11041090
}
11051091

11061092
$l = self::identity($this->n)->asArray();
@@ -1171,9 +1157,9 @@ public function lu() : array
11711157
*/
11721158
public function eig(bool $normalize = true) : array
11731159
{
1174-
if ($this->m !== $this->n) {
1175-
throw new RuntimeException('Cannot decompose a non square'
1176-
. ' matrix.');
1160+
if (!$this->isSquare()) {
1161+
throw new RuntimeException('Cannot decompose a non'
1162+
. ' square matrix.');
11771163
}
11781164

11791165
$jama = new JAMA($this->a);

src/Vector.php

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

12-
use function gettype;
13-
use function array_search;
14-
use function array_values;
15-
use function array_map;
16-
use function array_reduce;
17-
use function array_slice;
18-
use function array_merge;
19-
use function array_fill;
20-
use function is_int;
21-
use function is_float;
22-
use function intdiv;
23-
use function round;
24-
use function min;
25-
use function max;
26-
use function rand;
27-
use function sqrt;
28-
use function log;
29-
use function sin;
30-
use function cos;
31-
3212
/**
3313
* Vector
3414
*

tests/MatrixTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ public function test_shape()
218218
$this->assertEquals([3, 3], $this->c->shape());
219219
}
220220

221+
public function test_is_square()
222+
{
223+
$this->assertTrue($this->a->isSquare());
224+
$this->assertFalse($this->b->isSquare());
225+
$this->assertTrue($this->c->isSquare());
226+
}
227+
221228
public function test_size()
222229
{
223230
$this->assertEquals(9, $this->a->size());

0 commit comments

Comments
 (0)