Search in sources :

Example 1 with WeightedObservedPoints

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;
}
Also used : WeightedObservedPoints(org.hipparchus.fitting.WeightedObservedPoints) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) AbstractCurveFitter(org.hipparchus.fitting.AbstractCurveFitter)

Aggregations

AbstractCurveFitter (org.hipparchus.fitting.AbstractCurveFitter)1 WeightedObservedPoints (org.hipparchus.fitting.WeightedObservedPoints)1 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)1