@@ -255,7 +255,7 @@ public static function gaussian(int $m, int $n) : self
255
255
for ($ i = 0 ; $ i < $ m ; $ i ++) {
256
256
$ row = [];
257
257
258
- if (! empty ( $ extras) ) {
258
+ if ($ extras ) {
259
259
$ row [] = array_pop ($ extras );
260
260
}
261
261
@@ -265,8 +265,10 @@ public static function gaussian(int $m, int $n) : self
265
265
266
266
$ r = sqrt (-2. * log ($ r1 ));
267
267
268
- $ row [] = $ r * sin ($ r2 * TWO_PI );
269
- $ row [] = $ r * cos ($ r2 * TWO_PI );
268
+ $ phi = $ r2 * TWO_PI ;
269
+
270
+ $ row [] = $ r * sin ($ phi );
271
+ $ row [] = $ r * cos ($ phi );
270
272
}
271
273
272
274
if (count ($ row ) > $ n ) {
@@ -443,6 +445,16 @@ public function shape() : array
443
445
return [$ this ->m , $ this ->n ];
444
446
}
445
447
448
+ /**
449
+ * Return the shape of the tensor as a string.
450
+ *
451
+ * @return string
452
+ */
453
+ public function shapeString () : string
454
+ {
455
+ return (string ) $ this ->m . ' x ' . (string ) $ this ->n ;
456
+ }
457
+
446
458
/**
447
459
* Is this a square matrix?
448
460
*
@@ -674,21 +686,20 @@ public function reduce(callable $fn, $initial = 0)
674
686
}
675
687
676
688
/**
677
- * Transpose the matrix i.e row become columns and columns
678
- * become rows.
689
+ * Transpose the matrix i.e row become columns and columns become rows.
679
690
*
680
691
* @return self
681
692
*/
682
693
public function transpose () : self
683
694
{
684
695
if ($ this ->m > 1 ) {
685
- $ b = array_map (null , ...$ this ->a );
686
- } else {
687
- $ b = [];
696
+ return self ::quick (array_map (null , ...$ this ->a ));
697
+ }
698
+
699
+ $ b = [];
688
700
689
- for ($ i = 0 ; $ i < $ this ->n ; $ i ++) {
690
- $ b [] = array_column ($ this ->a , $ i );
691
- }
701
+ for ($ i = 0 ; $ i < $ this ->n ; $ i ++) {
702
+ $ b [] = array_column ($ this ->a , $ i );
692
703
}
693
704
694
705
return self ::quick ($ b );
@@ -788,8 +799,8 @@ public function matmul(Matrix $b) : self
788
799
. " $ this ->n rows but Matrix B has {$ b ->m ()}. " );
789
800
}
790
801
791
- $ p = $ b ->n ();
792
802
$ bHat = $ b ->asArray ();
803
+ $ p = $ b ->n ();
793
804
794
805
$ c = [];
795
806
@@ -1158,8 +1169,9 @@ public function lu() : array
1158
1169
public function eig (bool $ normalize = true ) : array
1159
1170
{
1160
1171
if (!$ this ->isSquare ()) {
1161
- throw new RuntimeException ('Cannot decompose a non '
1162
- . ' square matrix. ' );
1172
+ throw new RuntimeException ('Cannot eigen decompose a non '
1173
+ . ' square matrix, ' . implode (' x ' , $ this ->shape ())
1174
+ . ' matrix given. ' );
1163
1175
}
1164
1176
1165
1177
$ jama = new JAMA ($ this ->a );
@@ -2326,14 +2338,11 @@ public function repeat(int $m = 1, int $n = 1) : self
2326
2338
*/
2327
2339
protected function multiplyMatrix (Matrix $ b ) : self
2328
2340
{
2329
- if ($ b ->m () !== $ this ->m ) {
2330
- throw new DimensionalityMismatchException ('Matrix A require '
2331
- . " $ this ->m rows but Matrix B has {$ b ->m ()}. " );
2332
- }
2333
-
2334
- if ($ b ->n () !== $ this ->n ) {
2335
- throw new DimensionalityMismatchException ('Matrix A requires '
2336
- . " $ this ->n columns but Matrix B has {$ b ->n ()}. " );
2341
+ if ($ b ->shape () !== $ this ->shape ()) {
2342
+ throw new DimensionalityMismatchException (
2343
+ "{$ this ->shapeString ()}"
2344
+ . " matrix needed but {$ b ->shapeString ()} given. "
2345
+ );
2337
2346
}
2338
2347
2339
2348
$ c = [];
@@ -2361,14 +2370,11 @@ protected function multiplyMatrix(Matrix $b) : self
2361
2370
*/
2362
2371
protected function divideMatrix (Matrix $ b ) : self
2363
2372
{
2364
- if ($ b ->m () !== $ this ->m ) {
2365
- throw new DimensionalityMismatchException ('Matrix A requires '
2366
- . " $ this ->m rows but Matrix B has {$ b ->m ()}. " );
2367
- }
2368
-
2369
- if ($ b ->n () !== $ this ->n ) {
2370
- throw new DimensionalityMismatchException ('Matrix A requires '
2371
- . " $ this ->n columns but Matrix B has {$ b ->n ()}. " );
2373
+ if ($ b ->shape () !== $ this ->shape ()) {
2374
+ throw new DimensionalityMismatchException (
2375
+ "{$ this ->shapeString ()}"
2376
+ . " matrix needed but {$ b ->shapeString ()} given. "
2377
+ );
2372
2378
}
2373
2379
2374
2380
$ c = [];
@@ -2396,14 +2402,11 @@ protected function divideMatrix(Matrix $b) : self
2396
2402
*/
2397
2403
protected function addMatrix (Matrix $ b ) : self
2398
2404
{
2399
- if ($ b ->m () !== $ this ->m ) {
2400
- throw new DimensionalityMismatchException ('Matrix A requires '
2401
- . " $ this ->m rows but Matrix B has {$ b ->m ()}. " );
2402
- }
2403
-
2404
- if ($ b ->n () !== $ this ->n ) {
2405
- throw new DimensionalityMismatchException ('Matrix A requires '
2406
- . " $ this ->n columns but Matrix B has {$ b ->n ()}. " );
2405
+ if ($ b ->shape () !== $ this ->shape ()) {
2406
+ throw new DimensionalityMismatchException (
2407
+ "{$ this ->shapeString ()}"
2408
+ . " matrix needed but {$ b ->shapeString ()} given. "
2409
+ );
2407
2410
}
2408
2411
2409
2412
$ c = [];
@@ -2431,14 +2434,11 @@ protected function addMatrix(Matrix $b) : self
2431
2434
*/
2432
2435
protected function subtractMatrix (Matrix $ b ) : self
2433
2436
{
2434
- if ($ b ->m () !== $ this ->m ) {
2435
- throw new DimensionalityMismatchException ('Matrix A requires '
2436
- . " $ this ->m rows but Matrix B has {$ b ->m ()}. " );
2437
- }
2438
-
2439
- if ($ b ->n () !== $ this ->n ) {
2440
- throw new DimensionalityMismatchException ('Matrix A requires '
2441
- . " $ this ->n columns but Matrix B has {$ b ->n ()}. " );
2437
+ if ($ b ->shape () !== $ this ->shape ()) {
2438
+ throw new DimensionalityMismatchException (
2439
+ "{$ this ->shapeString ()}"
2440
+ . " matrix needed but {$ b ->shapeString ()} given. "
2441
+ );
2442
2442
}
2443
2443
2444
2444
$ c = [];
@@ -2467,14 +2467,11 @@ protected function subtractMatrix(Matrix $b) : self
2467
2467
*/
2468
2468
protected function powMatrix (Matrix $ b ) : self
2469
2469
{
2470
- if ($ b ->m () !== $ this ->m ) {
2471
- throw new DimensionalityMismatchException ('Matrix A requires '
2472
- . " $ this ->m rows but Matrix B has {$ b ->m ()}. " );
2473
- }
2474
-
2475
- if ($ b ->n () !== $ this ->n ) {
2476
- throw new DimensionalityMismatchException ('Matrix A requires '
2477
- . " $ this ->n columns but Matrix B has {$ b ->n ()}. " );
2470
+ if ($ b ->shape () !== $ this ->shape ()) {
2471
+ throw new DimensionalityMismatchException (
2472
+ "{$ this ->shapeString ()}"
2473
+ . " matrix needed but {$ b ->shapeString ()} given. "
2474
+ );
2478
2475
}
2479
2476
2480
2477
$ c = [];
@@ -2503,14 +2500,11 @@ protected function powMatrix(Matrix $b) : self
2503
2500
*/
2504
2501
protected function modMatrix (Matrix $ b ) : self
2505
2502
{
2506
- if ($ b ->m () !== $ this ->m ) {
2507
- throw new DimensionalityMismatchException ('Matrix A requires '
2508
- . " $ this ->m rows but Matrix B has {$ b ->m ()}. " );
2509
- }
2510
-
2511
- if ($ b ->n () !== $ this ->n ) {
2512
- throw new DimensionalityMismatchException ('Matrix A requires '
2513
- . " $ this ->n columns but Matrix B has {$ b ->n ()}. " );
2503
+ if ($ b ->shape () !== $ this ->shape ()) {
2504
+ throw new DimensionalityMismatchException (
2505
+ "{$ this ->shapeString ()}"
2506
+ . " matrix needed but {$ b ->shapeString ()} given. "
2507
+ );
2514
2508
}
2515
2509
2516
2510
$ c = [];
@@ -2539,14 +2533,11 @@ protected function modMatrix(Matrix $b) : self
2539
2533
*/
2540
2534
protected function equalMatrix (Matrix $ b ) : self
2541
2535
{
2542
- if ($ b ->m () !== $ this ->m ) {
2543
- throw new DimensionalityMismatchException ('Matrix A requires '
2544
- . " $ this ->m rows but Matrix B has {$ b ->m ()}. " );
2545
- }
2546
-
2547
- if ($ b ->n () !== $ this ->n ) {
2548
- throw new DimensionalityMismatchException ('Matrix A requires '
2549
- . " $ this ->n columns but Matrix B has {$ b ->n ()}. " );
2536
+ if ($ b ->shape () !== $ this ->shape ()) {
2537
+ throw new DimensionalityMismatchException (
2538
+ "{$ this ->shapeString ()}"
2539
+ . " matrix needed but {$ b ->shapeString ()} given. "
2540
+ );
2550
2541
}
2551
2542
2552
2543
$ c = [];
@@ -2575,14 +2566,11 @@ protected function equalMatrix(Matrix $b) : self
2575
2566
*/
2576
2567
protected function notEqualMatrix (Matrix $ b ) : self
2577
2568
{
2578
- if ($ b ->m () !== $ this ->m ) {
2579
- throw new DimensionalityMismatchException ('Matrix A requires '
2580
- . " $ this ->m rows but Matrix B has {$ b ->m ()}. " );
2581
- }
2582
-
2583
- if ($ b ->n () !== $ this ->n ) {
2584
- throw new DimensionalityMismatchException ('Matrix A requires '
2585
- . " $ this ->n columns but Matrix B has {$ b ->n ()}. " );
2569
+ if ($ b ->shape () !== $ this ->shape ()) {
2570
+ throw new DimensionalityMismatchException (
2571
+ "{$ this ->shapeString ()}"
2572
+ . " matrix needed but {$ b ->shapeString ()} given. "
2573
+ );
2586
2574
}
2587
2575
2588
2576
$ c = [];
@@ -2611,14 +2599,11 @@ protected function notEqualMatrix(Matrix $b) : self
2611
2599
*/
2612
2600
protected function greaterMatrix (Matrix $ b ) : self
2613
2601
{
2614
- if ($ b ->m () !== $ this ->m ) {
2615
- throw new DimensionalityMismatchException ('Matrix A requires '
2616
- . " $ this ->m rows but Matrix B has {$ b ->m ()}. " );
2617
- }
2618
-
2619
- if ($ b ->n () !== $ this ->n ) {
2620
- throw new DimensionalityMismatchException ('Matrix A requires '
2621
- . " $ this ->n columns but Matrix B has {$ b ->n ()}. " );
2602
+ if ($ b ->shape () !== $ this ->shape ()) {
2603
+ throw new DimensionalityMismatchException (
2604
+ "{$ this ->shapeString ()}"
2605
+ . " matrix needed but {$ b ->shapeString ()} given. "
2606
+ );
2622
2607
}
2623
2608
2624
2609
$ c = [];
@@ -2647,14 +2632,11 @@ protected function greaterMatrix(Matrix $b) : self
2647
2632
*/
2648
2633
protected function greaterEqualMatrix (Matrix $ b ) : self
2649
2634
{
2650
- if ($ b ->m () !== $ this ->m ) {
2651
- throw new DimensionalityMismatchException ('Matrix A requires '
2652
- . " $ this ->m rows but Matrix B has {$ b ->m ()}. " );
2653
- }
2654
-
2655
- if ($ b ->n () !== $ this ->n ) {
2656
- throw new DimensionalityMismatchException ('Matrix A requires '
2657
- . " $ this ->n columns but Matrix B has {$ b ->n ()}. " );
2635
+ if ($ b ->shape () !== $ this ->shape ()) {
2636
+ throw new DimensionalityMismatchException (
2637
+ "{$ this ->shapeString ()}"
2638
+ . " matrix needed but {$ b ->shapeString ()} given. "
2639
+ );
2658
2640
}
2659
2641
2660
2642
$ c = [];
@@ -2683,14 +2665,11 @@ protected function greaterEqualMatrix(Matrix $b) : self
2683
2665
*/
2684
2666
protected function lessMatrix (Matrix $ b ) : self
2685
2667
{
2686
- if ($ b ->m () !== $ this ->m ) {
2687
- throw new DimensionalityMismatchException ('Matrix A requires '
2688
- . " $ this ->m rows but Matrix B has {$ b ->m ()}. " );
2689
- }
2690
-
2691
- if ($ b ->n () !== $ this ->n ) {
2692
- throw new DimensionalityMismatchException ('Matrix A requires '
2693
- . " $ this ->n columns but Matrix B has {$ b ->n ()}. " );
2668
+ if ($ b ->shape () !== $ this ->shape ()) {
2669
+ throw new DimensionalityMismatchException (
2670
+ "{$ this ->shapeString ()}"
2671
+ . " matrix needed but {$ b ->shapeString ()} given. "
2672
+ );
2694
2673
}
2695
2674
2696
2675
$ c = [];
@@ -2719,14 +2698,11 @@ protected function lessMatrix(Matrix $b) : self
2719
2698
*/
2720
2699
protected function lessEqualMatrix (Matrix $ b ) : self
2721
2700
{
2722
- if ($ b ->m () !== $ this ->m ) {
2723
- throw new DimensionalityMismatchException ('Matrix A requires '
2724
- . " $ this ->m rows but Matrix B has {$ b ->m ()}. " );
2725
- }
2726
-
2727
- if ($ b ->n () !== $ this ->n ) {
2728
- throw new DimensionalityMismatchException ('Matrix A requires '
2729
- . " $ this ->n columns but Matrix B has {$ b ->n ()}. " );
2701
+ if ($ b ->shape () !== $ this ->shape ()) {
2702
+ throw new DimensionalityMismatchException (
2703
+ "{$ this ->shapeString ()}"
2704
+ . " matrix needed but {$ b ->shapeString ()} given. "
2705
+ );
2730
2706
}
2731
2707
2732
2708
$ c = [];
0 commit comments