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 Frobenius norms of vectors and matrices. 20 */ 21 public class FrobeniusNormComputer extends NormComputer { 22 23 /** 24 * Constructor of this class. 25 */ 26 public FrobeniusNormComputer() { 27 super(); 28 } 29 30 /** 31 * Returns norm type being used by this class. 32 * 33 * @return Norm type being used by this class. 34 */ 35 @Override 36 public NormType getNormType() { 37 return NormType.FROBENIUS_NORM; 38 } 39 40 /** 41 * Computes norm of provided matrix. 42 * 43 * @param m matrix being used for norm computation. 44 * @return norm of provided matrix. 45 */ 46 public static double norm(final Matrix m) { 47 final var rows = m.getRows(); 48 final var columns = m.getColumns(); 49 final var length = rows * columns; 50 var sum = 0.0; 51 double value; 52 for (var i = 0; i < length; i++) { 53 value = m.getElementAtIndex(i); 54 sum += value * value; 55 } 56 return Math.sqrt(sum); 57 } 58 59 /** 60 * Computes norm of provided array and stores the jacobian into provided 61 * instance. 62 * 63 * @param array array being used for norm computation. 64 * @param jacobian instance where jacobian will be stored. Must be 1xN, 65 * where N is length of array. 66 * @return norm of provided vector. 67 * @throws WrongSizeException if provided jacobian is not 1xN, where N is 68 * length of array. 69 */ 70 public static double norm(final double[] array, final Matrix jacobian) throws WrongSizeException { 71 if (jacobian != null && (jacobian.getRows() != 1 || jacobian.getColumns() != array.length)) { 72 throw new WrongSizeException(); 73 } 74 75 final var norm = FrobeniusNormComputer.norm(array); 76 77 if (jacobian != null) { 78 jacobian.fromArray(array); 79 jacobian.multiplyByScalar(1.0 / norm); 80 } 81 82 return norm; 83 } 84 85 /** 86 * Computes norm of provided matrix. 87 * 88 * @param m Matrix being used for norm computation. 89 * @return Norm of provided matrix. 90 */ 91 @Override 92 public double getNorm(final Matrix m) { 93 return norm(m); 94 } 95 96 /** 97 * Computes norm of provided array. 98 * 99 * @param array array being used for norm computation. 100 * @return norm of provided vector. 101 */ 102 public static double norm(final double[] array) { 103 var sum = 0.0; 104 for (final var value : array) { 105 sum += value * value; 106 } 107 return Math.sqrt(sum); 108 } 109 110 /** 111 * Computes norm of provided array. 112 * 113 * @param array Array being used for norm computation. 114 * @return Norm of provided vector. 115 */ 116 @Override 117 public double getNorm(final double[] array) { 118 return norm(array); 119 } 120 }