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());
}
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());
}
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")));
}
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());
}
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;
}
Aggregations