Search in sources :

Example 1 with Rational

use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.

the class Minus method simplify.

public static Expression simplify(Expression expression) {
    Expression result = expression;
    Expression first = expression.get(0);
    Expression second = expression.get(1);
    if (Expressions.isNumber(first)) {
        Rational firstValue = first.rationalValue();
        if (Expressions.isNumber(second)) {
            Rational secondValue = second.rationalValue();
            result = Expressions.makeSymbol(firstValue.subtract(secondValue));
        } else if (firstValue.isZero()) {
            result = Expressions.apply("-", second);
        }
    } else if (Expressions.isNumber(second) && second.rationalValue().isZero()) {
        result = first;
    }
    return result;
}
Also used : Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression)

Example 2 with Rational

use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.

the class Division method simplify.

public static Expression simplify(Expression expression) {
    if (expression.get(1).equals(INFINITY) || expression.get(1).equals(MINUS_INFINITY)) {
        if (expression.get(0).equals(INFINITY) || expression.get(0).equals(MINUS_INFINITY)) {
            throw new Error("Undefined value for division of infinities " + expression);
        } else {
            return ZERO;
        }
    }
    if (expression.get(0).equals(INFINITY) || expression.get(0).equals(MINUS_INFINITY)) {
        if (expression.get(1).equals(INFINITY) || expression.get(1).equals(MINUS_INFINITY)) {
            throw new Error("Undefined value for division of infinities " + expression);
        } else {
            return expression.get(0);
        }
    }
    if (expression.get(0).equals(expression.get(1))) {
        // if numerator and denominator are equal, result is 1.
        return ONE;
    }
    if (expression.get(0).equals(0)) {
        // if numerator is 0, fraction is 0.
        return ZERO;
    }
    if (expression.get(1).equals(1)) {
        // if denominator is 1, fraction is numerator.
        return expression.get(0);
    }
    if (ExpressionIsSymbolOfType.apply(expression.get(0), Number.class) && ExpressionIsSymbolOfType.apply(expression.get(1), Number.class)) {
        Rational numerator = expression.get(0).rationalValue();
        Rational denominator = expression.get(1).rationalValue();
        Rational quotient = Util.divisionWithArbitraryPrecisionIfPossible(numerator, denominator);
        if (quotient != null) {
            return Expressions.makeSymbol(quotient);
        }
    }
    return expression;
}
Also used : Rational(com.sri.ai.util.math.Rational)

Example 3 with Rational

use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.

the class DefaultMonomialTest method testGetNumericConstantFactor.

@Test
public void testGetNumericConstantFactor() {
    Assert.assertEquals(new Rational(0), makeMonomial("0").getNumericConstantFactor());
    Assert.assertEquals(new Rational(0), makeMonomial("0*x^2").getNumericConstantFactor());
    Assert.assertEquals(new Rational(1), makeMonomial("1").getNumericConstantFactor());
    Assert.assertEquals(new Rational(-1), makeMonomial("-1").getNumericConstantFactor());
    Assert.assertEquals(new Rational(2), makeMonomial("2").getNumericConstantFactor());
    // Ensure the numeric constant is set to 1 if not explicitly represented in the expression
    Assert.assertEquals(new Rational(1), makeMonomial("x^2").getNumericConstantFactor());
    Assert.assertEquals(new Rational(4), makeMonomial("4*x^2").getNumericConstantFactor());
    Assert.assertEquals(new Rational(4), makeMonomial("y*4*x^2").getNumericConstantFactor());
    // Test that numerical constants are multiplied together
    Assert.assertEquals(new Rational(8), makeMonomial("y^3*2*x^2*4").getNumericConstantFactor());
    // Test edge case where the numeric constant is represented as a power	
    Assert.assertEquals(new Rational(8), makeMonomial("2^3*x^2").getNumericConstantFactor());
    // Test edge case where the numeric constant is represented as a power and a separate constant
    Assert.assertEquals(new Rational(24), makeMonomial("2^3*x^2*3").getNumericConstantFactor());
}
Also used : Rational(com.sri.ai.util.math.Rational) Test(org.junit.Test)

Example 4 with Rational

use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.

the class DefaultMonomialTest method testGetPowerOfFactor.

@Test
public void testGetPowerOfFactor() {
    // Implicit power of 1
    Monomial m = makeMonomial("2*x");
    Assert.assertEquals(2, m.getFactors().size());
    Assert.assertEquals(new Rational(0), m.getPowerOfFactor(Expressions.parse("1")));
    Assert.assertEquals(new Rational(1), m.getPowerOfFactor(Expressions.parse("2")));
    Assert.assertEquals(new Rational(1), m.getPowerOfFactor(Expressions.parse("x")));
    m = makeMonomial("2*x^3*y^7*z^11");
    Assert.assertEquals(new Rational(3), m.getPowerOfFactor(Expressions.parse("x")));
    Assert.assertEquals(new Rational(7), m.getPowerOfFactor(Expressions.parse("y")));
    Assert.assertEquals(new Rational(11), m.getPowerOfFactor(Expressions.parse("z")));
    m = makeMonomial("2*z^3*y^7*x^11");
    Assert.assertEquals(new Rational(11), m.getPowerOfFactor(Expressions.parse("x")));
    Assert.assertEquals(new Rational(7), m.getPowerOfFactor(Expressions.parse("y")));
    Assert.assertEquals(new Rational(3), m.getPowerOfFactor(Expressions.parse("z")));
    m = makeMonomial("2*z^3*x^7*y^11");
    Assert.assertEquals(new Rational(7), m.getPowerOfFactor(Expressions.parse("x")));
    Assert.assertEquals(new Rational(11), m.getPowerOfFactor(Expressions.parse("y")));
    Assert.assertEquals(new Rational(3), m.getPowerOfFactor(Expressions.parse("z")));
    m = makeMonomial("2*z^3*x^7*x^11");
    Assert.assertEquals(new Rational(18), m.getPowerOfFactor(Expressions.parse("x")));
    Assert.assertEquals(new Rational(0), m.getPowerOfFactor(Expressions.parse("y")));
    Assert.assertEquals(new Rational(3), m.getPowerOfFactor(Expressions.parse("z")));
    // If factor not present in the monomial its power is 0.
    m = makeMonomial("2");
    Assert.assertEquals(new Rational(0), m.getPowerOfFactor(Expressions.parse("x")));
}
Also used : Rational(com.sri.ai.util.math.Rational) DefaultMonomial(com.sri.ai.grinder.polynomial.core.DefaultMonomial) Monomial(com.sri.ai.grinder.polynomial.api.Monomial) Test(org.junit.Test)

Example 5 with Rational

use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.

the class DefaultMonomialTest method testGetPowersOfOrderedNonNumericConstantFactors.

@Test
public void testGetPowersOfOrderedNonNumericConstantFactors() {
    Monomial m = makeMonomial("2");
    Assert.assertEquals(0, m.getPowersOfNonNumericFactors().size());
    m = makeMonomial("2*x");
    Assert.assertEquals(1, m.getPowersOfNonNumericFactors().size());
    Assert.assertEquals(Arrays.asList(new Rational(1)), m.getPowersOfNonNumericFactors());
    m = makeMonomial("2*x^3*y^7*z^11");
    Assert.assertEquals(3, m.getPowersOfNonNumericFactors().size());
    Assert.assertEquals(Arrays.asList(new Rational(3), new Rational(7), new Rational(11)), m.getPowersOfNonNumericFactors());
    m = makeMonomial("2*z^3*y^7*x^11");
    Assert.assertEquals(3, m.getPowersOfNonNumericFactors().size());
    Assert.assertEquals(Arrays.asList(new Rational(11), new Rational(7), new Rational(3)), m.getPowersOfNonNumericFactors());
    m = makeMonomial("2*z^3*x^7*y^11");
    Assert.assertEquals(3, m.getPowersOfNonNumericFactors().size());
    Assert.assertEquals(Arrays.asList(new Rational(7), new Rational(11), new Rational(3)), m.getPowersOfNonNumericFactors());
    m = makeMonomial("2*z^3*x^7*x^11");
    Assert.assertEquals(2, m.getPowersOfNonNumericFactors().size());
    Assert.assertEquals(Arrays.asList(new Rational(18), new Rational(3)), m.getPowersOfNonNumericFactors());
}
Also used : Rational(com.sri.ai.util.math.Rational) DefaultMonomial(com.sri.ai.grinder.polynomial.core.DefaultMonomial) Monomial(com.sri.ai.grinder.polynomial.api.Monomial) Test(org.junit.Test)

Aggregations

Rational (com.sri.ai.util.math.Rational)47 Expression (com.sri.ai.expresso.api.Expression)22 Test (org.junit.Test)17 Monomial (com.sri.ai.grinder.polynomial.api.Monomial)12 ArrayList (java.util.ArrayList)10 Polynomial (com.sri.ai.grinder.polynomial.api.Polynomial)6 Type (com.sri.ai.expresso.api.Type)5 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)4 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)4 Context (com.sri.ai.grinder.sgdpllt.api.Context)4 List (java.util.List)4 Symbol (com.sri.ai.expresso.api.Symbol)3 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)3 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)3 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)3 RealInterval (com.sri.ai.expresso.type.RealInterval)3 DefaultFunctionApplication (com.sri.ai.expresso.core.DefaultFunctionApplication)2 FunctionType (com.sri.ai.expresso.type.FunctionType)2 TupleType (com.sri.ai.expresso.type.TupleType)2 DefaultMonomial (com.sri.ai.grinder.polynomial.core.DefaultMonomial)2