1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.linear;
18
19 import org.junit.jupiter.api.Test;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22
23 class RealMatrixTest {
24
25 @Test
26 void testDefaultMultiplyTransposed() {
27 RealMatrix a = MatrixUtils.createRealMatrix(new double[][] { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} });
28 RealMatrix b = MatrixUtils.createRealMatrix(new double[][] { {4d, -5d, 6d} });
29 RealMatrix abTRef = a.multiplyTransposed(b);
30 DefaultMatrix dma = new DefaultMatrix(a);
31 DefaultMatrix dmb = new DefaultMatrix(b);
32 RealMatrix abT = dma.multiplyTransposed(dmb);
33 assertEquals(0.0, abT.subtract(abTRef).getNorm1(), 1.0e-10);
34 }
35
36 @Test
37 void testDefaultTransposeMultiply() {
38 RealMatrix a = MatrixUtils.createRealMatrix(new double[][] { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} });
39 RealMatrix b = MatrixUtils.createRealMatrix(new double[][] { {4d}, {-5d}, {6d} });
40 RealMatrix aTbRef = a.transposeMultiply(b);
41 DefaultMatrix dma = new DefaultMatrix(a);
42 DefaultMatrix dmb = new DefaultMatrix(b);
43 RealMatrix aTb = dma.transposeMultiply(dmb);
44 assertEquals(0.0, aTb.subtract(aTbRef).getNorm1(), 1.0e-10);
45 }
46
47 @Test
48 void testDefaultMap() {
49 RealMatrix a = MatrixUtils.createRealMatrix(new double[][] { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} });
50 assertEquals(0.0, a.add(a.map(x -> -x)).getNorm1(), 1.0e-10);
51 }
52
53
54
55 private class DefaultMatrix extends AbstractRealMatrix {
56
57 RealMatrix m;
58 public DefaultMatrix(RealMatrix m) {
59 this.m = m;
60 }
61
62 @Override
63 public int getRowDimension() {
64 return m.getRowDimension();
65 }
66
67 @Override
68 public int getColumnDimension() {
69 return m.getColumnDimension();
70 }
71
72 @Override
73 public RealMatrix createMatrix(int rowDimension, int columnDimension) {
74 return m.createMatrix(rowDimension, columnDimension);
75 }
76
77 @Override
78 public RealMatrix copy() {
79 return m.copy();
80 }
81
82 @Override
83 public double getEntry(int row, int column) {
84 return m.getEntry(row, column);
85 }
86
87 @Override
88 public void setEntry(int row, int column, double value) {
89 m.setEntry(row, column, value);
90 }
91
92 }
93
94 }