Search in sources :

Example 1 with Monomial

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

the class DefaultMonomialTest method testExponentiate.

@Test
public void testExponentiate() {
    Monomial m = makeMonomial("0");
    Assert.assertEquals(makeMonomial("0"), m.exponentiate(3));
    m = makeMonomial("2");
    Assert.assertEquals(makeMonomial("8"), m.exponentiate(3));
    m = makeMonomial("2*x");
    Assert.assertEquals(makeMonomial("8*x^3"), m.exponentiate(3));
    m = makeMonomial("2*x^2");
    Assert.assertEquals(makeMonomial("8*x^6"), m.exponentiate(3));
    m = makeMonomial("2*y^2*x^3");
    Assert.assertEquals(makeMonomial("8*x^9*y^6"), m.exponentiate(3));
    m = makeMonomial("2*y^2*x^3");
    Assert.assertEquals(makeMonomial("1"), m.exponentiate(0));
}
Also used : DefaultMonomial(com.sri.ai.grinder.polynomial.core.DefaultMonomial) Monomial(com.sri.ai.grinder.polynomial.api.Monomial) Test(org.junit.Test)

Example 2 with Monomial

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

the class DefaultMonomialTest method testTimes.

@Test
public void testTimes() {
    Monomial m1 = makeMonomial("2");
    Monomial m2 = makeMonomial("3");
    Assert.assertEquals(makeMonomial("6"), m1.times(m2));
    m1 = makeMonomial("x");
    m2 = makeMonomial("x");
    Assert.assertEquals(makeMonomial("x^2"), m1.times(m2));
    m1 = makeMonomial("2*x^2");
    m2 = makeMonomial("3*x^3");
    Assert.assertEquals(makeMonomial("6*x^5"), m1.times(m2));
    m1 = makeMonomial("3*x^2*y");
    m2 = makeMonomial("3*x^3*z^2");
    Assert.assertEquals(makeMonomial("9*x^5*y*z^2"), m1.times(m2));
    m1 = makeMonomial("0");
    m2 = makeMonomial("3*x^3*z^2");
    Assert.assertEquals(makeMonomial("0"), m1.times(m2));
    m1 = makeMonomial("3*x^2*y");
    m2 = makeMonomial("0");
    Assert.assertEquals(makeMonomial("0"), m1.times(m2));
}
Also used : DefaultMonomial(com.sri.ai.grinder.polynomial.core.DefaultMonomial) Monomial(com.sri.ai.grinder.polynomial.api.Monomial) Test(org.junit.Test)

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

Example 4 with Monomial

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

the class DefaultMonomialTest method testDegree.

@Test
public void testDegree() {
    Monomial m = makeMonomial("2");
    Assert.assertEquals(0, m.degree());
    m = makeMonomial("x");
    Assert.assertEquals(1, m.degree());
    m = makeMonomial("2*x");
    Assert.assertEquals(1, m.degree());
    m = makeMonomial("2*x^3");
    Assert.assertEquals(3, m.degree());
    m = makeMonomial("2*x^3*y");
    Assert.assertEquals(4, m.degree());
    m = makeMonomial("2*x^3*y*z^5");
    Assert.assertEquals(9, m.degree());
}
Also used : DefaultMonomial(com.sri.ai.grinder.polynomial.core.DefaultMonomial) Monomial(com.sri.ai.grinder.polynomial.api.Monomial) Test(org.junit.Test)

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

Aggregations

Monomial (com.sri.ai.grinder.polynomial.api.Monomial)31 DefaultMonomial (com.sri.ai.grinder.polynomial.core.DefaultMonomial)19 Test (org.junit.Test)18 Rational (com.sri.ai.util.math.Rational)13 ArrayList (java.util.ArrayList)12 Expression (com.sri.ai.expresso.api.Expression)10 Polynomial (com.sri.ai.grinder.polynomial.api.Polynomial)9 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 DefaultMonomial.isLegalExponent (com.sri.ai.grinder.polynomial.core.DefaultMonomial.isLegalExponent)1