001/* 002 * $Id$ 003 */ 004 005package edu.jas.poly; 006 007 008import java.util.ArrayList; 009import java.util.Arrays; 010import java.util.Comparator; 011import java.util.List; 012 013import edu.jas.structure.RingElem; 014 015 016/** 017 * Ordered list of vectors of polynomials. Mainly for storage and printing / 018 * toString and conversions to other representations. Lists of polynomials in 019 * this list are sorted according to the head terms of the first column. 020 * @author Heinz Kredel 021 */ 022 023public class OrderedModuleList<C extends RingElem<C>> extends ModuleList<C> { 024 025 026 /** 027 * Constructor. 028 * @param r polynomial ring factory. 029 * @param l list of list of polynomials. 030 */ 031 public OrderedModuleList(GenPolynomialRing<C> r, List<List<GenPolynomial<C>>> l) { 032 super(r, sort(r, ModuleList.padCols(r, l))); 033 } 034 035 036 /** 037 * Comparison with any other object. 038 * @see java.lang.Object#equals(java.lang.Object) 039 */ 040 @Override 041 @SuppressWarnings("unchecked") 042 // not jet working 043 public boolean equals(Object m) { 044 if (!super.equals(m)) { 045 return false; 046 } 047 OrderedModuleList<C> ml = null; 048 try { 049 ml = (OrderedModuleList<C>) m; 050 } catch (ClassCastException ignored) { 051 } 052 if (ml == null) { 053 return false; 054 } 055 // compare sorted lists, done already in super.equals() 056 return true; 057 } 058 059 060 /** 061 * Hash code for OrderedModuleList. 062 * @see java.lang.Object#hashCode() 063 */ 064 @Override 065 public int hashCode() { 066 return super.hashCode(); 067 } 068 069 070 /** 071 * Sort a list of vectors of polynomials with respect to the ascending order 072 * of the leading Exponent vectors of the first column. The term order is 073 * taken from the ring. 074 * @param r polynomial ring factory. 075 * @param l list of polynomial lists. 076 * @return sorted list of polynomial lists from l. 077 */ 078 @SuppressWarnings({ "unchecked", "cast" }) 079 public static <C extends RingElem<C>> List<List<GenPolynomial<C>>> sort(GenPolynomialRing<C> r, 080 List<List<GenPolynomial<C>>> l) { 081 if (l == null) { 082 return l; 083 } 084 if (l.size() <= 1) { // nothing to sort 085 return l; 086 } 087 final Comparator<ExpVector> evc = r.tord.getAscendComparator(); 088 Comparator<List<GenPolynomial<C>>> cmp = new Comparator<List<GenPolynomial<C>>>() { 089 090 091 public int compare(List<GenPolynomial<C>> l1, List<GenPolynomial<C>> l2) { 092 int c = 0; 093 for (int i = 0; i < l1.size(); i++) { 094 GenPolynomial<C> p1 = l1.get(i); 095 GenPolynomial<C> p2 = l2.get(i); 096 ExpVector e1 = p1.leadingExpVector(); 097 ExpVector e2 = p2.leadingExpVector(); 098 if (e1 == e2) { 099 continue; 100 } 101 if (e1 == null && e2 != null) { 102 return -1; 103 } 104 if (e1 != null && e2 == null) { 105 return 1; 106 } 107 if (e1 == null || e2 == null) { // for findbugs 108 continue; // cannot happen 109 } 110 if (e1.length() != e2.length()) { 111 if (e1.length() > e2.length()) { 112 return 1; 113 } 114 return -1; 115 } 116 c = evc.compare(e1, e2); 117 if (c != 0) { 118 return c; 119 } 120 } 121 return c; 122 } 123 }; 124 125 List<GenPolynomial<C>>[] s = null; 126 try { 127 s = (List<GenPolynomial<C>>[]) new List[l.size()]; 128 //System.out.println("s.length = " + s.length ); 129 //s = l.toArray(s); does not work 130 //for ( int i = 0; i < l.size(); i++ ) { 131 // s[i] = l.get(i); 132 //} 133 int i = 0; 134 for (List<GenPolynomial<C>> p : l) { 135 s[i++] = p; 136 } 137 Arrays.<List<GenPolynomial<C>>> sort(s, cmp); 138 return new ArrayList<List<GenPolynomial<C>>>(Arrays.<List<GenPolynomial<C>>> asList(s)); 139 } catch (ClassCastException ok) { 140 System.out.println("Warning: polynomials not sorted"); 141 } 142 return l; // unsorted 143 } 144 145}