Files
oopuebung/uebung03/src/polynomial/Polynomial.java
2025-05-02 13:48:51 +02:00

188 lines
4.9 KiB
Java

package polynomial;
import java.util.Arrays;
public class Polynomial {
private final int[] werte;
private final int degree;
public Polynomial(int[] w) {
werte = w.clone();
int deg = 0;
for(int i = 0; i<w.length; i++){
if (werte[i] != 0){
deg = i;
}
}
degree = deg;
//gleichwertige Variante
/*for (int i = w.length; i >= 0; i--) {
if (w[i] != 0) {
degree = i;
}
}*/
}
public Polynomial() {
this(new int[]{0,1});
}
//Option 1 Kopie des Arrays
public int[] getCoeef() {
return werte.clone();
}
//Option 2 Koeef an der Stelle i
public int getCoeefAt(int i) {
return i > degree ? 0 : werte[i];
}
public static Polynomial constant(int v) {
return new Polynomial(new int[]{v});
}
@Override
public String toString() {
if (isNull()){return "0";}
String result = "";
for (int i = degree; i >= 0; i--) {
if (werte[i] == 0) {continue;}
if (!result.isEmpty()) {
if (werte[i] > 0) {result += " + ";}
if (werte[i] < 0) {result += " - ";}
}
if (result.isEmpty() && werte[i] <0){result += "-";} // Minus am Anfang
int absCoeff = Math.abs(werte[i]);
if (absCoeff != 1 && i == 0) {result += absCoeff;}
if (absCoeff == 1 && i == 0) {result += "1";}
if (absCoeff != 1 && i != 0) {result += absCoeff +"*";}
if (i > 0) {
result += "x";
if (i > 1) {result += "^" + i;}
}
}
return result;
}
public Boolean isNull(){
for(int i = werte.length-1; i >= 0; i--){
if (werte[i] != 0) {
return false;
}
}
return true;
}
public String toString1() {
StringBuilder result = new StringBuilder();
for (int i = degree; i >= 0; i--) {
if (werte[i] == 0) {continue;}
if (!result.isEmpty()) {
if (werte[i] > 0) {result.append(" + ");}
if (werte[i] < 0) {result.append(" - ");}
}
if (result.isEmpty() && werte[i] <0){result.append("-");} // Minus am Anfang
int absCoeff = Math.abs(werte[i]);
if (absCoeff != 1 && i == 0) {result.append(absCoeff);}
if (absCoeff == 1 && i == 0) {result.append(1);}
if (absCoeff != 1 && i != 0) {
result.append(absCoeff);
result.append("*");}
if (i > 0) {
result.append("x");
if (i > 1) {result.append("^");
result.append(i);
}
}
}
return result.toString();
}
public Polynomial add(Polynomial p2){
int[] p1 = getCoeef();
int[] p2Coeff = p2.getCoeef();
int[] p3 = new int[Math.max(p1.length,p2Coeff.length)];
for(int x = p3.length-1; x >= 0; x --){
int coeff1 = (x < p1.length) ? p1[x] : 0;
int coeff2 = (x < p2Coeff.length) ? p2Coeff[x] : 0;
p3[x] = coeff1 + coeff2;
}
Polynomial result = new Polynomial(p3);
return result;
}
public Polynomial sub(Polynomial that) {
int[] res = Arrays.copyOf(werte, Math.max(this.degree, that.degree) + 1);
for (int i = 0; i <= that.degree; i++)
res[i] -= that.werte[i];
return new Polynomial(res);
}
public Polynomial mult(Polynomial that) {
int[] res = new int[this.degree + that.degree + 1];
Arrays.fill(res, 0);
for (int i = 0; i <= this.degree; i++)
for (int j = 0; j <= that.degree; j++)
res[i + j] += this.werte[i] * that.werte[j];
return new Polynomial(res);
}
public Polynomial exp(int e) {
if (e < 0)
throw new IllegalArgumentException("Exponent must not be negative");
else if (e == 0)
return constant(1);
else if (e == 1)
return this;
else if (e % 2 == 0)
return this.mult(this).exp(e / 2);
else
return this.mult(this.exp(e - 1));
}
public Polynomial add(int v) {
int[] res = Arrays.copyOf(werte, werte.length);
res[0] += v;
return new Polynomial(res);
}
public Polynomial sub(int v) {
return this.add(-v);
}
public Polynomial mult(int v) {
int[] res = Arrays.copyOf(werte, werte.length);
for (int j = 0; j < res.length; j++)
res[j] *= v;
return new Polynomial(res);
}
public int apply(int v) {
int res = werte[werte.length - 1];
for (int i = werte.length - 2; i >= 0; i--)
res = res + v * werte[i];
return res;
}
}