Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplying polynomial by constant in Java

Tags:

java

math

I'm having some problems with multiplying a Polynomial by a constant (double). It works when theres only one coefficient but when more then one is there, it gives a ArrayIndexOutOfBounds error and points to the setCoefficient method. Any help? Thanks

public class Poly {
    private float[] coefficients;

    public Poly() {
        coefficients = new float[1];
        coefficients[0] = 0;
    }

    public Poly(int degree) {
        coefficients = new float[degree+1];
        for (int i = 0; i <= degree; i++)
            coefficients[i] = 0;
    }

    public Poly(float[] a) {
        coefficients = new float[a.length];
        for (int i = 0; i < a.length; i++)
            coefficients[i] = a[i];
    }

    public int getDegree() {
        return coefficients.length-1;
    }

    public float getCoefficient(int i) {
        return coefficients[i];
    }

    public void setCoefficient(int i, float value) {
        coefficients[i] = value;
    }

    public Poly add(Poly p) {
        int n = getDegree();
        int m = p.getDegree();
        Poly result = new Poly(Poly.max(n, m));
        int i;
        for (i = 0; i <= Poly.min(n, m); i++) 
            result.setCoefficient(i, coefficients[i] + p.getCoefficient(i));
        if (i <= n) {
            //we have to copy the remaining coefficients from this object
            for ( ; i <= n; i++) 
                result.setCoefficient(i, coefficients[i]);
        } else {
            // we have to copy the remaining coefficients from p
            for ( ; i <= m; i++) 
                result.setCoefficient(i, p.getCoefficient(i));
        }
        return result;
    }

    public void displayPoly () {
        for (int i=0; i < coefficients.length; i++)
            System.out.print(" "+coefficients[i]);
        System.out.println();
    }

    private static int max (int n, int m) {
        if (n > m)
            return n;
        return m;
    }

    private static int min (int n, int m) {
        if (n > m)
            return m;
        return n;
    }

    public Poly multiplyCon (double c){
        int n = getDegree();
        Poly results = new Poly();
        // can work when multiplying only 1 coefficient
        for (int i =0; i <= coefficients.length-1; i++){
                // errors ArrayIndexOutOfBounds for setCoefficient
            results.setCoefficient(i, (float)(coefficients[i] * c)); 
        }
        return results;
    }
}
like image 693
John Smith Avatar asked Sep 19 '11 19:09

John Smith


People also ask

Can we multiply float and int in Java?

This is not possible.

Can we multiply long and int in Java?

In the above example, we have taken the following two integers. int val1 = 9898; int val2 = 6784; Now we will cast and multiply them to a long.


2 Answers

Replace Poly results = new Poly(); with Poly results = new Poly(n);.

like image 94
Kerrek SB Avatar answered Oct 23 '22 08:10

Kerrek SB


I believe you should replace in multiplyCon method

Poly results = new Poly();

with

Poly results = new Poly(n);

Poly default constructor creates array with only one coefficient, which explains why multiplying a one-coefficient Polynomial works.

like image 4
Andrei LED Avatar answered Oct 23 '22 09:10

Andrei LED