commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1185351 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/analysis/FunctionUtils.java test/java/org/apache/commons/math/analysis/FunctionUtilsTest.java
Date Mon, 17 Oct 2011 20:03:50 GMT
Author: erans
Date: Mon Oct 17 20:03:50 2011
New Revision: 1185351

URL: http://svn.apache.org/viewvc?rev=1185351&view=rev
Log:
"add", "multiply", "compose" instances of "DifferentiableUnivariateRealFunction".

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/FunctionUtils.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/FunctionUtilsTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/FunctionUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/FunctionUtils.java?rev=1185351&r1=1185350&r2=1185351&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/FunctionUtils.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/FunctionUtils.java
Mon Oct 17 20:03:50 2011
@@ -53,6 +53,43 @@ public class FunctionUtils {
     }
 
     /**
+     * Compose functions.  The functions in the argument list are composed
+     * sequentially, in the order given.  For example, compose(f1,f2,f3)
+     * acts like f1(f2(f3(x))).
+     *
+     * @param f List of functions.
+     * @return the composite function.
+     */
+    public static DifferentiableUnivariateRealFunction compose(final DifferentiableUnivariateRealFunction
... f) {
+        return new DifferentiableUnivariateRealFunction() {
+            /** {@inheritDoc} */
+            public double value(double x) {
+                double r = x;
+                for (int i = f.length - 1; i >= 0; i--) {
+                    r = f[i].value(r);
+                }
+                return r;
+            }
+
+            /** {@inheritDoc} */
+            public UnivariateRealFunction derivative() {
+                return new UnivariateRealFunction() {
+                    /** {@inheritDoc} */
+                    public double value(double x) {
+                        double p = 1;
+                        double r = x;
+                        for (int i = f.length - 1; i >= 0; i--) {
+                            p *= f[i].derivative().value(r);
+                            r = f[i].value(r);
+                        }
+                        return p;
+                    }
+                };
+            }
+        };
+    }
+
+    /**
      * Add functions.
      *
      * @param f List of functions.
@@ -72,6 +109,39 @@ public class FunctionUtils {
     }
 
     /**
+     * Add functions.
+     *
+     * @param f List of functions.
+     * @return a function that computes the sum of the functions.
+     */
+    public static DifferentiableUnivariateRealFunction add(final DifferentiableUnivariateRealFunction
... f) {
+        return new DifferentiableUnivariateRealFunction() {
+            /** {@inheritDoc} */
+            public double value(double x) {
+                double r = f[0].value(x);
+                for (int i = 1; i < f.length; i++) {
+                    r += f[i].value(x);
+                }
+                return r;
+            }
+
+            /** {@inheritDoc} */
+            public UnivariateRealFunction derivative() {
+                return new UnivariateRealFunction() {
+                    /** {@inheritDoc} */
+                    public double value(double x) {
+                        double r = f[0].derivative().value(x);
+                        for (int i = 1; i < f.length; i++) {
+                            r += f[i].derivative().value(x);
+                        }
+                        return r;
+                    }
+                };
+            }
+        };
+    }
+
+    /**
      * Multiply functions.
      *
      * @param f List of functions.
@@ -91,6 +161,45 @@ public class FunctionUtils {
     }
 
     /**
+     * Multiply functions.
+     *
+     * @param f List of functions.
+     * @return a function that computes the product of the functions.
+     */
+    public static DifferentiableUnivariateRealFunction multiply(final DifferentiableUnivariateRealFunction
... f) {
+        return new DifferentiableUnivariateRealFunction() {
+            /** {@inheritDoc} */
+            public double value(double x) {
+                double r = f[0].value(x);
+                for (int i = 1; i < f.length; i++) {
+                    r *= f[i].value(x);
+                }
+                return r;
+            }
+
+            /** {@inheritDoc} */
+            public UnivariateRealFunction derivative() {
+                return new UnivariateRealFunction() {
+                    /** {@inheritDoc} */
+                    public double value(double x) {
+                        double sum = 0;
+                        for (int i = 0; i < f.length; i++) {
+                            double prod = f[i].derivative().value(x);
+                            for (int j = 0; j < f.length; j++) {
+                                if (i != j) {
+                                    prod *= f[j].value(x);
+                                }
+                            }
+                            sum += prod;
+                        }
+                        return sum;
+                    }
+                };
+            }
+        };
+    }
+
+    /**
      * Returns the univariate function <br/>
      * {@code h(x) = combiner(f(x), g(x))}.
      *

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/FunctionUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/FunctionUtilsTest.java?rev=1185351&r1=1185350&r2=1185351&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/FunctionUtilsTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/FunctionUtilsTest.java
Mon Oct 17 20:03:50 2011
@@ -25,6 +25,8 @@ import org.apache.commons.math.analysis.
 import org.apache.commons.math.analysis.function.Power;
 import org.apache.commons.math.analysis.function.Sin;
 import org.apache.commons.math.analysis.function.Sinc;
+import org.apache.commons.math.analysis.function.Cos;
+import org.apache.commons.math.analysis.function.Cosh;
 import org.apache.commons.math.analysis.BivariateRealFunction;
 import org.apache.commons.math.analysis.function.Add;
 import org.apache.commons.math.analysis.function.Multiply;
@@ -32,6 +34,7 @@ import org.apache.commons.math.analysis.
 import org.apache.commons.math.analysis.function.Min;
 import org.apache.commons.math.analysis.function.Max;
 import org.apache.commons.math.analysis.function.Pow;
+import org.apache.commons.math.analysis.function.Log;
 import org.apache.commons.math.analysis.MultivariateRealFunction;
 
 import org.junit.Assert;
@@ -64,6 +67,31 @@ public class FunctionUtilsTest {
     }
 
     @Test
+    public void testComposeDifferentiable() {
+        DifferentiableUnivariateRealFunction id = new Identity();
+        Assert.assertEquals(1, FunctionUtils.compose(id, id, id).derivative().value(3), EPS);
+
+        DifferentiableUnivariateRealFunction c = new Constant(4);
+        Assert.assertEquals(0, FunctionUtils.compose(id, c).derivative().value(3), EPS);
+        Assert.assertEquals(0, FunctionUtils.compose(c, id).derivative().value(3), EPS);
+
+        DifferentiableUnivariateRealFunction m = new Minus();
+        Assert.assertEquals(-1, FunctionUtils.compose(m).derivative().value(3), EPS);
+        Assert.assertEquals(1, FunctionUtils.compose(m, m).derivative().value(3), EPS);
+
+        DifferentiableUnivariateRealFunction inv = new Inverse();
+        Assert.assertEquals(0.25, FunctionUtils.compose(inv, m, id).derivative().value(2),
EPS);
+
+        DifferentiableUnivariateRealFunction pow = new Power(2);
+        Assert.assertEquals(108, FunctionUtils.compose(pow, pow).derivative().value(3), EPS);
+
+        DifferentiableUnivariateRealFunction log = new Log();
+        double a = 9876.54321;
+        Assert.assertEquals(pow.derivative().value(a) / pow.value(a),
+                            FunctionUtils.compose(log, pow).derivative().value(a), EPS);
+    }
+
+    @Test
     public void testAdd() {
         UnivariateRealFunction id = new Identity();
         UnivariateRealFunction c = new Constant(4);
@@ -76,6 +104,19 @@ public class FunctionUtilsTest {
     }
 
     @Test
+    public void testAddDifferentiable() {
+        DifferentiableUnivariateRealFunction sin = new Sin();
+        DifferentiableUnivariateRealFunction c = new Constant(4);
+        DifferentiableUnivariateRealFunction m = new Minus();
+        DifferentiableUnivariateRealFunction inv = new Inverse();
+
+        final double a = 123.456;
+        Assert.assertEquals(- 1 / (a * a) -1 + Math.cos(a),
+                            FunctionUtils.add(inv, m, c, sin).derivative().value(a),
+                            EPS);
+    }
+
+    @Test
     public void testMultiply() {
         UnivariateRealFunction c = new Constant(4);
         Assert.assertEquals(16, FunctionUtils.multiply(c, c).value(12345), EPS);
@@ -86,6 +127,24 @@ public class FunctionUtilsTest {
     }
 
     @Test
+    public void testMultiplyDifferentiable() {
+        DifferentiableUnivariateRealFunction c = new Constant(4);
+        DifferentiableUnivariateRealFunction id = new Identity();
+        final double a = 1.2345678;
+        Assert.assertEquals(8 * a, FunctionUtils.multiply(c, id, id).derivative().value(a),
EPS);
+
+        DifferentiableUnivariateRealFunction inv = new Inverse();
+        DifferentiableUnivariateRealFunction pow = new Power(2.5);
+        DifferentiableUnivariateRealFunction cos = new Cos();
+        Assert.assertEquals(1.5 * Math.sqrt(a) * Math.cos(a) - Math.pow(a, 1.5) * Math.sin(a),
+                            FunctionUtils.multiply(inv, pow, cos).derivative().value(a),
EPS);
+
+        DifferentiableUnivariateRealFunction cosh = new Cosh();
+        Assert.assertEquals(1.5 * Math.sqrt(a) * Math.cosh(a) + Math.pow(a, 1.5) * Math.sinh(a),
+                            FunctionUtils.multiply(inv, pow, cosh).derivative().value(a),
8 * EPS);
+    }
+
+    @Test
     public void testCombine() {
         BivariateRealFunction bi = new Add();
         UnivariateRealFunction id = new Identity();



Mime
View raw message