1 /* 2 * Copyright (C) 2015 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.numerical.fitting; 17 18 import com.irurueta.algebra.Matrix; 19 20 import java.util.Arrays; 21 22 /** 23 * Base class to fit a multi dimension function y = f(x1, x2, ...) by using 24 * provided data (x, y) 25 */ 26 public abstract class MultiDimensionFitter extends Fitter { 27 /** 28 * Input points x where a multidimensional function f(x1, x2, ...) is 29 * evaluated where each column of the matrix represents each dimension of 30 * the point and each row is related to each sample corresponding to 31 * provided y pairs of values 32 */ 33 protected Matrix x; 34 35 /** 36 * Result of evaluation of multidimensional function f(x1, x2, ...) 37 * at provided x points. This is provided as input data along x array 38 */ 39 protected double[] y; 40 41 /** 42 * Standard deviations of each pair of points (x, y). 43 */ 44 protected double[] sig; 45 46 /** 47 * Number of samples (x, y) in provided input data 48 */ 49 protected int ndat; 50 51 /** 52 * Estimated parameters of linear single dimensional function 53 */ 54 protected double[] a; 55 56 /** 57 * Covariance of estimated parameters of linear single dimensional function 58 */ 59 protected Matrix covar; 60 61 /** 62 * Estimated chi square value of input data 63 */ 64 protected double chisq; 65 66 /** 67 * Constructor 68 */ 69 protected MultiDimensionFitter() { 70 } 71 72 /** 73 * Constructor 74 * 75 * @param x input points x where a multidimensional function f(x1, x2, ...) 76 * is evaluated 77 * @param y result of evaluation of multidimensional function 78 * f(x1, x2, ...) at provided x points 79 * @param sig standard deviations of each pair of points (x, y) 80 * @throws IllegalArgumentException if provided matrix rows and arrays 81 * don't have the same length 82 */ 83 protected MultiDimensionFitter(final Matrix x, final double[] y, final double[] sig) { 84 setInputData(x, y, sig); 85 } 86 87 /** 88 * Constructor 89 * 90 * @param x input points x where a multidimensional function f(x1, x2, ...) 91 * is evaluated 92 * @param y result of evaluation of linear multidimensional function 93 * f(x1, x2, ...) at provided x points 94 * @param sig standard deviation of all pair of points assuming that 95 * standard deviations are constant 96 * @throws IllegalArgumentException if provided matrix rows and arrays 97 * don't have the same length 98 */ 99 protected MultiDimensionFitter(final Matrix x, final double[] y, final double sig) { 100 setInputData(x, y, sig); 101 } 102 103 /** 104 * Returns input points x where a multidimensional function f(x1, x2, ...) 105 * is evaluated and where each column of the matrix represents 106 * each dimension of the point and each row is related to each sample 107 * corresponding to provided y pairs of values 108 * 109 * @return input point x 110 */ 111 public Matrix getX() { 112 return x; 113 } 114 115 /** 116 * Returns result of evaluation of multidimensional function f(x) 117 * at provided x points. This is provided as input data along with x array 118 * 119 * @return result of evaluation 120 */ 121 public double[] getY() { 122 return y; 123 } 124 125 /** 126 * Returns standard deviations of each pair of points (x,y). 127 * 128 * @return standard deviations of each pair of points (x,y) 129 */ 130 public double[] getSig() { 131 return sig; 132 } 133 134 /** 135 * Sets required input data to start function fitting 136 * 137 * @param x input points x where a multidimensional function f(x1, x2, ...) 138 * is evaluated and where each column of the matrix represents each 139 * dimension of the point and each row is related to each sample 140 * corresponding to provided y pairs of values 141 * @param y result of evaluation of multidimensional function 142 * f(x1, x2, ...) at provided x points. This is provided as input data along 143 * with x array 144 * @param sig standard deviations of each pair of points (x,y) 145 * @throws IllegalArgumentException if provided arrays don't have the same 146 * size 147 */ 148 public final void setInputData(final Matrix x, final double[] y, final double[] sig) { 149 if (x.getRows() != y.length || sig.length != y.length) { 150 throw new IllegalArgumentException(); 151 } 152 153 this.x = x; 154 this.y = y; 155 this.sig = sig; 156 157 ndat = y.length; 158 } 159 160 /** 161 * Sets required input data to start function fitting and assuming constant 162 * standard deviation errors in input data 163 * 164 * @param x input points x where a multidimensional function f(x1, x2, ...) 165 * is evaluated and where each column of the matrix represents each 166 * dimension of the point and each row is related to each sample 167 * corresponding to provided y pairs of values 168 * @param y result of evaluation of multidimensional function 169 * f(x1, x2, ...) at provided x points. This is provided as input data along 170 * with x array 171 * @param sig standard deviation of all pair of points assuming that 172 * standard deviations are constant 173 * @throws IllegalArgumentException if provided arrays don't have the same 174 * size 175 */ 176 public final void setInputData(final Matrix x, final double[] y, final double sig) { 177 if (x.getRows() != y.length) { 178 throw new IllegalArgumentException(); 179 } 180 181 this.x = x; 182 this.y = y; 183 184 this.sig = new double[y.length]; 185 Arrays.fill(this.sig, sig); 186 187 ndat = y.length; 188 } 189 190 /** 191 * Returns estimated parameters of linear single dimensional function 192 * 193 * @return estimated parameters 194 */ 195 public double[] getA() { 196 return a; 197 } 198 199 /** 200 * Returns covariance of estimated parameters of linear single dimensional 201 * function 202 * 203 * @return covariance of estimated parameters 204 */ 205 public Matrix getCovar() { 206 return covar; 207 } 208 209 /** 210 * Returns estimated chi square value of input data 211 * 212 * @return estimated chi square value of input data 213 */ 214 public double getChisq() { 215 return chisq; 216 } 217 }