/*
 * $Id$
 */

package edu.jas.root;


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

import edu.jas.arith.BigDecimal;
import edu.jas.arith.BigRational;
import edu.jas.arith.Rational;
import edu.jas.poly.AlgebraicNumber;
import edu.jas.poly.AlgebraicNumberRing;
import edu.jas.poly.GenPolynomial;
import edu.jas.structure.GcdRingElem;
import edu.jas.structure.Power;
import edu.jas.structure.RingFactory;


/**
 * Real algebraic number factory class based on AlgebraicNumberRing with
 * RingElem interface. Objects of this class are immutable with the exception of
 * the isolating intervals.
 * @author Heinz Kredel
 */

public class RealAlgebraicRing & Rational>
/*extends AlgebraicNumberRing*/
implements RingFactory> {


    /**
     * Representing AlgebraicNumberRing.
     */
    public final AlgebraicNumberRing algebraic;


    /**
     * Isolating interval for a real root. Note:  interval may shrink
     * eventually.
     */
    /*package*/Interval root;


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


    /**
     * Precision of the isolating interval for a real root.
     */
    protected C eps;


    /**
     * Real root computation engine.
     */
    public final RealRootsSturm engine;


    /**
     * The constructor creates a RealAlgebraicNumber factory object from a
     * GenPolynomial objects module.
     * @param m module GenPolynomial.
     * @param root isolating interval for a real root.
     */
    public RealAlgebraicRing(GenPolynomial m, Interval root) {
        algebraic = new AlgebraicNumberRing(m);
        this.root = root;
        engine = new RealRootsSturm();
        if (m.ring.characteristic().signum() > 0) {
            throw new RuntimeException("characteristic not zero");
        }
        C e = m.ring.coFac.fromInteger(10L);
        e = e.inverse();
        //C x = Power.positivePower(e, BigDecimal.DEFAULT_PRECISION);
        e = Power.positivePower(e, PRECISION); // better not too much for speed
        eps = e;
    }


    /**
     * The constructor creates a RealAlgebraicNumber factory object from a
     * GenPolynomial objects module.
     * @param m module GenPolynomial.
     * @param root isolating interval for a real root.
     * @param isField indicator if m is prime.
     */
    public RealAlgebraicRing(GenPolynomial m, Interval root, boolean isField) {
        algebraic = new AlgebraicNumberRing(m, isField);
        this.root = root;
        engine = new RealRootsSturm();
        if (m.ring.characteristic().signum() > 0) {
            throw new RuntimeException("characteristic not zero");
        }
        C e = m.ring.coFac.fromInteger(10L);
        e = e.inverse();
        e = Power.positivePower(e, PRECISION); //BigDecimal.DEFAULT_PRECISION);
        eps = e;
    }


    /**
     * Get the module part.
     * @return modul. public GenPolynomial getModul() { return
     *         algebraic.modul; }
     */


    /**
     * Get the interval for the real root. Note:  interval may shrink
     * later.
     * @return real root isolating interval
     */
    public synchronized Interval getRoot() {
        return root;
    }


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


    /**
     * Get the epsilon.
     * @return eps.
     */
    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) {
        this.eps = algebraic.ring.coFac.parse(e.toString());
    }


    /**
     * RealAlgebraicRing half interval.
     */
    public void halfInterval() {
        Interval v = engine.halfInterval(root, algebraic.modul);
        //System.out.println("old v = " + ring.root + ", new v = " + v);
        setRoot(v);
    }


    /**
     * 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 algebraic.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, algebraic.getZERO());
    }


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


    /**
     * Get the generating element.
     * @return alpha as RealAlgebraicNumber.
     */
    public RealAlgebraicNumber getGenerator() {
        return new RealAlgebraicNumber(this, algebraic.getGenerator());
    }


    /**
     * 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 = algebraic.generators();
        List> gens = new ArrayList>(agens.size());
        for (AlgebraicNumber a : agens) {
            gens.add(getZERO().sum(a.getVal()));
        }
        return gens;
    }


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


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


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


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


    /**
     * Characteristic of this ring.
     * @return characteristic of this ring.
     */
    public java.math.BigInteger characteristic() {
        return algebraic.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, algebraic.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, algebraic.fromInteger(a));
    }


    /**
     * Get the String representation as RingFactory.
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "RealAlgebraicRing[ " + algebraic.modul.toString() + " in " + root + " | isField="
                        + algebraic.isField() + " :: " + algebraic.ring.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 "RealN( " + algebraic.modul.toScript() + ", " + root.toScript()
        //+ ", " + algebraic.isField() 
        //+ ", " + algebraic.ring.toScript() 
                        + " )";
    }


    /**
     * Comparison with any other object.
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    @SuppressWarnings("unchecked")
    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 algebraic.equals(a.algebraic) && root.equals(a.root);
    }


    /**
     * Hash code for this RealAlgebraicNumber.
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return 37 * algebraic.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, algebraic.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, algebraic.random(n, rnd));
    }


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


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

}

 

Ads help maintain this website.