/*
* $Id$
*/
package edu.jas.root;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import edu.jas.arith.Rational;
import edu.jas.poly.AlgebraicNumber;
import edu.jas.poly.AlgebraicNumberRing;
import edu.jas.poly.Complex;
import edu.jas.poly.ComplexRing;
import edu.jas.poly.GenPolynomial;
import edu.jas.poly.GenPolynomialRing;
import edu.jas.poly.PolyUtil;
import edu.jas.structure.GcdRingElem;
import edu.jas.structure.RingFactory;
import edu.jas.structure.UnaryFunctor;
/**
* Polynomial utilities related to real and complex roots.
* @author Heinz Kredel
*/
public class PolyUtilRoot {
private static final Logger logger = Logger.getLogger(PolyUtilRoot.class);
private static boolean debug = logger.isDebugEnabled();
/**
* Convert to RealAlgebraicNumber coefficients. Represent as polynomial with
* RealAlgebraicNumber coefficients, C is e.g. ModInteger or BigRational.
* @param pfac result polynomial factory.
* @param A polynomial with C coefficients to be converted.
* @return polynomial with RealAlgebraicNumber<C> coefficients.
*/
public static & Rational> GenPolynomial> convertToAlgebraicCoefficients(
GenPolynomialRing> pfac, GenPolynomial A) {
RealAlgebraicRing afac = (RealAlgebraicRing) pfac.coFac;
if (debug) {
logger.info("afac = " + afac);
}
return PolyUtil.> map(pfac, A, new CoeffToReAlg(afac));
}
/**
* Convert to recursive RealAlgebraicNumber coefficients. Represent as
* polynomial with recursive RealAlgebraicNumber coefficients, C is e.g.
* ModInteger or BigRational.
* @param depth recursion depth of RealAlgebraicNumber coefficients.
* @param pfac result polynomial factory.
* @param A polynomial with C coefficients to be converted.
* @return polynomial with RealAlgebraicNumber<C> coefficients.
*/
public static & Rational> GenPolynomial> convertToRecAlgebraicCoefficients(
int depth, GenPolynomialRing> pfac, GenPolynomial A) {
RealAlgebraicRing afac = (RealAlgebraicRing) pfac.coFac;
return PolyUtil.> map(pfac, A, new CoeffToRecReAlg(depth, afac));
}
/**
* Convert to RealAlgebraicNumber coefficients. Represent as polynomial with
* RealAlgebraicNumber coefficients, C is e.g. ModInteger or BigRational.
* @param pfac result polynomial factory.
* @param A recursive polynomial with GenPolynomial<BigInteger>
* coefficients to be converted.
* @return polynomial with RealAlgebraicNumber<C> coefficients.
*/
public static & Rational> GenPolynomial> convertRecursiveToAlgebraicCoefficients(
GenPolynomialRing> pfac, GenPolynomial> A) {
RealAlgebraicRing afac = (RealAlgebraicRing) pfac.coFac;
return PolyUtil., RealAlgebraicNumber> map(pfac, A, new PolyToReAlg(afac));
}
/**
* Convert to AlgebraicNumber coefficients. Represent as polynomial with
* AlgebraicNumber coefficients.
* @param afac result polynomial factory.
* @param A polynomial with RealAlgebraicNumber<C> coefficients to be
* converted.
* @return polynomial with AlgebraicNumber<C> coefficients.
*/
public static & Rational> GenPolynomial> algebraicFromRealCoefficients(
GenPolynomialRing> afac, GenPolynomial> A) {
AlgebraicNumberRing cfac = (AlgebraicNumberRing) afac.coFac;
return PolyUtil., AlgebraicNumber> map(afac, A, new AlgFromRealCoeff(
cfac));
}
/**
* Convert to RealAlgebraicNumber coefficients. Represent as polynomial with
* RealAlgebraicNumber coefficients.
* @param rfac result polynomial factory.
* @param A polynomial with AlgebraicNumber<C> coefficients to be
* converted.
* @return polynomial with RealAlgebraicNumber<C> coefficients.
*/
public static & Rational> GenPolynomial> realFromAlgebraicCoefficients(
GenPolynomialRing> rfac, GenPolynomial> A) {
RealAlgebraicRing cfac = (RealAlgebraicRing) rfac.coFac;
return PolyUtil., RealAlgebraicNumber> map(rfac, A, new RealFromAlgCoeff(
cfac));
}
/**
* Convert to RealAlgebraicNumber coefficients. Represent as polynomial with
* RealAlgebraicNumber coefficients, C is e.g. BigRational.
* @param pfac result polynomial factory.
* @param A polynomial with C coefficients to be converted.
* @return polynomial with RealAlgebraicNumber<C> coefficients.
*/
public static & Rational> GenPolynomial> convertToRealCoefficients(
GenPolynomialRing> pfac, GenPolynomial A) {
RealAlgebraicRing afac = (RealAlgebraicRing) pfac.coFac;
return PolyUtil.> map(pfac, A, new CoeffToReal(afac));
}
/**
* Convert to ComplexAlgebraicNumber coefficients. Represent as polynomial
* with ComplexAlgebraicNumber coefficients, C is e.g. BigRational.
* @param pfac result polynomial factory.
* @param A polynomial with C coefficients to be converted.
* @return polynomial with ComplexAlgebraicNumber<C> coefficients.
*/
public static & Rational> GenPolynomial> convertToComplexCoefficients(
GenPolynomialRing> pfac, GenPolynomial A) {
ComplexAlgebraicRing afac = (ComplexAlgebraicRing) pfac.coFac;
return PolyUtil.> map(pfac, A, new CoeffToComplex(afac));
}
/**
* Convert to ComplexAlgebraicNumber coefficients. Represent as polynomial
* with ComplexAlgebraicNumber coefficients, C is e.g. BigRational.
* @param pfac result polynomial factory.
* @param A polynomial with C coefficients to be converted.
* @return polynomial with ComplexAlgebraicNumber<C> coefficients.
*/
public static & Rational> GenPolynomial> convertToComplexCoefficientsFromComplex(
GenPolynomialRing> pfac, GenPolynomial> A) {
ComplexAlgebraicRing afac = (ComplexAlgebraicRing) pfac.coFac;
return PolyUtil., ComplexAlgebraicNumber> map(pfac, A,
new CoeffToComplexFromComplex(afac));
}
}
/**
* Polynomial to algebraic functor.
*/
class PolyToReAlg & Rational> implements
UnaryFunctor, RealAlgebraicNumber> {
final protected RealAlgebraicRing afac;
public PolyToReAlg(RealAlgebraicRing fac) {
if (fac == null) {
throw new IllegalArgumentException("fac must not be null");
}
afac = fac;
}
public RealAlgebraicNumber eval(GenPolynomial c) {
if (c == null) {
return afac.getZERO();
}
return new RealAlgebraicNumber(afac, c);
}
}
/**
* Coefficient to algebraic functor.
*/
class CoeffToReAlg & Rational> implements UnaryFunctor> {
final protected RealAlgebraicRing afac;
final protected GenPolynomial zero;
public CoeffToReAlg(RealAlgebraicRing fac) {
if (fac == null) {
throw new IllegalArgumentException("fac must not be null");
}
afac = fac;
GenPolynomialRing pfac = afac.algebraic.ring;
zero = pfac.getZERO();
}
public RealAlgebraicNumber eval(C c) {
if (c == null) {
return afac.getZERO();
}
return new RealAlgebraicNumber(afac, zero.sum(c));
}
}
/**
* Coefficient to recursive algebraic functor.
*/
class CoeffToRecReAlg & Rational> implements UnaryFunctor> {
final protected List> lfac;
final int depth;
@SuppressWarnings("unchecked")
public CoeffToRecReAlg(int depth, RealAlgebraicRing fac) {
if (fac == null) {
throw new IllegalArgumentException("fac must not be null");
}
RealAlgebraicRing afac = fac;
this.depth = depth;
lfac = new ArrayList>(this.depth);
lfac.add(fac);
for (int i = 1; i < this.depth; i++) {
RingFactory rf = afac.algebraic.ring.coFac;
if (!(rf instanceof RealAlgebraicRing)) {
throw new IllegalArgumentException("fac depth to low");
}
afac = (RealAlgebraicRing) (Object) rf;
lfac.add(afac);
}
}
@SuppressWarnings("unchecked")
public RealAlgebraicNumber eval(C c) {
if (c == null) {
return lfac.get(0).getZERO();
}
C ac = c;
RealAlgebraicRing af = lfac.get(lfac.size() - 1);
GenPolynomial zero = af.algebraic.ring.getZERO();
RealAlgebraicNumber an = new RealAlgebraicNumber(af, zero.sum(ac));
for (int i = lfac.size() - 2; i >= 0; i--) {
af = lfac.get(i);
zero = af.algebraic.ring.getZERO();
ac = (C) (Object) an;
an = new RealAlgebraicNumber(af, zero.sum(ac));
}
return an;
}
}
/**
* Coefficient to algebraic from real algebraic functor.
*/
class AlgFromRealCoeff & Rational> implements
UnaryFunctor, AlgebraicNumber> {
final protected AlgebraicNumberRing afac;
public AlgFromRealCoeff(AlgebraicNumberRing fac) {
if (fac == null) {
throw new IllegalArgumentException("fac must not be null");
}
afac = fac;
}
public AlgebraicNumber eval(RealAlgebraicNumber c) {
if (c == null) {
return afac.getZERO();
}
return c.number;
}
}
/**
* Coefficient to real algebriac from algebraic functor.
*/
class RealFromAlgCoeff & Rational> implements
UnaryFunctor, RealAlgebraicNumber> {
final protected RealAlgebraicRing rfac;
public RealFromAlgCoeff(RealAlgebraicRing fac) {
if (fac == null) {
throw new IllegalArgumentException("fac must not be null");
}
rfac = fac;
}
public RealAlgebraicNumber eval(AlgebraicNumber c) {
if (c == null) {
return rfac.getZERO();
}
return new RealAlgebraicNumber(rfac, c);
}
}
/**
* Coefficient to real algebraic functor.
*/
class CoeffToReal & Rational> implements UnaryFunctor> {
final protected RealAlgebraicRing rfac;
final protected AlgebraicNumber zero;
public CoeffToReal(RealAlgebraicRing fac) {
if (fac == null) {
throw new IllegalArgumentException("fac must not be null");
}
rfac = fac;
AlgebraicNumberRing afac = rfac.algebraic;
zero = afac.getZERO();
}
public RealAlgebraicNumber eval(C c) {
if (c == null) {
return rfac.getZERO();
}
return new RealAlgebraicNumber(rfac, zero.sum(c));
}
}
/**
* Coefficient to complex algebraic functor.
*/
class CoeffToComplex & Rational> implements
UnaryFunctor> {
final protected ComplexAlgebraicRing cfac;
final protected AlgebraicNumber> zero;
final protected ComplexRing cr;
public CoeffToComplex(ComplexAlgebraicRing fac) {
if (fac == null) {
throw new IllegalArgumentException("fac must not be null");
}
cfac = fac;
AlgebraicNumberRing> afac = cfac.algebraic;
zero = afac.getZERO();
cr = (ComplexRing) afac.ring.coFac;
}
public ComplexAlgebraicNumber eval(C c) {
if (c == null) {
return cfac.getZERO();
}
return new ComplexAlgebraicNumber(cfac, zero.sum(new Complex(cr, c)));
}
}
/**
* Coefficient to complex algebraic from complex functor.
*/
class CoeffToComplexFromComplex & Rational> implements
UnaryFunctor, ComplexAlgebraicNumber> {
final protected ComplexAlgebraicRing cfac;
final protected AlgebraicNumber> zero;
//final protected ComplexRing cr;
public CoeffToComplexFromComplex(ComplexAlgebraicRing fac) {
if (fac == null) {
throw new IllegalArgumentException("fac must not be null");
}
cfac = fac;
AlgebraicNumberRing> afac = cfac.algebraic;
zero = afac.getZERO();
//cr = (ComplexRing) afac.ring.coFac;
}
public ComplexAlgebraicNumber eval(Complex c) {
if (c == null) {
return cfac.getZERO();
}
return new ComplexAlgebraicNumber(cfac, zero.sum(c));
}
}
Ads help maintain this website.