Search in sources :

Example 6 with ISignedNumber

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

the class Max method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 1);
    if (ast.isAST0()) {
        return F.CNInfinity;
    }
    if (ast.arg1().isInterval1()) {
        IAST list = (IAST) ast.arg1().getAt(1);
        try {
            return (ISignedNumber) list.arg2();
        } catch (ClassCastException cca) {
        // do nothing
        }
    }
    IAST resultList = EvalAttributes.flatten(F.List, ast);
    if (resultList.isPresent()) {
        return maximum(resultList, true);
    }
    return maximum(ast, false);
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IAST(org.matheclipse.core.interfaces.IAST)

Example 7 with ISignedNumber

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

the class Interval method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 2);
    if (ast.isInterval1()) {
        IAST list = (IAST) ast.arg1();
        try {
            ISignedNumber min = (ISignedNumber) list.arg1();
            ISignedNumber max = (ISignedNumber) list.arg2();
            if (min.greaterThan(max).isTrue()) {
                throw new WrongArgumentType(ast, ast.get(1), 1, "Min > Mac in interval");
            }
        } catch (ClassCastException cca) {
        // do nothing
        }
    }
    return F.NIL;
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IAST(org.matheclipse.core.interfaces.IAST)

Example 8 with ISignedNumber

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

the class Expr2LP method expr2ObjectiveFunction.

private ISignedNumber expr2ObjectiveFunction(final IExpr expr, double[] coefficients) throws ArithmeticException, ClassCastException {
    if (expr instanceof IAST) {
        final IAST ast = (IAST) expr;
        if (ast.isPlus()) {
            double constantTerm = 0.0;
            for (int i = 1; i < ast.size(); i++) {
                IExpr temp = ast.get(i);
                ISignedNumber num = expr2ObjectiveFunction(temp, coefficients);
                if (num != null) {
                    constantTerm += num.doubleValue();
                }
            }
            return F.num(constantTerm);
        } else if (ast.isTimes()) {
            ISymbol variable = null;
            double value = 1.0;
            for (int i = 1; i < ast.size(); i++) {
                IExpr temp = ast.get(i);
                if (temp.isVariable()) {
                    if (variable != null) {
                        throw new WrongArgumentType(temp, "Conversion from expression to linear programming expression failed");
                    }
                    variable = (ISymbol) temp;
                    continue;
                }
                ISignedNumber num = temp.evalSignedNumber();
                if (num != null) {
                    value *= num.doubleValue();
                    continue;
                }
                throw new WrongArgumentType(temp, "Conversion from expression to linear programming expression failed");
            }
            if (variable != null) {
                for (int i = 0; i < coefficients.length; i++) {
                    if (variable.equals(fVariables.get(i))) {
                        coefficients[i] += value;
                        return null;
                    }
                }
                throw new WrongArgumentType(ast, "Conversion from expression to linear programming expression failed");
            }
            return F.num(value);
        }
    } else if (expr.isVariable()) {
        ISymbol variable = (ISymbol) expr;
        for (int i = 0; i < coefficients.length; i++) {
            if (variable.equals(fVariables.get(i))) {
                coefficients[i] += 1.0d;
                return null;
            }
        }
        throw new WrongArgumentType(expr, "Conversion from expression to linear programming expression failed");
    }
    ISignedNumber num = expr.evalSignedNumber();
    if (num != null) {
        return num;
    }
    throw new WrongArgumentType(expr, "Conversion from expression to linear programming expression failed");
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) LinearConstraint(org.hipparchus.optim.linear.LinearConstraint)

Example 9 with ISignedNumber

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

the class Expr2Object method toDoubleMatrix.

/**
	 * 
	 * @param ast
	 * @return <code>null</code> if ast is no matrix
	 * @throws WrongArgumentType
	 * @deprecated use {@link IExpr#toDoubleMatrix()}
	 */
public static double[][] toDoubleMatrix(IAST ast) throws WrongArgumentType {
    int[] dim = ast.isMatrix();
    if (dim == null) {
        return null;
    }
    double[][] result = new double[dim[0]][dim[1]];
    ISignedNumber signedNumber;
    for (int i = 1; i <= dim[0]; i++) {
        IAST row = (IAST) ast.get(i);
        for (int j = 1; j <= dim[1]; j++) {
            signedNumber = row.get(j).evalSignedNumber();
            if (signedNumber != null) {
                result[i - 1][j - 1] = signedNumber.doubleValue();
            } else {
                throw new WrongArgumentType(ast, ast.get(i), i, "Conversion into a matrix of double values not possible!");
            }
        }
    }
    return result;
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IAST(org.matheclipse.core.interfaces.IAST)

Example 10 with ISignedNumber

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

the class Expr2Object method toDoubleVector.

/**
	 * 
	 * @param ast
	 * @return
	 * @throws WrongArgumentType
	 * @deprecated use {@link IExpr#toDoubleVector()}
	 */
public static double[] toDoubleVector(IAST ast) throws WrongArgumentType {
    double[] result = new double[ast.size() - 1];
    ISignedNumber signedNumber;
    for (int i = 1; i < ast.size(); i++) {
        signedNumber = ast.get(i).evalSignedNumber();
        if (signedNumber != null) {
            result[i - 1] = signedNumber.doubleValue();
        } else {
            throw new WrongArgumentType(ast, ast.get(i), i, "Conversion into a vector of double values not possible!");
        }
    }
    return result;
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType)

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