I'm working on a project where I have 2 classes, one class for Monomials and another one for polynomials which are built out of monomials. I would like to make a simpler method for adding and multiplying polynomials, other than my naive method.
public class Monomial {
/**
* @post this.getCoefficient() == coefficient
* @post for every v, 'a'<=v<='z', isVariable(v) == false
*/
private int[] exponents=new int[26];
private int ceof;
public Monomial(int coefficient) {
this.ceof=coefficient;
}
public class Polynomial {
/**
* Creates a polynomial with (safe copies of) the given monomials
*
* @pre monomials != null
* @pre for all i, 0 <= i < monmials.length : monomials[i] != null
* @post for all i, 0 <= i < monmials.length : monomials[i].getCoefficient()
* == getMonomial(i).getCoefficient()
* @post for all i,v, 0 <= i < monmials.length, 'a'<=v<='z' :
* monomials[i].getDegree(v) == getMonomial(i).getDegree(v)
*/
private Monomial[] monoms;
public Polynomial(Monomial[] monomials) {
this.monoms=monomials;
}
/**
* @return the number of monomials in this polynomial
*/
public int getMonomialCount() {
return (this.monoms).length;
}
/**
* @pre 0<=index < getMonomialCount()
* @return a safe copy of the monomial at the given index
*/
public Monomial getMonomial(int index) {
Monomial copy = this.monoms[index].getCopy();
return copy;
}
/**
* @pre other != null
* @post Creates a new Polynomial which is the sum of this polynomial and
* other. E.g., the sum of 13b^2x^3z+15 and -4b^2x^3z is
* 13b^2x^3z+15-4b^2x^3z
*/
public Polynomial add(Polynomial other) {
Monomial[] sum=new Monomial[this.monoms.length+other.monoms.length];
boolean stop=false;
int maincounter = 0;
int thiscounter = 0;
int othercounter = 0;
while(stop==false){
if(maincounter<=this.monoms.length-1){
sum[maincounter]= this.monoms[thiscounter].getCopy();
thiscounter++;
maincounter++;
}
else{
sum[maincounter]= other.monoms[othercounter].getCopy();
othercounter++;
maincounter++;
}
if(maincounter>sum.length-1){
stop=true;
}
}
return new Polynomial(sum);
}
/**
* @pre other != null
* @post Creates a new Polynomial which is the product of this polynomial
* and other. E.g., the product of 13b^2x^3z+15 and -4b^2x^3z is
* -52b^4x^6z^2-60b^2x^3z
*/
public Polynomial multiply(Polynomial other) {
int coefficient=0;
int newDegree=0;
Monomial newMonomial;
Monomial thisMonomial;
Monomial otherMonomial;
Monomial[] result=new Monomial[this.getMonomialCount()*other.getMonomialCount()];
for(int i=0;i<this.monoms.length;i++){
thisMonomial=this.monoms[i];
for(int j=0;j<other.monoms.length;j++){
otherMonomial=other.monoms[j];
coefficient=thisMonomial.getCoefficient()*otherMonomial.getCoefficient();
newMonomial=new Monomial(coefficient);
for(int k=0;k<26;k++){
if(thisMonomial.isVariable((char)('a'+k))==true&&otherMonomial.isVariable((char)('a'+k))==true){
newDegree=thisMonomial.getDegree((char)('a'+k))+otherMonomial.getDegree((char)('a'+k));
}
else{
if(thisMonomial.isVariable((char)('a'+k))==true){
newDegree=thisMonomial.getDegree((char)('a'+k));
}
else{
newDegree=otherMonomial.getDegree((char)('a'+k));
}
}
newMonomial.setDegree((char)('a'+k),newDegree);
}
result[i]=newMonomial.getCopy();
}
}
Polynomial Polyresult;
Polyresult=new Polynomial(result);
return Polyresult;
}