View Javadoc
1   /*
2    * Licensed to the Hipparchus project under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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.jupiter.api.Test;
23  
24  import static org.junit.jupiter.api.Assertions.assertEquals;
25  
26  class FieldMatrixTest {
27  
28      @Test
29      void testDefaultMultiplyTransposed() {
30          FieldMatrix<Binary64> a = createMatrix(new double[][] { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} });
31          FieldMatrix<Binary64> b = createMatrix(new double[][] { {4d, -5d, 6d} });
32          FieldMatrix<Binary64> abTRef = a.multiplyTransposed(b);
33          DefaultMatrix dma = new DefaultMatrix(a);
34          DefaultMatrix dmb = new DefaultMatrix(b);
35          FieldMatrix<Binary64> abT = dma.multiplyTransposed(dmb);
36          FieldMatrix<Binary64> diff = abT.subtract(abTRef);
37          for (int i = 0; i < diff.getRowDimension(); ++i) {
38              for (int j = 0; j < diff.getColumnDimension(); ++j) {
39                  assertEquals(0.0, diff.getEntry(i, j).doubleValue(), 1.0e-10);
40              }
41          }
42      }
43  
44      @Test
45      void testDefaultTransposeMultiply() {
46          FieldMatrix<Binary64> a = createMatrix(new double[][] { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} });
47          FieldMatrix<Binary64> b = createMatrix(new double[][] { {4d}, {-5d}, {6d} });
48          FieldMatrix<Binary64> aTbRef = a.transposeMultiply(b);
49          DefaultMatrix dma = new DefaultMatrix(a);
50          DefaultMatrix dmb = new DefaultMatrix(b);
51          FieldMatrix<Binary64> aTb = dma.transposeMultiply(dmb);
52          FieldMatrix<Binary64> diff = aTb.subtract(aTbRef);
53          for (int i = 0; i < diff.getRowDimension(); ++i) {
54              for (int j = 0; j < diff.getColumnDimension(); ++j) {
55                  assertEquals(0.0, diff.getEntry(i, j).doubleValue(), 1.0e-10);
56              }
57          }
58      }
59  
60      @Test
61      void testDefaultMap() {
62          FieldMatrix<Binary64> a = createMatrix(new double[][] { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} });
63          FieldMatrix<Binary64> result = a.add(a.map(x -> x.negate()));
64          result.walkInOptimizedOrder(new FieldMatrixPreservingVisitor<Binary64>() {
65              
66              @Override
67              public void visit(int row, int column, Binary64 value) {
68                  assertEquals(0.0, value.getReal(), 1.0e-10);
69              }
70              
71              @Override
72              public void start(int rows, int columns, int startRow, int endRow,
73                                int startColumn, int endColumn) {
74              }
75              
76              @Override
77              public Binary64 end() {
78                  return Binary64Field.getInstance().getZero();
79              }
80          });
81      }
82  
83      @Test
84      void testArithmeticalBlending() {
85          // Given
86          final Field<Binary64> field = Binary64Field.getInstance();
87  
88          final FieldMatrix<Binary64> matrix1 = MatrixUtils.createFieldMatrix(field, 2, 2);
89          matrix1.setEntry(0, 0, new Binary64(1));
90          matrix1.setEntry(0, 1, new Binary64(2));
91          matrix1.setEntry(1, 0, new Binary64(3));
92          matrix1.setEntry(1, 1, new Binary64(4));
93  
94          final FieldMatrix<Binary64> matrix2 = MatrixUtils.createFieldMatrix(field, 2, 2);
95          matrix2.setEntry(0, 0, new Binary64(2));
96          matrix2.setEntry(0, 1, new Binary64(4));
97          matrix2.setEntry(1, 0, new Binary64(9));
98          matrix2.setEntry(1, 1, new Binary64(16));
99  
100         final Binary64 blendingValue = new Binary64(0.65);
101 
102         // When
103         final FieldMatrix<Binary64> blendedMatrix = matrix1.blendArithmeticallyWith(matrix2, blendingValue);
104 
105         // Then
106         assertEquals(1.65 , blendedMatrix.getEntry(0,0).getReal(), 1.0e-15);
107         assertEquals(3.3  , blendedMatrix.getEntry(0,1).getReal(), 1.0e-15);
108         assertEquals(6.9  , blendedMatrix.getEntry(1,0).getReal(), 1.0e-15);
109         assertEquals(11.8 , blendedMatrix.getEntry(1,1).getReal(), 1.0e-15);
110     }
111 
112     // local class that does NOT override multiplyTransposed nor transposeMultiply nor map nor mapToSelf
113     // so the default methods are called
114     private class DefaultMatrix extends AbstractFieldMatrix<Binary64> {
115 
116         FieldMatrix<Binary64> m;
117         public DefaultMatrix(FieldMatrix<Binary64> m) {
118             super(Binary64Field.getInstance());
119             this.m = m;
120         }
121 
122         @Override
123         public int getRowDimension() {
124             return m.getRowDimension();
125         }
126 
127         @Override
128         public int getColumnDimension() {
129             return m.getColumnDimension();
130         }
131 
132         @Override
133         public FieldMatrix<Binary64> createMatrix(int rowDimension, int columnDimension) {
134             return m.createMatrix(rowDimension, columnDimension);
135         }
136 
137         @Override
138         public FieldMatrix<Binary64> copy() {
139             return m.copy();
140         }
141 
142         @Override
143         public void setEntry(int row, int column, Binary64 value) {
144             m.setEntry(row, column, value);
145         }
146 
147         @Override
148         public void addToEntry(int row, int column, Binary64 increment) {
149             m.addToEntry(row, column, increment);
150         }
151 
152         @Override
153         public void multiplyEntry(int row, int column, Binary64 factor) {
154             m.multiplyEntry(row, column, factor);
155         }
156 
157         @Override
158         public Binary64 getEntry(int row, int column) {
159             return m.getEntry(row, column);
160         }
161 
162     }
163 
164     private FieldMatrix<Binary64> createMatrix(double[][] a) {
165         FieldMatrix<Binary64> m = MatrixUtils.createFieldMatrix(Binary64Field.getInstance(),
166                                                                  a.length, a[0].length);
167         for (int i = 0; i < m.getRowDimension(); ++i) {
168             for (int j = 0; j < m.getColumnDimension(); ++j) {
169                 m.setEntry(i, j, new Binary64(a[i][j]));
170             }
171         }
172         return m;
173     }
174 
175 }