public static decimal[] GaussNewton(Func <decimal[], decimal[, ]> J, Func <decimal[], decimal[]> r, decimal[] p0) { decimal[] p = p0; decimal a; int iter = 0; while (iter < 100) //TODO: while error is large { a = 1; decimal[] pAdd = LinearLeastSquares(J(p), MathDecimal.Negative(r(p)), "SVD"); while (MathDecimal.SquaredNorm2(r(p)) - MathDecimal.SquaredNorm2(r(MathDecimal.Sum(p, MathDecimal.Prod(a, pAdd)))) < 1M / 2M * a * MathDecimal.SquaredNorm2(MathDecimal.Prod(J(p), pAdd)) && a >= 0.0000000000001M) { a /= 2; } p = MathDecimal.Sum(p, MathDecimal.Prod(a, pAdd)); iter++; } return(p); }
public static decimal[] GaussNewton(Func <decimal[], decimal[, ]> J, Func <decimal[], decimal[]> r, decimal[] p0) { decimal[] p = p0; decimal a = 1; int iter = 0; while (iter < 100) //TODO: while error is large { decimal[] pAdd = LinearLeastSquares(J(p), MathDecimal.Negative(r(p))); int innerIter = 0; a = 1; while (MathDecimal.Pow2(MathDecimal.Norm2(r(p))) - MathDecimal.Pow2(MathDecimal.Norm2(MathDecimal.Sum(p, MathDecimal.Prod(a, pAdd)))) < 1M / 2M * a * MathDecimal.Pow2(MathDecimal.Norm2(MathDecimal.Prod(J(p), p))) && innerIter < 50) { a /= 2; innerIter++; } int s = 0; p = MathDecimal.Sum(p, MathDecimal.Prod(a, pAdd)); iter++; } return(p); }