1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| public class Monome implements Comparable<Monome> {
private double coef;
private int degre;
public final static Monome MONOME_1 = new Monome(1, 0);
public Monome(double coef, int degre) {
if (coef == 0) {
return new IllegalArgumentException(
"Le coefficient du monome est nul !");
}
this.coef = coef;
this.degre = degre;
}
public Monome(Monome m) {
this(m.coef, m.degre);
}
public double getCoef() {
return coef;
}
public int getDegre() {
return degre;
}
public int compareTo(Monome m) {
return m.degre - this.degre;
}
public String toString() {
StringBuffer sb = new StringBuffer(100);
if (m.coef != 1) {
sb.append(m.coef);
}
if (m.degre != 0) {
sb.append("x");
if (m.degre != 1) {
sb.append("^").append(m.degre);
}
}
return sb.toString();
}
}
}
public class Polynome {
private List<Monome> listeMonomes = new ArrayList<Monome>();
public Polynome(List<Monome> listeMonomes) {
normaliser(listeMonomes.clone());
}
public Polynome(Monome... monomes) {
normaliser(Arrays.asList(monomes));
}
public int size() {
return listeMonomes.size();
}
public double coef() {
return listeMonomes[0].coef;
}
public double degre() {
return listeMonomes[0].degre;
}
public void normaliser(List<Monome> listeMonomes) {
if (listeMonomes.size() == 0) {
this.listeMonomes.add(MONOME_1);
return;
}
boolean normalized = true;
boolean sorted = true;
for (int i = 0, size = listeMonomes.size(); i < size; i++) {
Monome mi = listeMonomes.get(i);
for (int j = i + 1; j < size; j++) {
Monome mj = listeMonomes.get(j);
if (mi.degre = mj.degre) {
normalized = false;
else if (mi.degre < mj.degre) {
sorted = false;
}
}
}
if (!sorted) {
Collections.sort(listeMonomes);
}
if (normalized) {
this.listeMonomes = listeMonomes;
return;
}
for (int i = 0, size = listeMonomes.size(); i < size; i++) {
Monome mi = listeMonomes.get(i);
int coef = mi.coef;
for (int j = i + 1; j < size; j++) {
Monome mj = listeMonomes.get(j);
if (mi.degre == mj.degre) {
coef += mj.coef;
} else {
i = j - 1;
break;
}
}
if (coef != 0) {
if (coef != mi.coef) {
mi = new Monome(coef, mi.degre);
}
this.listeMonomes.add(mi);
}
}
public Polynome deriver() {
List<Monome> listeMonomesDerives = new ArrayList<Monome>(size());
for (Monome m : listeMonomes) {
if (m.degre > 0) {
Momone mDerive = new Monome(m.coef * m.degre, m.degre - 1);
listeMonomesDerives.add(mDerive);
}
}
return new Polynome(listeMonomesDerives);
}
public String toString() {
StringBuffer sb = new StringBuffer(100);
for (Monome m : listeMonomes) {
sb.append(m).append("+");
}
sb.setLength(sb.length() - 1);
return sb.toString();
}
} |
Partager