Search in sources :

Example 41 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) Monomial(com.sri.ai.grinder.polynomial.api.Monomial) DefaultMonomial(com.sri.ai.grinder.polynomial.core.DefaultMonomial) Test(org.junit.Test)

Example 42 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").getNumericFactor());
    Assert.assertEquals(new Rational(0), makeMonomial("0*x^2").getNumericFactor());
    Assert.assertEquals(new Rational(1), makeMonomial("1").getNumericFactor());
    Assert.assertEquals(new Rational(-1), makeMonomial("-1").getNumericFactor());
    Assert.assertEquals(new Rational(2), makeMonomial("2").getNumericFactor());
    // Ensure the numeric constant is set to 1 if not explicitly represented in the expression
    Assert.assertEquals(new Rational(1), makeMonomial("x^2").getNumericFactor());
    Assert.assertEquals(new Rational(4), makeMonomial("4*x^2").getNumericFactor());
    Assert.assertEquals(new Rational(4), makeMonomial("y*4*x^2").getNumericFactor());
    // Test that numerical constants are multiplied together
    Assert.assertEquals(new Rational(8), makeMonomial("y^3*2*x^2*4").getNumericFactor());
    // Test edge case where the numeric constant is represented as a power
    Assert.assertEquals(new Rational(8), makeMonomial("2^3*x^2").getNumericFactor());
    // 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").getNumericFactor());
}
Also used : Rational(com.sri.ai.util.math.Rational) Test(org.junit.Test)

Example 43 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) Monomial(com.sri.ai.grinder.polynomial.api.Monomial) DefaultMonomial(com.sri.ai.grinder.polynomial.core.DefaultMonomial) Test(org.junit.Test)

Example 44 with Rational

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

the class DefaultPolynomialTest method testGetMapFromSignatureToMonomial.

@Test
public void testGetMapFromSignatureToMonomial() {
    Polynomial p = makePolynomial("3*x^2*y^4", "tuple()");
    Map<List<Rational>, Expression> expectedMapFromSignatureToMonomial = new HashMap<>();
    expectedMapFromSignatureToMonomial.put(Collections.emptyList(), makeMonomial("3*x^2*y^4"));
    assertEquals(expectedMapFromSignatureToMonomial, p.getMapFromSignatureToMonomial());
    p = makePolynomial("3*x^2*y^4", "tuple(x)");
    expectedMapFromSignatureToMonomial = new HashMap<>();
    expectedMapFromSignatureToMonomial.put(Arrays.asList(new Rational(2)), parse("x^2*3*y^4"));
    assertEquals(expectedMapFromSignatureToMonomial, p.getMapFromSignatureToMonomial());
    p = makePolynomial("3*x^2*y^4", "tuple(y)");
    expectedMapFromSignatureToMonomial = new HashMap<>();
    expectedMapFromSignatureToMonomial.put(Arrays.asList(new Rational(4)), parse("y^4*3*x^2"));
    assertEquals(expectedMapFromSignatureToMonomial, p.getMapFromSignatureToMonomial());
    p = makePolynomial("3*x^2*y^4", "tuple(x, y)");
    expectedMapFromSignatureToMonomial = new HashMap<>();
    expectedMapFromSignatureToMonomial.put(Arrays.asList(new Rational(2), new Rational(4)), parse("3*x^2*y^4"));
    assertEquals(expectedMapFromSignatureToMonomial, p.getMapFromSignatureToMonomial());
    p = makePolynomial("3*x^2*y^4", "tuple(y, x)");
    expectedMapFromSignatureToMonomial = new HashMap<>();
    expectedMapFromSignatureToMonomial.put(Arrays.asList(new Rational(4), new Rational(2)), parse("3*x^2*y^4"));
    assertEquals(expectedMapFromSignatureToMonomial, p.getMapFromSignatureToMonomial());
    p = makePolynomial("3*x^2*y^4", "tuple(x, y, z)");
    expectedMapFromSignatureToMonomial = new HashMap<>();
    expectedMapFromSignatureToMonomial.put(Arrays.asList(new Rational(2), new Rational(4), new Rational(0)), parse("3*x^2*y^4"));
    assertEquals(expectedMapFromSignatureToMonomial, p.getMapFromSignatureToMonomial());
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression) HashMap(java.util.HashMap) List(java.util.List) Test(org.junit.Test)

Example 45 with Rational

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

the class InversionPerformanceEvaluationTest method computeSize.

private Rational computeSize(Expression functionOnIntensionalSet, Rational resultSoFar, Context context) {
    IntensionalSet intensionalSet = (IntensionalSet) functionOnIntensionalSet.get(0);
    IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
    List<Expression> indices = IndexExpressions.getIndices(indexExpressionsSet);
    if (indices.size() != 1) {
        throw new UnsupportedOperationException("Currently only support singular indices");
    }
    Expression index = indices.get(0);
    Context intensionalSetContext = context.extendWith(indexExpressionsSet);
    Type type = GrinderUtil.getTypeOfExpression(index, intensionalSetContext);
    Rational result = resultSoFar.multiply(type.cardinality().rationalValue());
    Expression head = intensionalSet.getHead();
    if (Expressions.isFunctionApplicationWithArguments(head) && Sets.isIntensionalSet(head.get(0))) {
        result = computeSize(head, result, intensionalSetContext);
    }
    return result;
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Type(com.sri.ai.expresso.api.Type) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Aggregations

Rational (com.sri.ai.util.math.Rational)72 Expression (com.sri.ai.expresso.api.Expression)33 Test (org.junit.Test)26 Monomial (com.sri.ai.grinder.polynomial.api.Monomial)11 ArrayList (java.util.ArrayList)10 Type (com.sri.ai.expresso.api.Type)9 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)8 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)7 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)6 RealInterval (com.sri.ai.expresso.type.RealInterval)6 List (java.util.List)6 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)5 Symbol (com.sri.ai.expresso.api.Symbol)4 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)4 Categorical (com.sri.ai.expresso.type.Categorical)4 FunctionType (com.sri.ai.expresso.type.FunctionType)4 TupleType (com.sri.ai.expresso.type.TupleType)4 Polynomial (com.sri.ai.grinder.polynomial.api.Polynomial)4 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)3 HashMap (java.util.HashMap)3