/jas/src/main/java/edu/jas/gbufd/GroebnerBasePseudoRecSeq.java

http://symja.googlecode.com/ · Java · 253 lines · 166 code · 33 blank · 54 comment · 35 complexity · c36722ed1782be7a2c23ccc941feccd5 MD5 · raw file

  1. /*
  2. * $Id: GroebnerBasePseudoRecSeq.java 3807 2011-10-21 19:59:51Z kredel $
  3. */
  4. package edu.jas.gbufd;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.ListIterator;
  8. import java.util.Collections;
  9. import org.apache.log4j.Logger;
  10. import edu.jas.gb.GroebnerBaseAbstract;
  11. import edu.jas.gb.OrderedPairlist;
  12. import edu.jas.gb.Pair;
  13. import edu.jas.poly.GenPolynomial;
  14. import edu.jas.poly.GenPolynomialRing;
  15. import edu.jas.structure.GcdRingElem;
  16. import edu.jas.structure.RingFactory;
  17. import edu.jas.ufd.GCDFactory;
  18. import edu.jas.ufd.GreatestCommonDivisorAbstract;
  19. /**
  20. * Groebner Base with pseudo reduction sequential algorithm for integral
  21. * function coeffcients. Implements polynomial fraction free coefficients Groebner bases.
  22. * @param <C> base coefficient type
  23. * @author Heinz Kredel
  24. */
  25. public class GroebnerBasePseudoRecSeq<C extends GcdRingElem<C>> extends
  26. GroebnerBaseAbstract<GenPolynomial<C>> {
  27. private static final Logger logger = Logger.getLogger(GroebnerBasePseudoRecSeq.class);
  28. private final boolean debug = logger.isDebugEnabled();
  29. /**
  30. * Greatest common divisor engine for coefficient content and primitive
  31. * parts.
  32. */
  33. protected final GreatestCommonDivisorAbstract<C> engine;
  34. /**
  35. * Pseudo reduction engine.
  36. */
  37. protected final PseudoReduction<GenPolynomial<C>> red;
  38. /**
  39. * Coefficient ring factory.
  40. */
  41. protected final RingFactory<GenPolynomial<C>> cofac;
  42. /**
  43. * Base coefficient ring factory.
  44. */
  45. protected final RingFactory<C> baseCofac;
  46. /**
  47. * Constructor.
  48. * @param rf coefficient ring factory.
  49. */
  50. public GroebnerBasePseudoRecSeq(RingFactory<GenPolynomial<C>> rf) {
  51. this(new PseudoReductionSeq<GenPolynomial<C>>(), rf);
  52. }
  53. /**
  54. * Constructor.
  55. * @param red pseudo reduction engine.
  56. * @param rf coefficient ring factory. <b>Note:</b> red must be an instance
  57. * of PseudoReductionSeq.
  58. */
  59. public GroebnerBasePseudoRecSeq(PseudoReduction<GenPolynomial<C>> red, RingFactory<GenPolynomial<C>> rf) {
  60. super(red);
  61. this.red = red;
  62. cofac = rf;
  63. GenPolynomialRing<C> rp = (GenPolynomialRing<C>) cofac;
  64. baseCofac = rp.coFac;
  65. //engine = (GreatestCommonDivisorAbstract<C>)GCDFactory.<C>getImplementation( baseCofac );
  66. //not used:
  67. engine = GCDFactory.<C> getProxy(baseCofac);
  68. }
  69. /**
  70. * Groebner base using pairlist class.
  71. * @param modv module variable number.
  72. * @param F polynomial list.
  73. * @return GB(F) a Groebner base of F.
  74. */
  75. //@Override
  76. public List<GenPolynomial<GenPolynomial<C>>> GB(int modv, List<GenPolynomial<GenPolynomial<C>>> F) {
  77. GenPolynomial<GenPolynomial<C>> p;
  78. List<GenPolynomial<GenPolynomial<C>>> G = new ArrayList<GenPolynomial<GenPolynomial<C>>>();
  79. OrderedPairlist<GenPolynomial<C>> pairlist = null;
  80. int l = F.size();
  81. ListIterator<GenPolynomial<GenPolynomial<C>>> it = F.listIterator();
  82. while (it.hasNext()) {
  83. p = it.next();
  84. if (p.length() > 0) {
  85. p = engine.recursivePrimitivePart(p); //p.monic();
  86. p = p.abs();
  87. if (p.isConstant()) {
  88. G.clear();
  89. G.add(p);
  90. return G; // since no threads are activated
  91. }
  92. G.add(p);
  93. if (pairlist == null) {
  94. pairlist = new OrderedPairlist<GenPolynomial<C>>(modv, p.ring);
  95. }
  96. // putOne not required
  97. pairlist.put(p);
  98. } else {
  99. l--;
  100. }
  101. }
  102. if (l <= 1) {
  103. return G; // since no threads are activated
  104. }
  105. Pair<GenPolynomial<C>> pair;
  106. GenPolynomial<GenPolynomial<C>> pi;
  107. GenPolynomial<GenPolynomial<C>> pj;
  108. GenPolynomial<GenPolynomial<C>> S;
  109. GenPolynomial<GenPolynomial<C>> H;
  110. while (pairlist.hasNext()) {
  111. pair = pairlist.removeNext();
  112. if (pair == null)
  113. continue;
  114. pi = pair.pi;
  115. pj = pair.pj;
  116. if (false && logger.isDebugEnabled()) {
  117. logger.debug("pi = " + pi);
  118. logger.debug("pj = " + pj);
  119. }
  120. S = red.SPolynomial(pi, pj);
  121. if (S.isZERO()) {
  122. pair.setZero();
  123. continue;
  124. }
  125. if (logger.isDebugEnabled()) {
  126. logger.debug("ht(S) = " + S.leadingExpVector());
  127. }
  128. H = red.normalform(G, S);
  129. if (H.isZERO()) {
  130. pair.setZero();
  131. continue;
  132. }
  133. if (logger.isDebugEnabled()) {
  134. logger.debug("ht(H) = " + H.leadingExpVector());
  135. }
  136. H = engine.recursivePrimitivePart(H); //H.monic();
  137. H = H.abs();
  138. if (H.isConstant()) {
  139. G.clear();
  140. G.add(H);
  141. return G; // since no threads are activated
  142. }
  143. if (logger.isDebugEnabled()) {
  144. logger.debug("H = " + H);
  145. }
  146. if (H.length() > 0) {
  147. l++;
  148. G.add(H);
  149. pairlist.put(H);
  150. }
  151. }
  152. logger.debug("#sequential list = " + G.size());
  153. G = minimalGB(G);
  154. logger.info("" + pairlist);
  155. return G;
  156. }
  157. /**
  158. * Minimal ordered Groebner basis.
  159. * @param Gp a Groebner base.
  160. * @return a reduced Groebner base of Gp.
  161. */
  162. @Override
  163. public List<GenPolynomial<GenPolynomial<C>>> minimalGB(List<GenPolynomial<GenPolynomial<C>>> Gp) {
  164. if (Gp == null || Gp.size() <= 1) {
  165. return Gp;
  166. }
  167. // remove zero polynomials
  168. List<GenPolynomial<GenPolynomial<C>>> G = new ArrayList<GenPolynomial<GenPolynomial<C>>>(Gp.size());
  169. for (GenPolynomial<GenPolynomial<C>> a : Gp) {
  170. if (a != null && !a.isZERO()) { // always true in GB()
  171. // already positive a = a.abs();
  172. G.add(a);
  173. }
  174. }
  175. if (G.size() <= 1) {
  176. return G;
  177. }
  178. // remove top reducible polynomials
  179. GenPolynomial<GenPolynomial<C>> a;
  180. List<GenPolynomial<GenPolynomial<C>>> F;
  181. F = new ArrayList<GenPolynomial<GenPolynomial<C>>>(G.size());
  182. while (G.size() > 0) {
  183. a = G.remove(0);
  184. if (red.isTopReducible(G, a) || red.isTopReducible(F, a)) {
  185. // drop polynomial
  186. if (debug) {
  187. System.out.println("dropped " + a);
  188. List<GenPolynomial<GenPolynomial<C>>> ff;
  189. ff = new ArrayList<GenPolynomial<GenPolynomial<C>>>(G);
  190. ff.addAll(F);
  191. a = red.normalform(ff, a);
  192. if (!a.isZERO()) {
  193. System.out.println("error, nf(a) " + a);
  194. }
  195. }
  196. } else {
  197. F.add(a);
  198. }
  199. }
  200. G = F;
  201. if (G.size() <= 1) {
  202. return G;
  203. }
  204. Collections.reverse(G); // important for lex GB
  205. // reduce remaining polynomials
  206. int len = G.size();
  207. int i = 0;
  208. while (i < len) {
  209. a = G.remove(0);
  210. //System.out.println("doing " + a.length());
  211. a = red.normalform(G, a);
  212. a = engine.recursivePrimitivePart(a); //a.monic(); was not required
  213. a = a.abs();
  214. //a = red.normalform( F, a );
  215. G.add(a); // adds as last
  216. i++;
  217. }
  218. return G;
  219. }
  220. }