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.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      // local class that does NOT override multiplyTransposed nor transposeMultiply nor map nor mapToSelf
54      // so the default methods are called
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  }