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