1. Member
Join Date
Apr 2012
Posts
9
Rep Power
0

## work to do about Polynomial

Hello Iam starting learn java. I have a work to do about Polynomial and Iam with a lot difficults.

Can someone help me?

In this project I have to define a class designated Polynimial.

The objects in this class have to be immutable.
They ask me by the next public methods:

Polynomial (Fraction[] coefs - To creat a new polynomial with the coefs given. E.g.: 3x^2-2x/3+4 is represented by the vector [3,-2/3,4]. Assume that the first value of the vector (greatest coef) is differet of 0.

int degree() -- returns the degree of the polynomial

- Zero boolean () - checks if the polynomial is the zero polynomial.
• Polynomial scalar (Fraction f) - returns the polynomial multiplication by a scalar.
• Symmetric Polynomial () - returns the symmetric polynomial (eg, 3x ^ 2-2x / 3 - 4 is symmetric 3x ^ 2 - 2x / 3 4).
• Polynomial sum (Polynomial p) - returns the sum of the polynomial with the polynomial p given.
• Polynomial subtract (Polynomial p) - returns the subtraction of the polynomial with the polynomial p given.
• product polynomial (Polynomial p) - returns the product polynomial as the polynomial p.
• Polynomial quotient (Polynomial p) - returns the quotient of dividing the polynomial with the given polynomial p (p is the divisor).
given the polynomial p (p is the divisor).

• Polynomial remainder (Polynomial p) - returns the remainder after division of the polynomial with the given polynomial p (p is the divisor).
• apply Fraction (Fraction x) - returns the result of assigning the value of the given fraction of the polynomial variable (eg, x ^ 2 + 1/2 with x = 5 results in 51/2)
• Polynomial copy () - returns a copy of the polynomial
• String toString () - returns a description of the polynomial. Some examples of how this function should work:
1 + x 2 ^ 3 ^ 2 + x 3 + x 4
-1 / 2 x ^ 4 -56 / 5 x ^ 2 -4
-1 + A 5 x ^ x ^ 3 + 1 x -1
0
• static Polynomial zero () - This class method returns the zero polynomial.

It's my first java programme and Iam trying to do it alone. And I have to deliver the work until Sunday.

Sorry if my english is bad. :P
Last edited by Norm; 04-14-2012 at 03:35 AM. Reason: Move post to own thread

3. Member
Join Date
Apr 2012
Posts
9
Rep Power
0

## Re: work to do about Polynomial

I dont know how to do it:P Becouse Iam starting learn Java.
So I need help in all polynomial project. Can you help me?

4. Member
Join Date
Apr 2012
Posts
9
Rep Power
0

## Re: work to do about Polynomial

Can you correct this:
Java Code:
```public class Polynomial {

}

private final Fraction[]pol;

// coeficientes do polinomio.

/**
*  criar um novo polinomio com os coeficientes dados. assuma que o primeiro valor do vector é diferente de zero.
*/

public Polynomial(Fraction[] coefs){
pol=new Fraction[coefs.length];
for(int i=0; i<pol.length;i++);
// o indice do vector é igual ao grau associado a cada coeficiente.
pol[i]=coefs[coefs.lenght-i-1];

}

/**
* devolve o grau do polinomio.
*/

public int degree(){
int a=0
for int (i=0;i<this.pol.lenght;i++);
if((this.pol[i].isZero())==false);

a=i

return a;

}

/**
* verifica se o polinomio e o polinomio zero.
*/

public boolean isZero (){
for(int i=0;i<pol.length;i++){
if(pol[i].isZero()==false)
// verifica se existem coeficientes diferentes de zero.
return false;
}
else
return true;

}
}

/**
* devolve a multiplicacao do polinomio por um escalar.
*/

public Polynomial scalar(Fraction f){
Fraction []v=new Fraction[pol.length];
for (int i=0; i<pol.length;i++){
v[i]=pol[pol.length-i-1].product(f);
// multiplica todos os coeficientes pelo escalar
}
return new Polynomial(v);
}

/**
* devolve o polinomio simetrico.
*/

public Polynomial symmetric(){
return scalar(new Fraction (-1,1));
// multiplica todo o polinomio por -1.
}
}

/**
* devolve a soma do polinomio com o polinomio p dado.
*/

public Polynomial sum(Polynomial p){
Fraction[]v=new Fraction[Math.max(this.pol.length, p.pol.lenght)]
// grau do polinomio e o grau do polinomio de maior grau.
for (int i=0;i<v.length;i++){
if(i<Math.min(pol.length, p.pol.length));
v[v.length-i-1]=pol[i].sum(p.pol[i]);
// soma os indices iguais, se existirem nos dois polinomios.

else {
if(p.degree()<degree());
// se o grau de p for menor que as entradas de v sao substituidas pelas de this.pol
v[v.length-i-1]=pol[i];

else{
if(p.degree()>degree());
// se o grau de p for maior que as entradas de v sao substituidas pelas de p.pol
v[v.length-i-1]=p.pol[i];

else

// as entradas de v sao preenchidas com zero senao acontecer nenhum dos casos anteriores.
v[v.length-i-1]=new Fraction(0,1)
}
}
}
return new Polynomial (v);
}

/**
* devolve a subtracao do polinomio com o polinomio p dado.
*/

public Polynomial subtract(Polynomial p){
return this.sum(p.symmetric());
// subtrair é somar o simétrico.

}

/**
* devolve o produto do polinomio com o polinomio p dado.
*/

public Polynomial product(Polynomial p){
Fraction[]k=new Fraction[this.degree()+p.degree(+1)]
// o grau do polinomio é a soma do grau dos polinomios.
for(int i=0;i<k.length;i++){
k[k.length-i-1]=new Fraction(0,1);
for(int j=0;j<this.pol.length;j++){
for(int i=0;i<p.pol.length;i++){
if(i==j+1){
k[k.length-i-1]=k[k.length-i-1].sum(this.pol(j).product(p.pol[i]));
// o produto dos coeficientes fica com grau igual à soma dos graus associados a esse coeficiente.
}
}
}
}
return new Polynomial(k);
}

/**
* devolve o quociente da divisao do polinomio com o polinomio p dado (p é o divisor).
*/

public Polynomial quocient (Polynomial p){
Fraction[]h= new Fraction[Math.max(this.degree()-p.degree(),p.degree()-this.degree())+1];
// o grau do polinomio e a diferença dos graus.
Fraction []n=new Fraction[this.pol.length];
for(int i=0;i<this.pol.length;i++)
n[n.length-i-1]=this.pol[i];
Polynomial c=new Polynomial(n);
// cria um polinomio igual ao polinomio this
for(int i=0;i<h.length;i++)
h[i]=new Fraction(0,1);
Fraction []w=new Fraction[Math.max(this.degree()-p.degree(), p.degree()-this.degree())+1];
for(int i=0;i<h.length;i++){
if(c.degree()>=p.degree()){
h[h.length-1-(c.degree(-p.degree())]=c.pol[c.degree()].divison(p.pol[p.degree()]);
for(int j=0;j<w.length;j++){
if(h.length-1-(c.degree()-p-degree())!=j)
w[j]= new Fraction(0,1);
else
w[h.length-1-(c.degree()-p.degree())]=h[h.length-1-(c.degree()-p.degree()))];

}

Polynomial s= new Polynomial(w);
// polinomial auxiliar
c=c.subtract(s.product(p));
// substitui o polinomio c pelo resta da ultima divisao, de modo a voltar a dividi-lo pelo divisor se o seu grau for maior do que o divisor.
}

}

return new Polynomial(h);

}

/**
* devolve o resto da divisao do polinomio com o polinomio p dado (p e divisor).
*/

public Polynomial remainder(Polynomial p){
return this.subtract(p.product(this.quotiente(p)));

}

/**
* devolve o resultado de atribuir o valor da fracçao dada a variavel do polinomio.
*/

public Fraction apply(Fraction x){
for(int I=0;i<this.pol.length;i++){
r=r.sum(pol[i].product(x.potential(i));
}
return r;
}

/**
* devolve um copia do polinomio.
*/

public Polynomial copy (){
Fraction []v=new Fraction[pol.length];
for(i=0);i<pol.length;i++)
v[i]=pol[pol.length-i-1];
// cria um vector igual a pol com a ordem das entradas invertidas, de modo a criar um novo polinomio igual.
return new Polynomial (v);

}

/**
* devolve uma descriçao do polinomio.
*/

public String toString(){
String a="";
if(degree()==1){
a+=pol[1]+"x";
if(pol[0].isZero()==false){
if(pol[0].isPositive())
a+="+"+pol[0];
else
a+=pol[0];
}
}
if(degree()==0)
a+=pol[0];
if (degree()<1){
a+=pol[degree()]+"x^"+degree();
for(int i=degree()-1;i>1;i--)
if(pol[i].isZero()==false){
if(pol[i].isPositive())
a+="+"+pol[i]+"x^"+i;
else
a+=pol[i]+"x^"+i;
}
if(pol[1].isZero()==false){
if(pol[1].isPositive())
a+="+a"+pol[1]+"x";
else
a+=pol[1]+"x";

}

if(pol[0].isZero()==false){
if(pol[0].isPositive())
a+="x"+pol[0];
else
a+=pol[0]
}
}
return a;
}

/**
* este metodo de classe devolve o polinomio zero.
*/

public static Polynomial zero(){
Fraction[] z=new Fraction[1];
z[0]=new Fraction(0,1);
return new Polynomial(z);

}```
Last edited by Norm; 04-14-2012 at 07:47 PM. Reason: added code tags

5. ## Re: work to do about Polynomial

Why are you using an array to hold the fractions (terms) of the polynomial? e.g. the polynomial x^10000+1 wastes a lot of space. I'd suggest using a Map<Integer, Fraction> instead where the key is the exponent and the associated value is the coefficient of the polynomial, but first you have to implement a proper Fraction class; it has to implement the basic arithmetic (+, -, * and /) operations. Only then you can implement a Polynomial class.

kind regards,

Jos

6. Member
Join Date
Apr 2012
Posts
9
Rep Power
0

## Re: work to do about Polynomial

And How I do that?
I tried to read everything here to help me in my assignment.
But its more hard that I imagined.
I asked my friends to help me but they can help me becouse they are busy.

Can you write how I have to do?
What I have to use?

7. ## Re: work to do about Polynomial

Java Code:
```public class Fraction {
private int n, d;

private static int gcd(int a, int b) { ... }

public Fraction(int n, int d) { ... }

public Fraction add(Fraction f) { ... }

public Fraction sub(Fraction f) { ... }

public Fraction mul(Fraction f) { ... }

public Fraction div(Fraction f) { ... }

public String toString() { ... }

public boolean equals(Object o) { ... }

public int hashCode() { ... }
}```
you fill in the dots.

kind regards,

Jos

8. Member
Join Date
Apr 2012
Posts
9
Rep Power
0

## Re: work to do about Polynomial

And then how I introduce the polynomial?

9. ## Re: work to do about Polynomial

Something like this:

Java Code:
```public class Polynomial {
private Map<Integer, Fraction> terms= new TreeMap<Integer, Fraction>();

public Polynomial() { ... }

public Polynomial(Polynomial p) { ... }

...
}```
kind regards,

Jos

10. Member
Join Date
Apr 2012
Posts
9
Rep Power
0

## Re: work to do about Polynomial

Thank you for you help.
And sorry becouse I dont understand anything about Java :P

11. Member
Join Date
Apr 2012
Posts
9
Rep Power
0

## Re: work to do about Polynomial

Can you help me here?
Its given some errors

here:

//x^2+(1/2)
public Fraction apply(Fraction x){
Fraction y=new Fraction(0,1);
for(int i=0;i<this.pol.length;i++){
y=y.sum(pol[i].product(x.potential(i)));
}
return y;
}

It says that the method pol(int) is undefined for the type Polynomial.

And here:

public Polynomial product(Polynomial p){
Fraction[] k = new Fraction[this.degree()+p.degree()+1];
// o grau do polinomio é a soma do grau dos polinomios.
for(int i=0;i<k.length;i++){
k[k.length-i-1]=new Fraction(0,1);
for(int j=0;j<this.pol.length;j++){
for(int w=0;w<p.pol.length;w++){
if(i==j+w){
k[k.length-i-1]=k[k.length-i-1].sum(this.pol(j).product(p.pol[w]));

Says that the method potential(int) is undefined for the type Fraction.

Can you say what i have to change for be right?

12. ## Re: work to do about Polynomial

Originally Posted by Joana
Can you help me here?
Its given some errors

here:

//x^2+(1/2)
public Fraction apply(Fraction x){
Fraction y=new Fraction(0,1);
for(int i=0;i<this.pol.length;i++){
y=y.sum(pol[i].product(x.potential(i)));
}
return y;
}

It says that the method pol(int) is undefined for the type Polynomial.
I don't see any call to method pol( ... ) with an int parameter in that piece of code; maybe I'm blind ...

Originally Posted by Joana
And here:

public Polynomial product(Polynomial p){
Fraction[] k = new Fraction[this.degree()+p.degree()+1];
// o grau do polinomio é a soma do grau dos polinomios.
for(int i=0;i<k.length;i++){
k[k.length-i-1]=new Fraction(0,1);
for(int j=0;j<this.pol.length;j++){
for(int w=0;w<p.pol.length;w++){
if(i==j+w){
k[k.length-i-1]=k[k.length-i-1].sum(this.pol(j).product(p.pol[w]));

Says that the method potential(int) is undefined for the type Fraction.

Can you say what i have to change for be right?
And here I don't see any method call to potential( .... ); I must definitely be blind.

kind regards,

Jos

13. Member
Join Date
Apr 2012
Posts
9
Rep Power
0

## Re: work to do about Polynomial

Ok.
You are not blind.
How I create that method?

14. Member
Join Date
Apr 2012
Posts
9
Rep Power
0

## Re: work to do about Polynomial

This exists?

for(int i=0;i<pol.length; ){
Or is correct?

15. ## Re: work to do about Polynomial

Originally Posted by Joana
This exists?

for(int i=0;i<pol.length; ){
Or is correct?
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

db

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•