Search in sources :

Example 16 with Monomial

use of com.sri.ai.grinder.polynomial.api.Monomial 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 17 with Monomial

use of com.sri.ai.grinder.polynomial.api.Monomial in project aic-expresso by aic-sri-international.

the class DefaultMonomialTest method testDivideIllegalArgumentException.

@Test(expected = IllegalArgumentException.class)
public void testDivideIllegalArgumentException() {
    Monomial m1 = makeMonomial("2*x^3");
    Monomial m2 = makeMonomial("0*x");
    m1.divide(m2);
}
Also used : Monomial(com.sri.ai.grinder.polynomial.api.Monomial) DefaultMonomial(com.sri.ai.grinder.polynomial.core.DefaultMonomial) Test(org.junit.Test)

Example 18 with Monomial

use of com.sri.ai.grinder.polynomial.api.Monomial in project aic-expresso by aic-sri-international.

the class DefaultMonomialTest method testGetOrderedNonNumericConstantFactors.

@Test
public void testGetOrderedNonNumericConstantFactors() {
    Monomial m = makeMonomial("2");
    Assert.assertEquals(0, m.getOrderedNonNumericFactors().size());
    m = makeMonomial("2*x");
    Assert.assertEquals(1, m.getOrderedNonNumericFactors().size());
    Assert.assertEquals(Expressions.parse("tuple(x)").getArguments(), m.getOrderedNonNumericFactors());
    m = makeMonomial("2*x^3*y^7*z^11");
    Assert.assertEquals(3, m.getOrderedNonNumericFactors().size());
    Assert.assertEquals(Expressions.parse("(x, y, z)").getArguments(), m.getOrderedNonNumericFactors());
    m = makeMonomial("2*z^3*y^7*x^11");
    Assert.assertEquals(3, m.getOrderedNonNumericFactors().size());
    Assert.assertEquals(Expressions.parse("(x, y, z)").getArguments(), m.getOrderedNonNumericFactors());
    m = makeMonomial("2*z^3*x^7*y^11");
    Assert.assertEquals(3, m.getOrderedNonNumericFactors().size());
    Assert.assertEquals(Expressions.parse("(x, y, z)").getArguments(), m.getOrderedNonNumericFactors());
    m = makeMonomial("2*z^3*x^7*x^11");
    Assert.assertEquals(2, m.getOrderedNonNumericFactors().size());
    Assert.assertEquals(Expressions.parse("(x, z)").getArguments(), m.getOrderedNonNumericFactors());
    // Note: z is dropped as we know is = 1
    m = makeMonomial("2*x^3*y^7*z^0");
    Assert.assertEquals(2, m.getOrderedNonNumericFactors().size());
    Assert.assertEquals(Expressions.parse("(x, y)").getArguments(), m.getOrderedNonNumericFactors());
}
Also used : Monomial(com.sri.ai.grinder.polynomial.api.Monomial) DefaultMonomial(com.sri.ai.grinder.polynomial.core.DefaultMonomial) Test(org.junit.Test)

Example 19 with Monomial

use of com.sri.ai.grinder.polynomial.api.Monomial in project aic-expresso by aic-sri-international.

the class DefaultMonomialTest method testGetCoefficient.

@Test
public void testGetCoefficient() {
    Monomial m = makeMonomial("0");
    Assert.assertEquals(makeMonomial("0"), m.getCoefficient(Collections.emptyList()));
    Assert.assertEquals(makeMonomial("0"), m.getCoefficient(Expressions.parse("tuple(x)").getArguments()));
    m = makeMonomial("1");
    Assert.assertEquals(makeMonomial("1"), m.getCoefficient(Collections.emptyList()));
    Assert.assertEquals(makeMonomial("1"), m.getCoefficient(Expressions.parse("tuple(x)").getArguments()));
    m = makeMonomial("x");
    Assert.assertEquals(makeMonomial("x"), m.getCoefficient(Collections.emptyList()));
    Assert.assertEquals(makeMonomial("1"), m.getCoefficient(Expressions.parse("tuple(x)").getArguments()));
    m = makeMonomial("3*x^2");
    Assert.assertEquals(makeMonomial("3*x^2"), m.getCoefficient(Collections.emptyList()));
    Assert.assertEquals(makeMonomial("3*x^2"), m.getCoefficient(Expressions.parse("tuple(1)").getArguments()));
    Assert.assertEquals(makeMonomial("3*x^2"), m.getCoefficient(Expressions.parse("tuple(z)").getArguments()));
    Assert.assertEquals(makeMonomial("x^2"), m.getCoefficient(Expressions.parse("tuple(3)").getArguments()));
    Assert.assertEquals(makeMonomial("3"), m.getCoefficient(Expressions.parse("tuple(x)").getArguments()));
    Assert.assertEquals(makeMonomial("1"), m.getCoefficient(Expressions.parse("(3, x)").getArguments()));
    m = makeMonomial("3*x^2*y^4");
    Assert.assertEquals(makeMonomial("3*x^2*y^4"), m.getCoefficient(Collections.emptyList()));
    Assert.assertEquals(makeMonomial("3*x^2*y^4"), m.getCoefficient(Expressions.parse("tuple(1)").getArguments()));
    Assert.assertEquals(makeMonomial("3*y^4"), m.getCoefficient(Expressions.parse("tuple(x)").getArguments()));
}
Also used : Monomial(com.sri.ai.grinder.polynomial.api.Monomial) DefaultMonomial(com.sri.ai.grinder.polynomial.core.DefaultMonomial) Test(org.junit.Test)

Example 20 with Monomial

use of com.sri.ai.grinder.polynomial.api.Monomial in project aic-expresso by aic-sri-international.

the class DefaultMonomialTest method testAreLikeTermsUnionOfFactors.

@Test
public void testAreLikeTermsUnionOfFactors() {
    Monomial m1 = makeMonomial("2");
    Monomial m2 = makeMonomial("2");
    Assert.assertEquals(true, m1.areLikeTerms(m2, Monomial.orderedUnionOfNonNumericFactors(m1, m2)));
    Assert.assertEquals(true, m1.areLikeTerms(m2));
    m1 = makeMonomial("2");
    m2 = makeMonomial("3");
    Assert.assertEquals(true, m1.areLikeTerms(m2, Monomial.orderedUnionOfNonNumericFactors(m1, m2)));
    Assert.assertEquals(true, m1.areLikeTerms(m2));
    m1 = makeMonomial("x");
    m2 = makeMonomial("x");
    Assert.assertEquals(true, m1.areLikeTerms(m2, Monomial.orderedUnionOfNonNumericFactors(m1, m2)));
    Assert.assertEquals(true, m1.areLikeTerms(m2));
    m1 = makeMonomial("x");
    m2 = makeMonomial("y");
    Assert.assertEquals(false, m1.areLikeTerms(m2, Monomial.orderedUnionOfNonNumericFactors(m1, m2)));
    Assert.assertEquals(false, m1.areLikeTerms(m2));
    m1 = makeMonomial("2");
    m2 = makeMonomial("2*x");
    Assert.assertEquals(false, m1.areLikeTerms(m2, Monomial.orderedUnionOfNonNumericFactors(m1, m2)));
    Assert.assertEquals(false, m1.areLikeTerms(m2));
    m1 = makeMonomial("2*x");
    m2 = makeMonomial("2*x");
    Assert.assertEquals(true, m1.areLikeTerms(m2, Monomial.orderedUnionOfNonNumericFactors(m1, m2)));
    Assert.assertEquals(true, m1.areLikeTerms(m2));
    m1 = makeMonomial("2*x^1");
    m2 = makeMonomial("2*x");
    Assert.assertEquals(true, m1.areLikeTerms(m2, Monomial.orderedUnionOfNonNumericFactors(m1, m2)));
    Assert.assertEquals(true, m1.areLikeTerms(m2));
    m1 = makeMonomial("2*x");
    m2 = makeMonomial("2*x^2");
    Assert.assertEquals(false, m1.areLikeTerms(m2, Monomial.orderedUnionOfNonNumericFactors(m1, m2)));
    Assert.assertEquals(false, m1.areLikeTerms(m2));
    m1 = makeMonomial("2*y^1*x");
    m2 = makeMonomial("2*x^1*y");
    Assert.assertEquals(true, m1.areLikeTerms(m2, Monomial.orderedUnionOfNonNumericFactors(m1, m2)));
    Assert.assertEquals(true, m1.areLikeTerms(m2));
    m1 = makeMonomial("2*y^3*x^2");
    m2 = makeMonomial("2*x^2*y^7");
    Assert.assertEquals(false, m1.areLikeTerms(m2, Monomial.orderedUnionOfNonNumericFactors(m1, m2)));
    Assert.assertEquals(false, m1.areLikeTerms(m2));
    m1 = makeMonomial("2*y^1*x");
    m2 = makeMonomial("2*x^1*y*z^4");
    Assert.assertEquals(false, m1.areLikeTerms(m2, Monomial.orderedUnionOfNonNumericFactors(m1, m2)));
    Assert.assertEquals(false, m1.areLikeTerms(m2));
}
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