You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2012/08/18 20:10:35 UTC
svn commit: r1374631 [1/2] - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math3/analysis/function/
test/java/org/apache/commons/math3/analysis/function/
Author: luc
Date: Sat Aug 18 18:10:34 2012
New Revision: 1374631
URL: http://svn.apache.org/viewvc?rev=1374631&view=rev
Log:
the regular function objects (Acos, Sqrt, ...) now implement the new UnivariateDifferentiable interface.
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Acos.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Acosh.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Asin.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Asinh.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Atan.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Atanh.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Cbrt.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Constant.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Cos.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Cosh.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Exp.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Expm1.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Gaussian.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/HarmonicOscillator.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Identity.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Inverse.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Log.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Log10.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Log1p.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Logistic.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Logit.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Minus.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Power.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sigmoid.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sin.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sinc.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sinh.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sqrt.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Tan.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Tanh.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/function/GaussianTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/function/HarmonicOscillatorTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/function/LogisticTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/function/LogitTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/function/SigmoidTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/function/SincTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/function/SqrtTest.java
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Acos.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Acos.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Acos.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Acos.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -27,19 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Acos implements DifferentiableUnivariateFunction {
+public class Acos implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.acos(x);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return -1 / FastMath.sqrt(1 - x * x);
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.acos();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Acosh.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Acosh.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Acosh.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Acosh.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -27,19 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Acosh implements DifferentiableUnivariateFunction {
+public class Acosh implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.acosh(x);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return 1 / FastMath.sqrt(x * x - 1);
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.acosh();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Asin.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Asin.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Asin.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Asin.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -27,19 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Asin implements DifferentiableUnivariateFunction {
+public class Asin implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.asin(x);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return 1 / FastMath.sqrt(1 - x * x);
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.asin();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Asinh.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Asinh.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Asinh.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Asinh.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -27,19 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Asinh implements DifferentiableUnivariateFunction {
+public class Asinh implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.asinh(x);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return 1 / FastMath.sqrt(1 + x * x);
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.asinh();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Atan.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Atan.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Atan.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Atan.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -27,19 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Atan implements DifferentiableUnivariateFunction {
+public class Atan implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.atan(x);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return 1 / (1 + x * x);
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.atan();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Atanh.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Atanh.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Atanh.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Atanh.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -27,19 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Atanh implements DifferentiableUnivariateFunction {
+public class Atanh implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.atanh(x);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return 1 / (1 - x * x);
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.atanh();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Cbrt.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Cbrt.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Cbrt.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Cbrt.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -27,19 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Cbrt implements DifferentiableUnivariateFunction {
+public class Cbrt implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.cbrt(x);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return 1 / (3 * FastMath.cbrt(x * x));
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.cbrt();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Constant.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Constant.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Constant.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Constant.java Sat Aug 18 18:10:34 2012
@@ -18,6 +18,10 @@
package org.apache.commons.math3.analysis.function;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.FunctionUtils;
+import org.apache.commons.math3.analysis.UnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
/**
* Constant function.
@@ -25,7 +29,7 @@ import org.apache.commons.math3.analysis
* @since 3.0
* @version $Id$
*/
-public class Constant implements DifferentiableUnivariateFunction {
+public class Constant implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** Constant. */
private final double c;
@@ -41,8 +45,19 @@ public class Constant implements Differe
return c;
}
- /** {@inheritDoc} */
- public DifferentiableUnivariateFunction derivative() {
- return new Constant(0);
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
+ public UnivariateFunction derivative() {
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return new DerivativeStructure(t.getFreeParameters(), t.getOrder(), c);
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Cos.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Cos.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Cos.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Cos.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -27,19 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Cos implements DifferentiableUnivariateFunction {
+public class Cos implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.cos(x);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return -FastMath.sin(x);
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.cos();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Cosh.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Cosh.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Cosh.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Cosh.java Sat Aug 18 18:10:34 2012
@@ -18,6 +18,10 @@
package org.apache.commons.math3.analysis.function;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.FunctionUtils;
+import org.apache.commons.math3.analysis.UnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -26,14 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Cosh implements DifferentiableUnivariateFunction {
+public class Cosh implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.cosh(x);
}
- /** {@inheritDoc} */
- public DifferentiableUnivariateFunction derivative() {
- return new Sinh();
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
+ public UnivariateFunction derivative() {
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.cosh();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Exp.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Exp.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Exp.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Exp.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -27,19 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Exp implements DifferentiableUnivariateFunction {
+public class Exp implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.exp(x);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return FastMath.exp(x);
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.exp();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Expm1.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Expm1.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Expm1.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Expm1.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -27,19 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Expm1 implements DifferentiableUnivariateFunction {
+public class Expm1 implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.expm1(x);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return FastMath.exp(x);
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.expm1();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Gaussian.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Gaussian.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Gaussian.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Gaussian.java Sat Aug 18 18:10:34 2012
@@ -17,13 +17,19 @@
package org.apache.commons.math3.analysis.function;
+import java.util.Arrays;
+
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
import org.apache.commons.math3.analysis.ParametricUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.util.Precision;
/**
* <a href="http://en.wikipedia.org/wiki/Gaussian_function">
@@ -32,9 +38,11 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Gaussian implements DifferentiableUnivariateFunction {
+public class Gaussian implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** Mean. */
private final double mean;
+ /** Inverse of the standard deviation. */
+ private final double is;
/** Inverse of twice the square of the standard deviation. */
private final double i2s2;
/** Normalization factor. */
@@ -57,7 +65,8 @@ public class Gaussian implements Differe
this.norm = norm;
this.mean = mean;
- this.i2s2 = 1 / (2 * sigma * sigma);
+ this.is = 1 / sigma;
+ this.i2s2 = 0.5 * is * is;
}
/**
@@ -84,22 +93,12 @@ public class Gaussian implements Differe
return value(x - mean, norm, i2s2);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- final double diff = x - mean;
- final double g = Gaussian.value(diff, norm, i2s2);
-
- if (g == 0) {
- // Avoid returning NaN in case of overflow.
- return 0;
- } else {
- return -2 * diff * i2s2 * g;
- }
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
}
/**
@@ -195,4 +194,55 @@ public class Gaussian implements Differe
double i2s2) {
return norm * FastMath.exp(-xMinusMean * xMinusMean * i2s2);
}
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+
+ final double u = is * (t.getValue() - mean);
+ double[] f = new double[t.getOrder() + 1];
+
+ // the nth order derivative of the Gaussian has the form:
+ // dn(g(x)/dxn = (norm / s^n) P_n(u) exp(-u^2/2) with u=(x-m)/s
+ // where P_n(u) is a degree n polynomial with same parity as n
+ // P_0(u) = 1, P_1(u) = -u, P_2(u) = u^2 - 1, P_3(u) = -u^3 + 3 u...
+ // the general recurrence relation for P_n is:
+ // P_n(u) = P_(n-1)'(u) - u P_(n-1)(u)
+ // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array
+ final double[] p = new double[f.length];
+ p[0] = 1;
+ final double u2 = u * u;
+ double coeff = norm * FastMath.exp(-0.5 * u2);
+ if (coeff <= Precision.SAFE_MIN) {
+ Arrays.fill(f, 0.0);
+ } else {
+ f[0] = coeff;
+ for (int n = 1; n < f.length; ++n) {
+
+ // update and evaluate polynomial P_n(x)
+ double v = 0;
+ p[n] = -p[n - 1];
+ for (int k = n; k >= 0; k -= 2) {
+ v = v * u2 + p[k];
+ if (k > 2) {
+ p[k - 2] = (k - 1) * p[k - 1] - p[k - 3];
+ } else if (k == 2) {
+ p[0] = p[1];
+ }
+ }
+ if ((n & 0x1) == 1) {
+ v *= u;
+ }
+
+ coeff *= is;
+ f[n] = coeff * v;
+
+ }
+ }
+
+ return t.compose(f);
+
+ }
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/HarmonicOscillator.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/HarmonicOscillator.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/HarmonicOscillator.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/HarmonicOscillator.java Sat Aug 18 18:10:34 2012
@@ -17,11 +17,14 @@
package org.apache.commons.math3.analysis.function;
-import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.ParametricUnivariateFunction;
-import org.apache.commons.math3.exception.NullArgumentException;
+import org.apache.commons.math3.analysis.UnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.exception.DimensionMismatchException;
+import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.util.FastMath;
/**
@@ -31,7 +34,7 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class HarmonicOscillator implements DifferentiableUnivariateFunction {
+public class HarmonicOscillator implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** Amplitude. */
private final double amplitude;
/** Angular frequency. */
@@ -59,14 +62,12 @@ public class HarmonicOscillator implemen
return value(omega * x + phase, amplitude);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return -amplitude * omega * FastMath.sin(omega * x + phase);
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
}
/**
@@ -151,4 +152,26 @@ public class HarmonicOscillator implemen
double amplitude) {
return amplitude * FastMath.cos(xTimesOmegaPlusPhase);
}
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ final double x = t.getValue();
+ double[] f = new double[t.getOrder() + 1];
+
+ final double alpha = omega * x + phase;
+ f[0] = amplitude * FastMath.cos(alpha);
+ if (f.length > 1) {
+ f[1] = -amplitude * omega * FastMath.sin(alpha);
+ final double mo2 = - omega * omega;
+ for (int i = 2; i < f.length; ++i) {
+ f[i] = mo2 * f[i - 2];
+ }
+ }
+
+ return t.compose(f);
+
+ }
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Identity.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Identity.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Identity.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Identity.java Sat Aug 18 18:10:34 2012
@@ -18,6 +18,10 @@
package org.apache.commons.math3.analysis.function;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.FunctionUtils;
+import org.apache.commons.math3.analysis.UnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
/**
* Identity function.
@@ -25,14 +29,25 @@ import org.apache.commons.math3.analysis
* @since 3.0
* @version $Id$
*/
-public class Identity implements DifferentiableUnivariateFunction {
+public class Identity implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return x;
}
- /** {@inheritDoc} */
- public DifferentiableUnivariateFunction derivative() {
- return new Constant(1);
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
+ public UnivariateFunction derivative() {
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t;
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Inverse.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Inverse.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Inverse.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Inverse.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
/**
* Inverse function.
@@ -26,19 +29,25 @@ import org.apache.commons.math3.analysis
* @since 3.0
* @version $Id$
*/
-public class Inverse implements DifferentiableUnivariateFunction {
+public class Inverse implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return 1 / x;
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return -1 / (x * x);
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.reciprocal();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Log.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Log.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Log.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Log.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -27,19 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Log implements DifferentiableUnivariateFunction {
+public class Log implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.log(x);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return 1 / x;
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.log();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Log10.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Log10.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Log10.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Log10.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -27,22 +30,26 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Log10 implements DifferentiableUnivariateFunction {
- /** ln(10) = {@value}.*/
- private static final double LN_10 = FastMath.log(10);
+public class Log10 implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.log10(x);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return 1 / (x * LN_10);
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.log10();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Log1p.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Log1p.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Log1p.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Log1p.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -27,19 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Log1p implements DifferentiableUnivariateFunction {
+public class Log1p implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.log1p(x);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return 1 / (1 + x);
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.log1p();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Logistic.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Logistic.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Logistic.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Logistic.java Sat Aug 18 18:10:34 2012
@@ -17,9 +17,12 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
import org.apache.commons.math3.analysis.ParametricUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.exception.DimensionMismatchException;
@@ -32,7 +35,7 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Logistic implements DifferentiableUnivariateFunction {
+public class Logistic implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** Lower asymptote. */
private final double a;
/** Upper asymptote. */
@@ -82,20 +85,12 @@ public class Logistic implements Differe
return value(m - x, k, b, q, a, oneOverN);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- final double exp = q * FastMath.exp(b * (m - x));
- if (Double.isInfinite(exp)) {
- // Avoid returning NaN in case of overflow.
- return 0;
- }
- final double exp1 = exp + 1;
- return b * oneOverN * exp / FastMath.pow(exp1, oneOverN + 1);
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
}
/**
@@ -205,4 +200,12 @@ public class Logistic implements Differe
double oneOverN) {
return a + (k - a) / FastMath.pow(1 + q * FastMath.exp(b * mMinusX), oneOverN);
}
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.negate().add(m).multiply(b).exp().multiply(q).add(1).pow(oneOverN).reciprocal().multiply(k - a).add(a);
+ }
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Logit.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Logit.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Logit.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Logit.java Sat Aug 18 18:10:34 2012
@@ -17,11 +17,14 @@
package org.apache.commons.math3.analysis.function;
-import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.ParametricUnivariateFunction;
-import org.apache.commons.math3.exception.NullArgumentException;
+import org.apache.commons.math3.analysis.UnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.exception.DimensionMismatchException;
+import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.util.FastMath;
@@ -33,7 +36,7 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Logit implements DifferentiableUnivariateFunction {
+public class Logit implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** Lower bound. */
private final double lo;
/** Higher bound. */
@@ -64,14 +67,12 @@ public class Logit implements Differenti
return value(x, lo, hi);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return (hi - lo) / ((x - lo) * (hi - x));
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
}
/**
@@ -154,4 +155,52 @@ public class Logit implements Differenti
}
return FastMath.log((x - lo) / (hi - x));
}
+
+ /** {@inheritDoc}
+ * @since 3.1
+ * @exception OutOfRangeException if parameter is outside of function domain
+ */
+ public DerivativeStructure value(final DerivativeStructure t)
+ throws OutOfRangeException {
+ final double x = t.getValue();
+ if (x < lo || x > hi) {
+ throw new OutOfRangeException(x, lo, hi);
+ }
+ double[] f = new double[t.getOrder() + 1];
+
+ // function value
+ f[0] = FastMath.log((x - lo) / (hi - x));
+
+ if (Double.isInfinite(f[0])) {
+
+ if (f.length > 1) {
+ f[1] = Double.POSITIVE_INFINITY;
+ }
+ // fill the array with infinities
+ // (for x close to lo the signs will flip between -inf and +inf,
+ // for x close to hi the signs will always be +inf)
+ // this is probably overkill, since the call to compose at the end
+ // of the method will transform most infinities into NaN ...
+ for (int i = 2; i < f.length; ++i) {
+ f[i] = f[i - 2];
+ }
+
+ } else {
+
+ // function derivatives
+ final double invL = 1.0 / (x - lo);
+ double xL = invL;
+ final double invH = 1.0 / (hi - x);
+ double xH = invH;
+ for (int i = 1; i < f.length; ++i) {
+ f[i] = xL + xH;
+ xL *= -i * invL;
+ xH *= i * invH;
+ }
+ }
+
+ return t.compose(f);
+
+ }
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Minus.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Minus.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Minus.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Minus.java Sat Aug 18 18:10:34 2012
@@ -18,6 +18,10 @@
package org.apache.commons.math3.analysis.function;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.FunctionUtils;
+import org.apache.commons.math3.analysis.UnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
/**
* Minus function.
@@ -25,14 +29,25 @@ import org.apache.commons.math3.analysis
* @since 3.0
* @version $Id$
*/
-public class Minus implements DifferentiableUnivariateFunction {
+public class Minus implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return -x;
}
- /** {@inheritDoc} */
- public DifferentiableUnivariateFunction derivative() {
- return new Constant(-1);
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
+ public UnivariateFunction derivative() {
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.negate();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Power.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Power.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Power.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Power.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -27,7 +30,7 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Power implements DifferentiableUnivariateFunction {
+public class Power implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** Power. */
private final double p;
@@ -43,13 +46,19 @@ public class Power implements Differenti
return FastMath.pow(x, p);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return p * FastMath.pow(x, p - 1);
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.pow(p);
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sigmoid.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sigmoid.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sigmoid.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sigmoid.java Sat Aug 18 18:10:34 2012
@@ -17,9 +17,14 @@
package org.apache.commons.math3.analysis.function;
+import java.util.Arrays;
+
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
import org.apache.commons.math3.analysis.ParametricUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.util.FastMath;
@@ -34,7 +39,7 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Sigmoid implements DifferentiableUnivariateFunction {
+public class Sigmoid implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** Lower asymptote. */
private final double lo;
/** Higher asymptote. */
@@ -60,23 +65,14 @@ public class Sigmoid implements Differen
this.hi = hi;
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- final double exp = FastMath.exp(-x);
- if (Double.isInfinite(exp)) {
- // Avoid returning NaN in case of overflow.
- return 0;
- }
- final double exp1 = 1 + exp;
- return (hi - lo) * exp / (exp1 * exp1);
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
}
- /** {@inheritDoc} */
public double value(double x) {
return value(x, lo, hi);
}
@@ -157,4 +153,58 @@ public class Sigmoid implements Differen
double hi) {
return lo + (hi - lo) / (1 + FastMath.exp(-x));
}
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+
+ double[] f = new double[t.getOrder() + 1];
+ final double exp = FastMath.exp(-t.getValue());
+ if (Double.isInfinite(exp)) {
+
+ // special handling near lower boundary, to avoid NaN
+ f[0] = lo;
+ Arrays.fill(f, 1, f.length, 0.0);
+
+ } else {
+
+ // the nth order derivative of sigmoid has the form:
+ // dn(sigmoid(x)/dxn = P_n(exp(-x)) / (1+exp(-x))^(n+1)
+ // where P_n(t) is a degree n polynomial with normalized higher term
+ // P_0(t) = 1, P_1(t) = t, P_2(t) = t^2 - t, P_3(t) = t^3 - 4 t^2 + t...
+ // the general recurrence relation for P_n is:
+ // P_n(x) = n t P_(n-1)(t) - t (1 + t) P_(n-1)'(t)
+ final double[] p = new double[f.length];
+
+ final double inv = 1 / (1 + exp);
+ double coeff = hi - lo;
+ for (int n = 0; n < f.length; ++n) {
+
+ // update and evaluate polynomial P_n(t)
+ double v = 0;
+ p[n] = 1;
+ for (int k = n; k >= 0; --k) {
+ v = v * exp + p[k];
+ if (k > 1) {
+ p[k - 1] = (n - k + 2) * p[k - 2] - (k - 1) * p[k - 1];
+ } else {
+ p[0] = 0;
+ }
+ }
+
+ coeff *= inv;
+ f[n] = coeff * v;
+
+ }
+
+ // fix function value
+ f[0] += lo;
+
+ }
+
+ return t.compose(f);
+
+ }
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sin.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sin.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sin.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sin.java Sat Aug 18 18:10:34 2012
@@ -18,6 +18,10 @@
package org.apache.commons.math3.analysis.function;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.FunctionUtils;
+import org.apache.commons.math3.analysis.UnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -26,14 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Sin implements DifferentiableUnivariateFunction {
+public class Sin implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.sin(x);
}
- /** {@inheritDoc} */
- public DifferentiableUnivariateFunction derivative() {
- return new Cos();
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
+ public UnivariateFunction derivative() {
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.sin();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sinc.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sinc.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sinc.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sinc.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
-import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.FunctionUtils;
+import org.apache.commons.math3.analysis.UnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -32,13 +35,32 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Sinc implements DifferentiableUnivariateFunction {
+public class Sinc implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/**
- * Value below which the result of the computation will not change
- * anymore due to the finite precision of the "double" representation
- * of real numbers.
+ * Value below which the computations are done using Taylor series.
+ * <p>
+ * The Taylor series for sinc even order derivatives are:
+ * <pre>
+ * d^(2n)sinc/dx^(2n) = Sum_(k>=0) (-1)^(n+k) / ((2k)!(2n+2k+1)) x^(2k)
+ * = (-1)^n [ 1/(2n+1) - x^2/(4n+6) + x^4/(48n+120) - x^6/(1440n+5040) + O(x^8) ]
+ * </pre>
+ * </p>
+ * <p>
+ * The Taylor series for sinc odd order derivatives are:
+ * <pre>
+ * d^(2n+1)sinc/dx^(2n+1) = Sum_(k>=0) (-1)^(n+k+1) / ((2k+1)!(2n+2k+3)) x^(2k+1)
+ * = (-1)^(n+1) [ x/(2n+3) - x^3/(12n+30) + x^5/(240n+840) - x^7/(10080n+45360) + O(x^9) ]
+ * </pre>
+ * </p>
+ * <p>
+ * So the ratio of the fourth term with respect to the first term
+ * is always smaller than x^6/720, for all derivative orders.
+ * This implies that neglecting this term and using only the first three terms induces
+ * a relative error bounded by x^6/720. The SHORTCUT value is chosen such that this
+ * relative error is below double precision accuracy when |x| <= SHORTCUT.
+ * </p>
*/
- private static final double SHORTCUT = 1e-9;
+ private static final double SHORTCUT = 6.0e-3;
/** For normalized sinc function. */
private final boolean normalized;
@@ -60,58 +82,123 @@ public class Sinc implements Differentia
}
/** {@inheritDoc} */
- public double value(double x) {
- if (normalized) {
- final double piTimesX = Math.PI * x;
- return sinc(piTimesX);
+ public double value(final double x) {
+ final double scaledX = normalized ? FastMath.PI * x : x;
+ if (FastMath.abs(scaledX) <= SHORTCUT) {
+ // use Taylor series
+ final double scaledX2 = scaledX * scaledX;
+ return ((scaledX2 - 20) * scaledX2 + 120) / 120;
} else {
- return sinc(x);
+ // use definition expression
+ return FastMath.sin(scaledX) / scaledX;
}
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- if (normalized) {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- final double piTimesX = Math.PI * x;
- return sincDerivative(piTimesX);
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+
+ final double scaledX = (normalized ? FastMath.PI : 1) * t.getValue();
+ final double scaledX2 = scaledX * scaledX;
+
+ double[] f = new double[t.getOrder() + 1];
+
+ if (FastMath.abs(scaledX) <= SHORTCUT) {
+
+ for (int i = 0; i < f.length; ++i) {
+ final int k = i / 2;
+ if ((i & 0x1) == 0) {
+ // even derivation order
+ f[i] = (((k & 0x1) == 0) ? 1 : -1) *
+ (1.0 / (i + 1) - scaledX2 * (1.0 / (2 * i + 6) - scaledX2 / (24 * i + 120)));
+ } else {
+ // odd derivation order
+ f[i] = (((k & 0x1) == 0) ? -scaledX : scaledX) *
+ (1.0 / (i + 2) - scaledX2 * (1.0 / (6 * i + 24) - scaledX2 / (120 * i + 720)));
}
- };
+ }
+
} else {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return sincDerivative(x);
+
+ final double inv = 1 / scaledX;
+ final double cos = FastMath.cos(scaledX);
+ final double sin = FastMath.sin(scaledX);
+
+ f[0] = inv * sin;
+
+ // the nth order derivative of sinc has the form:
+ // dn(sinc(x)/dxn = [S_n(x) sin(x) + C_n(x) cos(x)] / x^(n+1)
+ // where S_n(x) is an even polynomial with degree n-1 or n (depending on parity)
+ // and C_n(x) is an odd polynomial with degree n-1 or n (depending on parity)
+ // S_0(x) = 1, S_1(x) = -1, S_2(x) = -x^2 + 2, S_3(x) = 3x^2 - 6...
+ // C_0(x) = 0, C_1(x) = x, C_2(x) = -2x, C_3(x) = -x^3 + 6x...
+ // the general recurrence relations for S_n and C_n are:
+ // S_n(x) = x S_(n-1)'(x) - n S_(n-1)(x) - x C_(n-1)(x)
+ // C_n(x) = x C_(n-1)'(x) - n C_(n-1)(x) + x S_(n-1)(x)
+ // as per polynomials parity, we can store both S_n and C_n in the same array
+ final double[] sc = new double[f.length];
+ sc[0] = 1;
+
+ double coeff = inv;
+ for (int n = 1; n < f.length; ++n) {
+
+ double s = 0;
+ double c = 0;
+
+ // update and evaluate polynomials S_n(x) and C_n(x)
+ final int kStart;
+ if ((n & 0x1) == 0) {
+ // even derivation order, S_n is degree n and C_n is degree n-1
+ sc[n] = 0;
+ kStart = n;
+ } else {
+ // odd derivation order, S_n is degree n-1 and C_n is degree n
+ sc[n] = sc[n - 1];
+ c = sc[n];
+ kStart = n - 1;
+ }
+
+ // in this loop, k is always even
+ for (int k = kStart; k > 1; k -= 2) {
+
+ // sine part
+ sc[k] = (k - n) * sc[k] - sc[k - 1];
+ s = s * scaledX2 + sc[k];
+
+ // cosine part
+ sc[k - 1] = (k - 1 - n) * sc[k - 1] + sc[k -2];
+ c = c * scaledX2 + sc[k - 1];
+
}
- };
+ sc[0] *= -n;
+ s = s * scaledX2 + sc[0];
+
+ coeff *= inv;
+ f[n] = coeff * (s * sin + c * scaledX * cos);
+
+ }
+
}
- }
- /**
- * @param x Argument.
- * @return {@code sin(x) / x}.
- */
- private static double sinc(double x) {
- // The direct assignment to 1 for values below 1e-9 is an efficiency
- // optimization on the ground that the result of the full computation
- // is indistinguishable from 1 due to the limited accuracy of the
- // floating point representation.
- return FastMath.abs(x) < SHORTCUT ? 1 :
- FastMath.sin(x) / x;
- }
+ if (normalized) {
+ double scale = FastMath.PI;
+ for (int i = 1; i < f.length; ++i) {
+ f[i] *= scale;
+ scale *= FastMath.PI;
+ }
+ }
+
+ return t.compose(f);
- /**
- * @param x Argument.
- * @return {@code (cos(x) - sin(x) / x) / x}.
- */
- private static double sincDerivative(double x) {
- // The direct assignment to 0 for values below 1e-9 is an efficiency
- // optimization on the ground that the result of the full computation
- // is indistinguishable from 1 due to the limited accuracy of the
- // floating point representation.
- return FastMath.abs(x) < SHORTCUT ? 0 :
- (FastMath.cos(x) - FastMath.sin(x) / x) / x;
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sinh.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sinh.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sinh.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sinh.java Sat Aug 18 18:10:34 2012
@@ -18,6 +18,10 @@
package org.apache.commons.math3.analysis.function;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.FunctionUtils;
+import org.apache.commons.math3.analysis.UnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -26,14 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Sinh implements DifferentiableUnivariateFunction {
+public class Sinh implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.sinh(x);
}
- /** {@inheritDoc} */
- public DifferentiableUnivariateFunction derivative() {
- return new Cosh();
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
+ public UnivariateFunction derivative() {
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.sinh();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sqrt.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sqrt.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sqrt.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Sqrt.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -27,19 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Sqrt implements DifferentiableUnivariateFunction {
+public class Sqrt implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.sqrt(x);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- return 1 / (2 * FastMath.sqrt(x));
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.sqrt();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Tan.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Tan.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Tan.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Tan.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -27,20 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Tan implements DifferentiableUnivariateFunction {
+public class Tan implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.tan(x);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- final double tanX = FastMath.tan(x);
- return 1 + tanX * tanX;
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.tan();
}
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Tanh.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Tanh.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Tanh.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/function/Tanh.java Sat Aug 18 18:10:34 2012
@@ -17,8 +17,11 @@
package org.apache.commons.math3.analysis.function;
+import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
import org.apache.commons.math3.util.FastMath;
/**
@@ -27,20 +30,25 @@ import org.apache.commons.math3.util.Fas
* @since 3.0
* @version $Id$
*/
-public class Tanh implements DifferentiableUnivariateFunction {
+public class Tanh implements UnivariateDifferentiable, DifferentiableUnivariateFunction {
/** {@inheritDoc} */
public double value(double x) {
return FastMath.tanh(x);
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)}
+ */
+ @Deprecated
public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- /** {@inheritDoc} */
- public double value(double x) {
- final double tanhX = FastMath.tanh(x);
- return 1 - tanhX * tanhX;
- }
- };
+ return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative();
+ }
+
+ /** {@inheritDoc}
+ * @since 3.1
+ */
+ public DerivativeStructure value(final DerivativeStructure t) {
+ return t.tanh();
}
+
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/function/GaussianTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/function/GaussianTest.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/function/GaussianTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/function/GaussianTest.java Sat Aug 18 18:10:34 2012
@@ -18,11 +18,12 @@
package org.apache.commons.math3.analysis.function;
import org.apache.commons.math3.analysis.UnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
+import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.exception.NullArgumentException;
-import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.util.FastMath;
-
import org.junit.Assert;
import org.junit.Test;
@@ -57,34 +58,38 @@ public class GaussianTest {
}
@Test
- public void testDerivative() {
- final Gaussian f = new Gaussian();
- final UnivariateFunction dfdx = f.derivative();
-
- Assert.assertEquals(0, dfdx.value(0), 0);
+ public void testDerivatives() {
+ final UnivariateDifferentiable gaussian = new Gaussian(2.0, 0.9, 3.0);
+ final DerivativeStructure dsX = new DerivativeStructure(1, 4, 0, 1.1);
+ final DerivativeStructure dsY = gaussian.value(dsX);
+ Assert.assertEquals( 1.9955604901712128349, dsY.getValue(), EPS);
+ Assert.assertEquals(-0.044345788670471396332, dsY.getPartialDerivative(1), EPS);
+ Assert.assertEquals(-0.22074348138190206174, dsY.getPartialDerivative(2), EPS);
+ Assert.assertEquals( 0.014760030401924800557, dsY.getPartialDerivative(3), EPS);
+ Assert.assertEquals( 0.073253159785035691678, dsY.getPartialDerivative(4), EPS);
}
@Test
public void testDerivativeLargeArguments() {
final Gaussian f = new Gaussian(0, 1e-50);
- final UnivariateFunction dfdx = f.derivative();
- Assert.assertEquals(0, dfdx.value(Double.NEGATIVE_INFINITY), 0);
- Assert.assertEquals(0, dfdx.value(-Double.MAX_VALUE), 0);
- Assert.assertEquals(0, dfdx.value(-1e50), 0);
- Assert.assertEquals(0, dfdx.value(-1e2), 0);
- Assert.assertEquals(0, dfdx.value(1e2), 0);
- Assert.assertEquals(0, dfdx.value(1e50), 0);
- Assert.assertEquals(0, dfdx.value(Double.MAX_VALUE), 0);
- Assert.assertEquals(0, dfdx.value(Double.POSITIVE_INFINITY), 0);
+ Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, Double.NEGATIVE_INFINITY)).getPartialDerivative(1), 0);
+ Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, -Double.MAX_VALUE)).getPartialDerivative(1), 0);
+ Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, -1e50)).getPartialDerivative(1), 0);
+ Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, -1e2)).getPartialDerivative(1), 0);
+ Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, 1e2)).getPartialDerivative(1), 0);
+ Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, 1e50)).getPartialDerivative(1), 0);
+ Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, Double.MAX_VALUE)).getPartialDerivative(1), 0);
+ Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, Double.POSITIVE_INFINITY)).getPartialDerivative(1), 0);
}
@Test
- public void testDerivativeNaN() {
+ public void testDerivativesNaN() {
final Gaussian f = new Gaussian(0, 1e-50);
- final UnivariateFunction dfdx = f.derivative();
-
- Assert.assertTrue(Double.isNaN(dfdx.value(Double.NaN)));
+ final DerivativeStructure fx = f.value(new DerivativeStructure(1, 5, 0, Double.NaN));
+ for (int i = 0; i <= fx.getOrder(); ++i) {
+ Assert.assertTrue(Double.isNaN(fx.getPartialDerivative(i)));
+ }
}
@Test(expected=NullArgumentException.class)
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/function/HarmonicOscillatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/function/HarmonicOscillatorTest.java?rev=1374631&r1=1374630&r2=1374631&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/function/HarmonicOscillatorTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/function/HarmonicOscillatorTest.java Sat Aug 18 18:10:34 2012
@@ -18,10 +18,11 @@
package org.apache.commons.math3.analysis.function;
import org.apache.commons.math3.analysis.UnivariateFunction;
-import org.apache.commons.math3.exception.NullArgumentException;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
import org.apache.commons.math3.exception.DimensionMismatchException;
+import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.util.FastMath;
-
+import org.apache.commons.math3.util.Precision;
import org.junit.Assert;
import org.junit.Test;
@@ -51,12 +52,33 @@ public class HarmonicOscillatorTest {
final double w = 0.34;
final double p = 5.6;
final HarmonicOscillator f = new HarmonicOscillator(a, w, p);
- final UnivariateFunction dfdx = f.derivative();
- final double d = 0.12345;
- for (int i = 0; i < 10; i++) {
- final double v = i * d;
- Assert.assertEquals(-a * w * FastMath.sin(w * v + p), dfdx.value(v), 0);
+ for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
+ final double d = 0.12345;
+ for (int i = 0; i < 10; i++) {
+ final double v = i * d;
+ final DerivativeStructure h = f.value(new DerivativeStructure(1, maxOrder, 0, v));
+ for (int k = 0; k <= maxOrder; ++k) {
+ final double trigo;
+ switch (k % 4) {
+ case 0:
+ trigo = +FastMath.cos(w * v + p);
+ break;
+ case 1:
+ trigo = -FastMath.sin(w * v + p);
+ break;
+ case 2:
+ trigo = -FastMath.cos(w * v + p);
+ break;
+ default:
+ trigo = +FastMath.sin(w * v + p);
+ break;
+ }
+ Assert.assertEquals(a * FastMath.pow(w, k) * trigo,
+ h.getPartialDerivative(k),
+ Precision.EPSILON);
+ }
+ }
}
}