Shankha057
Shankha057

Reputation: 1361

Adding two polynomials stored as linked list in java

I have a monomial structure that takes in a co-efficient and the the exponent of three variables as a single number.My monomial class looks like this:

public class Monomial {
private Float coeff;
private Integer exp;
private Integer x,y,z;

public Monomial() {
    this.coeff=null;
    this.x=this.y=this.z=null;
    this.exp=null;
}

public Monomial(Float coeff, Integer x, Integer y, Integer z) {
    this.coeff = coeff;
    this.x = x;
    this.y = y;
    this.z = z;
    this.exp = (100*x)+(10*y)+z;
}

public Monomial(Integer exp) {
    this.exp = exp;
    this.x=(exp-(exp%100))/100;
    this.z = exp%10;
    this.y = ((exp-z)/10)%10;
}

public Monomial(Float coeff, Integer exp) {
    this.coeff = coeff;
    this.exp = exp;
    this.x=(exp-(exp%100))/100;
    this.z = exp%10;
    this.y = ((exp-z)/10)%10;
}

}

My polynomial class is stored as a linked list of monomials.

I want to add 2 polynomials.Here is my addition function

public Polynomial addition(Polynomial a, Polynomial b) {
    LinkedList<Monomial> Main = new LinkedList<>();
    LinkedList<Monomial> temp1 = a.getPolynomial();
    LinkedList<Monomial> temp2 = b.getPolynomial();
    for(int i = 0;i<temp1.size();i++){
        for(int j = 0;j<temp2.size();j++){
            Integer c1= temp1.get(i).getExp();Integer c2 = temp2.get(j).getExp();
            if(c1.equals(c2)){
                Float k1 = temp1.get(i).getCoeff();Float k2 = temp2.get(j).getCoeff();
                Main.add(new Monomial( k1+k2,temp2.get(j).getExp()));
                //temp1.remove(i);temp2.remove(j);
            }
            else if(!c1.equals(c2)){
                Main.add(new Monomial(temp1.get(i).getCoeff(),temp1.get(i).getExp()));
                //temp1.remove(i);
                Main.add(new Monomial(temp2.get(j).getCoeff(),temp2.get(j).getExp()));
                //temp2.remove(j);
            }
        }
    }
    Polynomial ret = new Evaluator(Main);
    return ret;
}

My input looks like this Polynomial1: 10 1 2 3 11 4 5 6 Polynomial2: 12 1 2 3 13 4 5 6

Polynomial 1 can be interpreted as 10x(y^2)(z^3) and so on.

The output that I am getting is: 22.0 1 2 3 10.0 1 2 3 13.0 4 5 6 This is not the desired output.The calculation is not preformed correctly.I know that something is wrong with my loop body but I don't know what is it. I would like to know what went wrong and how to correct it.

Upvotes: 2

Views: 756

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148880

Your algorithm is plain wrong. Just use a paper and a pencil to see what happens: you process n times each monomial which is not what polynomial addition does.

The correct way is to build a method to add a monomial to an existing polynomial and iterate the added polynomial. But that's not all, your trick of calculating a single coeff to compare 3 integer values in a single operation only works if all individual coefficients are in 0-9 range...

Upvotes: 2

Related Questions