File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -645,6 +645,29 @@ public function det() : float
645
645
return (-1. ) ** $ swaps * $ dHat ;
646
646
}
647
647
648
+ /**
649
+ * Calculate the rank of the matrix i.e the number of pivots
650
+ * in its reduced row echelon form.
651
+ *
652
+ * @return int
653
+ */
654
+ public function rank () : int
655
+ {
656
+ $ pivots = 0 ;
657
+
658
+ foreach ($ this ->rref () as $ row ) {
659
+ foreach ($ row as $ value ) {
660
+ if ($ value != 0 ) {
661
+ $ pivots ++;
662
+
663
+ continue 2 ;
664
+ }
665
+ }
666
+ }
667
+
668
+ return $ pivots ;
669
+ }
670
+
648
671
/**
649
672
* Return the elementwise reciprocal of the matrix.
650
673
*
Original file line number Diff line number Diff line change @@ -350,6 +350,13 @@ public function test_det()
350
350
$ this ->assertEquals (-544. , $ this ->c ->det ());
351
351
}
352
352
353
+ public function test_rank ()
354
+ {
355
+ $ this ->assertEquals (3 , $ this ->a ->rank ());
356
+ $ this ->assertEquals (1 , $ this ->b ->rank ());
357
+ $ this ->assertEquals (3 , $ this ->c ->rank ());
358
+ }
359
+
353
360
public function test_reciprocal ()
354
361
{
355
362
$ z = $ this ->a ->reciprocal ();
You can’t perform that action at this time.
0 commit comments