/*
 * $Id$
 */

package edu.jas.application;


import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.log4j.Logger;

import edu.jas.kern.Scripting;
import edu.jas.arith.BigRational;
import edu.jas.arith.Rational;
import edu.jas.poly.AlgebraicNumber;
import edu.jas.poly.AlgebraicNumberRing;
import edu.jas.poly.PolyUtil;
import edu.jas.poly.TermOrder;
import edu.jas.poly.GenPolynomial;
import edu.jas.poly.GenPolynomialRing;
import edu.jas.root.RealRootTuple;
import edu.jas.root.PolyUtilRoot;
import edu.jas.root.Interval;
import edu.jas.structure.GcdRingElem;
import edu.jas.structure.Power;
import edu.jas.structure.RingFactory;


/**
 * Real algebraic number factory class based on bi-variate real algebraic
 * numbers. Objects of this class are immutable with the exception of the
 * isolating intervals. Bi-variate ideal implementation is in version 3614
 * 2011-04-28 09:20:34Z.
 * @author Heinz Kredel
 */

public class RealAlgebraicRing & Rational> implements
                RingFactory> {


    /**
     * Representing ideal with univariate polynomials IdealWithUniv.
     */
    /*package*/final IdealWithUniv univs;


    /**
     * Representing ResidueRing.
     */
    /*package*/final ResidueRing algebraic;


    /**
     * Isolating intervals for the real algebraic roots of the real and
     * imaginary part. Note:  intervals may shrink eventually.
     */
    /*package*/RealRootTuple root;


    /**
     * Recursive real root ring.
     */
    public final edu.jas.root.RealAlgebraicRing> realRing;


    /**
     * Epsilon of the isolating rectangle for a complex root.
     */
    protected C eps;


    /**
     * Precision of the isolating rectangle for a complex root.
     */
    public final int PRECISION = 9; //BigDecimal.DEFAULT_PRECISION;


    private static final Logger logger = Logger.getLogger(RealAlgebraicRing.class);


    /**
     * The constructor creates a RealAlgebraicNumber factory object from a
     * IdealWithUniv, ResidueRing and a root tuple.
     * @param m module IdealWithUniv<C>.
     * @param a module ResidueRing<C>.
     * @param r isolating rectangle for a complex root.
     */
    public RealAlgebraicRing(IdealWithUniv m, ResidueRing a, RealRootTuple r) {
        univs = m;
        algebraic = a;
        root = r;
        if (algebraic.characteristic().signum() > 0) {
            throw new IllegalArgumentException("characteristic not zero");
        }
        C e = m.ideal.list.ring.coFac.fromInteger(10L);
        e = e.inverse();
        e = Power.positivePower(e, PRECISION);
        eps = e;
        edu.jas.root.RealAlgebraicRing rfac1 = root.tuple.get(0).factory();
        edu.jas.root.RealAlgebraicRing rfac2 = root.tuple.get(1).factory();
        GenPolynomial p0 = PolyUtil. selectWithVariable(univs.ideal.list.list, 0);
        if (p0 == null) {
            throw new RuntimeException("no polynomial found in " + (0) + " of  " + univs.ideal);
        }
        //System.out.println("realRing, pol = " + p0.toScript());
        GenPolynomialRing pfac = p0.ring;
        GenPolynomialRing> prfac = pfac.recursive(1);
        //System.out.println("prfac = " + prfac);
        GenPolynomial> p0r = PolyUtil. recursive(prfac,p0);
        GenPolynomialRing> parfac 
            = new GenPolynomialRing>(rfac1,prfac);
        GenPolynomial> p0ar 
           = PolyUtilRoot. convertRecursiveToAlgebraicCoefficients(parfac,p0r);
        Interval r2 = rfac2.getRoot();
        edu.jas.root.RealAlgebraicNumber rleft = rfac1.getZERO().sum(r2.left);
        edu.jas.root.RealAlgebraicNumber rright = rfac1.getZERO().sum(r2.right);
        Interval> r2r = new Interval>(rleft,rright);
        edu.jas.root.RealAlgebraicRing> rr 
            = new edu.jas.root.RealAlgebraicRing>(p0ar,r2r); 
        logger.info("realRing = " + rr);
        realRing = rr;
    }


    /**
     * The constructor creates a RealAlgebraicNumber factory object from a
     * IdealWithUniv and a root tuple.
     * @param m module IdealWithUniv<C>.
     * @param root isolating rectangle for a complex root.
     */
    public RealAlgebraicRing(IdealWithUniv m, RealRootTuple root) {
        this(m, new ResidueRing(m.ideal), root);
    }


    /**
     * The constructor creates a RealAlgebraicNumber factory object from a
     * IdealWithUniv and a root tuple.
     * @param m module IdealWithUniv<C>.
     * @param root isolating rectangle for a complex root.
     * @param isField indicator if m is maximal.
     */
    public RealAlgebraicRing(IdealWithUniv m, RealRootTuple root, boolean isField) {
        this(m, new ResidueRing(m.ideal, isField), root);
    }


    /**
     * Set a refined rectangle for the complex root. Note:  rectangle may
     * shrink eventually.
     * @param v rectangle.
     */
    public synchronized void setRoot(RealRootTuple v) {
        // assert v is contained in root
        this.root = v;
    }


    /**
     * Get rectangle for the complex root.
     * @return v rectangle.
     */
    public synchronized RealRootTuple getRoot() {
        return this.root;
    }


    /**
     * Get epsilon.
     * @return epsilon.
     */
    public synchronized C getEps() {
        return this.eps;
    }


    /**
     * Set a new epsilon.
     * @param e epsilon.
     */
    public synchronized void setEps(C e) {
        this.eps = e;
    }


    /**
     * Set a new epsilon.
     * @param e epsilon.
     */
    public synchronized void setEps(BigRational e) {
        edu.jas.root.RealAlgebraicRing r = (edu.jas.root.RealAlgebraicRing) realRing.algebraic.ring.coFac;
        this.eps = r.algebraic.ring.coFac.parse(e.toString());
    }


    /**
     * Is this structure finite or infinite.
     * @return true if this structure is finite, else false.
     * @see edu.jas.structure.ElemFactory#isFinite()
     */
    public boolean isFinite() {
        return realRing.isFinite();
    }


    /**
     * Copy RealAlgebraicNumber element c.
     * @param c
     * @return a copy of c.
     */
    public RealAlgebraicNumber copy(RealAlgebraicNumber c) {
        return new RealAlgebraicNumber(this, c.number);
    }


    /**
     * Get the zero element.
     * @return 0 as RealAlgebraicNumber.
     */
    public RealAlgebraicNumber getZERO() {
        return new RealAlgebraicNumber(this, realRing.getZERO());
    }


    /**
     * Get the one element.
     * @return 1 as RealAlgebraicNumber.
     */
    public RealAlgebraicNumber getONE() {
        return new RealAlgebraicNumber(this, realRing.getONE());
    }


    /**
     * Get a list of the generating elements.
     * @return list of generators for the algebraic structure.
     * @see edu.jas.structure.ElemFactory#generators()
     */
    public List> generators() {
        List>> agens = realRing
                        .generators();
        List> gens = new ArrayList>(agens.size());
        for (edu.jas.root.RealAlgebraicNumber> a : agens) {
            gens.add(getZERO().sum(a));
        }
        return gens;
    }


    /**
     * Query if this ring is commutative.
     * @return true if this ring is commutative, else false.
     */
    public boolean isCommutative() {
        return realRing.isCommutative();
    }


    /**
     * Query if this ring is associative.
     * @return true if this ring is associative, else false.
     */
    public boolean isAssociative() {
        return realRing.isAssociative();
    }


    /**
     * Query if this ring is a field.
     * @return true if algebraic is prime, else false.
     */
    public boolean isField() {
        return realRing.isField();
    }


    /**
     * Assert that this ring is a field.
     * @param isField true if this ring is a field, else false.
     */
    public void setField(boolean isField) {
        realRing.setField(isField);
    }


    /**
     * Characteristic of this ring.
     * @return characteristic of this ring.
     */
    public java.math.BigInteger characteristic() {
        return realRing.characteristic();
    }


    /**
     * Get a RealAlgebraicNumber element from a BigInteger value.
     * @param a BigInteger.
     * @return a RealAlgebraicNumber.
     */
    public RealAlgebraicNumber fromInteger(java.math.BigInteger a) {
        return new RealAlgebraicNumber(this, realRing.fromInteger(a));
    }


    /**
     * Get a RealAlgebraicNumber element from a long value.
     * @param a long.
     * @return a RealAlgebraicNumber.
     */
    public RealAlgebraicNumber fromInteger(long a) {
        return new RealAlgebraicNumber(this, realRing.fromInteger(a));
    }


    /**
     * Get the String representation as RingFactory.
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "RealAlgebraicRing[ " + realRing.toString() + " in " + root + " | isField="
                        + realRing.isField() + ", algebraic.ideal=" + algebraic.ideal.toString() + " ]";
    }


    /**
     * Get a scripting compatible string representation.
     * @return script compatible representation for this ElemFactory.
     * @see edu.jas.structure.ElemFactory#toScript()
     */
    @Override
    public String toScript() {
        // Python case
        return "RealRecN( " + realRing.toScript() + ", " + root.toScript()
        //+ ", " + realRing.isField() 
        //+ ", " + realRing.ring.toScript() 
                        + " )";
    }


    /**
     * Comparison with any other object.
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    @SuppressWarnings("unchecked")
    // not jet working
    public boolean equals(Object b) {
        if (!(b instanceof RealAlgebraicRing)) {
            return false;
        }
        RealAlgebraicRing a = null;
        try {
            a = (RealAlgebraicRing) b;
        } catch (ClassCastException e) {
        }
        if (a == null) {
            return false;
        }
        return realRing.equals(a.realRing) && root.equals(a.root);
    }


    /**
     * Hash code for this RealAlgebraicNumber.
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return 37 * realRing.hashCode() + root.hashCode();
    }


    /**
     * RealAlgebraicNumber random.
     * @param n such that 0 ≤ v ≤ (2n-1).
     * @return a random integer mod modul.
     */
    public RealAlgebraicNumber random(int n) {
        return new RealAlgebraicNumber(this, realRing.random(n));
    }


    /**
     * RealAlgebraicNumber random.
     * @param n such that 0 ≤ v ≤ (2n-1).
     * @param rnd is a source for random bits.
     * @return a random integer mod modul.
     */
    public RealAlgebraicNumber random(int n, Random rnd) {
        return new RealAlgebraicNumber(this, realRing.random(n, rnd));
    }


    /**
     * Parse RealAlgebraicNumber from String.
     * @param s String.
     * @return RealAlgebraicNumber from s.
     */
    public RealAlgebraicNumber parse(String s) {
        return new RealAlgebraicNumber(this, realRing.parse(s));
    }


    /**
     * Parse RealAlgebraicNumber from Reader.
     * @param r Reader.
     * @return next RealAlgebraicNumber from r.
     */
    public RealAlgebraicNumber parse(Reader r) {
        return new RealAlgebraicNumber(this, realRing.parse(r));
    }

}

 

Ads help maintain this website.