commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject cvs commit: jakarta-commons/math/src/java/org/apache/commons/math/analysis PolynomialFunction.java
Date Fri, 02 Apr 2004 21:08:49 GMT
psteitz     2004/04/02 13:08:48

  Modified:    math/src/java/org/apache/commons/math/analysis
                        PolynomialFunction.java
  Log:
  Modified constructor to copy input coefficients array.
  Changed evaluation to use Horner's method.
  Exposed coefficients as read-only property
  Implemented DifferentiableUnivariateRealFunction interfaces
  Dropped redundant firstDerivative, secondDerivative methods
  
  Revision  Changes    Path
  1.8       +98 -71    jakarta-commons/math/src/java/org/apache/commons/math/analysis/PolynomialFunction.java
  
  Index: PolynomialFunction.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/analysis/PolynomialFunction.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- PolynomialFunction.java	22 Feb 2004 22:01:29 -0000	1.7
  +++ PolynomialFunction.java	2 Apr 2004 21:08:48 -0000	1.8
  @@ -20,112 +20,139 @@
   import java.io.Serializable;
   
   /**
  - * Represents a polynomial function with real coefficients.
  + * Immutable representation of a real polynomial function with real coefficients.
  + * <p>
  + * <a href="http://mathworld.wolfram.com/HornersMethod.html">Horner's Method</a>
  + *  is used to evaluate the function.   
    * 
    * @version $Revision$ $Date$
    */
  -public class PolynomialFunction implements UnivariateRealFunction, Serializable {
  +public class PolynomialFunction implements DifferentiableUnivariateRealFunction, Serializable
{
   
       /**
  -     * The coefficients of the polynomial, ordered by degree -- i.e.,  c[0] is the constant
term
  -     * and c[n] is the coefficient of x^n where n is the degree of the polynomial.
  +     * The coefficients of the polynomial, ordered by degree -- i.e.,  coefficients[0]
is the constant term
  +     * and coefficients[n] is the coefficient of x^n where n is the degree of the polynomial.
        */
  -    private double c[];
  +    private double coefficients[];
   
       /**
  -     * Construct a polynomial with the given coefficients
  +     * Construct a polynomial with the given coefficients.
  +     * <p>
  +     * The constructor makes a copy of the input array and assigns the copy to
  +     *  the coefficients property.
        * 
        * @param c polynominal coefficients
  +     * @throws NullPointerException if c is null
  +     * @throws IllegalArgumentException if c is empty
        */
       public PolynomialFunction(double c[]) {
           super();
  -        this.c = new double[c.length];
  -        System.arraycopy(c, 0, this.c, 0, c.length);
  +        if (c.length < 1) {
  +            throw new IllegalArgumentException("Polynomial coefficient array must have
postive length.");
  +        }
  +        this.coefficients = new double[c.length];
  +        System.arraycopy(c, 0, this.coefficients, 0, c.length);
       }
   
       /**
        * Compute the value of the function for the given argument.
  -     *
  -     * <p>This can be explicitly determined by 
  -     *   <tt>c_n * x^n + ... + c_1 * x  + c_0</tt>
  -     * </p>
  +     * <p>
  +     *  The value returned is <br>
  +     *   <code>coefficients[n] * x^n + ... + coefficients[1] * x  + coefficients[0]</code>
        *
        * @param x the argument for which the function value should be computed
  -     * @return the value
  -     * @throws MathException if the function couldn't be computed due to
  -     *  missing additional data or other environmental problems.
  +     * @return the value of the polynomial at the given point
        * @see UnivariateRealFunction#value(double)
        */
  -    public double value(double x)  {
  -
  -        double value = c[0];
  -
  -        for (int i=1; i < c.length; i++ ) {
  -            value += c[i] * Math.pow( x, (int)i);
  -        }
  -
  -        return value;
  +    public double value(double x) {
  +       return evaluate(coefficients, x);
       }
   
   
       /**
  -     * Compute the value for the first derivative of the function.
  -     *
  -     * <p>This can be explicitly determined by 
  -     *   <tt>n * c_n * x^(n-1) + ... + 2 * c_2 * x  + c_1</tt>
  -     * </p>
  -     *
  -     * @param x the point for which the first derivative should be computed
  -     * @return the value
  +     *  Returns the degree of the polynomial
  +     * 
  +     * @return the degree of the polynomial
        */
  -    public double firstDerivative(double x)  {
  -
  -        if (this.degree() == 0) {
  -            return 0;
  +    public int degree() {
  +        return coefficients.length - 1;
  +    }
  +    
  +    /**
  +     * Returns a copy of the coefficients array.
  +     * <p>
  +     * Changes made to the returned copy will not affect the coefficients of
  +     * the polynomial.
  +     * 
  +     * @return  a fresh copy of the coefficients array
  +     */
  +    public double[] getCoefficients() {
  +        double[] out = new double[coefficients.length];
  +        System.arraycopy(coefficients,0, out, 0, coefficients.length);
  +        return out;
  +    }
  +    
  +    /**
  +     * Uses Horner's Method to evaluate the polynomial with the given coefficients at
  +     * the argument.
  +     * 
  +     * @param coefficients  the coefficients of the polynomial to evaluate
  +     * @param argument  the input value
  +     * @return  the value of the polynomial 
  +     * @throws IllegalArgumentException if coefficients is empty
  +     * @throws NullPointerException if coefficients is null
  +     */
  +    protected static double evaluate(double[] coefficients, double argument) {
  +        int n = coefficients.length;
  +        if (n < 1) {
  +            throw new IllegalArgumentException("Coefficient array must have positive length
for evaluation");
           }
  -        double value = c[1];
  -
  -        if ( c.length > 1 ) {
  -            for (int i=2; i < c.length; i++ ) {
  -                value += i * c[i] * Math.pow( x, (int)i-1);
  -            }
  +        double result = coefficients[n - 1];
  +        for (int j = n -2; j >=0; j--) {
  +            result = argument * result + coefficients[j];
           }
  -
  -        return value;
  +        return result;
       }
  -
  +    
       /**
  -     * Compute the value for the second derivative of the function.
  -     * 
  -     * <p>This can be explicitly determined by 
  -     *   <tt>n * (n-1) * c_n * x^(n-2) + ... + 3 * 2 * c_3 * x  + 2 * c_2</tt>
  -     * </p>
  +     * Returns the coefficients of the derivative of the polynomial with the given coefficients.
        * 
  -     * @param x the point for which the first derivative should be computed
  -     * @return the value
  -     */
  -    public double secondDerivative(double x)  {
  -
  -        if (this.degree() < 2) {
  -            return 0;
  +     * @param coefficients  the coefficients of the polynomial to differentiate
  +     * @return the coefficients of the derivative or null if coefficients has length 1.
  +     * @throws IllegalArgumentException if coefficients is empty
  +     * @throws NullPointerException if coefficients is null
  +     */
  +    protected static double[] differentiate(double[] coefficients) {
  +        int n = coefficients.length;
  +        if (n < 1) {
  +            throw new IllegalArgumentException("Coefficient array must have positive length
for differentiation");
           }
  -        double value = 2.0 * c[2];
  -
  -        if ( c.length > 2 ) {
  -            for (int i=3; i < c.length; i++ ) {
  -                value += i * (i-1) * c[i] * Math.pow( x, (int)i-2);
  -            }
  +        if (n == 1) {
  +            return new double[]{0};
           }
  -
  -        return value;
  +        double[] result = new double[n - 1];
  +        for (int i = n - 1; i  > 0; i--) {
  +            result[i - 1] = (double) i * coefficients[i];
  +        }
  +        return result;
       }
  -
  +    
       /**
  -     *  Returns the degree of the polynomial
  +     * Returns the derivative as a PolynomialRealFunction
        * 
  -     * @return the degree of the polynomial
  +     * @return  the derivative polynomial
        */
  -    public int degree() {
  -        return c.length - 1;
  +    public PolynomialFunction polynomialDerivative() {
  +        return new PolynomialFunction(differentiate(coefficients));
  +    }
  +    
  +    /**
  +     * Returns the derivative as a UnivariateRealFunction
  +     * 
  +     * @return  the derivative function
  +     */
  +    public UnivariateRealFunction derivative() {
  +        return polynomialDerivative();
       }
  +   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message