Search in sources :

Example 86 with SortedMap

use of java.util.SortedMap in project symja_android_library by axkr.

the class ExprPolynomial method subtractMultiple.

/**
	 * GenPolynomial subtract a multiple.
	 * 
	 * @param a
	 *            coefficient.
	 * @param S
	 *            GenPolynomial.
	 * @return this - a S.
	 */
public ExprPolynomial subtractMultiple(IExpr a, ExprPolynomial S) {
    if (a == null || a.isZero()) {
        return this;
    }
    if (S == null || S.isZero()) {
        return this;
    }
    if (this.isZero()) {
        return S.multiply(a.negate());
    }
    assert (ring.nvar == S.ring.nvar);
    ExprPolynomial n = this.copy();
    SortedMap<ExpVectorLong, IExpr> nv = n.val;
    SortedMap<ExpVectorLong, IExpr> sv = S.val;
    for (Map.Entry<ExpVectorLong, IExpr> me : sv.entrySet()) {
        ExpVectorLong f = me.getKey();
        // assert y != null
        IExpr y = me.getValue();
        y = a.multiply(y);
        IExpr x = nv.get(f);
        if (x != null) {
            x = x.subtract(y);
            if (!x.isZero()) {
                nv.put(f, x);
            } else {
                nv.remove(f);
            }
        } else if (!y.isZero()) {
            nv.put(f, y.negate());
        }
    }
    return n;
}
Also used : JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 87 with SortedMap

use of java.util.SortedMap in project symja_android_library by axkr.

the class ExprPolynomial method subtract.

/**
	 * GenPolynomial subtraction.
	 * 
	 * @param S
	 *            GenPolynomial.
	 * @return this-S.
	 */
public ExprPolynomial subtract(ExprPolynomial S) {
    if (S == null) {
        return this;
    }
    if (S.isZero()) {
        return this;
    }
    if (this.isZero()) {
        return S.negate();
    }
    assert (ring.nvar == S.ring.nvar);
    // new GenPolynomial(ring, val);
    ExprPolynomial n = this.copy();
    SortedMap<ExpVectorLong, IExpr> nv = n.val;
    SortedMap<ExpVectorLong, IExpr> sv = S.val;
    for (Map.Entry<ExpVectorLong, IExpr> me : sv.entrySet()) {
        ExpVectorLong e = me.getKey();
        // sv.get(e); // assert y != null
        IExpr y = me.getValue();
        IExpr x = nv.get(e);
        if (x != null) {
            x = x.subtract(y);
            if (!x.isZero()) {
                nv.put(e, x);
            } else {
                nv.remove(e);
            }
        } else {
            nv.put(e, y.negate());
        }
    }
    return n;
}
Also used : JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 88 with SortedMap

use of java.util.SortedMap in project symja_android_library by axkr.

the class ExprPolynomial method sum.

/**
	 * GenPolynomial summation.
	 * 
	 * @param S
	 *            GenPolynomial.
	 * @return this+S.
	 */
// public <T extends GenPolynomial> T sum(T /*GenPolynomial*/ S) {
public ExprPolynomial sum(ExprPolynomial S) {
    if (S == null) {
        return this;
    }
    if (S.isZero()) {
        return this;
    }
    if (this.isZero()) {
        return S;
    }
    assert (ring.nvar == S.ring.nvar);
    // new GenPolynomial(ring, val);
    ExprPolynomial n = this.copy();
    SortedMap<ExpVectorLong, IExpr> nv = n.val;
    SortedMap<ExpVectorLong, IExpr> sv = S.val;
    for (Map.Entry<ExpVectorLong, IExpr> me : sv.entrySet()) {
        ExpVectorLong e = me.getKey();
        // sv.get(e); // assert y != null
        IExpr y = me.getValue();
        IExpr x = nv.get(e);
        if (x != null) {
            x = x.add(y);
            if (!x.isZero()) {
                nv.put(e, x);
            } else {
                nv.remove(e);
            }
        } else {
            nv.put(e, y);
        }
    }
    return n;
}
Also used : JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 89 with SortedMap

use of java.util.SortedMap in project symja_android_library by axkr.

the class ExprPolynomial method divide.

/**
	 * GenPolynomial division. Division by coefficient ring element. Fails, if
	 * exact division is not possible.
	 * 
	 * @param s
	 *            coefficient.
	 * @return this/s.
	 */
public ExprPolynomial divide(IExpr s) {
    if (s == null || s.isZero()) {
        throw new ArithmeticException("division by zero");
    }
    if (this.isZero()) {
        return this;
    }
    // C t = s.inverse();
    // return multiply(t);
    ExprPolynomial p = ring.getZero().copy();
    SortedMap<ExpVectorLong, IExpr> pv = p.val;
    for (Map.Entry<ExpVectorLong, IExpr> m : val.entrySet()) {
        ExpVectorLong e = m.getKey();
        IExpr c1 = m.getValue();
        IExpr c = c1.divide(s);
        if (debug) {
            IExpr x = c1.remainder(s);
            if (!x.isZero()) {
                logger.info("divide x = " + x);
                throw new ArithmeticException("no exact division: " + c1 + "/" + s);
            }
        }
        if (c.isZero()) {
            throw new ArithmeticException("no exact division: " + c1 + "/" + s + ", in " + this);
        }
        // or m1.setValue( c )
        pv.put(e, c);
    }
    return p;
}
Also used : JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 90 with SortedMap

use of java.util.SortedMap in project symja_android_library by axkr.

the class ExprPolynomial method compareTo.

/**
	 * GenPolynomial comparison.
	 * 
	 * @param b
	 *            GenPolynomial.
	 * @return sign(this-b).
	 */
public int compareTo(ExprPolynomial b) {
    if (b == null) {
        return 1;
    }
    SortedMap<ExpVectorLong, IExpr> av = this.val;
    SortedMap<ExpVectorLong, IExpr> bv = b.val;
    Iterator<Map.Entry<ExpVectorLong, IExpr>> ai = av.entrySet().iterator();
    Iterator<Map.Entry<ExpVectorLong, IExpr>> bi = bv.entrySet().iterator();
    int s = 0;
    int c = 0;
    while (ai.hasNext() && bi.hasNext()) {
        Map.Entry<ExpVectorLong, IExpr> aie = ai.next();
        Map.Entry<ExpVectorLong, IExpr> bie = bi.next();
        ExpVectorLong ae = aie.getKey();
        ExpVectorLong be = bie.getKey();
        s = ae.compareTo(be);
        if (s != 0) {
            // " + ring.toScript(be));
            return s;
        }
        if (c == 0) {
            // av.get(ae);
            IExpr ac = aie.getValue();
            // bv.get(be);
            IExpr bc = bie.getValue();
            c = ac.compareTo(bc);
        }
    }
    if (ai.hasNext()) {
        // System.out.println("ai = " + ai);
        return 1;
    }
    if (bi.hasNext()) {
        // System.out.println("bi = " + bi);
        return -1;
    }
    // now all keys are equal
    return c;
}
Also used : JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap) PrettyPrint(edu.jas.kern.PrettyPrint)

Aggregations

SortedMap (java.util.SortedMap)216 Map (java.util.Map)162 TreeMap (java.util.TreeMap)108 HashMap (java.util.HashMap)59 Iterator (java.util.Iterator)31 NavigableMap (java.util.NavigableMap)31 ArrayList (java.util.ArrayList)27 Test (org.junit.Test)25 IOException (java.io.IOException)20 ImmutableMap (com.google.common.collect.ImmutableMap)19 JASIExpr (org.matheclipse.core.convert.JASIExpr)16 IExpr (org.matheclipse.core.interfaces.IExpr)16 List (java.util.List)15 File (java.io.File)14 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)14 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)12 Entry (java.util.Map.Entry)12 ConcurrentNavigableMap (java.util.concurrent.ConcurrentNavigableMap)12 ConcurrentMap (java.util.concurrent.ConcurrentMap)11 LinkedHashMap (java.util.LinkedHashMap)9