Search in sources :

Example 21 with ISymbol

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

the class PatternMap method setValue.

public boolean setValue(IPatternObject pattern, IExpr expr) {
    ISymbol sym = pattern.getSymbol();
    IExpr temp = pattern;
    if (sym != null) {
        temp = sym;
    }
    int indx = get(temp);
    if (indx >= 0) {
        fPatternValuesArray[indx] = expr;
        return true;
    }
    throw new IllegalStateException("Pattern:" + pattern + " is not available");
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 22 with ISymbol

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

the class JASIExpr method expr2IExprPoly.

private GenPolynomial<IExpr> expr2IExprPoly(final IExpr exprPoly) throws ArithmeticException, ClassCastException {
    if (exprPoly instanceof IAST) {
        final IAST ast = (IAST) exprPoly;
        GenPolynomial<IExpr> result = fPolyFactory.getZERO();
        GenPolynomial<IExpr> p = fPolyFactory.getZERO();
        if (ast.isPlus()) {
            IExpr expr = ast.arg1();
            result = expr2IExprPoly(expr);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = expr2IExprPoly(expr);
                result = result.sum(p);
            }
            return result;
        } else if (ast.isTimes()) {
            IExpr expr = ast.arg1();
            result = expr2IExprPoly(expr);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = expr2IExprPoly(expr);
                result = result.multiply(p);
            }
            return result;
        } else if (ast.isPower()) {
            final IExpr expr = ast.arg1();
            if (expr instanceof ISymbol) {
                ExpVector leer = fPolyFactory.evzero;
                int ix = leer.indexVar(expr.toString(), fPolyFactory.getVars());
                if (ix >= 0) {
                    int exponent = -1;
                    try {
                        exponent = Validate.checkPowerExponent(ast);
                    } catch (WrongArgumentType e) {
                    //
                    }
                    if (exponent < 0) {
                        throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
                    }
                    ExpVector e = ExpVector.create(fVariables.size(), ix, exponent);
                    return fPolyFactory.getONE().multiply(e);
                }
            }
        } else if (fNumericFunction) {
            if (ast.isNumericFunction()) {
                return new GenPolynomial<IExpr>(fPolyFactory, ast);
            }
        }
    } else if (exprPoly instanceof ISymbol) {
        ExpVector leer = fPolyFactory.evzero;
        int ix = leer.indexVar(exprPoly.toString(), fPolyFactory.getVars());
        if (ix >= 0) {
            ExpVector e = ExpVector.create(fVariables.size(), ix, 1L);
            return fPolyFactory.getONE().multiply(e);
        }
        if (fNumericFunction) {
            if (exprPoly.isNumericFunction()) {
                return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
            }
            throw new ClassCastException(exprPoly.toString());
        } else {
            return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
        }
    } else if (exprPoly instanceof IInteger) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    } else if (exprPoly instanceof IFraction) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    }
    if (exprPoly.isFree(t -> fVariables.contains(t), true)) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    } else {
        for (int i = 0; i < fVariables.size(); i++) {
            if (fVariables.get(i).equals(exprPoly)) {
                ExpVector e = ExpVector.create(fVariables.size(), i, 1L);
                return fPolyFactory.getONE().multiply(e);
            }
        }
    }
    throw new ClassCastException(exprPoly.toString());
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) GenPolynomial(edu.jas.poly.GenPolynomial) ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IInteger(org.matheclipse.core.interfaces.IInteger) ExpVector(edu.jas.poly.ExpVector) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 23 with ISymbol

use of org.matheclipse.core.interfaces.ISymbol 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 24 with ISymbol

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

the class JASConvert method expr2Poly.

/**
	 * Convert the given expression into a
	 * <a href="http://krum.rz.uni-mannheim.de/jas/">JAS</a> polynomial
	 * 
	 * @param exprPoly
	 * @param numeric2Rational
	 *            if <code>true</code>, <code>INum</code> double values are
	 *            converted to <code>BigRational</code> internally
	 * 
	 * @return
	 * @throws ArithmeticException
	 * @throws ClassCastException
	 */
private GenPolynomial<C> expr2Poly(final IExpr exprPoly, boolean numeric2Rational) throws ArithmeticException, ClassCastException {
    if (exprPoly instanceof IAST) {
        final IAST ast = (IAST) exprPoly;
        if (ast.isSlot()) {
            try {
                return fPolyFactory.univariate(ast.toString(), 1L);
            } catch (IllegalArgumentException iae) {
            // fall through
            }
        } else {
            GenPolynomial<C> result = fPolyFactory.getZERO();
            GenPolynomial<C> p = fPolyFactory.getZERO();
            if (ast.isPlus()) {
                IExpr expr = ast.arg1();
                result = expr2Poly(expr, numeric2Rational);
                for (int i = 2; i < ast.size(); i++) {
                    expr = ast.get(i);
                    p = expr2Poly(expr, numeric2Rational);
                    result = result.sum(p);
                }
                return result;
            } else if (ast.isTimes()) {
                IExpr expr = ast.arg1();
                result = expr2Poly(expr, numeric2Rational);
                for (int i = 2; i < ast.size(); i++) {
                    expr = ast.get(i);
                    p = expr2Poly(expr, numeric2Rational);
                    result = result.multiply(p);
                }
                return result;
            } else if (ast.isPower() && ast.arg1().isSymbol()) {
                final ISymbol expr = (ISymbol) ast.arg1();
                int exponent = -1;
                try {
                    exponent = Validate.checkPowerExponent(ast);
                } catch (WrongArgumentType e) {
                }
                if (exponent < 0) {
                    throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
                }
                try {
                    return fPolyFactory.univariate(expr.getSymbolName(), exponent);
                } catch (IllegalArgumentException iae) {
                // fall through
                }
            } else if (ast.isPower() && ast.arg1().isSlot()) {
                final IAST expr = (IAST) ast.arg1();
                int exponent = -1;
                try {
                    exponent = Validate.checkPowerExponent(ast);
                } catch (WrongArgumentType e) {
                }
                if (exponent < 0) {
                    throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
                }
                try {
                    return fPolyFactory.univariate(expr.toString(), exponent);
                } catch (IllegalArgumentException iae) {
                // fall through
                }
            }
        }
    } else if (exprPoly instanceof ISymbol) {
        try {
            return fPolyFactory.univariate(((ISymbol) exprPoly).getSymbolName(), 1L);
        } catch (IllegalArgumentException iae) {
        // fall through
        }
    } else if (exprPoly instanceof IInteger) {
        return fPolyFactory.fromInteger((java.math.BigInteger) ((IInteger) exprPoly).asType(java.math.BigInteger.class));
    } else if (exprPoly instanceof IFraction) {
        return fraction2Poly((IFraction) exprPoly);
    } else if (exprPoly instanceof INum && numeric2Rational) {
        IFraction frac = F.fraction(((INum) exprPoly).getRealPart());
        return fraction2Poly(frac);
    } else if (exprPoly instanceof IComplexNum && numeric2Rational) {
        if (F.isZero(((IComplexNum) exprPoly).getImaginaryPart())) {
            // the imaginary part is zero
            IFraction frac = F.fraction(((INum) exprPoly).getRealPart());
            return fraction2Poly(frac);
        }
    }
    throw new ClassCastException(exprPoly.toString());
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IInteger(org.matheclipse.core.interfaces.IInteger) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) INum(org.matheclipse.core.interfaces.INum)

Example 25 with ISymbol

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

the class JASModInteger method expr2Poly.

/**
	 * Convert the given expression into a
	 * <a href="http://krum.rz.uni-mannheim.de/jas/">JAS</a> polynomial
	 * 
	 * @param exprPoly
	 * @param numeric2Rational
	 *            if <code>true</code>, <code>INum</code> double values are
	 *            converted to <code>BigRational</code> internally
	 * 
	 * @return
	 * @throws ArithmeticException
	 * @throws ClassCastException
	 */
private GenPolynomial<ModLong> expr2Poly(final IExpr exprPoly, boolean numeric2Rational) throws ArithmeticException, ClassCastException {
    if (exprPoly instanceof IAST) {
        final IAST ast = (IAST) exprPoly;
        GenPolynomial<ModLong> result = fPolyFactory.getZERO();
        GenPolynomial<ModLong> p = fPolyFactory.getZERO();
        if (ast.isPlus()) {
            IExpr expr = ast.arg1();
            result = expr2Poly(expr, numeric2Rational);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = expr2Poly(expr, numeric2Rational);
                result = result.sum(p);
            }
            return result;
        } else if (ast.isTimes()) {
            IExpr expr = ast.arg1();
            result = expr2Poly(expr, numeric2Rational);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = expr2Poly(expr, numeric2Rational);
                result = result.multiply(p);
            }
            return result;
        } else if (ast.isPower()) {
            final IExpr expr = ast.arg1();
            for (int i = 0; i < fVariables.size(); i++) {
                if (fVariables.get(i).equals(expr)) {
                    int exponent = -1;
                    try {
                        exponent = Validate.checkPowerExponent(ast);
                    } catch (WrongArgumentType e) {
                    }
                    if (exponent < 0) {
                        throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
                    }
                    ExpVector e = ExpVector.create(fVariables.size(), i, exponent);
                    return fPolyFactory.valueOf(e);
                }
            }
        }
    } else if (exprPoly instanceof ISymbol) {
        for (int i = 0; i < fVariables.size(); i++) {
            if (fVariables.get(i).equals(exprPoly)) {
                ExpVector e = ExpVector.create(fVariables.size(), i, 1L);
                return fPolyFactory.getONE().multiply(e);
            }
        }
    // class cast exception
    } else if (exprPoly instanceof IInteger) {
        return fPolyFactory.fromInteger((java.math.BigInteger) ((IInteger) exprPoly).asType(java.math.BigInteger.class));
    }
    throw new ClassCastException(exprPoly.toString());
}
Also used : ModLong(edu.jas.arith.ModLong) ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IInteger(org.matheclipse.core.interfaces.IInteger) ExpVector(edu.jas.poly.ExpVector) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

ISymbol (org.matheclipse.core.interfaces.ISymbol)117 IExpr (org.matheclipse.core.interfaces.IExpr)79 IAST (org.matheclipse.core.interfaces.IAST)76 IInteger (org.matheclipse.core.interfaces.IInteger)18 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)12 INum (org.matheclipse.core.interfaces.INum)10 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)10 IFraction (org.matheclipse.core.interfaces.IFraction)9 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)8 IComplex (org.matheclipse.core.interfaces.IComplex)7 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)7 ArrayList (java.util.ArrayList)6 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)6 HashSet (java.util.HashSet)5 EvalEngine (org.matheclipse.core.eval.EvalEngine)5 Num (org.matheclipse.core.expression.Num)5 GenPolynomial (edu.jas.poly.GenPolynomial)4 Options (org.matheclipse.core.eval.util.Options)4 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)4 ExpVector (edu.jas.poly.ExpVector)3