diff --git a/src/main/java/org/apache/commons/math/analysis/function/Acos.java b/src/main/java/org/apache/commons/math/analysis/function/Acos.java index bce111474..97f029036 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Acos.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Acos.java @@ -18,6 +18,7 @@ package org.apache.commons.math.analysis.function; import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,9 +27,19 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Acos implements UnivariateRealFunction { +public class Acos implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.acos(x); } + + /** {@inheritDoc} */ + public UnivariateRealFunction derivative() { + return new UnivariateRealFunction() { + /** {@inheritDoc} */ + public double value(double x) { + return -1 / FastMath.sqrt(1 - x * x); + } + }; + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Acosh.java b/src/main/java/org/apache/commons/math/analysis/function/Acosh.java index 352ee3460..c6d72089e 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Acosh.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Acosh.java @@ -18,6 +18,7 @@ package org.apache.commons.math.analysis.function; import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,9 +27,19 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Acosh implements UnivariateRealFunction { +public class Acosh implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.acosh(x); } + + /** {@inheritDoc} */ + public UnivariateRealFunction derivative() { + return new UnivariateRealFunction() { + /** {@inheritDoc} */ + public double value(double x) { + return 1 / FastMath.sqrt(x * x - 1); + } + }; + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Asin.java b/src/main/java/org/apache/commons/math/analysis/function/Asin.java index 8ffe80c4b..c4edf7408 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Asin.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Asin.java @@ -18,6 +18,7 @@ package org.apache.commons.math.analysis.function; import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,9 +27,19 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Asin implements UnivariateRealFunction { +public class Asin implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.asin(x); } + + /** {@inheritDoc} */ + public UnivariateRealFunction derivative() { + return new UnivariateRealFunction() { + /** {@inheritDoc} */ + public double value(double x) { + return 1 / FastMath.sqrt(1 - x * x); + } + }; + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Asinh.java b/src/main/java/org/apache/commons/math/analysis/function/Asinh.java index 465d72ddc..40eae84f8 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Asinh.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Asinh.java @@ -18,6 +18,7 @@ package org.apache.commons.math.analysis.function; import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,9 +27,19 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Asinh implements UnivariateRealFunction { +public class Asinh implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.asinh(x); } + + /** {@inheritDoc} */ + public UnivariateRealFunction derivative() { + return new UnivariateRealFunction() { + /** {@inheritDoc} */ + public double value(double x) { + return 1 / FastMath.sqrt(1 + x * x); + } + }; + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Atan.java b/src/main/java/org/apache/commons/math/analysis/function/Atan.java index bd0379a26..7a2b702c8 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Atan.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Atan.java @@ -18,6 +18,7 @@ package org.apache.commons.math.analysis.function; import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,9 +27,19 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Atan implements UnivariateRealFunction { +public class Atan implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.atan(x); } + + /** {@inheritDoc} */ + public UnivariateRealFunction derivative() { + return new UnivariateRealFunction() { + /** {@inheritDoc} */ + public double value(double x) { + return 1 / (1 + x * x); + } + }; + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Atanh.java b/src/main/java/org/apache/commons/math/analysis/function/Atanh.java index 6ac3fb3ba..e851e1997 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Atanh.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Atanh.java @@ -18,6 +18,7 @@ package org.apache.commons.math.analysis.function; import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,9 +27,19 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Atanh implements UnivariateRealFunction { +public class Atanh implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.atanh(x); } + + /** {@inheritDoc} */ + public UnivariateRealFunction derivative() { + return new UnivariateRealFunction() { + /** {@inheritDoc} */ + public double value(double x) { + return 1 / (1 - x * x); + } + }; + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Cbrt.java b/src/main/java/org/apache/commons/math/analysis/function/Cbrt.java index f5b3c9bfe..c682b691c 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Cbrt.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Cbrt.java @@ -18,6 +18,7 @@ package org.apache.commons.math.analysis.function; import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,9 +27,19 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Cbrt implements UnivariateRealFunction { +public class Cbrt implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.cbrt(x); } + + /** {@inheritDoc} */ + public UnivariateRealFunction derivative() { + return new UnivariateRealFunction() { + /** {@inheritDoc} */ + public double value(double x) { + return 1 / (3 * FastMath.cbrt(x * x)); + } + }; + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Constant.java b/src/main/java/org/apache/commons/math/analysis/function/Constant.java index 563197cf2..55c44056a 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Constant.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Constant.java @@ -17,7 +17,7 @@ package org.apache.commons.math.analysis.function; -import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; /** * Constant function. @@ -25,7 +25,7 @@ import org.apache.commons.math.analysis.UnivariateRealFunction; * @version $Id$ * @since 3.0 */ -public class Constant implements UnivariateRealFunction { +public class Constant implements DifferentiableUnivariateRealFunction { /** Constant. */ private final double c; @@ -40,4 +40,9 @@ public class Constant implements UnivariateRealFunction { public double value(double x) { return c; } + + /** {@inheritDoc} */ + public DifferentiableUnivariateRealFunction derivative() { + return new Constant(0); + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Cos.java b/src/main/java/org/apache/commons/math/analysis/function/Cos.java index 4c24c6bce..9b527ba2c 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Cos.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Cos.java @@ -18,6 +18,7 @@ package org.apache.commons.math.analysis.function; import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,9 +27,19 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Cos implements UnivariateRealFunction { +public class Cos implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.cos(x); } + + /** {@inheritDoc} */ + public UnivariateRealFunction derivative() { + return new UnivariateRealFunction() { + /** {@inheritDoc} */ + public double value(double x) { + return -FastMath.sin(x); + } + }; + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Cosh.java b/src/main/java/org/apache/commons/math/analysis/function/Cosh.java index a4edc8ced..8db168bfe 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Cosh.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Cosh.java @@ -17,7 +17,7 @@ package org.apache.commons.math.analysis.function; -import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,9 +26,14 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Cosh implements UnivariateRealFunction { +public class Cosh implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.cosh(x); } + + /** {@inheritDoc} */ + public DifferentiableUnivariateRealFunction derivative() { + return new Sinh(); + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Exp.java b/src/main/java/org/apache/commons/math/analysis/function/Exp.java index 5ba7d29c5..ce3144c90 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Exp.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Exp.java @@ -18,6 +18,7 @@ package org.apache.commons.math.analysis.function; import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,9 +27,19 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Exp implements UnivariateRealFunction { +public class Exp implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.exp(x); } + + /** {@inheritDoc} */ + public UnivariateRealFunction derivative() { + return new UnivariateRealFunction() { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.exp(x); + } + }; + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Expm1.java b/src/main/java/org/apache/commons/math/analysis/function/Expm1.java index 644ce1206..567c986e3 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Expm1.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Expm1.java @@ -18,6 +18,7 @@ package org.apache.commons.math.analysis.function; import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,9 +27,19 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Expm1 implements UnivariateRealFunction { +public class Expm1 implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.expm1(x); } + + /** {@inheritDoc} */ + public UnivariateRealFunction derivative() { + return new UnivariateRealFunction() { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.exp(x); + } + }; + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Identity.java b/src/main/java/org/apache/commons/math/analysis/function/Identity.java index c1640a045..994492a5c 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Identity.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Identity.java @@ -17,7 +17,7 @@ package org.apache.commons.math.analysis.function; -import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; /** * Identity function. @@ -25,9 +25,14 @@ import org.apache.commons.math.analysis.UnivariateRealFunction; * @version $Id$ * @since 3.0 */ -public class Identity implements UnivariateRealFunction { +public class Identity implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return x; } + + /** {@inheritDoc} */ + public DifferentiableUnivariateRealFunction derivative() { + return new Constant(1); + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Inverse.java b/src/main/java/org/apache/commons/math/analysis/function/Inverse.java index 285fa33b9..284f4459b 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Inverse.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Inverse.java @@ -18,6 +18,7 @@ package org.apache.commons.math.analysis.function; import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; /** * Inverse function. @@ -25,9 +26,19 @@ import org.apache.commons.math.analysis.UnivariateRealFunction; * @version $Id$ * @since 3.0 */ -public class Inverse implements UnivariateRealFunction { +public class Inverse implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return 1 / x; } + + /** {@inheritDoc} */ + public UnivariateRealFunction derivative() { + return new UnivariateRealFunction() { + /** {@inheritDoc} */ + public double value(double x) { + return -1 / (x * x); + } + }; + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Log.java b/src/main/java/org/apache/commons/math/analysis/function/Log.java index 0750f22fe..e81f77590 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Log.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Log.java @@ -18,6 +18,7 @@ package org.apache.commons.math.analysis.function; import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,9 +27,19 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Log implements UnivariateRealFunction { +public class Log implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.log(x); } + + /** {@inheritDoc} */ + public UnivariateRealFunction derivative() { + return new UnivariateRealFunction() { + /** {@inheritDoc} */ + public double value(double x) { + return 1 / x; + } + }; + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Log1p.java b/src/main/java/org/apache/commons/math/analysis/function/Log1p.java index 7ec24599c..c85d4882b 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Log1p.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Log1p.java @@ -18,6 +18,7 @@ package org.apache.commons.math.analysis.function; import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,9 +27,19 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Log1p implements UnivariateRealFunction { +public class Log1p implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.log1p(x); } + + /** {@inheritDoc} */ + public UnivariateRealFunction derivative() { + return new UnivariateRealFunction() { + /** {@inheritDoc} */ + public double value(double x) { + return 1 / (1 + x); + } + }; + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Power.java b/src/main/java/org/apache/commons/math/analysis/function/Power.java index 41ee256e7..f5bca1db2 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Power.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Power.java @@ -18,6 +18,7 @@ package org.apache.commons.math.analysis.function; import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,7 +27,7 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Power implements UnivariateRealFunction { +public class Power implements DifferentiableUnivariateRealFunction { /** Power. */ private final double p; @@ -41,4 +42,14 @@ public class Power implements UnivariateRealFunction { public double value(double x) { return FastMath.pow(x, p); } + + /** {@inheritDoc} */ + public UnivariateRealFunction derivative() { + return new UnivariateRealFunction() { + /** {@inheritDoc} */ + public double value(double x) { + return p * FastMath.pow(x, p - 1); + } + }; + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Sin.java b/src/main/java/org/apache/commons/math/analysis/function/Sin.java index a8bd7e3ac..126d7e1d5 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Sin.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Sin.java @@ -17,7 +17,7 @@ package org.apache.commons.math.analysis.function; -import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,9 +26,14 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Sin implements UnivariateRealFunction { +public class Sin implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.sin(x); } + + /** {@inheritDoc} */ + public DifferentiableUnivariateRealFunction derivative() { + return new Cos(); + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Sinh.java b/src/main/java/org/apache/commons/math/analysis/function/Sinh.java index 8d9090069..be538c3b4 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Sinh.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Sinh.java @@ -17,7 +17,7 @@ package org.apache.commons.math.analysis.function; -import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,9 +26,14 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Sinh implements UnivariateRealFunction { +public class Sinh implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.sinh(x); } + + /** {@inheritDoc} */ + public DifferentiableUnivariateRealFunction derivative() { + return new Cosh(); + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Tan.java b/src/main/java/org/apache/commons/math/analysis/function/Tan.java index 31f7fa211..5cf535130 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Tan.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Tan.java @@ -18,6 +18,7 @@ package org.apache.commons.math.analysis.function; import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,9 +27,20 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Tan implements UnivariateRealFunction { +public class Tan implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.tan(x); } + + /** {@inheritDoc} */ + public UnivariateRealFunction derivative() { + return new UnivariateRealFunction() { + /** {@inheritDoc} */ + public double value(double x) { + final double tanX = FastMath.tan(x); + return 1 + tanX * tanX; + } + }; + } } diff --git a/src/main/java/org/apache/commons/math/analysis/function/Tanh.java b/src/main/java/org/apache/commons/math/analysis/function/Tanh.java index 165ed2e3d..524812d9e 100644 --- a/src/main/java/org/apache/commons/math/analysis/function/Tanh.java +++ b/src/main/java/org/apache/commons/math/analysis/function/Tanh.java @@ -18,6 +18,7 @@ package org.apache.commons.math.analysis.function; import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.util.FastMath; /** @@ -26,9 +27,20 @@ import org.apache.commons.math.util.FastMath; * @version $Id$ * @since 3.0 */ -public class Tanh implements UnivariateRealFunction { +public class Tanh implements DifferentiableUnivariateRealFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.tanh(x); } + + /** {@inheritDoc} */ + public UnivariateRealFunction derivative() { + return new UnivariateRealFunction() { + /** {@inheritDoc} */ + public double value(double x) { + final double tanhX = FastMath.tanh(x); + return 1 - tanhX * tanhX; + } + }; + } }