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.hipparchus.Field;
20 import org.hipparchus.util.Binary64;
21 import org.hipparchus.util.Binary64Field;
22 import org.junit.Assert;
23 import org.junit.Test;
24
25 public class FieldMatrixTest {
26
27 @Test
28 public void testDefaultMultiplyTransposed() {
29 FieldMatrix<Binary64> a = createMatrix(new double[][] { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} });
30 FieldMatrix<Binary64> b = createMatrix(new double[][] { {4d, -5d, 6d} });
31 FieldMatrix<Binary64> abTRef = a.multiplyTransposed(b);
32 DefaultMatrix dma = new DefaultMatrix(a);
33 DefaultMatrix dmb = new DefaultMatrix(b);
34 FieldMatrix<Binary64> abT = dma.multiplyTransposed(dmb);
35 FieldMatrix<Binary64> diff = abT.subtract(abTRef);
36 for (int i = 0; i < diff.getRowDimension(); ++i) {
37 for (int j = 0; j < diff.getColumnDimension(); ++j) {
38 Assert.assertEquals(0.0, diff.getEntry(i, j).doubleValue(), 1.0e-10);
39 }
40 }
41 }
42
43 @Test
44 public void testDefaultTransposeMultiply() {
45 FieldMatrix<Binary64> a = createMatrix(new double[][] { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} });
46 FieldMatrix<Binary64> b = createMatrix(new double[][] { {4d}, {-5d}, {6d} });
47 FieldMatrix<Binary64> aTbRef = a.transposeMultiply(b);
48 DefaultMatrix dma = new DefaultMatrix(a);
49 DefaultMatrix dmb = new DefaultMatrix(b);
50 FieldMatrix<Binary64> aTb = dma.transposeMultiply(dmb);
51 FieldMatrix<Binary64> diff = aTb.subtract(aTbRef);
52 for (int i = 0; i < diff.getRowDimension(); ++i) {
53 for (int j = 0; j < diff.getColumnDimension(); ++j) {
54 Assert.assertEquals(0.0, diff.getEntry(i, j).doubleValue(), 1.0e-10);
55 }
56 }
57 }
58
59 @Test
60 public void testDefaultMap() {
61 FieldMatrix<Binary64> a = createMatrix(new double[][] { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} });
62 FieldMatrix<Binary64> result = a.add(a.map(x -> x.negate()));
63 result.walkInOptimizedOrder(new FieldMatrixPreservingVisitor<Binary64>() {
64
65 @Override
66 public void visit(int row, int column, Binary64 value) {
67 Assert.assertEquals(0.0, value.getReal(), 1.0e-10);
68 }
69
70 @Override
71 public void start(int rows, int columns, int startRow, int endRow,
72 int startColumn, int endColumn) {
73 }
74
75 @Override
76 public Binary64 end() {
77 return Binary64Field.getInstance().getZero();
78 }
79 });
80 }
81
82 @Test
83 public void testArithmeticalBlending() {
84
85 final Field<Binary64> field = Binary64Field.getInstance();
86
87 final FieldMatrix<Binary64> matrix1 = MatrixUtils.createFieldMatrix(field, 2, 2);
88 matrix1.setEntry(0, 0, new Binary64(1));
89 matrix1.setEntry(0, 1, new Binary64(2));
90 matrix1.setEntry(1, 0, new Binary64(3));
91 matrix1.setEntry(1, 1, new Binary64(4));
92
93 final FieldMatrix<Binary64> matrix2 = MatrixUtils.createFieldMatrix(field, 2, 2);
94 matrix2.setEntry(0, 0, new Binary64(2));
95 matrix2.setEntry(0, 1, new Binary64(4));
96 matrix2.setEntry(1, 0, new Binary64(9));
97 matrix2.setEntry(1, 1, new Binary64(16));
98
99 final Binary64 blendingValue = new Binary64(0.65);
100
101
102 final FieldMatrix<Binary64> blendedMatrix = matrix1.blendArithmeticallyWith(matrix2, blendingValue);
103
104
105 Assert.assertEquals(1.65 , blendedMatrix.getEntry(0,0).getReal(), 1.0e-15);
106 Assert.assertEquals(3.3 , blendedMatrix.getEntry(0,1).getReal(), 1.0e-15);
107 Assert.assertEquals(6.9 , blendedMatrix.getEntry(1,0).getReal(), 1.0e-15);
108 Assert.assertEquals(11.8 , blendedMatrix.getEntry(1,1).getReal(), 1.0e-15);
109 }
110
111
112
113 private class DefaultMatrix extends AbstractFieldMatrix<Binary64> {
114
115 FieldMatrix<Binary64> m;
116 public DefaultMatrix(FieldMatrix<Binary64> m) {
117 super(Binary64Field.getInstance());
118 this.m = m;
119 }
120
121 @Override
122 public int getRowDimension() {
123 return m.getRowDimension();
124 }
125
126 @Override
127 public int getColumnDimension() {
128 return m.getColumnDimension();
129 }
130
131 @Override
132 public FieldMatrix<Binary64> createMatrix(int rowDimension, int columnDimension) {
133 return m.createMatrix(rowDimension, columnDimension);
134 }
135
136 @Override
137 public FieldMatrix<Binary64> copy() {
138 return m.copy();
139 }
140
141 @Override
142 public void setEntry(int row, int column, Binary64 value) {
143 m.setEntry(row, column, value);
144 }
145
146 @Override
147 public void addToEntry(int row, int column, Binary64 increment) {
148 m.addToEntry(row, column, increment);
149 }
150
151 @Override
152 public void multiplyEntry(int row, int column, Binary64 factor) {
153 m.multiplyEntry(row, column, factor);
154 }
155
156 @Override
157 public Binary64 getEntry(int row, int column) {
158 return m.getEntry(row, column);
159 }
160
161 }
162
163 private FieldMatrix<Binary64> createMatrix(double[][] a) {
164 FieldMatrix<Binary64> m = MatrixUtils.createFieldMatrix(Binary64Field.getInstance(),
165 a.length, a[0].length);
166 for (int i = 0; i < m.getRowDimension(); ++i) {
167 for (int j = 0; j < m.getColumnDimension(); ++j) {
168 m.setEntry(i, j, new Binary64(a[i][j]));
169 }
170 }
171 return m;
172 }
173
174 }