Package jmathexpr

Examples of jmathexpr.Expression


       
        return new Subtraction(l, r);
    }
   
    private Expression simplify() {
        Expression l = lhs.evaluate();
        Expression r = rhs.evaluate();
       
        if (l.equals(r)) { // a - a = 0
            return Naturals.zero();
        } else if (l instanceof Addition && ((Addition) l).rhs().equals(r)) { // (a + b) - b = a
            return ((Addition) l).lhs();
          // contradicts to  
//        } else if (l instanceof Negation) { // -a - b = -(a + b)
//            return new Negation(new Addition(((Negation) l).getChild(), r)).evaluate();
        } else if (r instanceof ANumber && ((ANumber) r).isNegative()) { // a - (-b) = a + b
            return new Addition(l, ((ANumber) r).negate()).evaluate();
        } else if (r instanceof Negation) { // a - (-b) = a + b
            return new Addition(l, ((Negation) r).getChild()).evaluate();
        } else if (l instanceof Operation && l.getPrecedence() == Precedence.Addition) {
            return new Sum(l, new Negation(r)).evaluate();
        } else if (r instanceof Operation && r.getPrecedence() == Precedence.Addition) {
            return new Sum(l, new Negation(r)).evaluate();
        }
       
        return new Subtraction(l, r);
    }
View Full Code Here


    }
   
    @Override
    public boolean isApplicable(Rule rule) {
        boolean isApplicable = false;
        Expression transformed = this;
       
        if (rule.isRecursive()) {
            Expression l = lhs, r = rhs;

            if (lhs.isApplicable(rule)) {
                isApplicable = true;
                l = rule.subapply();
            }
View Full Code Here

            }

            @Override
            public Expression apply() {
                Equality eq = (Equality) target;
                Expression lhs = Sum.add(eq.lhs(), a.hit());
                Expression rhs = Sum.add(eq.rhs(), a.hit());

                return new Equality(lhs, rhs);
            }
View Full Code Here

            return equations;
        }

        @Override
        public Expression subapply() {
            Expression applied = rule.apply();
           
            if (variations.hasNextRule()) {
                rule = variations.nextRule();
            }
           
View Full Code Here

     */
    public abstract Set solve() throws EquationSolveException;

    @Override
    public Expression evaluate() {
        Expression evaluated = super.evaluate();
       
        if (evaluated instanceof TruthValue) {
            return evaluated;
        } else {
            Expression reduced =
                    new Subtraction(((Equality) evaluated).lhs(), ((Equality) evaluated).rhs()).evaluate();
            TruthValue isZero = isZero(reduced);
           
            if (isZero != null) {
                return isZero;
View Full Code Here

   
    private boolean satisfies(Expression expr) {
        try {
            x.setValue(expr);

            Expression evaluated = evaluate();

            if (evaluated instanceof TruthValue) {
                boolean satisfied = ((TruthValue) evaluated).toBoolean();
               
                System.out.printf("    check: %s at %s = %s: %s%n",
View Full Code Here

    public static boolean isConvertible(Expression expr, Variable x) {
        if (!(expr instanceof Equality)) {
            return false;
        }
       
        Expression lhs = ((Equality) expr).lhs();
        Expression rhs = ((Equality) expr).rhs();
        boolean lhsIsLinear = isLinear(lhs, x);
        boolean rhsIsLinear = isLinear(rhs, x);
       
        return (lhsIsLinear && (rhs.isConstant() || rhsIsLinear))
                || (rhsIsLinear && (lhs.isConstant() || lhsIsLinear));
    }
View Full Code Here

        if (pattern.matches(expr)) {
            return true;
        } else if (expr instanceof Negation) {
            return isLinear(((Negation) expr).getChild(), x);
        } else if (expr instanceof Division) {
            Expression lhs = ((Division) expr).lhs();
            Expression rhs = ((Division) expr).rhs();
           
            return rhs.isConstant() && isLinear(lhs, x);
        } else if (expr instanceof Operation
                && expr.getPrecedence() == Precedence.Addition) {
            boolean isLinear = false;
           
            for (Expression t : ((Operation) expr).operands()) {
View Full Code Here

            ANumber coef = ca.lt(cc) ? ca : cc;

            if (coef.isNegative()) {
                coef = coef.negate();
               
                Expression cx = coef.isOne() ? x.evaluate() : new Multiplication(coef, x.evaluate());
               
                return new Equality(Sum.add(((Equality) target).lhs(), cx),
                    Sum.add(((Equality) target).rhs(), cx));
            } else {
                Expression cx = coef.isOne() ? x.evaluate() : new Multiplication(coef, x.evaluate());
               
                return new Equality(Sum.subtract(((Equality) target).lhs(), cx),
                    Sum.subtract(((Equality) target).rhs(), cx));
            }
        }
View Full Code Here

        super(operand, Sign.Subtraction);
    }
   
    @Override
    public Expression evaluate() {
        Expression simplified = simplify();
       
        if (!(simplified instanceof Negation)) {
            return simplified;
        }
       
        Expression o = ((Negation) simplified).operand;
        Expression value;
       
        if (o instanceof ANumber) {
            value = ((ANumber) o).negate();
        } else if (o instanceof Multiplication) {
            Multiplication t = (Multiplication) o;
View Full Code Here

TOP

Related Classes of jmathexpr.Expression

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.