use of org.hipparchus.fitting.AbstractCurveFitter in project symja_android_library by axkr.
the class Fit method numericEval.
@Override
public IExpr numericEval(final IAST ast, EvalEngine engine) {
Validate.checkSize(ast, 4);
if (ast.arg2().isSignedNumber() && ast.arg3().isSymbol()) {
int degree = ((ISignedNumber) ast.arg2()).toInt();
double[] initialGuess = new double[degree];
for (int i = 0; i < degree; i++) {
initialGuess[i] = 1.0;
}
AbstractCurveFitter fitter = PolynomialCurveFitter.create(degree);
int[] isMatrix = ast.arg1().isMatrix();
WeightedObservedPoints obs = new WeightedObservedPoints();
if (isMatrix != null && isMatrix[1] == 2) {
final double[][] elements = Expr2Object.toDoubleMatrix((IAST) ast.arg1());
if (elements == null) {
return F.NIL;
}
for (int i = 0; i < elements.length; i++) {
obs.add(1.0, elements[i][0], elements[i][1]);
}
} else {
int rowSize = ast.arg1().isVector();
if (rowSize < 0) {
return F.NIL;
}
final double[] elements = Expr2Object.toDoubleVector((IAST) ast.arg1());
for (int i = 0; i < elements.length; i++) {
obs.add(1.0, i + 1, elements[i]);
}
}
return Convert.polynomialFunction2Expr(fitter.fit(obs.toList()), (ISymbol) ast.arg3());
}
return F.NIL;
}
use of org.hipparchus.fitting.AbstractCurveFitter in project Orekit by CS-SI.
the class SecularAndHarmonic method fit.
/**
* Fit parameters.
* @see #getFittedParameters()
*/
public void fit() {
final AbstractCurveFitter fitter = new AbstractCurveFitter() {
/**
* {@inheritDoc}
*/
@Override
protected LeastSquaresProblem getProblem(final Collection<WeightedObservedPoint> observations) {
// Prepare least-squares problem.
final int len = observations.size();
final double[] target = new double[len];
final double[] weights = new double[len];
int i = 0;
for (final WeightedObservedPoint obs : observations) {
target[i] = obs.getY();
weights[i] = obs.getWeight();
++i;
}
final AbstractCurveFitter.TheoreticalValuesFunction model = new AbstractCurveFitter.TheoreticalValuesFunction(new LocalParametricFunction(), observations);
// build a new least squares problem set up to fit a secular and harmonic curve to the observed points
return new LeastSquaresBuilder().maxEvaluations(Integer.MAX_VALUE).maxIterations(Integer.MAX_VALUE).start(fitted).target(target).weight(new DiagonalMatrix(weights)).model(model.getModelFunction(), model.getModelFunctionJacobian()).build();
}
};
fitted = fitter.fit(observedPoints);
}
Aggregations