use of org.hipparchus.fitting.WeightedObservedPoints 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;
}
Aggregations