use of com.sri.ai.grinder.polynomial.api.Monomial in project aic-expresso by aic-sri-international.
the class DefaultMonomialTest method testGetFunctor.
//
// Expression API related tests
@Test
public void testGetFunctor() {
Monomial m = makeMonomial("0");
Assert.assertNull(m.getFunctor());
m = makeMonomial("z^4*y^2*x^3");
Assert.assertEquals(Expressions.makeSymbol("*"), m.getFunctor());
}
use of com.sri.ai.grinder.polynomial.api.Monomial in project aic-expresso by aic-sri-international.
the class DefaultMonomialTest method testSet.
@Test
public void testSet() {
Monomial m = makeMonomial("x^2*y^3");
Assert.assertEquals(makeMonomial("2*y^3"), m.set(0, Expressions.parse("2")));
Assert.assertEquals(makeMonomial("x^5"), m.set(1, Expressions.parse("x^3")));
Assert.assertEquals(makeMonomial("x^2*z^4"), m.set(1, Expressions.parse("z^4")));
m = makeMonomial("x^2*y^3");
// Note: x^2 is set to a factor (i.e. negative exponent)
Assert.assertEquals(makeMonomial("(x^(-2))^1*y^3"), m.set(0, Expressions.parse("x^(-2)")));
m = makeMonomial("x^2*y^3");
// Note: by changing the functor the old monomial is transformed into a factor
Assert.assertEquals(makeMonomial("1*(x^2+y^3)^1"), m.set(-1, Expressions.makeSymbol("+")));
m = makeMonomial("x^2*y^3");
// Note: the x^2 becomes a factor (2+3)^1
Assert.assertEquals(makeMonomial("1*(2+3)^1*y^3"), m.set(0, Expressions.parse("2 + 3")));
m = makeMonomial("x^2*y^3");
// Note: the x^2 becomes a factor (2-3)^1
Assert.assertEquals(makeMonomial("1*(2-3)^1*y^3"), m.set(0, Expressions.parse("2 - 3")));
m = makeMonomial("x^2*y^3");
// Note: the x^2 becomes a factor (2/3)^1
Assert.assertEquals(makeMonomial("1*(2/3)^1*y^3"), m.set(0, Expressions.parse("2 / 3")));
}
use of com.sri.ai.grinder.polynomial.api.Monomial 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.grinder.polynomial.api.Monomial in project aic-expresso by aic-sri-international.
the class DefaultMonomialTest method testGetSignature.
@Test
public void testGetSignature() {
Monomial m = makeMonomial("2*x");
Assert.assertEquals(Arrays.asList(new Rational(1)), m.getSignature(Expressions.parse("tuple(x)").getArguments()));
//
// Examples form Javadoc
// signature(3 * y * x^2, (x, y, z)) = (2, 1, 0)
m = makeMonomial("3 * y * x^2");
Assert.assertEquals(Arrays.asList(new Rational(2), new Rational(1), new Rational(0)), m.getSignature(Expressions.parse("tuple(x, y, z)").getArguments()));
Assert.assertEquals(Arrays.asList(new Rational(2), new Rational(1)), m.getSignature());
// signature(3, (x, y, z)) = (0, 0, 0)
m = makeMonomial("3");
Assert.assertEquals(Arrays.asList(new Rational(0), new Rational(0), new Rational(0)), m.getSignature(Expressions.parse("tuple(x, y, z)").getArguments()));
Assert.assertEquals(Collections.emptyList(), m.getSignature());
// signature(3 * (x + 2), (x + 2, x, y, z)) = (1, 0, 0, 0)
m = makeMonomial("3 * (x + 2)");
Assert.assertEquals(Arrays.asList(new Rational(1), new Rational(0), new Rational(0), new Rational(0)), m.getSignature(Expressions.parse("tuple(x + 2, x, y, z)").getArguments()));
Assert.assertEquals(Arrays.asList(new Rational(1)), m.getSignature());
// signature(3 * (x + 2), (3, x + 2, x, y, z)) = (1, 1, 0, 0, 0)
m = makeMonomial("3 * (x + 2)");
Assert.assertEquals(Arrays.asList(new Rational(1), new Rational(1), new Rational(0), new Rational(0), new Rational(0)), m.getSignature(Expressions.parse("tuple(3, x + 2, x, y, z)").getArguments()));
Assert.assertEquals(Arrays.asList(new Rational(1)), m.getSignature());
}
use of com.sri.ai.grinder.polynomial.api.Monomial in project aic-expresso by aic-sri-international.
the class DefaultMonomialTest method testGetFactors.
@Test
public void testGetFactors() {
// A numeric constant, 1 factor.
Monomial m = makeMonomial("2");
Assert.assertEquals(1, m.getFactors().size());
// If a zero numeric constant factor, the monomial is automatically collapsed to 0
m = makeMonomial("0*x^2");
Assert.assertEquals(1, m.getFactors().size());
// A single non numeric constant factor, the default numeric constant 1 is present as a factor
m = makeMonomial("x");
Assert.assertEquals(2, m.getFactors().size());
Assert.assertEquals(new HashSet<>(Expressions.parse("(1, x)").getArguments()), m.getFactors());
// A numeric constant factor and a single non numeric factor
m = makeMonomial("2*x");
Assert.assertEquals(2, m.getFactors().size());
Assert.assertEquals(new HashSet<>(Expressions.parse("(2, x)").getArguments()), m.getFactors());
// Two non-numeric constant factors
m = makeMonomial("y^2*x^3");
Assert.assertEquals(3, m.getFactors().size());
Assert.assertEquals(new HashSet<>(Expressions.parse("(1, x, y)").getArguments()), m.getFactors());
// A numeric constant factor and two non-numeric constant factors
m = makeMonomial("4*y^2*x^3");
Assert.assertEquals(3, m.getFactors().size());
Assert.assertEquals(new HashSet<>(Expressions.parse("(4, x, y)").getArguments()), m.getFactors());
// Ensure duplicates are correctly collapsed
m = makeMonomial("4*y^2*y^3");
Assert.assertEquals(2, m.getFactors().size());
Assert.assertEquals(new HashSet<>(Expressions.parse("(4, y)").getArguments()), m.getFactors());
//
// Factors examples
m = makeMonomial("10 * |Dogs|^2 * |People|^3 * f(y) * x^2");
Assert.assertEquals(5, m.getFactors().size());
Assert.assertEquals(new HashSet<>(Expressions.parse("(10, |Dogs|, |People|, f(y), x)").getArguments()), m.getFactors());
//
// treated as a factors as a non-integer exponent
m = makeMonomial("10 * 2^a");
Assert.assertEquals(2, m.getFactors().size());
Assert.assertEquals(new HashSet<>(Expressions.parse("(10, 2^a)").getArguments()), m.getFactors());
// treated as a factors as a non-integer exponent
m = makeMonomial("10 * 2^1.1");
Assert.assertEquals(2, m.getFactors().size());
Assert.assertEquals(new HashSet<>(Expressions.parse("(10, 2^1.1)").getArguments()), m.getFactors());
}
Aggregations