Package jmathexpr

Examples of jmathexpr.Expression


                return new Equality(new Subtraction(ax, b), c).matches(expr);
            }

            @Override
            public Expression apply() {
                Expression term = b.hit();
               
                return new Equality(Sum.add(((Equality) target).lhs(), term),
                        Sum.add(((Equality) target).rhs(), term));
            }
View Full Code Here


                return new Equality(new Subtraction(b, ax), c).matches(expr);
            }

            @Override
            public Expression apply() {
                Expression var = ax.hit();
               
                return new Equality(Sum.add(((Equality) target).lhs(), var),
                        Sum.add(((Equality) target).rhs(), var));
            }
View Full Code Here

       
        @Override
        public boolean matches(Expression expr) {
            target = expr;
           
            Expression ax = new Multiplication(a, x);

            boolean matches = new Equality(ax, b).matches(expr) || new Equality(b, ax).matches(expr);
           
            // Don't match the case x = b (a = 1)
            matches = matches && !a.hit().equals(Naturals.one());
View Full Code Here

            return matches;
        }
       
        @Override
        public Expression apply() {
            Expression coef = a.hit();

            return new Equality(new Division(((Equality) target).lhs(), coef),
                    new Division(((Equality) target).rhs(), coef));
        }
View Full Code Here

    }

    @Override
    public TruthValue contains(Expression element) {
        if (element instanceof ANumber) {
            Expression leftTest = leftClosed ? new LE(a, element).evaluate() : new LT(a, element).evaluate();
            Expression rightTest = rightClosed ? new LE(element, b).evaluate() : new LT(element, b).evaluate();
           
            if (leftTest instanceof TruthValue && rightTest instanceof TruthValue) {
                return ((TruthValue) leftTest).and((TruthValue) rightTest);
            }
        }
View Full Code Here

        super (lhs, rhs, Sign.Addition);
    }
   
    @Override
    public Expression evaluate() {
        Expression simplified = simplify();
       
        if (!(simplified instanceof Addition)) return simplified;
       
        Expression l = ((Addition) simplified).lhs;
        Expression r = ((Addition) simplified).rhs;
       
        if (l instanceof ANumber && r instanceof ANumber) {
            return ((ANumber) l).add((ANumber) r);
        }
       
View Full Code Here

       
        return new Addition(l, r);
    }
   
    private Expression simplify() {
        Expression l = lhs.evaluate();
        Expression r = rhs.evaluate();
       
        if (l.equals(r)) {
            return new Multiplication(Naturals.getInstance().create(2), l);
        } else if (l instanceof Division && r instanceof Division) {
            Division dl = (Division) l;
            Division dr = (Division) r;
           
            if (dl.rhs().equals(dr.rhs())) { // common denominator
                return new Division(new Addition(dl.lhs(), dr.lhs()), dl.rhs()).evaluate();
            }
        } else if (l instanceof Division) { // a/b + c = (a + bc) / b
            Division div = (Division) l;
           
            return new Division(new Addition(
                    div.lhs(), new Multiplication(div.rhs(), r)), div.rhs()).evaluate();
        } else if (l instanceof Negation) { // -a + b = b - a
            if (r instanceof Negation) { // -a - b = -(a + b)
                return new Negation(new Addition(((Negation) l).getChild(), ((Negation) r).getChild()));
            } else { // -a + b = b - a
                return new Subtraction(r, ((Negation) l).getChild()).evaluate();
            }
        } else if (l.getPrecedence() == Precedence.Addition || r.getPrecedence() == Precedence.Addition) {
            return new Sum(l, r).evaluate();
        } else if (r instanceof Negation) { // a + (-b) = a - b
            return new Subtraction(l, ((Negation) r).getChild()).evaluate();
        }
       
View Full Code Here

        super(lhs, rhs, RelationSymbol.Equality);
    }
   
    @Override
    public Expression evaluate() {
        Expression l = lhs.evaluate();
        Expression r = rhs.evaluate();
       
        if (l.getClass().equals(r.getClass())) {
            return TruthValue.valueOf(l.equals(r));
        } else if (l instanceof ANumber && r instanceof ANumber) {
            return TruthValue.valueOf(Numbers.equal((ANumber) l, (ANumber) r));
        } else {
            return new Equality(l, r);
View Full Code Here

     *
     * @return either the factorized form of this sum or this
     */
    public Expression factorize() {
        IntegerNumber l = null, r = null;
        Expression lrest = null, rrest = null;
       
        if (lhs instanceof IntegerNumber) {
            l = (IntegerNumber) lhs;
            lrest = Naturals.one();
        } else if (lhs instanceof Multiplication
View Full Code Here

        }
    }
   
    @Override
    public Expression evaluate() {
        Expression simplified = simplify();
       
        if (!(simplified instanceof Subtraction)) return simplified;
       
        Expression l = ((Subtraction) simplified).lhs;
        Expression r = ((Subtraction) simplified).rhs;
       
        if (l instanceof ANumber && r instanceof ANumber) {
            return  ((ANumber) l).subtract((ANumber) r);
        } else if (l instanceof Polynomial) {
            return ((Polynomial) l).subtract(r);
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.