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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations