Examples of ANumber


Examples of jmathexpr.arithmetic.ANumber

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

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

Examples of jmathexpr.arithmetic.ANumber

    }

    @Override
    public Cardinality cardinality() {
        if (a instanceof ANumber && b instanceof ANumber) {
            ANumber l = (ANumber) a;
            ANumber r = (ANumber) b;
           
            if (l.lt(r)) {
                return Reals.getInstance().cardinality();
            } else if (l.equals(r)) {
                if (leftClosed && rightClosed) {
View Full Code Here

Examples of jmathexpr.arithmetic.ANumber

            return new Equality(new Sum(ax, b), new Sum(cx, d)).matches(expr);
        }

        @Override
        public Expression apply() {
            ANumber ca = (ANumber) ax.coefficient();
            ANumber cc = (ANumber) cx.coefficient();
            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

Examples of jmathexpr.arithmetic.ANumber

            return expr.contains(new Sqrt(new FunctionPattern(x)));
        }

        @Override
        public Expression apply() {
            ANumber two = Naturals.getInstance().create(2);
            Equality eq = (Equality) target;
           
            return new Equality(new Exponentiation(eq.lhs(), two),
                    new Exponentiation(eq.rhs(), two));
        }
View Full Code Here

Examples of jmathexpr.arithmetic.ANumber

        }       
    }
   
    private Expression combineConstants(Map<Expression, List<ANumber>> selected) {
        Sum combined = new Sum();
        ANumber sum;

        for (Expression c : selected.keySet()) {
            sum = Naturals.zero();
           
            for (ANumber a : selected.get(c)) {
                sum = sum.add(a);
            }
           
            if (sum.isOne()) {
                combined.add(c);
            } else if (!sum.isZero()) {
                if (c instanceof ANumber) { // c = 1
                    combined.add(sum);
                } else if (sum.negate().isOne()) {
                    combined.add(new Negation(c));
                } else {
                    combined.add(new Multiplication(sum, c));
                }
            }
View Full Code Here

Examples of jmathexpr.arithmetic.ANumber

    @Override
    public Expression evaluate() {
        Expression x = arg.evaluate();
       
        if (x instanceof ANumber) {
            ANumber a = (ANumber) x;
           
            if (a.isNegative()) {
                return a.negate();
            } else {
                return a;
            }
        } else if (x instanceof Negation) { // |-x| = x
            return ((Negation) x).getChild();
View Full Code Here

Examples of jmathexpr.arithmetic.ANumber

    @Override
    public Expression evaluate() {
        Expression x = arg.evaluate();
       
        if (x instanceof ANumber) {
            ANumber a = (ANumber) x;
           
            if (a instanceof NaturalNumber) {
                return Naturals.sqrt((NaturalNumber) a);
            } else if (!a.isNegative()) {
                if (a instanceof IntegerNumber) {
                    return Naturals.sqrt(a.toNatural());
                } else if (a instanceof RationalNumber) {
                    return sqrt((RationalNumber) a);
                }
            }
        } else if (x instanceof Multiplication) {
View Full Code Here

Examples of jmathexpr.arithmetic.ANumber

                r = new Division(r, p).evaluate();
                divisor = (NaturalNumber) divisor.multiply(p);
            }
        }
       
        ANumber simplifiedDenominator = denominator.divide(divisor);
        Expression numerator;
       
        if (sum instanceof Addition) {
            numerator = new Addition(l, r).evaluate();
        } else if (sum instanceof Subtraction) {
            numerator = new Subtraction(l, r).evaluate();
        } else {
            throw new IllegalStateException(String.format(
                    "Unexpected operation type: %s (%s)", sum, sum.getClass()));
        }
       
        if (simplifiedDenominator.isOne()) {
            return numerator;
        } else {
            return new Division(numerator, simplifiedDenominator);
        }
    }
View Full Code Here

Examples of jmathexpr.arithmetic.ANumber

        }
       
        Expression two = Naturals.getInstance().create(2);
       
        if (l instanceof ANumber) {
            ANumber a = (ANumber) l;
           
            if (a.negate().isOne()) {
                return new Negation(r).evaluate();
            } else if (r instanceof Division) {
                Division d = (Division) r;
               
                if (d.rhs() instanceof ANumber) { // a x/b = a/b x
                    l = a.divide((ANumber) d.rhs());
                    r = d.lhs();
                }
            } else if (r instanceof Multiplication) {
                Multiplication m = (Multiplication) r;
               
                if (m.lhs instanceof ANumber) { // a (bx) = (ab) x
                    l = a.multiply((ANumber) m.lhs);
                    r = m.rhs;
                } else if (m.rhs instanceof ANumber) { // a (xb) = (ab) x
                    l = a.multiply((ANumber) m.rhs);
                    r = m.lhs;
                }
            } else if (r instanceof Negation) { // a (-x) = -ax
                l = a.negate();
                r = ((Negation) r).getChild();
               
                return new Multiplication(l, r).evaluate();
            } else if (a.isNegative()) { // -a x = -(ax)
                return new Negation(new Multiplication(a.negate(), r)).evaluate();
            }
        } else if (l instanceof Negation) { // (-a)b = -(ab)
            return new Negation(new Multiplication(((Negation) l).getChild(), r)).evaluate();
        } else if (l instanceof Multiplication) { // (ab) c
            Multiplication m = (Multiplication) l;
           
            if (m.rhs instanceof Sqrt && r instanceof Sqrt) {
                l = m.lhs;
                r = new Multiplication(m.rhs, r).evaluate();
            } else if (r instanceof ANumber) { // (ax) c = (ac) x
                l = new Multiplication(m.lhs, r).evaluate();
                r = m.rhs;
            }
        } else if (r instanceof Division) {
            Division d = (Division) r;
           
            if (l.equals(d.rhs())) { // a x/a = x
                if (d.rhs() instanceof ANumber && ((ANumber) d.rhs()).isZero()) {
                    throw new ArithmeticException("Division by zero: " + this);
                }
               
                return d.lhs();
            } else {
                return new Division(new Multiplication(l, d.lhs()), d.rhs()).evaluate();
            }
        } else if (r instanceof Multiplication) { // a (b c)
            Multiplication m = (Multiplication) r;
           
            if (l.equals(m.lhs)) { // a (a c) = a^2 c
                l = new Exponentiation(l, two).evaluate();
                r = m.rhs;
            } else if (l.equals(m.rhs)) { // a (b a) = a^2 b
                l = new Exponentiation(l, two).evaluate();
                r = m.lhs;
            } else if (l instanceof Negation) {
                if (m.lhs instanceof ANumber) { // -a (nx) = -n * ax
                    r = new Multiplication(l, m.rhs).evaluate();
                    l = ((ANumber) m.lhs).negate();
                }               
            } else if (m.lhs instanceof ANumber) {
                r = new Multiplication(l, m.rhs).evaluate();
                l = m.lhs;
            }
        } else if (r instanceof ANumber) {
            ANumber b = (ANumber) r;
           
            if (l instanceof Multiplication) {
                Multiplication m = (Multiplication) l;
               
                if (m.rhs instanceof ANumber) { // (xa) b = x (ab)
View Full Code Here

Examples of jmathexpr.arithmetic.ANumber

            Expression b = p.getCoefficient(1);
            Expression c = p.getCoefficient(0);
            NaturalNumber two = Naturals.getInstance().create(2);
           
            if (dd instanceof ANumber) {
                ANumber d = (ANumber) dd;
               
                if (d.isNegative()) {
                    return new EmptySet();
                } else if (d.isZero()) {
                    return new FiniteSet(new Division(new Negation(b),
                            new Multiplication(two, a)).evaluate());
                }
            }
           
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.