vec_math
Class Polynom

java.lang.Object
  extended by vec_math.Polynom
All Implemented Interfaces:
Serializable, Derivative, Function

public class Polynom
extends Object
implements Serializable, Derivative

A representation of a polynomial of the form

   p(x) = ∑aixi
   
This class provides methods for setting the coefficients and evaluating the polynomial at a given value for x. Manipulation of the polynom is not intended, but may be added in subclasses.

See Also:
Serialized Form

Field Summary
private  double[] a
          The constants of the polynom.
private  double first
          The value of the 1st derivative of the polynom at the last evaluation.
private  double oldx
          The last x value the polynom was evaluated for.
private  double val
          The value of the polynom at the last evaluation.
 
Constructor Summary
Polynom()
          Constructs a new polynom.
Polynom(double[] coef)
          Constructs a new polynom.
Polynom(String coef)
          Constructs a new polynom.
 
Method Summary
private  void calculate(double x)
          Evaluates the polynom and its first derivative simultaneously.
 double derivative(double x)
          Returns the value of the first derivative of the polynom at the given value x.
 double evaluate(double x)
          Returns the value of the polynom for the given value of x.
 double[] getCoefficients()
          Returns the coefficients.
 int getDegree()
          Returns the degree of the polynom.
 boolean setCoefficients(double[] coef)
          Sets the polynomial coefficients.
 boolean setCoefficients(String comma)
          Sets the polynomial coefficients from a comma-separated list of doubles.
 String toString()
          Returns a string description of the polynom.
 boolean valid()
          Returns true if the coefficients of this polynom has been set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

a

private double[] a
The constants of the polynom.


oldx

private double oldx
The last x value the polynom was evaluated for.


val

private double val
The value of the polynom at the last evaluation.


first

private double first
The value of the 1st derivative of the polynom at the last evaluation.

Constructor Detail

Polynom

public Polynom()
Constructs a new polynom. The polynomial coefficients are not set.


Polynom

public Polynom(String coef)
Constructs a new polynom. The polynomial coefficients are initialized with the argument string.


Polynom

public Polynom(double[] coef)
Constructs a new polynom. The polynomial coefficients are initialized with the argument array.

Method Detail

setCoefficients

public boolean setCoefficients(double[] coef)
Sets the polynomial coefficients.

Parameters:
coef - The coefficients of the polynom as a double array.

setCoefficients

public boolean setCoefficients(String comma)
Sets the polynomial coefficients from a comma-separated list of doubles. The coeffivcient with index zero is the leftmost double value. The degree of the polynomial follows from the number of double values parsable.
If parsing fails somewhere the entire sequence is marked invalid.

Parameters:
comma - A comma-separated striong holding the coefficients.

getCoefficients

public double[] getCoefficients()
Returns the coefficients. If the coefficients have not been set earlier null is returned.


getDegree

public int getDegree()
Returns the degree of the polynom. If the coefficients are invalid -1 is returned. Note that the number of coefficients present is the degree of the polynom augmented by one.


valid

public boolean valid()
Returns true if the coefficients of this polynom has been set.


evaluate

public double evaluate(double x)
Returns the value of the polynom for the given value of x.

Specified by:
evaluate in interface Function

derivative

public double derivative(double x)
Returns the value of the first derivative of the polynom at the given value x.

Specified by:
derivative in interface Derivative
Returns:
dp(x)/dx

toString

public String toString()
Returns a string description of the polynom. The coefficients are listed comma-separated, starting with the constant coefficient.

Overrides:
toString in class Object

calculate

private void calculate(double x)
Evaluates the polynom and its first derivative simultaneously. If the coefficients of the polynom are not set NaN is used.