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

edu.jas.gbufd.PolyGBUtil Maven / Gradle / Ivy

The newest version!
/*
 * $Id: PolyGBUtil.java 4049 2012-07-25 17:10:49Z kredel $
 */

package edu.jas.gbufd;


import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import edu.jas.gb.GroebnerBaseAbstract;
import edu.jas.poly.ExpVector;
import edu.jas.poly.GenPolynomial;
import edu.jas.poly.GenPolynomialRing;
import edu.jas.poly.PolyUtil;
import edu.jas.structure.GcdRingElem;
import edu.jas.structure.RingElem;


/**
 * Package gbufd utilities.
 * @author Heinz Kredel
 */

public class PolyGBUtil {


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


    private static boolean debug = logger.isDebugEnabled();


    /**
     * Test for resultant.
     * @param A generic polynomial.
     * @param B generic polynomial.
     * @param r generic polynomial.
     * @return true if res(A,B) isContained in ideal(A,B), else false.
     */
    public static > 
      boolean isResultant(GenPolynomial A, GenPolynomial B, GenPolynomial r) {
        if (r == null || r.isZERO()) {
            return true;
        }
        GroebnerBaseAbstract bb = GBFactory. getImplementation(r.ring.coFac);
        List> F = new ArrayList>(2);
        F.add(A);
        F.add(B);
        List> G = bb.GB(F);
        //System.out.println("G = " + G);
        GenPolynomial n = bb.red.normalform(G, r);
        //System.out.println("n = " + n);
        return n.isZERO();
    }


    /**
     * Top pseudo reduction wrt the main variables.
     * @param P generic polynomial.
     * @param A list of generic polynomials sorted according to appearing main variables.
     * @return top pseudo remainder of P wrt. A for the appearing variables.
     */
    public static > 
      GenPolynomial topPseudoRemainder(List> A, GenPolynomial P) {
        if (A == null || A.isEmpty()) {
            return P.monic();
        }
        if (P.isZERO()) {
            return P;
        }
        //System.out.println("remainder, P = " + P);
        GenPolynomialRing pfac = A.get(0).ring;
        if (pfac.nvar <= 1) { // recursion base 
            GenPolynomial R = PolyUtil. baseSparsePseudoRemainder(P, A.get(0));
            return R.monic();
        }
        // select polynomials according to the main variable
        GenPolynomialRing> rfac = pfac.recursive(1);
        GenPolynomial Q = A.get(0); // wrong, must eventually search polynomial
        GenPolynomial> qr = PolyUtil. recursive(rfac, Q);
        GenPolynomial> pr = PolyUtil. recursive(rfac, P);
        GenPolynomial> rr;
        if (qr.isONE()) {
            return P.ring.getZERO();
        }
        if (qr.degree(0) > 0) {
            rr = PolyUtil. recursiveSparsePseudoRemainder(pr, qr);
            //System.out.println("remainder, pr = " + pr);
            //System.out.println("remainder, qr = " + qr);
            //System.out.println("remainder, rr = " + rr);
        } else {
            rr = pr;
        }
        if (rr.degree(0) > 0) {
            GenPolynomial R = PolyUtil. distribute(pfac, rr);
            return R.monic();
            // not further reduced wrt. other variables = top-reduction only
        }
        List> zeroDeg = zeroDegrees(A);
        GenPolynomial R = topPseudoRemainder(zeroDeg, rr.leadingBaseCoefficient());
        R = R.extend(pfac, 0, 0L);
        return R.monic();
    }


    /**
     * Top coefficient pseudo remainder of the leading coefficient of P wrt A in the main variables.
     * @param P generic polynomial in n+1 variables.
     * @param A list of generic polynomials in n variables sorted according to appearing main variables.
     * @return pseudo remainder of the leading coefficient of P wrt A.
     */
    public static > 
      GenPolynomial topCoefficientPseudoRemainder(List> A, GenPolynomial P) {
        if (A == null || A.isEmpty()) {
            return P.monic();
        }
        if (P.isZERO()) {
            return P;
        }
        GenPolynomialRing pfac = P.ring;
        GenPolynomialRing pfac1 = A.get(0).ring;
        if (pfac1.nvar <= 1) { // recursion base 
            GenPolynomial a = A.get(0);
            GenPolynomialRing> rfac = pfac.recursive(pfac.nvar - 1);
            GenPolynomial> pr = PolyUtil. recursive(rfac, P);
            // ldcf(P,x_m) = q a + r 
            GenPolynomial> rr = PolyGBUtil. coefficientPseudoRemainderBase(pr, a);
            GenPolynomial R = PolyUtil. distribute(pfac, rr);
            return R.monic();
        }
        // select polynomials according to the main variable
        GenPolynomialRing> rfac1 = pfac1.recursive(1);
        int nv = pfac.nvar - pfac1.nvar;
        GenPolynomialRing> rfac = pfac.recursive(1 + nv);
        GenPolynomialRing>> rfac2 = rfac.recursive(nv);
        if (debug) {
            logger.info("rfac =" + rfac);
        }
        GenPolynomial> pr = PolyUtil. recursive(rfac, P);
        GenPolynomial>> pr2 = PolyUtil.> recursive(rfac2, pr);
        //System.out.println("recursion, pr2 = " + pr2);
        GenPolynomial Q = A.get(0);
        GenPolynomial> qr = PolyUtil. recursive(rfac1, Q);
        GenPolynomial>> rr;
        if (qr.isONE()) {
            return P.ring.getZERO();
        }
        if (qr.degree(0) > 0) {
            // pseudo remainder:  ldcf(P,x_m) = a q + r 
            rr = PolyGBUtil. coefficientPseudoRemainder(pr2, qr);
            //System.out.println("recursion, qr  = " + qr);
            //System.out.println("recursion, pr  = " + pr2);
            //System.out.println("recursion, rr  = " + rr);
        } else {
            rr = pr2;
        }
        // reduction wrt. the other variables
        List> zeroDeg = zeroDegrees(A);
        GenPolynomial> Rr = PolyUtil.> distribute(rfac, rr);
        GenPolynomial R = PolyUtil. distribute(pfac, Rr);
        R = topCoefficientPseudoRemainder(zeroDeg, R);
        return R.monic();
    }


    /**
     * Polynomial leading coefficient pseudo remainder.
     * @param P generic polynomial in n+1 variables.
     * @param A generic polynomial in n variables.
     * @return pseudo remainder of the leading coefficient of P wrt A, with
     *         ldcf(A)m' P = quotient * A + remainder.
     */
    public static > 
      GenPolynomial>> coefficientPseudoRemainder(
                    GenPolynomial>> P, GenPolynomial> A) {
        if (A == null || A.isZERO()) { // findbugs
            throw new ArithmeticException(P + " division by zero " + A);
        }
        if (A.isONE()) {
            return P.ring.getZERO();
        }
        if (P.isZERO() || P.isONE()) {
            return P;
        }
        GenPolynomialRing>> pfac = P.ring;
        GenPolynomialRing> afac = A.ring; // == pfac.coFac
        GenPolynomial>> r = P;
        GenPolynomial> h;
        GenPolynomial>> hr;
        GenPolynomial> ldcf = P.leadingBaseCoefficient();
        long m = ldcf.degree(0);
        long n = A.degree(0);
        GenPolynomial c = A.leadingBaseCoefficient();
        GenPolynomial> cc = afac.getZERO().sum(c);
        //System.out.println("cc = " + cc);
        ExpVector e = A.leadingExpVector();
        for (long i = m; i >= n; i--) {
            if (r.isZERO()) {
                return r;
            }
            GenPolynomial> p = r.leadingBaseCoefficient();
            ExpVector g = r.leadingExpVector();
            long k = p.degree(0);
            if (i == k) {
                GenPolynomial pl = p.leadingBaseCoefficient();
                ExpVector f = p.leadingExpVector();
                f = f.subtract(e);
                r = r.multiply(cc); // coeff cc
                h = A.multiply(pl, f); // coeff ac
                hr = new GenPolynomial>>(pfac, h, g);
                r = r.subtract(hr);
            } else {
                r = r.multiply(cc);
            }
            //System.out.println("r = " + r);
        }
        if (r.degree(0) < P.degree(0)) { // recursion for degree
            r = coefficientPseudoRemainder(r, A);
        }
        return r;
    }


    /**
     * Polynomial leading coefficient pseudo remainder, base case.
     * @param P generic polynomial in 1+1 variables.
     * @param A generic polynomial in 1 variable.
     * @return pseudo remainder of the leading coefficient of P wrt. A, with
     *         ldcf(A)m' P = quotient * A + remainder.
     */
    public static > 
      GenPolynomial> coefficientPseudoRemainderBase(
                    GenPolynomial> P, GenPolynomial A) {
        if (A == null || A.isZERO()) { // findbugs
            throw new ArithmeticException(P + " division by zero " + A);
        }
        if (A.isONE()) {
            return P.ring.getZERO();
        }
        if (P.isZERO() || P.isONE()) {
            return P;
        }
        GenPolynomialRing> pfac = P.ring;
        GenPolynomialRing afac = A.ring; // == pfac.coFac
        GenPolynomial> r = P;
        GenPolynomial h;
        GenPolynomial> hr;
        GenPolynomial ldcf = P.leadingBaseCoefficient();
        long m = ldcf.degree(0);
        long n = A.degree(0);
        C c = A.leadingBaseCoefficient();
        GenPolynomial cc = afac.getZERO().sum(c);
        //System.out.println("cc = " + cc);
        ExpVector e = A.leadingExpVector();
        for (long i = m; i >= n; i--) {
            if (r.isZERO()) {
                return r;
            }
            GenPolynomial p = r.leadingBaseCoefficient();
            ExpVector g = r.leadingExpVector();
            long k = p.degree(0);
            if (i == k) {
                C pl = p.leadingBaseCoefficient();
                ExpVector f = p.leadingExpVector();
                f = f.subtract(e);
                r = r.multiply(cc); // coeff cc
                h = A.multiply(pl, f); // coeff ac
                hr = new GenPolynomial>(pfac, h, g);
                r = r.subtract(hr);
            } else {
                r = r.multiply(cc);
            }
            //System.out.println("r = " + r);
        }
        if (r.degree(0) < P.degree(0)) { // recursion for degree
            r = coefficientPseudoRemainderBase(r, A);
        }
        return r;
    }


    /**
     * Extract polynomials with degree zero in the main variable.
     * @param A list of generic polynomials in n variables.
     * @return Z = [a_i] with deg(a_i,x_n) = 0 and in n-1 variables.
     */
    public static > List> zeroDegrees(List> A) {
        if (A == null || A.isEmpty()) {
            return A;
        }
        GenPolynomialRing pfac = A.get(0).ring;
        GenPolynomialRing> rfac = pfac.recursive(1);
        List> zeroDeg = new ArrayList>(A.size());
        for (int i = 0; i < A.size(); i++) {
            GenPolynomial q = A.get(i);
            GenPolynomial> fr = PolyUtil. recursive(rfac, q);
            if (fr.degree(0) == 0) {
                zeroDeg.add(fr.leadingBaseCoefficient());
            }
        }
        return zeroDeg;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy