/*
* $Id$
*/
package edu.jas.poly;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import edu.jas.structure.RingElem;
/**
* Ordered list of polynomials. Mainly for storage and printing / toString and
* conversions to other representations. Polynomials in this list are sorted
* according to their head terms.
* @author Heinz Kredel
*/
public class OrderedPolynomialList> extends PolynomialList {
/**
* Constructor.
* @param r polynomial ring factory.
* @param l list of polynomials.
*/
public OrderedPolynomialList(GenPolynomialRing r, List> l) {
super(r, sort(r, l));
}
/**
* Comparison with any other object.
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
@SuppressWarnings("unchecked")
public boolean equals(Object p) {
if (!super.equals(p)) {
return false;
}
OrderedPolynomialList pl = null;
try {
pl = (OrderedPolynomialList) p;
} catch (ClassCastException ignored) {
}
if (pl == null) {
return false;
}
// compare sorted lists
// done already in super.equals()
return true;
}
/**
* Hash code for OrderedPolynomialList.
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return super.hashCode();
}
/**
* Sort a list of polynomials with respect to the ascending order of the
* leading Exponent vectors. The term order is taken from the ring.
* @param r polynomial ring factory.
* @param L polynomial list.
* @return sorted polynomial list from L.
*/
@SuppressWarnings("unchecked")
public static > List> sort(GenPolynomialRing r,
List> L) {
if (L == null) {
return L;
}
if (L.size() <= 1) { // nothing to sort
return L;
}
final Comparator evc = r.tord.getAscendComparator();
Comparator> cmp = new Comparator>() {
public int compare(GenPolynomial p1, GenPolynomial p2) {
ExpVector e1 = p1.leadingExpVector();
ExpVector e2 = p2.leadingExpVector();
if (e1 == null) {
return -1; // dont care
}
if (e2 == null) {
return 1; // dont care
}
if (e1.length() != e2.length()) {
if (e1.length() > e2.length()) {
return 1; // dont care
}
return -1; // dont care
}
return evc.compare(e1, e2);
}
};
GenPolynomial[] s = null;
try {
s = (GenPolynomial[]) new GenPolynomial[L.size()];
int i = 0;
for (GenPolynomial p : L) {
s[i++] = p;
}
Arrays.> sort(s, cmp);
return new ArrayList>(Arrays.> asList(s));
} catch (ClassCastException ok) {
System.out.println("Warning: polynomials not sorted");
}
return L; // unsorted
}
}
Ads help maintain this website.