Search in sources :

Example 26 with Monomial

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

the class DefaultMonomialTest method testGet.

@Test
public void testGet() {
    Monomial m = makeMonomial("x^2*y^3");
    Assert.assertEquals(Expressions.makeSymbol("*"), m.get(-1));
    Assert.assertEquals(Expressions.parse("x^2"), m.get(0));
    Assert.assertEquals(Expressions.parse("y^3"), m.get(1));
    m = makeMonomial("z^4*y^2*x^3*6");
    Assert.assertEquals(Expressions.makeSymbol("*"), m.get(-1));
    Assert.assertEquals(Expressions.parse("6"), m.get(0));
    Assert.assertEquals(Expressions.parse("x^3"), m.get(1));
    Assert.assertEquals(Expressions.parse("y^2"), m.get(2));
    Assert.assertEquals(Expressions.parse("z^4"), m.get(3));
}
Also used : Monomial(com.sri.ai.grinder.polynomial.api.Monomial) DefaultMonomial(com.sri.ai.grinder.polynomial.core.DefaultMonomial) Test(org.junit.Test)

Example 27 with Monomial

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

the class DefaultMonomialTest method testOrderedUnionOfNonNumericConstantFactors.

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

Example 28 with Monomial

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

the class DefaultMonomial method times.

@Override
public Monomial times(Monomial multiplier) {
    Monomial result;
    // Optimization: return 0 if either numeric factor is 0
    if (isZero() || multiplier.isZero()) {
        result = ZERO;
    } else if (isOne()) {
        // Optimization, neutral element
        result = multiplier;
    } else if (multiplier.isOne()) {
        // Optimization, neutral element
        result = this;
    } else {
        List<Expression> combinedNonNumericFactors = Monomial.orderedUnionOfNonNumericFactors(this, multiplier);
        List<Rational> thisSignature = this.getSignature(combinedNonNumericFactors);
        List<Rational> multiplierSignature = multiplier.getSignature(combinedNonNumericFactors);
        Rational resultNumericFactor = getNumericFactor().multiply(multiplier.getNumericFactor());
        List<Rational> resultPowers = zipWith((power1, power2) -> power1.add(power2), thisSignature, multiplierSignature);
        result = make(resultNumericFactor, combinedNonNumericFactors, resultPowers);
    }
    return result;
}
Also used : Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression) Monomial(com.sri.ai.grinder.polynomial.api.Monomial)

Example 29 with Monomial

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

the class DefaultPolynomial method minus.

@Override
public Polynomial minus(Polynomial subtrahend) throws IllegalArgumentException {
    assertSameVariables(subtrahend);
    Polynomial result;
    if (subtrahend.isZero()) {
        result = this;
    } else {
        List<Monomial> negatedSubtrahendSummands = new ArrayList<>();
        subtrahend.getMonomials().forEach(summand -> negatedSubtrahendSummands.add(summand.times(DefaultMonomial.MINUS_ONE)));
        Polynomial negatedSubtrahend = new DefaultPolynomial(negatedSubtrahendSummands, getVariables());
        result = add(negatedSubtrahend);
    }
    return result;
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) Monomial(com.sri.ai.grinder.polynomial.api.Monomial) ArrayList(java.util.ArrayList)

Example 30 with Monomial

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

the class DefaultPolynomial method add.

@Override
public Polynomial add(Polynomial summand) throws IllegalArgumentException {
    assertSameVariables(summand);
    Polynomial result;
    if (isZero()) {
        result = summand;
    } else if (summand.isZero()) {
        result = this;
    } else {
        List<Monomial> summands = new ArrayList<>();
        Set<List<Rational>> combinedSignatures = new LinkedHashSet<>(this.getMonomials().size() + summand.getMonomials().size());
        combinedSignatures.addAll(this.getMapFromSignatureToMonomial().keySet());
        combinedSignatures.addAll(summand.getMapFromSignatureToMonomial().keySet());
        for (List<Rational> signature : combinedSignatures) {
            // NOTE: at least one of these assignments is guaranteed to be non-null.
            Monomial m1 = this.getMapFromSignatureToMonomial().get(signature);
            Monomial m2 = summand.getMapFromSignatureToMonomial().get(signature);
            if (m1 == null) {
                if (!m2.isZero()) {
                    summands.add(m2);
                }
            } else if (m2 == null) {
                if (!m1.isZero()) {
                    summands.add(m1);
                }
            } else {
                Monomial sum = addMonomialsWithSameSignature(m1, m2);
                if (!sum.isZero()) {
                    summands.add(sum);
                }
            }
        }
        // In case all the summands cancel each other out
        if (summands.isEmpty()) {
            result = makeFromMonomial(Expressions.ZERO, variables);
        } else {
            result = new DefaultPolynomial(summands, getVariables());
        }
    }
    return result;
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Rational(com.sri.ai.util.math.Rational) Monomial(com.sri.ai.grinder.polynomial.api.Monomial) ArrayList(java.util.ArrayList) List(java.util.List)

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