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

edu.jas.ufd.SquarefreeInfiniteAlgebraicFieldCharP Maven / Gradle / Ivy

The newest version!
/*
 * $Id: SquarefreeInfiniteAlgebraicFieldCharP.java 3290 2010-08-26 09:18:48Z
 * kredel $
 */

package edu.jas.ufd;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.log4j.Logger;

import edu.jas.gb.GroebnerBaseAbstract;
import edu.jas.gb.GroebnerBaseSeq;
import edu.jas.gb.Reduction;
import edu.jas.gb.ReductionSeq;
import edu.jas.poly.AlgebraicNumber;
import edu.jas.poly.AlgebraicNumberRing;
import edu.jas.poly.ExpVector;
import edu.jas.poly.GenPolynomial;
import edu.jas.poly.GenPolynomialRing;
import edu.jas.poly.Monomial;
import edu.jas.poly.PolyUtil;
import edu.jas.structure.GcdRingElem;
import edu.jas.structure.Power;
import edu.jas.structure.RingFactory;


/**
 * Squarefree decomposition for algebraic extensions of infinite coefficient
 * fields of characteristic p > 0.
 * @author Heinz Kredel
 */

public class SquarefreeInfiniteAlgebraicFieldCharP> extends
                SquarefreeFieldCharP> {


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


    //private final boolean debug = logger.isDebugEnabled();


    /**
     * Squarefree engine for infinite ring of characteristic p base coefficients.
     */
    protected final SquarefreeAbstract aengine;


    /**
     * Constructor.
     */
    public SquarefreeInfiniteAlgebraicFieldCharP(RingFactory> fac) {
        super(fac);
        // isFinite() predicate now present
        if (fac.isFinite()) {
            throw new IllegalArgumentException("fac must be in-finite");
        }
        AlgebraicNumberRing afac = (AlgebraicNumberRing) fac;
        GenPolynomialRing rfac = afac.ring;
        //System.out.println("rfac = " + rfac);
        //System.out.println("rfac = " + rfac.coFac);
        aengine = SquarefreeFactory. getImplementation(rfac);
        //System.out.println("aengine = " + aengine);
    }


    /* --------- algebraic number char-th roots --------------------- */

    /**
     * Squarefree factors of a AlgebraicNumber.
     * @param P AlgebraicNumber.
     * @return [p_1 -> e_1,...,p_k - > e_k] with P = prod_{i=1, ..., k}
     *         p_i**e_k.
     */
    @Override
    public SortedMap, Long> squarefreeFactors(AlgebraicNumber P) {
        if (P == null) {
            throw new IllegalArgumentException(this.getClass().getName() + " P == null");
        }
        SortedMap, Long> factors = new TreeMap, Long>();
        if (P.isZERO()) {
            return factors;
        }
        if (P.isONE()) {
            factors.put(P, 1L);
            return factors;
        }
        GenPolynomial an = P.val;
        AlgebraicNumberRing pfac = P.ring;
        if (!an.isONE()) {
            //System.out.println("an = " + an);
            //System.out.println("aengine = " + aengine);
            SortedMap, Long> nfac = aengine.squarefreeFactors(an);
            //System.out.println("nfac = " + nfac);
            for (Map.Entry, Long> me : nfac.entrySet()) {
                GenPolynomial nfp = me.getKey();
                AlgebraicNumber nf = new AlgebraicNumber(pfac, nfp);
                factors.put(nf, me.getValue()); //nfac.get(nfp));
            }
        }
        if (factors.size() == 0) {
            factors.put(P, 1L);
        }
        return factors;
    }


    /**
     * Characteristics root of a AlgebraicNumber.
     * @param P AlgebraicNumber.
     * @return [p -> k] if exists k with e=charactristic(P)*k and P = p**e,
     *         else null.
     */
    public SortedMap, Long> rootCharacteristic(AlgebraicNumber P) {
        if (P == null) {
            throw new IllegalArgumentException(this.getClass().getName() + " P == null");
        }
        java.math.BigInteger c = P.ring.characteristic();
        if (c.signum() == 0) {
            return null;
        }
        SortedMap, Long> root = new TreeMap, Long>();
        if (P.isZERO()) {
            return root;
        }
        if (P.isONE()) {
            root.put(P, 1L);
            return root;
        }
        // generate system of equations
        AlgebraicNumberRing afac = P.ring;
        long deg = afac.modul.degree(0);
        int d = (int) deg;
        String[] vn = GenPolynomialRing.newVars("c", d);
        GenPolynomialRing> pfac = new GenPolynomialRing>(afac, d, vn);
        List>> uv = (List>>) pfac
                        .univariateList();
        GenPolynomial> cp = pfac.getZERO();
        GenPolynomialRing apfac = afac.ring;
        long i = 0;
        for (GenPolynomial> pa : uv) {
            GenPolynomial ca = apfac.univariate(0, i++);
            GenPolynomial> pb = pa.multiply(new AlgebraicNumber(afac, ca));
            cp = cp.sum(pb);
        }
        GenPolynomial> cpp = Power
                        .>> positivePower(cp, c);
        if (logger.isInfoEnabled()) {
            logger.info("cp   = " + cp);
            logger.info("cp^p = " + cpp);
            logger.info("P    = " + P);
        }
        GenPolynomialRing ppfac = new GenPolynomialRing(apfac.coFac, pfac);
        List> gl = new ArrayList>();
        if (deg == c.longValue() && afac.modul.length() == 2) {
            logger.info("deg(" + deg + ") == char_p(" + c.longValue() + ")");
            for (Monomial> m : cpp) {
                ExpVector f = m.e;
                AlgebraicNumber a = m.c;
                //System.out.println("a  = " + a + " : " + a.toScriptFactory());
                GenPolynomial ap = a.val;
                for (Monomial ma : ap) {
                    ExpVector e = ma.e;
                    C cc = ma.c;
                    C pc = P.val.coefficient(e);
                    C cc1 = ((RingFactory) pc.factory()).getONE();
                    C pc1 = ((RingFactory) pc.factory()).getZERO();
                    //System.out.println("cc = " + cc + ", e = " + e);
                    //System.out.println("pc = " + pc + " : " + cc.toScriptFactory());
                    if (cc instanceof AlgebraicNumber && pc instanceof AlgebraicNumber) {
                        throw new UnsupportedOperationException(
                                        "case multiple algebraic extensions not implemented");
                    } else if (cc instanceof Quotient && pc instanceof Quotient) {
                        Quotient ccp = (Quotient) (Object) cc;
                        Quotient pcp = (Quotient) (Object) pc;
                        if (pcp.isConstant()) {
                            //logger.error("finite field not allowed here " + afac.toScript());
                            throw new ArithmeticException("finite field not allowed here " + afac.toScript());
                        }
                        //C dc = cc.divide(pc);
                        Quotient dcp = ccp.divide(pcp);
                        if (dcp.isConstant()) { // not possible: dc.isConstant() 
                            //System.out.println("dcp = " + dcp + " : " + cc.toScriptFactory()); //  + ", dc = " + dc);
                            //if ( dcp.num.isConstant() ) 
                            cc1 = cc;
                            pc1 = pc;
                        }
                        GenPolynomial r = new GenPolynomial(ppfac, cc1, f);
                        r = r.subtract(pc1);
                        //System.out.println("r = " + r);
                        gl.add(r);
                    }
                }
            }
        } else {
            for (Monomial> m : cpp) {
                ExpVector f = m.e;
                AlgebraicNumber a = m.c;
                //System.out.println("m = " + m);
                GenPolynomial ap = a.val;
                for (Monomial ma : ap) {
                    ExpVector e = ma.e;
                    C cc = ma.c;
                    C pc = P.val.coefficient(e);
                    GenPolynomial r = new GenPolynomial(ppfac, cc, f);
                    r = r.subtract(pc);
                    //System.out.println("r = " + r);
                    gl.add(r);
                }
            }
        }
        if (logger.isInfoEnabled()) {
            logger.info("equations = " + gl);
        }
        // solve system of equations and construct result
        Reduction red = new ReductionSeq();
        gl = red.irreducibleSet(gl);
        GroebnerBaseAbstract bb = new GroebnerBaseSeq(); //GBFactory.getImplementation();
        int z = bb.commonZeroTest(gl);
        if (z < 0) { // no solution
            return null;
        }
        if (logger.isInfoEnabled()) {
            logger.info("solution = " + gl);
        }
        GenPolynomial car = apfac.getZERO();
        for (GenPolynomial pl : gl) {
            if (pl.length() <= 1) {
                continue;
            }
            if (pl.length() > 2) {
                throw new IllegalArgumentException("dim > 0 not implemented " + pl);
            }
            //System.out.println("pl = " + pl);
            ExpVector e = pl.leadingExpVector();
            int[] v = e.dependencyOnVariables();
            if (v == null || v.length == 0) {
                continue;
            }
            int vi = v[0];
            //System.out.println("vi = " + vi);
            GenPolynomial ca = apfac.univariate(0, deg - 1 - vi);
            //System.out.println("ca = " + ca);
            C tc = pl.trailingBaseCoefficient();
            tc = tc.negate();
            if (e.maxDeg() == c.longValue()) { // p-th root of tc ...
                //SortedMap br = aengine.rootCharacteristic(tc);
                SortedMap br = aengine.squarefreeFactors(tc);
                //System.out.println("br = " + br);
                if (br != null && br.size() > 0) {
                    C cc = apfac.coFac.getONE();
                    for (Map.Entry me : br.entrySet()) {
                        C bc = me.getKey();
                        long ll = me.getValue(); //br.get(bc);
                        if (ll % c.longValue() == 0L) {
                            long fl = ll / c.longValue();
                            cc = cc.multiply(Power. positivePower(bc, fl));
                        } else { // fail ?
                            cc = cc.multiply(bc);
                        }
                    }
                    //System.out.println("cc = " + cc);
                    tc = cc;
                }
            }
            ca = ca.multiply(tc);
            car = car.sum(ca);
        }
        AlgebraicNumber rr = new AlgebraicNumber(afac, car);
        if (logger.isInfoEnabled()) {
            logger.info("solution AN = " + rr);
            //System.out.println("rr = " + rr);
        }
        root.put(rr, 1L);
        return root;
    }


    /**
     * GenPolynomial char-th root main variable.
     * @param P univariate GenPolynomial with AlgebraicNumber coefficients.
     * @return char-th_rootOf(P), or null, if P is no char-th root.
     */
    public GenPolynomial> rootCharacteristic(GenPolynomial> P) {
        if (P == null || P.isZERO()) {
            return P;
        }
        GenPolynomialRing> pfac = P.ring;
        if (pfac.nvar > 1) {
            // go to recursion
            GenPolynomialRing> cfac = pfac.contract(1);
            GenPolynomialRing>> rfac = new GenPolynomialRing>>(
                            cfac, 1);
            GenPolynomial>> Pr = PolyUtil.> recursive(
                            rfac, P);
            GenPolynomial>> Prc = recursiveUnivariateRootCharacteristic(Pr);
            if (Prc == null) {
                return null;
            }
            GenPolynomial> D = PolyUtil.> distribute(pfac, Prc);
            return D;
        }
        RingFactory> rf = pfac.coFac;
        if (rf.characteristic().signum() != 1) {
            // basePthRoot not possible
            throw new IllegalArgumentException(P.getClass().getName() + " only for ModInteger polynomials "
                            + rf);
        }
        long mp = rf.characteristic().longValue();
        GenPolynomial> d = pfac.getZERO().copy();
        for (Monomial> m : P) {
            ExpVector f = m.e;
            long fl = f.getVal(0);
            if (fl % mp != 0) {
                return null;
            }
            fl = fl / mp;
            SortedMap, Long> sm = rootCharacteristic(m.c);
            if (sm == null) {
                return null;
            }
            if (logger.isInfoEnabled()) {
                logger.info("sm_alg,root = " + sm);
            }
            AlgebraicNumber r = rf.getONE();
            for (Map.Entry, Long> me : sm.entrySet()) {
                AlgebraicNumber rp = me.getKey();
                long gl = me.getValue(); //sm.get(rp);
                if (gl > 1) {
                    rp = Power.> positivePower(rp, gl);
                }
                r = r.multiply(rp);
            }
            ExpVector e = ExpVector.create(1, 0, fl);
            d.doPutToMap(e, r);
        }
        logger.info("sm_alg,root,d = " + d);
        return d;
    }


    /**
     * GenPolynomial char-th root univariate polynomial.
     * @param P GenPolynomial.
     * @return char-th_rootOf(P).
     */
    @Override
    public GenPolynomial> baseRootCharacteristic(GenPolynomial> P) {
        if (P == null || P.isZERO()) {
            return P;
        }
        GenPolynomialRing> pfac = P.ring;
        if (pfac.nvar > 1) {
            // basePthRoot not possible by return type
            throw new IllegalArgumentException(P.getClass().getName() + " only for univariate polynomials");
        }
        RingFactory> rf = pfac.coFac;
        if (rf.characteristic().signum() != 1) {
            // basePthRoot not possible
            throw new IllegalArgumentException(P.getClass().getName() + " only for char p > 0 " + rf);
        }
        long mp = rf.characteristic().longValue();
        GenPolynomial> d = pfac.getZERO().copy();
        for (Monomial> m : P) {
            //System.out.println("m = " + m);
            ExpVector f = m.e;
            long fl = f.getVal(0);
            if (fl % mp != 0) {
                return null;
            }
            fl = fl / mp;
            SortedMap, Long> sm = rootCharacteristic(m.c);
            if (sm == null) {
                return null;
            }
            if (logger.isInfoEnabled()) {
                logger.info("sm_alg,base,root = " + sm);
            }
            AlgebraicNumber r = rf.getONE();
            for (Map.Entry, Long> me : sm.entrySet()) {
                AlgebraicNumber rp = me.getKey();
                //System.out.println("rp = " + rp);
                long gl = me.getValue(); //sm.get(rp);
                //System.out.println("gl = " + gl);
                AlgebraicNumber re = rp;
                if (gl > 1) {
                    re = Power.> positivePower(rp, gl);
                }
                //System.out.println("re = " + re);
                r = r.multiply(re);
            }
            ExpVector e = ExpVector.create(1, 0, fl);
            d.doPutToMap(e, r);
        }
        if (logger.isInfoEnabled()) {
            logger.info("sm_alg,base,d = " + d);
        }
        return d;
    }


    /**
     * GenPolynomial char-th root univariate polynomial with polynomial
     * coefficients.
     * @param P recursive univariate GenPolynomial.
     * @return char-th_rootOf(P), or null if P is no char-th root.
     */
    @Override
    public GenPolynomial>> recursiveUnivariateRootCharacteristic(
                    GenPolynomial>> P) {
        if (P == null || P.isZERO()) {
            return P;
        }
        GenPolynomialRing>> pfac = P.ring;
        if (pfac.nvar > 1) {
            // basePthRoot not possible by return type
            throw new IllegalArgumentException(P.getClass().getName()
                            + " only for univariate recursive polynomials");
        }
        RingFactory>> rf = pfac.coFac;
        if (rf.characteristic().signum() != 1) {
            // basePthRoot not possible
            throw new IllegalArgumentException(P.getClass().getName() + " only for char p > 0 " + rf);
        }
        long mp = rf.characteristic().longValue();
        GenPolynomial>> d = pfac.getZERO().copy();
        for (Monomial>> m : P) {
            ExpVector f = m.e;
            long fl = f.getVal(0);
            if (fl % mp != 0) {
                return null;
            }
            fl = fl / mp;
            GenPolynomial> r = rootCharacteristic(m.c);
            if (r == null) {
                return null;
            }
            ExpVector e = ExpVector.create(1, 0, fl);
            d.doPutToMap(e, r);
        }
        return d;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy