Search in sources :

Example 21 with Monomial

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());
}
Also used : Monomial(com.sri.ai.grinder.polynomial.api.Monomial) DefaultMonomial(com.sri.ai.grinder.polynomial.core.DefaultMonomial) Test(org.junit.Test)

Example 22 with Monomial

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")));
}
Also used : Monomial(com.sri.ai.grinder.polynomial.api.Monomial) DefaultMonomial(com.sri.ai.grinder.polynomial.core.DefaultMonomial) Test(org.junit.Test)

Example 23 with Monomial

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")));
}
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 24 with Monomial

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());
}
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 25 with Monomial

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());
}
Also used : Monomial(com.sri.ai.grinder.polynomial.api.Monomial) DefaultMonomial(com.sri.ai.grinder.polynomial.core.DefaultMonomial) Test(org.junit.Test)

Aggregations

Monomial (com.sri.ai.grinder.polynomial.api.Monomial)32 DefaultMonomial (com.sri.ai.grinder.polynomial.core.DefaultMonomial)19 Test (org.junit.Test)18 Rational (com.sri.ai.util.math.Rational)12 ArrayList (java.util.ArrayList)11 Expression (com.sri.ai.expresso.api.Expression)10 Polynomial (com.sri.ai.grinder.polynomial.api.Polynomial)8 Expressions.containsAnyOfGivenCollectionAsSubExpression (com.sri.ai.expresso.helper.Expressions.containsAnyOfGivenCollectionAsSubExpression)4 LinkedHashMap (java.util.LinkedHashMap)4 List (java.util.List)4 DefaultFunctionApplication (com.sri.ai.expresso.core.DefaultFunctionApplication)3 Pair (com.sri.ai.util.base.Pair)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 Set (java.util.Set)2 Beta (com.google.common.annotations.Beta)1 AbstractExpressionWrapper (com.sri.ai.expresso.helper.AbstractExpressionWrapper)1 ExpressionComparator (com.sri.ai.expresso.helper.ExpressionComparator)1 Expressions (com.sri.ai.expresso.helper.Expressions)1