View Javadoc
1   /*
2    * Copyright (C) 2012 Alberto Irurueta Carro (alberto@irurueta.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.irurueta.algebra;
17  
18  /**
19   * Class in charge of computing infinity norms of vectors and matrices.
20   * Infinity norm is defined as the maximum row sum on a matrix or vector.
21   * <p>
22   * For the case of arrays, this library considers arrays as column matrices
23   * with one column and array length rows, hence absolute value of first element
24   * in array is returned as infinity norm.
25   */
26  public class InfinityNormComputer extends NormComputer {
27  
28      /**
29       * Constructor of this class.
30       */
31      public InfinityNormComputer() {
32          super();
33      }
34  
35      /**
36       * Returns norm type being used by this class.
37       *
38       * @return Norm type being used by this class.
39       */
40      @Override
41      public NormType getNormType() {
42          return NormType.INFINITY_NORM;
43      }
44  
45      /**
46       * Computes norm of provided matrix.
47       *
48       * @param m matrix being used for norm computation.
49       * @return norm of provided matrix.
50       */
51      @SuppressWarnings("DuplicatedCode")
52      public static double norm(final Matrix m) {
53          final var rows = m.getRows();
54          final var columns = m.getColumns();
55          double rowSum;
56          var maxRowSum = 0.0;
57  
58          for (var i = 0; i < rows; i++) {
59              rowSum = 0.0;
60              for (var j = 0; j < columns; j++) {
61                  rowSum += Math.abs(m.getElementAt(i, j));
62              }
63  
64              maxRowSum = Math.max(rowSum, maxRowSum);
65          }
66  
67          return maxRowSum;
68      }
69  
70      /**
71       * Computes norm of provided array and stores the jacobian into provided
72       * instance.
73       *
74       * @param array    array being used for norm computation.
75       * @param jacobian instance where jacobian will be stored. Must be 1xN,
76       *                 where N is length of array.
77       * @return norm of provided vector.
78       * @throws WrongSizeException if provided jacobian is not 1xN, where N is
79       *                            length of array.
80       */
81      public static double norm(final double[] array, final Matrix jacobian) throws WrongSizeException {
82          if (jacobian != null && (jacobian.getRows() != 1 || jacobian.getColumns() != array.length)) {
83              throw new WrongSizeException();
84          }
85  
86          final var norm = InfinityNormComputer.norm(array);
87  
88          if (jacobian != null) {
89              jacobian.fromArray(array);
90              jacobian.multiplyByScalar(1.0 / norm);
91          }
92  
93          return norm;
94      }
95  
96      /**
97       * Computes norm of provided matrix.
98       *
99       * @param m Matrix being used for norm computation.
100      * @return Norm of provided matrix.
101      */
102     @Override
103     public double getNorm(final Matrix m) {
104         return norm(m);
105     }
106 
107     /**
108      * Computes norm of provided array.
109      *
110      * @param array array being used for norm computation.
111      * @return norm of provided vector.
112      */
113     public static double norm(final double[] array) {
114         return Math.abs(array[0]);
115     }
116 
117     /**
118      * Computes norm of provided array.
119      *
120      * @param array Array being used for norm computation.
121      * @return Norm of provided vector.
122      */
123     @Override
124     public double getNorm(final double[] array) {
125         return norm(array);
126     }
127 }