common
Class Polynomial

java.lang.Object
  extended by common.Polynomial

public class Polynomial
extends java.lang.Object

Provides an implementation for a univariate polynomial P(x).

Author:
vohuudtr

Constructor Summary
Polynomial(java.math.BigInteger[] coefficients)
          Constructing the univariate polynomial with given coefficients in Java BigInteger type.
Polynomial(int highestDegree)
          Constructing the univariate polynomial with a given highest degree that the polynomial can have.
Polynomial(int[] coefficients)
          Constructing the univariate polynomial with given coefficients in integer type.
 
Method Summary
 Polynomial add(Polynomial p)
          Returns a new univariate polynomial as the sum of this polynomial and another polynomial.
static Polynomial createLagrange(int[] x, int i)
          Creates a univariate polynomial in Lagrange form.
 int degree()
          Returns the polynomial degree.
 java.math.BigInteger getCoefficient(int degree)
          Returns coefficient corresponding the given degree.
 java.math.BigInteger[] getCoefficients()
          Returns all coefficients of the polynomial.
static void main(java.lang.String[] args)
          For testing purpose.
 Polynomial mod(java.math.BigInteger m)
          Returns a new univariate polynomial by mod-ing all coefficients of this polynomial by a given modulus.
 Polynomial multiply(java.math.BigInteger k)
          Returns a new univariate polynomial by multiplying this polynomial with a scalar value in Java BigInteger type.
 Polynomial multiply(int k)
          Returns a new univariate polynomial by multiplying this polynomial with a scalar value in integer type.
 Polynomial multiply(Polynomial p)
          Returns a new univariate polynomial by multiplying this polynomial with another polynomial.
 Polynomial negate()
          Returns a new univariate polynomial by negating this polynomial.
 Polynomial pow(int exponent)
          Returns a new univariate polynomial as an exponentiation of this polynomial.
 void setCoefficient(int degree, java.math.BigInteger coeff)
          Sets coefficient at a specified degree with given value in Java BigInteger type.
 void setCoefficient(int degree, int coeff)
          Sets coefficient of a specified degree with given value in integer type.
 Polynomial subtract(Polynomial p)
          Returns a new univariate polynomial as the result of subtracting this polynomial by another polynomial.
 java.lang.String toString()
          Returns a string representing this polynomial.
 java.math.BigInteger value(java.math.BigInteger x)
          Evaluates the polynomial at a given point.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Polynomial

public Polynomial(int highestDegree)
Constructing the univariate polynomial with a given highest degree that the polynomial can have. At initialization, all coefficients are set to 0, and the polynomial degree is 0.

Parameters:
highestDegree - the highest degree.

Polynomial

public Polynomial(java.math.BigInteger[] coefficients)
Constructing the univariate polynomial with given coefficients in Java BigInteger type. The polynomial degree is determined based on the values of the given coefficients.

Parameters:
coefficients - given coefficients.

Polynomial

public Polynomial(int[] coefficients)
Constructing the univariate polynomial with given coefficients in integer type. The polynomial degree is determined based on the values of the given coefficients.

Parameters:
coefficients - given coefficients.
Method Detail

degree

public int degree()
Returns the polynomial degree.

Returns:
the polynomial degree.

getCoefficient

public java.math.BigInteger getCoefficient(int degree)
Returns coefficient corresponding the given degree.

Parameters:
degree - degree of the queried coefficient.
Returns:
the queried coefficient. If the queried degree is greater than the polynomial degree, 0 is returned.

getCoefficients

public java.math.BigInteger[] getCoefficients()
Returns all coefficients of the polynomial.

Returns:
array of all coefficients.

setCoefficient

public void setCoefficient(int degree,
                           java.math.BigInteger coeff)
Sets coefficient at a specified degree with given value in Java BigInteger type.

Parameters:
degree - specified degree.
coeff - new value for the coefficient.

setCoefficient

public void setCoefficient(int degree,
                           int coeff)
Sets coefficient of a specified degree with given value in integer type.

Parameters:
degree - specified degree.
coeff - new value for the coefficient.

value

public java.math.BigInteger value(java.math.BigInteger x)
Evaluates the polynomial at a given point.

Parameters:
x - given value x.
Returns:
P(x).

add

public Polynomial add(Polynomial p)
Returns a new univariate polynomial as the sum of this polynomial and another polynomial.

Parameters:
p - another polynomial.
Returns:
sum of the polynomials.

subtract

public Polynomial subtract(Polynomial p)
Returns a new univariate polynomial as the result of subtracting this polynomial by another polynomial.

Parameters:
p - another polynomial.
Returns:
subtraction of this polynomial by another polynomial.

negate

public Polynomial negate()
Returns a new univariate polynomial by negating this polynomial.

Returns:
the negation of this polynomial.

multiply

public Polynomial multiply(java.math.BigInteger k)
Returns a new univariate polynomial by multiplying this polynomial with a scalar value in Java BigInteger type.

Parameters:
k - scalar value.
Returns:
result of the multiplication.

multiply

public Polynomial multiply(int k)
Returns a new univariate polynomial by multiplying this polynomial with a scalar value in integer type.

Parameters:
k - scalar value.
Returns:
result of the multiplication.

multiply

public Polynomial multiply(Polynomial p)
Returns a new univariate polynomial by multiplying this polynomial with another polynomial. The multiplication is done among coefficients of two polynomials.

Parameters:
p - another polynomial.
Returns:
result of the multiplication.

pow

public Polynomial pow(int exponent)
Returns a new univariate polynomial as an exponentiation of this polynomial.

Parameters:
exponent - desired exponent.
Returns:
result of the exponentiation.

mod

public Polynomial mod(java.math.BigInteger m)
Returns a new univariate polynomial by mod-ing all coefficients of this polynomial by a given modulus.

Parameters:
m - the modulus.
Returns:
result of the mod operation.

toString

public java.lang.String toString()
Returns a string representing this polynomial.

Overrides:
toString in class java.lang.Object

createLagrange

public static Polynomial createLagrange(int[] x,
                                        int i)
Creates a univariate polynomial in Lagrange form. \f$l_i(x) = \prod_{m \ne i} \frac{x-x_m}{x_i-x_m}\f$

Parameters:
x - array of all points \f$x_m\f$.
i - index \f$i\f$ of \f$x_i\f$.

main

public static void main(java.lang.String[] args)
For testing purpose.

Parameters:
args -