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