001/* 002 * $Id: OrderedPolynomialList.java 4057 2012-07-26 20:35:44Z kredel $ 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 polynomials. Mainly for storage and printing / toString and 018 * conversions to other representations. Polynomials in this list are sorted 019 * according to their head terms. 020 * @author Heinz Kredel 021 */ 022 023public class OrderedPolynomialList<C extends RingElem<C>> extends PolynomialList<C> { 024 025 026 /** 027 * Constructor. 028 * @param r polynomial ring factory. 029 * @param l list of polynomials. 030 */ 031 public OrderedPolynomialList(GenPolynomialRing<C> r, List<GenPolynomial<C>> l) { 032 super(r, sort(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 public boolean equals(Object p) { 043 if (!super.equals(p)) { 044 return false; 045 } 046 OrderedPolynomialList<C> pl = null; 047 try { 048 pl = (OrderedPolynomialList<C>) p; 049 } catch (ClassCastException ignored) { 050 } 051 if (pl == null) { 052 return false; 053 } 054 // compare sorted lists 055 // done already in super.equals() 056 return true; 057 } 058 059 060 /** 061 * Hash code for OrderedPolynomialList. 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 polynomials with respect to the ascending order of the 072 * leading Exponent vectors. The term order is taken from the ring. 073 * @param r polynomial ring factory. 074 * @param L polynomial list. 075 * @return sorted polynomial list from L. 076 */ 077 @SuppressWarnings("unchecked") 078 public static <C extends RingElem<C>> List<GenPolynomial<C>> sort(GenPolynomialRing<C> r, 079 List<GenPolynomial<C>> L) { 080 if (L == null) { 081 return L; 082 } 083 if (L.size() <= 1) { // nothing to sort 084 return L; 085 } 086 final Comparator<ExpVector> evc = r.tord.getAscendComparator(); 087 Comparator<GenPolynomial<C>> cmp = new Comparator<GenPolynomial<C>>() { 088 089 090 public int compare(GenPolynomial<C> p1, GenPolynomial<C> p2) { 091 ExpVector e1 = p1.leadingExpVector(); 092 ExpVector e2 = p2.leadingExpVector(); 093 if (e1 == null) { 094 return -1; // dont care 095 } 096 if (e2 == null) { 097 return 1; // dont care 098 } 099 if (e1.length() != e2.length()) { 100 if (e1.length() > e2.length()) { 101 return 1; // dont care 102 } 103 return -1; // dont care 104 } 105 return evc.compare(e1, e2); 106 } 107 }; 108 GenPolynomial<C>[] s = null; 109 try { 110 s = (GenPolynomial<C>[]) new GenPolynomial[L.size()]; 111 int i = 0; 112 for (GenPolynomial<C> p : L) { 113 s[i++] = p; 114 } 115 Arrays.<GenPolynomial<C>> sort(s, cmp); 116 return new ArrayList<GenPolynomial<C>>(Arrays.<GenPolynomial<C>> asList(s)); 117 } catch (ClassCastException ok) { 118 System.out.println("Warning: polynomials not sorted"); 119 } 120 return L; // unsorted 121 } 122 123}