// Threadsafe public double Eval(double x, double y) { double[] v = new double[factory.NTERMS]; factory.Basis(new XX(x, y), v, factory.NTERMS); double r = 0; for (int i = 0; i < factory.NTERMS; i++) { r += v[i] * coeff[i]; } return(r); }
public static Poly2D PolyFit2D(Poly2DFactory poly2dfactory, int NX, int NY, double[,] data, XX[,] pos) { int NPT = 0; for (int i = 0; i < data.GetLength(0); i++) { for (int j = 0; j < data.GetLength(1); j++) { if (!Double.IsNaN(data[i, j])) { NPT++; } } } int NTERMS = poly2dfactory.NTERMS; XX[] xx = new XX[NPT]; double[] z = new double[NPT]; // The function value at each point double[] sig = new double[NPT]; // Measurement errors // flatten: data -> z, pos -> xx const float SIGMIN = 0.02f; int rowno = 0; for (int i = 0; i < NX; i++) { for (int j = 0; j < NY; j++) { if (Double.IsNaN(data[i, j])) { continue; } xx[rowno] = pos[i, j]; z[rowno] = data[i, j]; sig[rowno] = Math.Max(z[rowno] * SIGMIN, SIGMIN); // note that 1/sig[i] is used in FindFit rowno++; } } Debug.Assert(rowno == NPT); // do the fit double[] coeff, w; double[,] u, v; double chiSq; FindFit(xx, z, sig, NTERMS, delegate(X _x, double[] _p, int _np) { poly2dfactory.Basis((XX)_x, _p, _np); }, out coeff, out u, out v, out w, out chiSq); Debug.Assert(coeff.Length == NTERMS); Debug.Assert(w.Length == NTERMS); Debug.Assert((u.GetLength(0) == NPT) && (u.GetLength(1) == NTERMS)); Debug.Assert((v.GetLength(0) == NTERMS) && (v.GetLength(1) == NTERMS)); // calculate covariances double[,] covar = null; #if false // not used covar = new double[NTERMS, NTERMS]; ComputeFitCovariance(v, NTERMS, w, covar); #endif return(new Poly2D(poly2dfactory, coeff, covar, chiSq)); }