All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.jas.root.RootFactory Maven / Gradle / Ivy

The newest version!
/*
 * $Id: RootFactory.java 3974 2012-07-01 12:29:44Z kredel $
 */

package edu.jas.root;


import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Map;

import edu.jas.arith.Rational;
import edu.jas.arith.BigRational;
import edu.jas.poly.Complex;
import edu.jas.poly.ComplexRing;
import edu.jas.poly.PolyUtil;
import edu.jas.poly.GenPolynomial;
import edu.jas.poly.GenPolynomialRing;
import edu.jas.structure.GcdRingElem;
import edu.jas.ufd.FactorAbstract;
import edu.jas.ufd.FactorFactory;
import edu.jas.ufd.SquarefreeAbstract;
import edu.jas.ufd.SquarefreeFactory;


/**
 * Roots factory.
 * @author Heinz Kredel
 */
public class RootFactory {


    /**
     * Is real algebraic number a root of a polynomial.
     * @param f univariate polynomial.
     * @param r real algebraic number.
     * @return true, if f(r) == 0, else false;
     */
    public static  & Rational> 
           boolean isRoot(GenPolynomial f, RealAlgebraicNumber r) {
        RealAlgebraicRing rr = r.factory(); 
        GenPolynomialRing> rfac 
           = new GenPolynomialRing>(rr,f.factory());
        GenPolynomial> p;
        p = PolyUtilRoot. convertToRealCoefficients(rfac,f);
        RealAlgebraicNumber a = PolyUtil.> evaluateMain(rr,p,r);
        return a.isZERO();
    }


    /**
     * Real algebraic numbers.
     * @param f univariate polynomial.
     * @return a list of different real algebraic numbers.
     */
    public static  & Rational> 
           List> realAlgebraicNumbers(GenPolynomial f) {
        RealRoots rr = new RealRootsSturm();
        SquarefreeAbstract engine = SquarefreeFactory. getImplementation(f.ring.coFac);
        Map,Long> SF = engine.squarefreeFactors(f);
        Set> S = SF.keySet();
        List> list = new ArrayList>();
        for (GenPolynomial sp : S) {
            List> iv = rr.realRoots(sp);
            for (Interval I : iv) {
                RealAlgebraicRing rar = new RealAlgebraicRing(sp, I);
                RealAlgebraicNumber rn = rar.getGenerator();
                long mult = SF.get(sp);
                for ( int i = 0; i < mult; i++ ) {
                     list.add(rn);
                }
            }
        }
        return list;
    }


    /**
     * Real algebraic numbers.
     * @param f univariate polynomial.
     * @param eps rational precision.
     * @return a list of different real algebraic numbers.
     */
    public static  & Rational> 
           List> realAlgebraicNumbers(GenPolynomial f, BigRational eps) {
        RealRoots rr = new RealRootsSturm();
        SquarefreeAbstract engine = SquarefreeFactory. getImplementation(f.ring.coFac);
        Map,Long> SF = engine.squarefreeFactors(f);
        Set> S = SF.keySet();
        List> list = new ArrayList>();
        for (GenPolynomial sp : S) {
            List> iv = rr.realRoots(sp,eps);
            for (Interval I : iv) {
                RealAlgebraicRing rar = new RealAlgebraicRing(sp, I);
                rar.setEps(eps);
                RealAlgebraicNumber rn = rar.getGenerator();
                long mult = SF.get(sp);
                for ( int i = 0; i < mult; i++ ) {
                     list.add(rn);
                }
            }
        }
        return list;
    }


    /**
     * Real algebraic numbers from a field.
     * @param f univariate polynomial.
     * @return a list of different real algebraic numbers from a field.
     */
    public static  & Rational> 
           List> realAlgebraicNumbersField(GenPolynomial f) {
        RealRoots rr = new RealRootsSturm();
        FactorAbstract engine = FactorFactory. getImplementation(f.ring.coFac);
        Map,Long> SF = engine.baseFactors(f);
        Set> S = SF.keySet();
        List> list = new ArrayList>();
        for (GenPolynomial sp : S) {
            List> iv = rr.realRoots(sp);
            for (Interval I : iv) {
                RealAlgebraicRing rar = new RealAlgebraicRing(sp, I, true);//field
                RealAlgebraicNumber rn = rar.getGenerator();
                long mult = SF.get(sp);
                for ( int i = 0; i < mult; i++ ) {
                     list.add(rn);
                }
            }
        }
        return list;
    }


    /**
     * Real algebraic numbers from a field.
     * @param f univariate polynomial.
     * @param eps rational precision.
     * @return a list of different real algebraic numbers from a field.
     */
    public static  & Rational> 
           List> realAlgebraicNumbersField(GenPolynomial f, BigRational eps) {
        RealRoots rr = new RealRootsSturm();
        FactorAbstract engine = FactorFactory. getImplementation(f.ring.coFac);
        Map,Long> SF = engine.baseFactors(f);
        Set> S = SF.keySet();
        List> list = new ArrayList>();
        for (GenPolynomial sp : S) {
            List> iv = rr.realRoots(sp,eps);
            for (Interval I : iv) {
                RealAlgebraicRing rar = new RealAlgebraicRing(sp, I, true);//field
                rar.setEps(eps);
                RealAlgebraicNumber rn = rar.getGenerator();
                long mult = SF.get(sp);
                for ( int i = 0; i < mult; i++ ) {
                     list.add(rn);
                }
            }
        }
        return list;
    }


    /**
     * Real algebraic numbers from a irreducible polynomial.
     * @param f univariate irreducible polynomial.
     * @return a list of different real algebraic numbers from a field.
     */
    public static  & Rational> 
           List> realAlgebraicNumbersIrred(GenPolynomial f) {
        RealRoots rr = new RealRootsSturm();
        List> list = new ArrayList>();
        List> iv = rr.realRoots(f);
        for (Interval I : iv) {
            RealAlgebraicRing rar = new RealAlgebraicRing(f, I, true);//field
            RealAlgebraicNumber rn = rar.getGenerator();
            list.add(rn);
        }
        return list;
    }


    /**
     * Real algebraic numbers from a irreducible polynomial.
     * @param f univariate irreducible polynomial.
     * @param eps rational precision.
     * @return a list of different real algebraic numbers from a field.
     */
    public static  & Rational> 
           List> realAlgebraicNumbersIrred(GenPolynomial f, BigRational eps) {
        RealRoots rr = new RealRootsSturm();
        List> list = new ArrayList>();
        List> iv = rr.realRoots(f,eps);
        for (Interval I : iv) {
            RealAlgebraicRing rar = new RealAlgebraicRing(f, I, true);//field
            rar.setEps(eps);
            RealAlgebraicNumber rn = rar.getGenerator();
            list.add(rn);
        }
        return list;
    }


    /**
     * Is complex algebraic number a root of a polynomial.
     * @param f univariate polynomial.
     * @param r complex algebraic number.
     * @return true, if f(r) == 0, else false;
     */
    public static  & Rational> 
           boolean isRoot(GenPolynomial f, ComplexAlgebraicNumber r) {
        ComplexAlgebraicRing cr = r.factory(); 
        GenPolynomialRing> cfac 
           = new GenPolynomialRing>(cr,f.factory());
        GenPolynomial> p;
        p = PolyUtilRoot. convertToComplexCoefficients(cfac,f);
        ComplexAlgebraicNumber a = PolyUtil.> evaluateMain(cr,p,r);
        return a.isZERO();
    }


    /**
     * Is complex algebraic number a root of a complex polynomial.
     * @param f univariate complex polynomial.
     * @param r complex algebraic number.
     * @return true, if f(r) == 0, else false;
     */
    public static  & Rational> 
           boolean isRootComplex(GenPolynomial> f, ComplexAlgebraicNumber r) {
        ComplexAlgebraicRing cr = r.factory(); 
        GenPolynomialRing> cfac 
           = new GenPolynomialRing>(cr,f.factory());
        GenPolynomial> p;
        p = PolyUtilRoot. convertToComplexCoefficientsFromComplex(cfac,f);
        ComplexAlgebraicNumber a = PolyUtil.> evaluateMain(cr,p,r);
        return a.isZERO();
    }


    /**
     * Complex algebraic numbers.
     * @param f univariate polynomial.
     * @return a list of different complex algebraic numbers.
     */
    public static  & Rational> 
           List> complexAlgebraicNumbersComplex(GenPolynomial> f) {
        ComplexRoots cr = new ComplexRootsSturm(f.ring.coFac);
        SquarefreeAbstract> engine = SquarefreeFactory
                .> getImplementation(f.ring.coFac);
        Map>,Long> SF = engine.squarefreeFactors(f);
        Set>> S = SF.keySet();
        List> list = new ArrayList>();
        for (GenPolynomial> sp : S) {
            List> iv = cr.complexRoots(sp);
            for (Rectangle I : iv) {
                ComplexAlgebraicRing car = new ComplexAlgebraicRing(sp, I);
                ComplexAlgebraicNumber cn = car.getGenerator();
                long mult = SF.get(sp);
                for ( int i = 0; i < mult; i++ ) {
                     list.add(cn);
                }
            }
        }
        return list;
    }


    /**
     * Complex algebraic numbers.
     * @param f univariate polynomial.
     * @param eps rational precision.
     * @return a list of different complex algebraic numbers.
     */
    public static  & Rational> 
           List> complexAlgebraicNumbersComplex(GenPolynomial> f, BigRational eps) {
        ComplexRoots cr = new ComplexRootsSturm(f.ring.coFac);
        SquarefreeAbstract> engine = SquarefreeFactory
                .> getImplementation(f.ring.coFac);
        Map>,Long> SF = engine.squarefreeFactors(f);
        Set>> S = SF.keySet();
        List> list = new ArrayList>();
        for (GenPolynomial> sp : S) {
            List> iv = cr.complexRoots(sp);
            for (Rectangle I : iv) {
                Rectangle Iv = I;
                try {
                    Iv = cr.complexRootRefinement(I,sp,eps);
                } catch (InvalidBoundaryException e) {
                    e.printStackTrace();
                }
                ComplexAlgebraicRing car = new ComplexAlgebraicRing(sp, Iv);
                car.setEps(eps);
                ComplexAlgebraicNumber cn = car.getGenerator();
                long mult = SF.get(sp);
                for ( int i = 0; i < mult; i++ ) {
                     list.add(cn);
                }
            }
        }
        return list;
    }


    /**
     * Complex algebraic numbers.
     * @param f univariate (rational) polynomial.
     * @return a list of different complex algebraic numbers.
     */
    public static  & Rational> 
           List> complexAlgebraicNumbers(GenPolynomial f) {
        if ( f.ring.coFac instanceof Complex ) {
            throw new IllegalArgumentException("f already has Complex coefficients " + f.ring);
        }
        if ( f.ring.coFac instanceof ComplexAlgebraicRing ) {
            throw new UnsupportedOperationException("unsupported ComplexAlgebraicRing coefficients " + f.ring);
        }
        ComplexRing cr = new ComplexRing( f.ring.coFac );
        GenPolynomialRing> fac = new GenPolynomialRing>(cr,f.ring);
        GenPolynomial> fc = PolyUtil.complexFromAny(fac,f); 
        return complexAlgebraicNumbersComplex(fc);
    }


    /**
     * Complex algebraic numbers.
     * @param f univariate (rational) polynomial.
     * @param eps rational precision.
     * @return a list of different complex algebraic numbers.
     */
    public static  & Rational> 
                             List> complexAlgebraicNumbers(GenPolynomial f, BigRational eps) {
        if ( f.ring.coFac instanceof Complex ) {
            throw new IllegalArgumentException("f already has Complex coefficients " + f.ring);
        }
        if ( f.ring.coFac instanceof ComplexAlgebraicRing ) {
            throw new UnsupportedOperationException("unsupported ComplexAlgebraicRing coefficients " + f.ring);
        }
        ComplexRing cr = new ComplexRing( f.ring.coFac );
        GenPolynomialRing> fac = new GenPolynomialRing>(cr,f.ring);
        GenPolynomial> fc = PolyUtil.complexFromAny(fac,f); 
        return complexAlgebraicNumbersComplex(fc,eps);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy