Search in sources :

Example 41 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class Expr2LP method expr2ObjectiveFunction.

public LinearObjectiveFunction expr2ObjectiveFunction() {
    double[] coefficients = new double[fVariables.size()];
    ISignedNumber num = expr2ObjectiveFunction(fExpr, coefficients);
    if (num == null) {
        return new LinearObjectiveFunction(coefficients, 0);
    }
    return new LinearObjectiveFunction(coefficients, num.doubleValue());
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) LinearObjectiveFunction(org.hipparchus.optim.linear.LinearObjectiveFunction)

Example 42 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class Expr2LP method expr2Constraint.

public LinearConstraint expr2Constraint() {
    double[] coefficients = new double[fVariables.size()];
    if (fExpr.isAST()) {
        IAST ast = (IAST) fExpr;
        if (ast.isAST(F.Equal, 3)) {
            IExpr expr = F.eval(F.Subtract(ast.arg1(), ast.arg2()));
            ISignedNumber num = expr2ObjectiveFunction(expr, coefficients);
            if (num == null) {
                return new LinearConstraint(coefficients, Relationship.EQ, 0);
            }
            return new LinearConstraint(coefficients, Relationship.EQ, -1 * num.doubleValue());
        }
        if (ast.isAST(F.GreaterEqual, 3)) {
            IExpr expr = F.eval(F.Subtract(ast.arg1(), ast.arg2()));
            ISignedNumber num = expr2ObjectiveFunction(expr, coefficients);
            if (num == null) {
                return new LinearConstraint(coefficients, Relationship.GEQ, 0);
            }
            return new LinearConstraint(coefficients, Relationship.GEQ, -1 * num.doubleValue());
        }
        if (ast.isAST(F.LessEqual, 3)) {
            IExpr expr = F.eval(F.Subtract(ast.arg1(), ast.arg2()));
            ISignedNumber num = expr2ObjectiveFunction(expr, coefficients);
            if (num == null) {
                return new LinearConstraint(coefficients, Relationship.LEQ, 0);
            }
            return new LinearConstraint(coefficients, Relationship.LEQ, -1 * num.doubleValue());
        }
    }
    throw new WrongArgumentType(fExpr, "Conversion from expression to linear programming expression failed");
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) LinearConstraint(org.hipparchus.optim.linear.LinearConstraint) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 43 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class OutputFormFactory method convert.

public void convert(final Appendable buf, final IExpr o, final int precedence, boolean isASTHead) throws IOException {
    if (o instanceof IAST) {
        final IAST list = (IAST) o;
        IExpr header = list.head();
        if (!header.isSymbol()) {
            // print expressions like: f(#1, y)& [x]
            IAST[] derivStruct = list.isDerivativeAST1();
            if (derivStruct != null) {
                IAST a1Head = derivStruct[0];
                IAST headAST = derivStruct[1];
                if (a1Head.isAST1() && a1Head.arg1().isInteger() && headAST.isAST1() && headAST.arg1().isSymbol() && derivStruct[2] != null) {
                    try {
                        int n = ((IInteger) a1Head.arg1()).toInt();
                        // IExpr arg1 = listArg1.arg1();
                        if (n == 1 || n == 2) {
                            ISymbol f = (ISymbol) headAST.arg1();
                            convertSymbol(buf, f);
                            if (n == 1) {
                                append(buf, "'");
                            } else if (n == 2) {
                                append(buf, "''");
                            }
                            convertArgs(buf, f, list);
                            return;
                        }
                    } catch (ArithmeticException ae) {
                    }
                }
            }
            convert(buf, header, Integer.MIN_VALUE, true);
            convertFunctionArgs(buf, list);
            return;
        }
        ISymbol head = list.topHead();
        final Operator operator = getOperator(head);
        if (operator != null) {
            if (operator instanceof PostfixOperator) {
                if (list.isAST1()) {
                    convertPostfixOperator(buf, list, (PostfixOperator) operator, precedence);
                    return;
                }
            } else {
                if (convertOperator(operator, list, buf, isASTHead ? Integer.MAX_VALUE : precedence, head)) {
                    return;
                }
            }
        }
        if (list.isList() || list instanceof ASTRealVector || list instanceof ASTRealMatrix) {
            convertList(buf, list);
            return;
        }
        if (head.equals(F.Part) && (list.size() >= 3)) {
            convertPart(buf, list);
            return;
        }
        if (head.equals(F.Slot) && (list.isAST1()) && (list.arg1() instanceof IInteger)) {
            convertSlot(buf, list);
            return;
        }
        if (head.equals(F.SlotSequence) && (list.isAST1()) && (list.arg1() instanceof IInteger)) {
            convertSlotSequence(buf, list);
            return;
        }
        if ((head.equals(F.HoldForm) || head.equals(F.Defer)) && (list.isAST1())) {
            convert(buf, list.arg1());
            return;
        }
        if (head.equals(F.SeriesData) && (list.size() == 7)) {
            if (convertSeriesData(buf, list, precedence)) {
                return;
            }
        }
        if (list.isDirectedInfinity()) {
            // {
            if (list.isAST0()) {
                append(buf, "ComplexInfinity");
                return;
            }
            if (list.isAST1()) {
                if (list.arg1().isOne()) {
                    append(buf, "Infinity");
                    return;
                } else if (list.arg1().isMinusOne()) {
                    if (ASTNodeFactory.PLUS_PRECEDENCE < precedence) {
                        append(buf, "(");
                    }
                    append(buf, "-Infinity");
                    if (ASTNodeFactory.PLUS_PRECEDENCE < precedence) {
                        append(buf, ")");
                    }
                    return;
                } else if (list.arg1().isImaginaryUnit()) {
                    append(buf, "I*Infinity");
                    return;
                } else if (list.arg1().isNegativeImaginaryUnit()) {
                    append(buf, "-I*Infinity");
                    return;
                }
            }
        }
        convertAST(buf, list);
        return;
    }
    if (o instanceof ISignedNumber) {
        convertNumber(buf, (ISignedNumber) o, precedence, NO_PLUS_CALL);
        return;
    }
    if (o instanceof IComplexNum) {
        convertDoubleComplex(buf, (IComplexNum) o, precedence, NO_PLUS_CALL);
        return;
    }
    if (o instanceof IComplex) {
        convertComplex(buf, (IComplex) o, precedence, NO_PLUS_CALL);
        return;
    }
    if (o instanceof ISymbol) {
        convertSymbol(buf, (ISymbol) o);
        return;
    }
    if (o instanceof IPatternObject) {
        convertPattern(buf, (IPatternObject) o);
        return;
    }
    convertString(buf, o.toString());
}
Also used : PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) Operator(org.matheclipse.parser.client.operator.Operator) InfixOperator(org.matheclipse.parser.client.operator.InfixOperator) PrefixOperator(org.matheclipse.parser.client.operator.PrefixOperator) ISymbol(org.matheclipse.core.interfaces.ISymbol) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) ASTRealMatrix(org.matheclipse.core.expression.ASTRealMatrix) IComplex(org.matheclipse.core.interfaces.IComplex) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IInteger(org.matheclipse.core.interfaces.IInteger) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) ASTRealVector(org.matheclipse.core.expression.ASTRealVector)

Example 44 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class Times method convertTimesOperator.

/**
	 * Converts a given <code>Times[...]</code> function into the corresponding MathML output.
	 * 
	 * @param buf
	 * @param timesAST
	 * @param precedence
	 * @param caller
	 * @return
	 */
private boolean convertTimesOperator(final StringBuffer buf, final IAST timesAST, final int precedence, final int caller) {
    int size = timesAST.size();
    if (size > 1) {
        IExpr arg1 = timesAST.arg1();
        if (arg1.isMinusOne()) {
            if (size == 2) {
                fFactory.tagStart(buf, fFirstTag);
                precedenceOpen(buf, precedence);
                fFactory.convert(buf, arg1, fPrecedence);
            } else {
                if (caller == PLUS_CALL) {
                    fFactory.tag(buf, "mo", "-");
                    if (size == 3) {
                        fFactory.convert(buf, timesAST.arg2(), fPrecedence);
                        return true;
                    }
                    fFactory.tagStart(buf, fFirstTag);
                } else {
                    fFactory.tagStart(buf, fFirstTag);
                    precedenceOpen(buf, precedence);
                    fFactory.tag(buf, "mo", "-");
                }
            }
        } else if (arg1.isOne()) {
            if (size == 2) {
                fFactory.tagStart(buf, fFirstTag);
                precedenceOpen(buf, precedence);
                fFactory.convert(buf, arg1, fPrecedence);
            } else {
                if (caller == PLUS_CALL) {
                    if (size == 3) {
                        fFactory.convert(buf, timesAST.arg2(), fPrecedence);
                        return true;
                    }
                    fFactory.tagStart(buf, fFirstTag);
                } else {
                    fFactory.tagStart(buf, fFirstTag);
                    precedenceOpen(buf, precedence);
                }
            }
        } else {
            if (caller == PLUS_CALL) {
                if ((arg1 instanceof ISignedNumber) && (((ISignedNumber) arg1).isNegative())) {
                    fFactory.tag(buf, "mo", "-");
                    fFactory.tagStart(buf, fFirstTag);
                    arg1 = ((ISignedNumber) arg1).opposite();
                } else {
                    fFactory.tag(buf, "mo", "+");
                    fFactory.tagStart(buf, fFirstTag);
                }
            } else {
                fFactory.tagStart(buf, fFirstTag);
                precedenceOpen(buf, precedence);
            }
            fFactory.convert(buf, arg1, fPrecedence);
            if (fOperator.compareTo("") != 0) {
                fFactory.tag(buf, "mo", fOperator);
            }
        }
    }
    for (int i = 2; i < size; i++) {
        fFactory.convert(buf, timesAST.get(i), fPrecedence);
        if ((i < timesAST.size() - 1) && (fOperator.compareTo("") != 0)) {
            fFactory.tag(buf, "mo", fOperator);
        }
    }
    precedenceClose(buf, precedence);
    fFactory.tagEnd(buf, fFirstTag);
    return true;
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 45 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber 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

ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)49 IExpr (org.matheclipse.core.interfaces.IExpr)31 IAST (org.matheclipse.core.interfaces.IAST)22 ISymbol (org.matheclipse.core.interfaces.ISymbol)10 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)7 LinearConstraint (org.hipparchus.optim.linear.LinearConstraint)3 Num (org.matheclipse.core.expression.Num)3 IInteger (org.matheclipse.core.interfaces.IInteger)3 IntegerDistribution (org.hipparchus.distribution.IntegerDistribution)2 RealDistribution (org.hipparchus.distribution.RealDistribution)2 MathIllegalStateException (org.hipparchus.exception.MathIllegalStateException)2 LinearObjectiveFunction (org.hipparchus.optim.linear.LinearObjectiveFunction)2 WrappedException (org.matheclipse.core.eval.exception.WrappedException)2 Options (org.matheclipse.core.eval.util.Options)2 ComplexNum (org.matheclipse.core.expression.ComplexNum)2 IComplex (org.matheclipse.core.interfaces.IComplex)2 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)2 INum (org.matheclipse.core.interfaces.INum)2 IRational (org.matheclipse.core.interfaces.IRational)2 ArrayList (java.util.ArrayList)1