
edu.jas.poly.Quotient Maven / Gradle / Ivy
The newest version!
/*
* $Id: Quotient.java 4125 2012-08-19 19:05:22Z kredel $
*/
package edu.jas.poly;
import org.apache.log4j.Logger;
import edu.jas.structure.GcdRingElem;
import edu.jas.structure.RingElem;
/**
* Quotient element based on RingElem pairs. Objects of this class are
* immutable.
* @author Heinz Kredel
*/
public class Quotient> implements RingElem> {
private static final Logger logger = Logger.getLogger(Quotient.class);
private final boolean debug = logger.isDebugEnabled();
/**
* Quotient class factory data structure.
*/
public final QuotientRing ring;
/**
* Numerator part of the element data structure.
*/
public final C num;
/**
* Denominator part of the element data structure.
*/
public final C den;
/**
* The constructor creates a Quotient object from a ring factory.
* @param r ring factory.
*/
public Quotient(QuotientRing r) {
this(r, r.ring.getZERO());
}
/**
* The constructor creates a Quotient object from a ring factory and a
* numerator element. The denominator is assumed to be 1.
* @param r ring factory.
* @param n numerator.
*/
public Quotient(QuotientRing r, C n) {
this(r, n, r.ring.getONE(), true);
}
/**
* The constructor creates a Quotient object from a ring factory and a
* numerator and denominator element.
* @param r ring factory.
* @param n numerator.
* @param d denominator.
*/
public Quotient(QuotientRing r, C n, C d) {
this(r, n, d, false);
}
/**
* The constructor creates a Quotient object from a ring factory and a
* numerator and denominator element.
* @param r ring factory.
* @param n numerator.
* @param d denominator.
* @param isred true if gcd(n,d) == 1, else false.
*/
@SuppressWarnings("unchecked")
protected Quotient(QuotientRing r, C n, C 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;
}
// must reduce to lowest terms
if (n instanceof GcdRingElem && d instanceof GcdRingElem) {
GcdRingElem ng = (GcdRingElem) n;
GcdRingElem dg = (GcdRingElem) d;
C gcd = (C) ng.gcd(dg);
if (debug) {
logger.info("gcd = " + gcd);
}
//RingElem gcd = ring.ring.getONE();
if (gcd.isONE()) {
num = n;
den = d;
} else {
num = n.divide(gcd);
den = d.divide(gcd);
}
// } else { // univariate polynomial?
} else {
logger.warn("gcd = ????");
num = n;
den = d;
}
}
/**
* Get the corresponding element factory.
* @return factory for this Element.
* @see edu.jas.structure.Element#factory()
*/
public QuotientRing factory() {
return ring;
}
/**
* Clone this.
* @see java.lang.Object#clone()
*/
@Override
public Quotient copy() {
return new Quotient(ring, num, den, true);
}
/**
* Is Quotient 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 Quotient 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 Quotient unit.
* @return If this is a unit then true is returned, else false.
* @see edu.jas.structure.RingElem#isUnit()
*/
public boolean isUnit() {
if (num.isZERO()) {
return false;
}
return true;
}
/**
* Get the String representation as RingElem.
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Quotient[ " + num.toString() + " / " + den.toString() + " ]";
}
/**
* Get a scripting compatible string representation.
* @return script compatible representation for this Element.
* @see edu.jas.structure.Element#toScript()
*/
//JAVA6only: @Override
public String toScript() {
// Python case
return "Quotient( " + 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()
*/
//JAVA6only: @Override
public String toScriptFactory() {
// Python case
return factory().toScript();
}
/**
* Quotient comparison.
* @param b Quotient.
* @return sign(this-b).
*/
//JAVA6only: @Override
public int compareTo(Quotient b) {
if (b == null || b.isZERO()) {
return this.signum();
}
C r = num.multiply(b.den);
C s = den.multiply(b.num);
C x = r.subtract(s);
return x.signum();
}
/**
* Comparison with any other object.
* @see java.lang.Object#equals(java.lang.Object)
*/
@SuppressWarnings("unchecked")
// not jet working
@Override
public boolean equals(Object b) {
if (!(b instanceof Quotient)) {
return false;
}
Quotient a = null;
try {
a = (Quotient) b;
} catch (ClassCastException e) {
}
if (a == null) {
return false;
}
return (0 == compareTo(a));
}
/**
* 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;
}
/**
* Quotient absolute value.
* @return the absolute value of this.
* @see edu.jas.structure.RingElem#abs()
*/
public Quotient abs() {
return new Quotient(ring, num.abs(), den, true);
}
/**
* Quotient summation.
* @param S Quotient.
* @return this+S.
*/
public Quotient sum(Quotient S) {
if (S == null || S.isZERO()) {
return this;
}
C n = num.multiply(S.den);
n = n.sum(den.multiply(S.num));
C d = den.multiply(S.den);
return new Quotient(ring, n, d, false);
}
/**
* Quotient negate.
* @return -this.
* @see edu.jas.structure.RingElem#negate()
*/
public Quotient negate() {
return new Quotient(ring, num.negate(), den, true);
}
/**
* Quotient signum.
* @see edu.jas.structure.RingElem#signum()
* @return signum(this).
*/
public int signum() {
return num.signum();
}
/**
* Quotient subtraction.
* @param S Quotient.
* @return this-S.
*/
public Quotient subtract(Quotient S) {
if (S == null || S.isZERO()) {
return this;
}
C n = num.multiply(S.den);
n = n.subtract(den.multiply(S.num));
C d = den.multiply(S.den);
return new Quotient(ring, n, d, false);
}
/**
* Quotient division.
* @param S Quotient.
* @return this/S.
*/
public Quotient divide(Quotient S) {
return multiply(S.inverse());
}
/**
* Quotient inverse.
* @see edu.jas.structure.RingElem#inverse()
* @return S with S = 1/this.
*/
public Quotient inverse() {
return new Quotient(ring, den, num, true);
}
/**
* Quotient remainder.
* @param S Quotient.
* @return this - (this/S)*S.
*/
public Quotient remainder(Quotient S) {
if (num.isZERO()) {
throw new ArithmeticException("element not invertible " + this);
}
return ring.getZERO();
}
/**
* Quotient multiplication.
* @param S Quotient.
* @return this*S.
*/
public Quotient multiply(Quotient S) {
if (S == null || S.isZERO()) {
return S;
}
if (num.isZERO()) {
return this;
}
if (S.isONE()) {
return this;
}
if (this.isONE()) {
return S;
}
C n = num.multiply(S.num);
C d = den.multiply(S.den);
return new Quotient(ring, n, d, false);
}
/**
* Quotient monic.
* @return this with monic value part.
*/
public Quotient monic() {
logger.info("monic not implemented");
return this;
}
/**
* Greatest common divisor. Note: Not implemented, throws
* UnsupportedOperationException.
* @param b other element.
* @return gcd(this,b).
*/
public Quotient gcd(Quotient b) {
throw new UnsupportedOperationException("gcd not implemented " + this.getClass().getName());
}
/**
* 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 Quotient[] egcd(Quotient b) {
throw new UnsupportedOperationException("egcd not implemented " + this.getClass().getName());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy