Search in sources :

Example 1 with AbstractCurveFitter

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

Example 2 with AbstractCurveFitter

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);
}
Also used : WeightedObservedPoint(org.hipparchus.fitting.WeightedObservedPoint) DiagonalMatrix(org.hipparchus.linear.DiagonalMatrix) Collection(java.util.Collection) WeightedObservedPoint(org.hipparchus.fitting.WeightedObservedPoint) AbstractCurveFitter(org.hipparchus.fitting.AbstractCurveFitter) LeastSquaresBuilder(org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresBuilder)

Aggregations

AbstractCurveFitter (org.hipparchus.fitting.AbstractCurveFitter)2 Collection (java.util.Collection)1 WeightedObservedPoint (org.hipparchus.fitting.WeightedObservedPoint)1 WeightedObservedPoints (org.hipparchus.fitting.WeightedObservedPoints)1 DiagonalMatrix (org.hipparchus.linear.DiagonalMatrix)1 LeastSquaresBuilder (org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresBuilder)1 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)1