File: TestSimpleMatrix.java

package info (click to toggle)
libejml-java 0.41%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,376 kB
  • sloc: java: 82,734; python: 81; makefile: 22
file content (1035 lines) | stat: -rw-r--r-- 30,845 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
/*
 * Copyright (c) 2020, Peter Abeles. All Rights Reserved.
 *
 * This file is part of Efficient Java Matrix Library (EJML).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.ejml.simple;

import org.ejml.EjmlUnitTests;
import org.ejml.UtilEjml;
import org.ejml.data.*;
import org.ejml.dense.row.CommonOps_DDRM;
import org.ejml.dense.row.NormOps_DDRM;
import org.ejml.dense.row.RandomMatrices_DDRM;
import org.ejml.ops.ConvertMatrixType;
import org.ejml.simple.ops.SimpleOperations_DSCC;
import org.ejml.sparse.csc.RandomMatrices_DSCC;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @author Peter Abeles
 */
public class TestSimpleMatrix {

    Random rand = new Random(76343);

    @Test
    public void randomNormal() {
        SimpleMatrix Q = SimpleMatrix.diag(5, 3, 12);
        Q.set(0, 1, 0.5);
        Q.set(1, 0, 0.5);

        int N = 200;
        double[] sum = new double[3];
        for (int i = 0; i < N; i++) {
            SimpleMatrix x = SimpleMatrix.randomNormal(Q, rand);

            for (int j = 0; j < x.getNumElements(); j++) {
                sum[j] += x.get(j);
            }
        }
        for (int i = 0; i < sum.length; i++) {
            sum[i] /= N;
            assertTrue(sum[i] != 0);
            assertEquals(0, sum[i], 0.3);
        }
    }

    @Test
    public void constructor_1d_array() {
        double[] d = new double[]{2, 5, 3, 9, -2, 6, 7, 4};
        SimpleMatrix s = new SimpleMatrix(3, 2, true, d);
        DMatrixRMaj m = new DMatrixRMaj(3, 2, true, d);

        EjmlUnitTests.assertEquals((DMatrixRMaj)m, (DMatrixRMaj)s.getMatrix(), UtilEjml.TEST_F64);
    }

    @Test
    public void constructor_2d_array() {
        double[][] d = new double[][]{{1, 2}, {3, 4}, {5, 6}};

        SimpleMatrix s = new SimpleMatrix(d);
        DMatrixRMaj mat = new DMatrixRMaj(d);

        EjmlUnitTests.assertEquals((DMatrixRMaj)mat, (DMatrixRMaj)s.getMatrix(), UtilEjml.TEST_F64);
    }

    @Test
    public void constructor_dense() {
        DMatrixRMaj mat = RandomMatrices_DDRM.rectangle(3, 2, rand);
        SimpleMatrix s = new SimpleMatrix(mat);

        assertTrue(mat != s.getMatrix());
        EjmlUnitTests.assertEquals(mat, s.getMatrix());
    }

    @Test
    public void constructor_simple() {
        SimpleMatrix orig = SimpleMatrix.random_DDRM(3, 2, 0, 1, rand);
        SimpleMatrix copy = new SimpleMatrix(orig);

        assertTrue(orig.mat != copy.mat);
        EjmlUnitTests.assertEquals(orig.mat, copy.mat);
    }

    @Test
    public void wrap() {
        DMatrixRMaj mat = RandomMatrices_DDRM.rectangle(3, 2, rand);

        SimpleMatrix s = SimpleMatrix.wrap(mat);

        assertTrue(s.mat == mat);
    }

    @Test
    public void identity() {
        SimpleMatrix s = SimpleMatrix.identity(3);

        DMatrixRMaj d = CommonOps_DDRM.identity(3);

        EjmlUnitTests.assertEquals(d, (DMatrixRMaj)s.mat, UtilEjml.TEST_F64);
    }

    @Test
    public void getMatrix() {
        SimpleMatrix s = new SimpleMatrix(3, 2);

        // make sure a new instance isn't returned
        assertTrue(s.mat == s.getMatrix());
    }

    @Test
    public void convertToSparse() {
        var data = new double[]{0, 1, 0, 1};
        var simpleMatrix = new SimpleMatrix(2, 2, true, data);
        assertTrue(simpleMatrix.getType().isDense());
        var denseMatrix = simpleMatrix.mat.copy();

        simpleMatrix.convertToSparse();

        assertFalse(simpleMatrix.getType().isDense());
        EjmlUnitTests.assertEquals(denseMatrix, simpleMatrix.mat);
    }

    @Test
    public void transformMatrix() {
        var data = new float[]{0, 1, 0, 1};
        var s = new SimpleMatrix(2, 2, true, data);

        var sparseDMatrix = s.getDSCC();
        var sparseFMatrix = s.getFSCC();
        var denseDMatrix = s.getDDRM();
        var denseFMatrix = s.getFDRM();

        EjmlUnitTests.assertEquals(sparseDMatrix, denseDMatrix);
        EjmlUnitTests.assertEquals(sparseDMatrix, sparseFMatrix);
        EjmlUnitTests.assertEquals(sparseDMatrix, denseFMatrix);
    }

    @Test
    public void transpose() {
        SimpleMatrix orig = SimpleMatrix.random_DDRM(3, 2, 0, 1, rand);
        SimpleMatrix tran = orig.transpose();

        DMatrixRMaj dTran = new DMatrixRMaj(2, 3);
        CommonOps_DDRM.transpose((DMatrixRMaj)orig.mat, dTran);

        EjmlUnitTests.assertEquals(dTran, (DMatrixRMaj)tran.mat, UtilEjml.TEST_F64);
    }

    @Test
    public void mult() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 2, 0, 1, rand);
        SimpleMatrix b = SimpleMatrix.random_DDRM(2, 3, 0, 1, rand);
        SimpleMatrix c = a.mult(b);

        DMatrixRMaj c_dense = new DMatrixRMaj(3, 3);
        CommonOps_DDRM.mult((DMatrixRMaj)a.mat, (DMatrixRMaj)b.mat, c_dense);

        EjmlUnitTests.assertEquals(c_dense, (DMatrixRMaj)c.mat, UtilEjml.TEST_F64);
    }

    @Test
    public void kron() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 2, 0, 1, rand);
        SimpleMatrix b = SimpleMatrix.random_DDRM(2, 3, 0, 1, rand);
        SimpleMatrix c = a.kron(b);

        DMatrixRMaj c_dense = new DMatrixRMaj(6, 6);
        CommonOps_DDRM.kron((DMatrixRMaj)a.getMatrix(), (DMatrixRMaj)b.getMatrix(), c_dense);

        EjmlUnitTests.assertEquals(c_dense, c.mat);
    }

//    @Test
//    public void mult_trans() {
//        SimpleMatrix a = SimpleMatrix.random(3,2,rand);
//        SimpleMatrix b = SimpleMatrix.random(2,3,rand);
//        SimpleMatrix c;
//
//        DMatrixRMaj c_dense = new DMatrixRMaj(3,3);
//        CommonOps.mult(a.mat,b.mat,c_dense);
//
//        c = a.mult(false,false,b);
//        EjmlUnitTests.assertEquals(c_dense,c.mat);
//        c = a.transpose().mult(true,false,b);
//        EjmlUnitTests.assertEquals(c_dense,c.mat);
//        c = a.mult(false,true,b.transpose());
//        EjmlUnitTests.assertEquals(c_dense,c.mat);
//        c = a.transpose().mult(true,true,b.transpose());
//        EjmlUnitTests.assertEquals(c_dense,c.mat);
//    }

    @Test
    public void plus() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 2, 0, 1, rand);
        SimpleMatrix b = SimpleMatrix.random_DDRM(3, 2, 0, 1, rand);
        SimpleMatrix c = a.plus(b);

        DMatrixRMaj c_dense = new DMatrixRMaj(3, 2);
        CommonOps_DDRM.add((DMatrixRMaj)a.mat, (DMatrixRMaj)b.mat, c_dense);

        EjmlUnitTests.assertEquals(c_dense, c.mat);
    }

    @Test
    public void plus_scalar() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 2, 0, 1, rand);
        double b = 2.5;
        SimpleMatrix c = a.plus(b);

        DMatrixRMaj c_dense = new DMatrixRMaj(3, 2);
        CommonOps_DDRM.add((DMatrixRMaj)a.mat, b, c_dense);

        EjmlUnitTests.assertEquals(c_dense, c.mat);
    }

    @Test
    public void dot() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(10, 1, -1, 1, rand);
        SimpleMatrix b = SimpleMatrix.random_DDRM(10, 1, -1, 1, rand);

        double expected = 0;
        for (int i = 0; i < 10; i++)
            expected += a.get(i)*b.get(i);

        double found = a.dot(b);

        assertEquals(expected, found, UtilEjml.TEST_F64);
    }

    @Test
    public void isVector() {
        assertTrue(new SimpleMatrix(1, 1).isVector());
        assertTrue(new SimpleMatrix(1, 10).isVector());
        assertTrue(new SimpleMatrix(10, 1).isVector());
        assertFalse(new SimpleMatrix(6, 5).isVector());
    }

    @Test
    public void minus_matrix_matrix() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 2, 0, 1, rand);
        SimpleMatrix b = SimpleMatrix.random_DDRM(3, 2, 0, 1, rand);
        SimpleMatrix c = a.minus(b);

        DMatrixRMaj c_dense = new DMatrixRMaj(3, 2);
        CommonOps_DDRM.subtract((DMatrixRMaj)a.mat, (DMatrixRMaj)b.mat, c_dense);

        EjmlUnitTests.assertEquals(c_dense, c.mat);
    }

    @Test
    public void minus_matrix_scalar() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 2, 0, 1, rand);
        double b = 0.14;
        SimpleMatrix c = a.minus(b);

        DMatrixRMaj c_dense = new DMatrixRMaj(3, 2);
        CommonOps_DDRM.subtract((DMatrixRMaj)a.mat, b, c_dense);

        EjmlUnitTests.assertEquals(c_dense, c.mat);
    }

    @Test
    public void plus_beta() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 2, 0, 1, rand);
        SimpleMatrix b = SimpleMatrix.random_DDRM(3, 2, 0, 1, rand);
        SimpleMatrix c = a.plus(2.5, b);

        DMatrixRMaj c_dense = new DMatrixRMaj(3, 2);
        CommonOps_DDRM.add((DMatrixRMaj)a.mat, 2.5, (DMatrixRMaj)b.mat, c_dense);

        EjmlUnitTests.assertEquals(c_dense, c.mat);
    }

    @Test
    public void invert() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 3, 0, 1, rand);
        SimpleMatrix inv = a.invert();

        DMatrixRMaj d_inv = new DMatrixRMaj(3, 3);
        CommonOps_DDRM.invert((DMatrixRMaj)a.mat, d_inv);

        EjmlUnitTests.assertEquals(d_inv, inv.mat);
    }

    @Test
    public void invert_NaN_INFINITY() {
        SimpleMatrix a = new SimpleMatrix(3, 3);
        try {
            a.fill(Double.NaN);
            a.invert();
            fail("Should have thrown an exception");
        } catch (RuntimeException ignore) {
        }

        try {
            a.fill(Double.POSITIVE_INFINITY);
            a.invert();
            fail("Should have thrown an exception");
        } catch (RuntimeException ignore) {
        }
    }

    @Test
    public void pseudoInverse() {
        // first test it against a non-square zero matrix
        SimpleMatrix inv = new SimpleMatrix(3, 4).pseudoInverse();
        assertEquals(0, inv.normF(), UtilEjml.TEST_F64);

        // now try it against a more standard matrix
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 3, 0, 1, rand);
        inv = a.pseudoInverse();

        DMatrixRMaj d_inv = new DMatrixRMaj(3, 3);
        CommonOps_DDRM.invert((DMatrixRMaj)a.mat, d_inv);

        EjmlUnitTests.assertEquals(d_inv, inv.mat);
    }

    @Test
    public void solve() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 3, 0, 1, rand);
        SimpleMatrix b = SimpleMatrix.random_DDRM(3, 2, 0, 1, rand);
        SimpleMatrix c = a.solve(b);

        DMatrixRMaj c_dense = new DMatrixRMaj(3, 2);
        CommonOps_DDRM.solve((DMatrixRMaj)a.mat, (DMatrixRMaj)b.mat, c_dense);

        EjmlUnitTests.assertEquals(c_dense, c.mat);
    }

    @Test
    public void solve_NaN_INFINITY() {
        SimpleMatrix a = new SimpleMatrix(3, 3);
        SimpleMatrix b = SimpleMatrix.random_DDRM(3, 2, 0, 1, rand);
        try {
            a.fill(Double.NaN);
            a.solve(b);
            fail("Should have thrown an exception");
        } catch (RuntimeException ignore) {
        }

        try {
            a.fill(Double.POSITIVE_INFINITY);
            a.solve(b);
            fail("Should have thrown an exception");
        } catch (RuntimeException ignore) {
        }
    }

    /**
     * See if it solves an over determined system correctly
     */
    @Test
    public void solve_notsquare() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(6, 3, 0, 1, rand);
        SimpleMatrix b = SimpleMatrix.random_DDRM(6, 2, 0, 1, rand);
        SimpleMatrix c = a.solve(b);

        DMatrixRMaj c_dense = new DMatrixRMaj(3, 2);
        CommonOps_DDRM.solve((DMatrixRMaj)a.mat, (DMatrixRMaj)b.mat, c_dense);

        EjmlUnitTests.assertEquals(c_dense, c.mat);
    }

    @Test
    public void set_double() {
        SimpleMatrix a = new SimpleMatrix(3, 3);
        a.fill(16.0);

        DMatrixRMaj d = new DMatrixRMaj(3, 3);
        CommonOps_DDRM.fill(d, 16.0);

        EjmlUnitTests.assertEquals(d, a.mat);
    }

    @Test
    public void zero() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 3, 0, 1, rand);
        a.zero();

        DMatrixRMaj d = new DMatrixRMaj(3, 3);

        EjmlUnitTests.assertEquals(d, a.mat);
    }

    @Test
    public void normF() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 3, 0, 1, rand);

        double norm = a.normF();
        double dnorm = NormOps_DDRM.fastNormF((DMatrixRMaj)a.mat);

        assertEquals(dnorm, norm, 1e-10);
    }

    @Test
    public void conditionP2() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 3, 0, 1, rand);

        double cond = NormOps_DDRM.conditionP2((DMatrixRMaj)a.getMatrix());
        double found = a.conditionP2();

        assertTrue(cond == found);
    }

    @Test
    public void determinant() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 3, 0, 1, rand);

        double det = a.determinant();
        double ddet = CommonOps_DDRM.det((DMatrixRMaj)a.mat);

        assertEquals(ddet, det, 1e-10);
    }

    @Test
    public void trace() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 3, 0, 1, rand);

        double trace = a.trace();
        double dtrace = CommonOps_DDRM.trace((DMatrixRMaj)a.mat);

        assertEquals(dtrace, trace, 1e-10);
    }

    @Test
    public void reshape() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 3, 0, 1, rand);
        DMatrixRMaj b = a.mat.copy();

        a.reshape(2, 3);
        b.reshape(2, 3, false);

        EjmlUnitTests.assertEquals(b, a.mat);
    }

    @Test
    public void set_element() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 3, 0, 1, rand);
        a.set(0, 1, 10.3);

        assertEquals(10.3, a.get(0, 1), 1e-6);
    }

    @Test
    public void setRow() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 3, 0, 1, rand);
        a.setRow(2, 1, 2, 3);

        assertEquals(2, a.get(2, 1), 1e-6);
        assertEquals(3, a.get(2, 2), 1e-6);
    }

    @Test
    public void setColumn() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 3, 0, 1, rand);
        a.setColumn(2, 1, 2, 3);

        assertEquals(2, a.get(1, 2), 1e-6);
        assertEquals(3, a.get(2, 2), 1e-6);
    }

    @Test
    public void get_2d() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 3, 0, 1, rand);

        assertEquals(((DMatrixRMaj)a.mat).get(0, 1), a.get(0, 1), 1e-6);
    }

    @Test
    public void get_1d() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 3, 0, 1, rand);

        assertEquals(((DMatrixRMaj)a.mat).get(3), a.get(3), 1e-6);
    }

    @Test
    public void getIndex() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 3, 0, 1, rand);

        assertEquals(((DMatrixRMaj)a.mat).getIndex(0, 2), a.getIndex(0, 2), 1e-6);
    }

    @Test
    public void copy() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 3, 0, 1, rand);
        SimpleMatrix b = a.copy();

        assertTrue(a.mat != b.mat);
        EjmlUnitTests.assertEquals(b.mat, a.mat);
    }

    @Test
    public void svd() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 4, 0, 1, rand);

        SimpleSVD<SimpleMatrix> svd = a.svd();

        SimpleMatrix U = svd.getU();
        SimpleMatrix W = svd.getW();
        SimpleMatrix V = svd.getV();

        SimpleMatrix a_found = U.mult(W).mult(V.transpose());

        EjmlUnitTests.assertEquals(a.mat, a_found.mat);
    }

    @Test
    public void eig() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(4, 4, 0, 1, rand);

        SimpleEVD evd = a.eig();

        assertEquals(4, evd.getNumberOfEigenvalues());

        for (int i = 0; i < 4; i++) {
            Complex_F64 c = evd.getEigenvalue(i);
            assertTrue(c != null);
            evd.getEigenVector(i);
        }
    }

    @Test
    public void insertIntoThis() {
        SimpleMatrix A = new SimpleMatrix(6, 4);
        SimpleMatrix B = SimpleMatrix.random_DDRM(3, 2, 0, 1, rand);

        DMatrixRMaj A_ = A.getMatrix().copy();

        A.insertIntoThis(1, 2, B);

        CommonOps_DDRM.insert((DMatrixRMaj)B.getMatrix(), A_, 1, 2);

        EjmlUnitTests.assertEquals(A_, A.getMatrix());
    }

    @Test
    public void combine() {
        SimpleMatrix A = new SimpleMatrix(6, 4);
        SimpleMatrix B = SimpleMatrix.random_DDRM(3, 4, 0, 1, rand);

        SimpleMatrix C = A.combine(2, 2, B);

        assertEquals(6, C.numRows());
        assertEquals(6, C.numCols());

        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                if (i >= 2 && i < 5 && j >= 2 && j < 6) {
                    // check to see if B was overlayed
                    assertTrue(B.get(i - 2, j - 2) == C.get(i, j));
                } else if (i >= 5 || j >= 4) {
                    // check zero padding
                    assertTrue(C.get(i, j) == 0);
                } else {
                    // see if the parts of A remain there
                    assertTrue(A.get(i, j) == C.get(i, j));
                }
            }
        }
    }

    @Test
    public void scale() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 2, 0, 1, rand);
        SimpleMatrix b = a.scale(1.5);

        for (int i = 0; i < a.numRows(); i++) {
            for (int j = 0; j < a.numCols(); j++) {
                assertEquals(a.get(i, j)*1.5, b.get(i, j), 1e-10);
            }
        }
    }

    @Test
    public void div_scalar() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 2, 0, 1, rand);
        SimpleMatrix b = a.divide(1.5);

        for (int i = 0; i < a.numRows(); i++) {
            for (int j = 0; j < a.numCols(); j++) {
                assertEquals(a.get(i, j)/1.5, b.get(i, j), 1e-10);
            }
        }
    }

    @Test
    public void elementSum() {
        SimpleMatrix a = new SimpleMatrix(7, 4);

        double expectedSum = 0;

        int index = 0;
        for (int i = 0; i < a.numRows(); i++) {
            for (int j = 0; j < a.numCols(); j++, index++) {
                expectedSum += index;
                a.set(i, j, index);
            }
        }

        assertEquals(expectedSum, a.elementSum(), UtilEjml.TEST_F64);
    }

    @Test
    public void elementMaxAbs() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(7, 5, 0, 1, rand);

        a.set(3, 4, -5);
        a.set(4, 4, 4);

        assertEquals(5, a.elementMaxAbs(), 0.0);
    }

    @Test
    public void elementMinAbs() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(7, 5, 4, 10, rand);

        a.set(3, 4, -2);
        a.set(4, 4, 0.5);

        assertEquals(0.5, a.elementMinAbs(), 0.0);
    }

    @Test
    public void elementMult() {
        SimpleMatrix A = SimpleMatrix.random_DDRM(4, 5, -1, 1, rand);
        SimpleMatrix B = SimpleMatrix.random_DDRM(4, 5, -1, 1, rand);

        SimpleMatrix C = A.elementMult(B);

        for (int i = 0; i < A.numRows(); i++) {
            for (int j = 0; j < A.numCols(); j++) {
                double expected = A.get(i, j)*B.get(i, j);

                assertTrue(expected == C.get(i, j));
            }
        }
    }

    @Test
    public void elementDiv() {
        SimpleMatrix A = SimpleMatrix.random_DDRM(4, 5, -1, 1, rand);
        SimpleMatrix B = SimpleMatrix.random_DDRM(4, 5, -1, 1, rand);

        SimpleMatrix C = A.elementDiv(B);

        for (int i = 0; i < A.numRows(); i++) {
            for (int j = 0; j < A.numCols(); j++) {
                double expected = A.get(i, j)/B.get(i, j);

                assertTrue(expected == C.get(i, j));
            }
        }
    }

    @Test
    public void elementPower_m() {
        SimpleMatrix A = SimpleMatrix.random_DDRM(4, 5, 0, 1, rand);
        SimpleMatrix B = SimpleMatrix.random_DDRM(4, 5, 0, 1, rand);

        SimpleMatrix C = A.elementPower(B);

        for (int i = 0; i < A.numRows(); i++) {
            for (int j = 0; j < A.numCols(); j++) {
                double expected = Math.pow(A.get(i, j), B.get(i, j));

                assertTrue(expected == C.get(i, j));
            }
        }
    }

    @Test
    public void elementPower_s() {
        SimpleMatrix A = SimpleMatrix.random_DDRM(4, 5, 0, 1, rand);
        double b = 1.1;

        SimpleMatrix C = A.elementPower(b);

        for (int i = 0; i < A.numRows(); i++) {
            for (int j = 0; j < A.numCols(); j++) {
                double expected = Math.pow(A.get(i, j), b);

                assertTrue(expected == C.get(i, j));
            }
        }
    }

    @Test
    public void elementLog() {
        SimpleMatrix A = SimpleMatrix.random_DDRM(4, 5, 0, 1, rand);

        SimpleMatrix C = A.elementLog();

        for (int i = 0; i < A.numRows(); i++) {
            for (int j = 0; j < A.numCols(); j++) {
                double expected = Math.log(A.get(i, j));

                assertTrue(expected == C.get(i, j));
            }
        }
    }

    @Test
    public void elementExp() {
        SimpleMatrix A = SimpleMatrix.random_DDRM(4, 5, 0, 1, rand);

        SimpleMatrix C = A.elementExp();

        for (int i = 0; i < A.numRows(); i++) {
            for (int j = 0; j < A.numCols(); j++) {
                double expected = Math.exp(A.get(i, j));

                assertTrue(expected == C.get(i, j));
            }
        }
    }

    @Test
    public void extractMatrix() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(7, 5, 0, 1, rand);

        SimpleMatrix b = a.extractMatrix(2, 5, 3, 5);

        for (int i = 2; i <= 4; i++) {
            for (int j = 3; j <= 4; j++) {
                double expected = a.get(i, j);
                double found = b.get(i - 2, j - 3);

                assertTrue(expected == found);
            }
        }
    }

    @Test
    public void diag_extract() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 4, 0, 1, rand);

        DMatrixRMaj found = a.diag().getMatrix();
        DMatrixRMaj expected = new DMatrixRMaj(3, 1);

        CommonOps_DDRM.extractDiag((DMatrixRMaj)a.getMatrix(), expected);

        EjmlUnitTests.assertEquals(found, expected);
    }

    @Test
    public void diag_vector() {
        SimpleMatrix a = SimpleMatrix.random_DDRM(3, 1, 0, 1, rand);

        DMatrixRMaj found = a.diag().getMatrix();
        SimpleMatrix expected = SimpleMatrix.diag(((DMatrixRMaj)a.getMatrix()).data);

        EjmlUnitTests.assertEquals(found, expected.getMatrix());
    }

    @Test
    public void extractVector() {
        SimpleMatrix A = SimpleMatrix.random_DDRM(10, 7, 0, 1, rand);

        SimpleMatrix c = A.extractVector(false, 2);
        SimpleMatrix r = A.extractVector(true, 2);

        assertEquals(A.numCols(), r.numCols());
        assertEquals(1, r.numRows());
        assertEquals(A.numRows(), c.numRows());
        assertEquals(1, c.numCols());

        for (int i = 0; i < A.numCols(); i++) {
            assertEquals(A.get(2, i), r.get(i), 1e-10);
        }

        for (int i = 0; i < A.numRows(); i++) {
            assertEquals(A.get(i, 2), c.get(i), 1e-10);
        }
    }

    @Test
    public void negative() {
        SimpleMatrix A = SimpleMatrix.random_DDRM(5, 7, -1, 1, rand);

        SimpleMatrix A_neg = A.negative();

        double value = A.plus(A_neg).normF();

        assertEquals(0, value, UtilEjml.TEST_F64);
    }

    @Test
    public void isInBounds() {
        SimpleMatrix A = new SimpleMatrix(10, 15);

        assertTrue(A.isInBounds(0, 0));
        assertTrue(A.isInBounds(9, 0));
        assertTrue(A.isInBounds(0, 14));
        assertTrue(A.isInBounds(3, 3));

        assertFalse(A.isInBounds(-1, 0));
        assertFalse(A.isInBounds(0, -1));
        assertFalse(A.isInBounds(10, 0));
        assertFalse(A.isInBounds(0, 15));
        assertFalse(A.isInBounds(3, 1000));
    }

    @Test
    public void equation() {
        SimpleMatrix A = SimpleMatrix.random_DDRM(5, 7, -1, 1, rand);
        SimpleMatrix B = SimpleMatrix.random_DDRM(5, 7, -1, 1, rand);

        SimpleMatrix orig = A.copy();

        // test each variable type
        A.equation("A = A + B", 24.5, "B");
        assertEquals(orig.get(0, 0) + 24.5, A.get(0, 0), UtilEjml.TEST_F64);

        // test implicit A =
        A.setTo(orig);
        A.equation("A + B", 24.5, "B");
        assertEquals(orig.get(0, 0) + 24.5, A.get(0, 0), UtilEjml.TEST_F64);

        A.equation("A(B,5) = 4", 4, "B");
        assertEquals(4, A.get(4, 5), UtilEjml.TEST_F64);

        A.setTo(orig);
        A.equation("A = A+B", B, "B");
        assertEquals(orig.get(0, 0) + B.get(0, 0), A.get(0, 0), UtilEjml.TEST_F64);

        A.setTo(orig);
        A.equation("A = A+B", B.getDDRM(), "B");
        assertEquals(orig.get(0, 0) + B.get(0, 0), A.get(0, 0), UtilEjml.TEST_F64);

        A.setTo(orig);
        A.equation("B = B+B", "B");
        assertEquals(orig.get(0, 0)*2, A.get(0, 0), UtilEjml.TEST_F64);

        // test implicit B
        A.setTo(orig);
        A.equation("B+B", "B");
        assertEquals(orig.get(0, 0)*2, A.get(0, 0), UtilEjml.TEST_F64);
    }

    /**
     * More detailed test is done in the CommonOps functin
     */
    @Test
    public void concatColumns() {
        SimpleMatrix A = SimpleMatrix.random_DDRM(5, 7, -1, 1, rand);
        SimpleMatrix B0 = SimpleMatrix.random_DDRM(4, 3, -1, 1, rand);
        SimpleMatrix B1 = SimpleMatrix.random_DDRM(4, 3, -1, 1, rand);

        SimpleMatrix C = A.concatColumns(B0, B1);
        SimpleMatrix D = new SimpleMatrix(5, 13);
        D.insertIntoThis(0, 0, A);
        D.insertIntoThis(0, 7, B0);
        D.insertIntoThis(0, 10, B1);

        assertTrue(C.isIdentical(D, UtilEjml.TEST_F64));
    }

    @Test
    public void concatRows() {
        SimpleMatrix A = SimpleMatrix.random_DDRM(5, 7, -1, 1, rand);
        SimpleMatrix B0 = SimpleMatrix.random_DDRM(4, 3, -1, 1, rand);
        SimpleMatrix B1 = SimpleMatrix.random_DDRM(4, 3, -1, 1, rand);

        SimpleMatrix C = A.concatRows(B0, B1);
        SimpleMatrix D = new SimpleMatrix(13, 7);
        D.insertIntoThis(0, 0, A);
        D.insertIntoThis(5, 0, B0);
        D.insertIntoThis(9, 0, B1);

        assertTrue(C.isIdentical(D, UtilEjml.TEST_F64));
    }

    @Test
    public void rows() {
        SimpleMatrix A = SimpleMatrix.random_DDRM(5, 7, -1, 1, rand);
        SimpleMatrix B = A.rows(1, 3);

        for (int i = 1; i < 3; i++) {
            for (int j = 0; j < 7; j++) {
                assertEquals(A.get(i, j), B.get(i - 1, j), UtilEjml.TEST_F64);
            }
        }
    }

    @Test
    public void cols() {
        SimpleMatrix A = SimpleMatrix.random_DDRM(5, 7, -1, 1, rand);
        SimpleMatrix B = A.rows(1, 3);

        for (int i = 1; i < 3; i++) {
            for (int j = 0; j < 7; j++) {
                assertEquals(A.get(i, j), B.get(i - 1, j), UtilEjml.TEST_F64);
            }
        }
    }

    @Test
    public void serialization() {
        List<Matrix> matrixTypes = new ArrayList<>();
        matrixTypes.add(new DMatrixRMaj(2, 3));
        matrixTypes.add(new FMatrixRMaj(2, 3));
        matrixTypes.add(new ZMatrixRMaj(2, 3));
        matrixTypes.add(new CMatrixRMaj(2, 3));
        matrixTypes.add(new DMatrixSparseCSC(2, 3));
//        matrixTypes.add( new FMatrixSparseCSC(2,3));

        DMatrixRMaj template = RandomMatrices_DDRM.rectangle(2, 3, rand);

        for (Matrix m : matrixTypes) {
            m.setTo(ConvertMatrixType.convert(template, m.getType()));

            SimpleMatrix A = new SimpleMatrix(m);
            try {
                ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
                ObjectOutputStream stream = new ObjectOutputStream(byteStream);
                stream.writeObject(A);
                stream.flush();
                stream.close();

                ByteArrayInputStream inputByte = new ByteArrayInputStream(byteStream.toByteArray());
                ObjectInputStream inputStream = new ObjectInputStream(inputByte);
                SimpleMatrix B = (SimpleMatrix)inputStream.readObject();

                assertTrue(A.isIdentical(B, 1e-4));
                assertNotNull(B.convertType);

                // If properly constructed this shouldn't fail
                B.mult(A.transpose());

                // UnsupportedOperation is considered an acceptable response
                try {
                    assertTrue(B.plus(0.1).isIdentical(A.plus(0.1), 1e-4));
                } catch (UnsupportedOperation | ConvertToDenseException ignore) {
                }

                byteStream.close();
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
                fail("Serialization failed");
            }
        }
    }

    @Test
    public void serialization2() {

    }

    /**
     * See if it correctly calls a specialized function that can handle different matrix types
     */
    @Test
    public void mult_specialized() {
        DMatrixSparse A = RandomMatrices_DSCC.rectangle(2, 2, 3, rand);
        DMatrixRMaj B = RandomMatrices_DDRM.rectangle(2, 3, rand);

        SimpleMatrix sA = SimpleMatrix.wrap(A);
        SimpleMatrix sB = SimpleMatrix.wrap(B);

        OpsCheckSpecial ops = new OpsCheckSpecial();
        sA.ops = ops;

        sA.mult(sB);
        assertTrue(ops.specalized);
    }

    /**
     * See if it correctly calls a specialized function that can handle different matrix types
     */
    @Test
    public void solve_specialized() {
        DMatrixSparse A = RandomMatrices_DSCC.rectangle(2, 2, 3, rand);
        DMatrixRMaj B = RandomMatrices_DDRM.rectangle(2, 2, rand);

        SimpleMatrix sA = SimpleMatrix.wrap(A);
        SimpleMatrix sB = SimpleMatrix.wrap(B);

        OpsCheckSpecial ops = new OpsCheckSpecial();
        sA.ops = ops;

        sA.solve(sB);
        assertTrue(ops.specalized);
    }

    /**
     * Helper used to test to see if a specialized function was called
     */
    public static class OpsCheckSpecial extends SimpleOperations_DSCC {
        public boolean specalized = false;

        @Override
        public void mult( DMatrixSparseCSC A, DMatrixRMaj B, DMatrixRMaj output ) {
            specalized = true;
        }

        @Override
        public void mult( DMatrixSparseCSC A, DMatrixSparseCSC B, DMatrixSparseCSC output ) {
            fail("Shouldn't have been called");
        }

        @Override
        public boolean solve( DMatrixSparseCSC A, DMatrixSparseCSC X, DMatrixSparseCSC B ) {
            fail("Shouldn't have been called");
            return true;
        }

        @Override
        public boolean solve( DMatrixSparseCSC A, DMatrixRMaj X, DMatrixRMaj B ) {
            specalized = true;
            return true;
        }
    }
}