You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2004/02/20 07:17:54 UTC
cvs commit: jakarta-commons/math/src/test/org/apache/commons/math/analysis PolynomialFunctionTest.java
psteitz 2004/02/19 22:17:54
Modified: math/src/java/org/apache/commons/math/analysis
PolynomialFunction.java
math/src/test/org/apache/commons/math/analysis
PolynomialFunctionTest.java
Log:
Added degree() method, degree checking in derivative computations, made constructor copy coefficient array, cleaned up javadoc.
Revision Changes Path
1.6 +29 -53 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.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- PolynomialFunction.java 18 Feb 2004 03:24:19 -0000 1.5
+++ PolynomialFunction.java 20 Feb 2004 06:17:54 -0000 1.6
@@ -17,56 +17,50 @@
*/
package org.apache.commons.math.analysis;
-
-
import java.io.Serializable;
+import java.util.Arrays;
+
import org.apache.commons.math.MathException;
/**
- * Represents a Polynomial function.
- * Spline functions map a certain interval of real numbers to real numbers.
- * A cubic spline consists of segments of cubic functions. For this class,
- * polynominal coefficents are used.
- * Arguments outside of the domain cause an IllegalArgumentException.
+ * Represents a polynomial function with real coefficients.
*
* @version $Revision$ $Date$
*/
public class PolynomialFunction implements UnivariateRealFunction, Serializable {
/**
- * The polynominal coefficients.
- * The index represents the coefficients of the polynomail, with
- * index 0 being the absolute coefficient and index N the coefficient
- * for the Nth power.
+ * 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.
*/
private double c[];
/**
- * Construct a function with the given segment delimiters and polynomial
- * coefficients.
+ * Construct a polynomial with the given coefficients
+ *
* @param c polynominal coefficients
*/
public PolynomialFunction(double c[]) {
super();
- // TODO: should copy the arguments here, for safety. This could be a major overhead.
- this.c = c;
+ this.c = new double[c.length];
+ System.arraycopy(c, 0, this.c, 0, c.length);
}
/**
- * Compute the value for the function.
+ * 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>
*
- * @param x the point for which the function value should be computed
+ * @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.
* @see UnivariateRealFunction#value(double)
*/
- public double value(double x) throws MathException {
+ public double value(double x) {
double value = c[0];
@@ -78,7 +72,6 @@
}
-
/**
* Compute the value for the first derivative of the function.
*
@@ -88,10 +81,12 @@
*
* @param x the point for which the first derivative should be computed
* @return the value
- * @throws MathException if the derivative couldn't be computed.
*/
- public double firstDerivative(double x) throws MathException {
+ public double firstDerivative(double x) {
+ if (this.degree() == 0) {
+ return 0;
+ }
double value = c[1];
if ( c.length > 1 ) {
@@ -112,10 +107,12 @@
*
* @param x the point for which the first derivative should be computed
* @return the value
- * @throws MathException if the second derivative couldn't be computed.
*/
- public double secondDerivative(double x) throws MathException {
+ public double secondDerivative(double x) {
+ if (this.degree() < 2) {
+ return 0;
+ }
double value = 2.0 * c[2];
if ( c.length > 2 ) {
@@ -127,33 +124,12 @@
return value;
}
-
- /**
- * local power function using integer powers.
- * <p>The Math.pow() function always returns absolute value,
- * and is a bit 'heavier' since it can handle double values
- * for the exponential value.</p>
- * @param x any double value
- * @param n must be 0 or greater
- * @return x^n (or 0 if n < 0 ).
- * @throws MathException if n < 0.
- */
-// private double pow( double x, int n ) throws MathException {
-// double value = x;
-// if ( n < 0 ) {
-// throw new MathException( "power n must be 0 or greater" );
-// } else if ( n == 0 ) {
-// // x^0 = 1 always.
-// value = 1.0;
-// } else {
-// // only multiply for powers > 1.
-// for (int i=1; i < n; i++) {
-// value *= x;
-// }
-// }
-
-// System.out.println("pow:"+x+"^"+n+"="+value);
-// return value;
-// }
-
+ /**
+ * Returns the degree of the polynomial
+ *
+ * @return the degree of the polynomial
+ */
+ public int degree() {
+ return c.length - 1;
+ }
}
1.5 +18 -13 jakarta-commons/math/src/test/org/apache/commons/math/analysis/PolynomialFunctionTest.java
Index: PolynomialFunctionTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/analysis/PolynomialFunctionTest.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- PolynomialFunctionTest.java 18 Feb 2004 03:24:20 -0000 1.4
+++ PolynomialFunctionTest.java 20 Feb 2004 06:17:54 -0000 1.5
@@ -41,7 +41,7 @@
*/
public void testConstants() throws MathException {
double[] c = { 2.5 };
- UnivariateRealFunction f = new PolynomialFunction( c );
+ PolynomialFunction f = new PolynomialFunction( c );
// verify that we are equal to c[0] at several (nonsymmetric) places
assertEquals( f.value( 0.0), c[0], error );
@@ -49,10 +49,13 @@
assertEquals( f.value( -123.5), c[0], error );
assertEquals( f.value( 3.0), c[0], error );
assertEquals( f.value( 456.89), c[0], error );
+
+ assertEquals(f.degree(), 0);
+ assertEquals(f.firstDerivative(0), 0, error);
+
+ assertEquals(f.secondDerivative(0), 0, error);
}
-
-
/**
* tests the value of a linear polynomial.
*
@@ -64,7 +67,7 @@
*/
public void testLinear() throws MathException {
double[] c = { -1.5, 3.0 };
- UnivariateRealFunction f = new PolynomialFunction( c );
+ PolynomialFunction f = new PolynomialFunction( c );
// verify that we are equal to c[0] when x=0
assertEquals( f.value( 0.0), c[0], error );
@@ -75,6 +78,10 @@
assertEquals( 0.0, f.value( 0.5), error );
assertEquals( 3.0, f.value( 1.5), error );
assertEquals( 7.5, f.value( 3.0), error );
+
+ assertEquals(f.degree(), 1);
+
+ assertEquals(f.secondDerivative(0), 0, error);
}
@@ -86,7 +93,7 @@
*/
public void testQuadratic() throws MathException {
double[] c = { -2.0, -3.0, 2.0 };
- UnivariateRealFunction f = new PolynomialFunction( c );
+ PolynomialFunction f = new PolynomialFunction( c );
// verify that we are equal to c[0] when x=0
assertEquals( f.value( 0.0), c[0], error );
@@ -108,7 +115,7 @@
*/
public void testQuintic() throws MathException {
double[] c = { 0.0, 0.0, 15.0, -13.0, -3.0, 1.0 };
- UnivariateRealFunction f = new PolynomialFunction( c );
+ PolynomialFunction f = new PolynomialFunction( c );
// verify that we are equal to c[0] when x=0
assertEquals( f.value( 0.0), c[0], error );
@@ -119,18 +126,20 @@
assertEquals( 0.0, f.value( -3.0), error );
assertEquals( 54.84375, f.value( -1.5), error );
assertEquals( -8.06637, f.value( 1.3), error );
+
+ assertEquals(f.degree(), 5);
}
/**
- * tests the derivative function by comparision
+ * tests the firstDerivative function by comparision
*
* <p>This will test the functions
* <tt>f(x) = x^3 - 2x^2 + 6x + 3, g(x) = 3x^2 - 4x + 6</tt>
* and <tt>h(x) = 6x - 4</tt>
*/
- public void testDerivativeComparision() throws MathException {
+ public void testfirstDerivativeComparision() throws MathException {
double[] f_coeff = { 3.0, 6.0, -2.0, 1.0 };
double[] g_coeff = { 6.0, -4.0, 3.0 };
double[] h_coeff = { -4.0, 6.0 };
@@ -151,9 +160,5 @@
// compare f'' = h
}
-
-
-
-
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org