10
10
use Exception ;
11
11
use Closure ;
12
12
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
-
35
13
/**
36
14
* Matrix
37
15
*
@@ -462,6 +440,16 @@ public function shape() : array
462
440
return [$ this ->m , $ this ->n ];
463
441
}
464
442
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
+
465
453
/**
466
454
* Return the number of elements in the tensor.
467
455
*
@@ -544,7 +532,7 @@ public function columnAsVector(int $index) : ColumnVector
544
532
*/
545
533
public function diagonalAsVector () : Vector
546
534
{
547
- if ($ this -> m !== $ this ->n ) {
535
+ if (! $ this ->isSquare () ) {
548
536
throw new RuntimeException ('Cannot trace diagonal of a '
549
537
. ' non square matrix. ' );
550
538
}
@@ -607,9 +595,7 @@ public function asColumnVectors() : array
607
595
*/
608
596
public function flatten () : Vector
609
597
{
610
- $ b = array_reduce ($ this ->a , 'array_merge ' , []);
611
-
612
- return Vector::quick ($ b );
598
+ return Vector::quick (array_reduce ($ this ->a , 'array_merge ' , []));
613
599
}
614
600
615
601
/**
@@ -739,9 +725,9 @@ public function inverse() : self
739
725
*/
740
726
public function determinant ()
741
727
{
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. ' );
745
731
}
746
732
747
733
[$ b , $ swaps ] = $ this ->ref ();
@@ -760,9 +746,9 @@ public function determinant()
760
746
*/
761
747
public function trace ()
762
748
{
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. ' );
766
752
}
767
753
768
754
return $ this ->diagonalAsVector ()->sum ();
@@ -1098,9 +1084,9 @@ public function rref() : self
1098
1084
*/
1099
1085
public function lu () : array
1100
1086
{
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. ' );
1104
1090
}
1105
1091
1106
1092
$ l = self ::identity ($ this ->n )->asArray ();
@@ -1171,9 +1157,9 @@ public function lu() : array
1171
1157
*/
1172
1158
public function eig (bool $ normalize = true ) : array
1173
1159
{
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. ' );
1177
1163
}
1178
1164
1179
1165
$ jama = new JAMA ($ this ->a );
0 commit comments