/*
* $Id$
*/
package edu.jas.application;
import org.apache.log4j.Logger;
import edu.jas.kern.PrettyPrint;
import edu.jas.poly.ExpVector;
import edu.jas.poly.GenPolynomial;
import edu.jas.structure.GcdRingElem;
import edu.jas.structure.RingElem;
import edu.jas.structure.QuotPair;
/**
* Local ring element based on GenPolynomial with RingElem interface. Objects of
* this class are (nearly) immutable.
* @author Heinz Kredel
*/
// To be fixed?: Not jet working because of monic GBs.
public class Local>
implements RingElem>, QuotPair> {
private static final Logger logger = Logger.getLogger(Local.class);
private final boolean debug = logger.isDebugEnabled();
/**
* Local class factory data structure.
*/
public final LocalRing ring;
/**
* Numerator part of the element data structure.
*/
protected final GenPolynomial num;
/**
* Denominator part of the element data structure.
*/
protected final GenPolynomial den;
/**
* Flag to remember if this local element is a unit. -1 is unknown, 1 is
* unit, 0 not a unit.
*/
protected int isunit = -1; // initially unknown
/**
* The constructor creates a Local object from a ring factory.
* @param r ring factory.
*/
public Local(LocalRing r) {
this(r, r.ring.getZERO());
}
/**
* The constructor creates a Local object from a ring factory and a
* numerator polynomial. The denominator is assumed to be 1.
* @param r ring factory.
* @param n numerator polynomial.
*/
public Local(LocalRing r, GenPolynomial n) {
this(r, n, r.ring.getONE(), true);
}
/**
* The constructor creates a Local object from a ring factory and a
* numerator and denominator polynomial.
* @param r ring factory.
* @param n numerator polynomial.
* @param d denominator polynomial.
*/
public Local(LocalRing r, GenPolynomial n, GenPolynomial d) {
this(r, n, d, false);
}
/**
* The constructor creates a Local object from a ring factory and a
* numerator and denominator polynomial.
* @param r ring factory.
* @param n numerator polynomial.
* @param d denominator polynomial.
* @param isred true if gcd(n,d) == 1, else false.
*/
protected Local(LocalRing r, GenPolynomial n, GenPolynomial d, boolean isred) {
if (d == null || d.isZERO()) {
throw new IllegalArgumentException("denominator may not be zero");
}
ring = r;
if (d.signum() < 0) {
n = n.negate();
d = d.negate();
}
if (isred) {
num = n;
den = d;
return;
}
GenPolynomial p = ring.ideal.normalform(d);
if (p == null || p.isZERO()) {
throw new IllegalArgumentException("denominator may not be in ideal");
}
//d = p; cant do this
C lc = d.leadingBaseCoefficient();
if (!lc.isONE() && lc.isUnit()) {
lc = lc.inverse();
n = n.multiply(lc);
d = d.multiply(lc);
}
if (n.compareTo(d) == 0) {
num = ring.ring.getONE();
den = ring.ring.getONE();
return;
}
if (n.negate().compareTo(d) == 0) {
num = ring.ring.getONE().negate();
den = ring.ring.getONE();
return;
}
if (n.isZERO()) {
num = n;
den = ring.ring.getONE();
return;
}
// must reduce to lowest terms
//GenPolynomial gcd = ring.ring.getONE();
GenPolynomial gcd = ring.engine.gcd(n, d);
if (debug) {
logger.info("gcd = " + gcd);
}
if (gcd.isONE()) {
num = n;
den = d;
} else {
// d not in ideal --> gcd not in ideal
//p = ring.ideal.normalform( gcd );
//if ( p == null || p.isZERO() ) { // todo: find nonzero factor
// num = n;
// den = d;
//} else {
num = n.divide(gcd);
den = d.divide(gcd);
//}
}
}
/**
* Get the corresponding element factory.
* @return factory for this Element.
* @see edu.jas.structure.Element#factory()
*/
public LocalRing factory() {
return ring;
}
/**
* Numerator.
* @see edu.jas.structure.QuotPair#numerator()
*/
public GenPolynomial numerator() {
return num;
}
/**
* Denominator.
* @see edu.jas.structure.QuotPair#denominator()
*/
public GenPolynomial denominator() {
return den;
}
/**
* Clone this.
* @see java.lang.Object#clone()
*/
@Override
public Local copy() {
return new Local(ring, num, den, true);
}
/**
* Is Local zero.
* @return If this is 0 then true is returned, else false.
* @see edu.jas.structure.RingElem#isZERO()
*/
public boolean isZERO() {
return num.isZERO();
}
/**
* Is Local one.
* @return If this is 1 then true is returned, else false.
* @see edu.jas.structure.RingElem#isONE()
*/
public boolean isONE() {
return num.equals(den);
}
/**
* Is Local unit.
* @return If this is a unit then true is returned, else false.
* @see edu.jas.structure.RingElem#isUnit()
*/
public boolean isUnit() {
if (isunit > 0) {
return true;
}
if (isunit == 0) {
return false;
}
// not jet known
if (num.isZERO()) {
isunit = 0;
return false;
}
GenPolynomial p = ring.ideal.normalform(num);
boolean u = (p != null && !p.isZERO());
if (u) {
isunit = 1;
} else {
isunit = 0;
}
return (u);
}
/**
* Is Qoutient a constant.
* @return true, if this has constant numerator and denominator, else false.
*/
public boolean isConstant() {
return num.isConstant() && den.isConstant();
}
/**
* Get the String representation as RingElem.
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
if (PrettyPrint.isTrue()) {
String s = "{ " + num.toString(ring.ring.getVars());
if (den.isONE()) {
return s + " }";
}
return s + "| " + den.toString(ring.ring.getVars()) + " }";
}
return "Local[ " + num.toString() + " | " + den.toString() + " ]";
}
/**
* Get a scripting compatible string representation.
* @return script compatible representation for this Element.
* @see edu.jas.structure.Element#toScript()
*/
@Override
public String toScript() {
// Python case
if (den.isONE()) {
return num.toScript();
}
return num.toScript() + " / " + den.toScript();
}
/**
* Get a scripting compatible string representation of the factory.
* @return script compatible representation for this ElemFactory.
* @see edu.jas.structure.Element#toScriptFactory()
*/
@Override
public String toScriptFactory() {
// Python case
return factory().toScript();
}
/**
* Local comparison.
* @param b Local.
* @return sign(this-b).
*/
@Override
public int compareTo(Local b) {
if (b == null || b.isZERO()) {
return this.signum();
}
if (this.isZERO()) {
return -b.signum();
}
// assume sign(den,b.den) > 0
int s1 = num.signum();
int s2 = b.num.signum();
int t = (s1 - s2) / 2;
if (t != 0) {
System.out.println("compareTo: t = " + t);
return t;
}
if (den.compareTo(b.den) == 0) {
return num.compareTo(b.num);
}
GenPolynomial r = num.multiply(b.den);
GenPolynomial s = den.multiply(b.num);
return r.compareTo(s);
//GenPolynomial x = r.subtract(s);
//return x.signum();
}
/**
* Comparison with any other object.
* @see java.lang.Object#equals(java.lang.Object)
*/
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object b) {
if (!(b instanceof Local)) {
return false;
}
Local a = null;
try {
a = (Local) b;
} catch (ClassCastException e) {
}
if (a == null) {
return false;
}
return compareTo(a) == 0;
}
/**
* Hash code for this local.
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
int h;
h = ring.hashCode();
h = 37 * h + num.hashCode();
h = 37 * h + den.hashCode();
return h;
}
/**
* Local absolute value.
* @return the absolute value of this.
* @see edu.jas.structure.RingElem#abs()
*/
public Local abs() {
return new Local(ring, num.abs(), den, true);
}
/**
* Local summation.
* @param S Local.
* @return this+S.
*/
public Local sum(Local S) {
if (S == null || S.isZERO()) {
return this;
}
GenPolynomial n = num.multiply(S.den);
n = n.sum(den.multiply(S.num));
GenPolynomial d = den.multiply(S.den);
return new Local(ring, n, d, false);
}
/**
* Local negate.
* @return -this.
* @see edu.jas.structure.RingElem#negate()
*/
public Local negate() {
return new Local(ring, num.negate(), den, true);
}
/**
* Local signum.
* @see edu.jas.structure.RingElem#signum()
* @return signum(this).
*/
public int signum() {
return num.signum();
}
/**
* Local subtraction.
* @param S Local.
* @return this-S.
*/
public Local subtract(Local S) {
if (S == null || S.isZERO()) {
return this;
}
GenPolynomial n = num.multiply(S.den);
n = n.subtract(den.multiply(S.num));
GenPolynomial d = den.multiply(S.den);
return new Local(ring, n, d, false);
}
/**
* Local division.
* @param S Local.
* @return this/S.
*/
public Local divide(Local S) {
return multiply(S.inverse());
}
/**
* Local inverse.
* @see edu.jas.structure.RingElem#inverse()
* @return S with S = 1/this if defined.
*/
public Local inverse() {
if (isONE()) {
return this;
}
if (isUnit()) {
return new Local(ring, den, num, true);
}
throw new ArithmeticException("element not invertible " + this);
}
/**
* Local remainder.
* @param S Local.
* @return this - (this/S)*S.
*/
public Local remainder(Local S) {
if (S.isUnit()) {
return ring.getZERO();
}
throw new UnsupportedOperationException("remainder not implemented" + S);
}
/**
* Local multiplication.
* @param S Local.
* @return this*S.
*/
public Local multiply(Local S) {
if (S == null || S.isZERO()) {
return S;
}
if (num.isZERO()) {
return this;
}
if (S.isONE()) {
return this;
}
if (this.isONE()) {
return S;
}
GenPolynomial n = num.multiply(S.num);
GenPolynomial d = den.multiply(S.den);
return new Local(ring, n, d, false);
}
/**
* Local multiplication by GenPolynomial.
* @param b GenPolynomial.
* @return this*b.
*/
public Local multiply(GenPolynomial b) {
if (b == null || b.isZERO()) {
return ring.getZERO();
}
if (num.isZERO()) {
return this;
}
if (b.isONE()) {
return this;
}
GenPolynomial n = num.multiply(b);
return new Local(ring, n, den, false);
}
/**
* Local multiplication by coefficient.
* @param b coefficient.
* @return this*b.
*/
public Local multiply(C b) {
if (b == null || b.isZERO()) {
return ring.getZERO();
}
if (num.isZERO()) {
return this;
}
if (b.isONE()) {
return this;
}
GenPolynomial n = num.multiply(b);
return new Local(ring, n, den, false);
}
/**
* Local multiplication by exponent.
* @param e exponent vector.
* @return this*b.
*/
public Local multiply(ExpVector e) {
if (e == null || e.isZERO()) {
return this;
}
if (num.isZERO()) {
return this;
}
GenPolynomial n = num.multiply(e);
return new Local(ring, n, den, false);
}
/**
* Local monic.
* @return this with monic value part.
*/
public Local monic() {
if (num.isZERO()) {
return this;
}
return this;
// non sense:
//C lbc = num.leadingBaseCoefficient();
//lbc = lbc.inverse();
//GenPolynomial n = num.multiply(lbc);
//GenPolynomial d = den.multiply(lbc);
//return new Local(ring, n, d, true);
}
/**
* Greatest common divisor.
* @param b other element.
* @return gcd(this,b).
*/
public Local gcd(Local b) {
GenPolynomial x = ring.engine.gcd(num, b.num);
GenPolynomial y = ring.engine.gcd(den, b.den);
return new Local(ring, x, y, true);
}
/**
* Extended greatest common divisor. Note: Not implemented, throws
* UnsupportedOperationException.
* @param b other element.
* @return [ gcd(this,b), c1, c2 ] with c1*this + c2*b = gcd(this,b).
*/
public Local[] egcd(Local b) {
throw new UnsupportedOperationException("egcd not implemented " + this.getClass().getName());
}
}
Ads help maintain this website.