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