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 2008/04/15 15:17:47 UTC
svn commit: r648235 - in
/commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences:
./ EightPointsScheme.java FiniteDifferencesDifferentiator.java
FourPointsScheme.java SixPointsScheme.java TwoPointsScheme.java
Author: luc
Date: Tue Apr 15 06:17:43 2008
New Revision: 648235
URL: http://svn.apache.org/viewvc?rev=648235&view=rev
Log:
created fallback finite differences implementation for differentiation
Added:
commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/
commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/EightPointsScheme.java (with props)
commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/FiniteDifferencesDifferentiator.java (with props)
commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/FourPointsScheme.java (with props)
commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/SixPointsScheme.java (with props)
commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/TwoPointsScheme.java (with props)
Added: commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/EightPointsScheme.java
URL: http://svn.apache.org/viewvc/commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/EightPointsScheme.java?rev=648235&view=auto
==============================================================================
--- commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/EightPointsScheme.java (added)
+++ commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/EightPointsScheme.java Tue Apr 15 06:17:43 2008
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.nabla.differences;
+
+import org.apache.commons.nabla.core.DifferentialPair;
+import org.apache.commons.nabla.core.UnivariateDerivative;
+import org.apache.commons.nabla.core.UnivariateDifferentiable;
+
+/** Eight-points finite differences scheme.
+ * The error model for the eight-points scheme is
+ * <code>-h<sup>8</sup>/630 f<sup>(9)</sup>(x) + O(h<sup>10</sup>)</code>.
+ */
+public class EightPointsScheme extends FiniteDifferencesDifferentiator {
+
+ /** Serializable UID. */
+ private static final long serialVersionUID = 39061646369246780L;
+
+ /** Scheme denominator. */
+ private final double denominator;
+
+ /** Build an 8-points finite differences scheme.
+ * @param h differences step size
+ */
+ public EightPointsScheme(final double h) {
+ super(h, -h * h * h * h * h * h * h * h / 630);
+ denominator = 840 * h;
+ }
+
+ /** {@inheritDoc} */
+ public UnivariateDerivative differentiate(final UnivariateDifferentiable d) {
+ return new UnivariateDerivative() {
+ public UnivariateDifferentiable getPrimitive() {
+ return d;
+ }
+ public DifferentialPair f(final DifferentialPair t) {
+ final double h = getStepSize();
+ final double u0 = t.getU0();
+ final double ft = d.f(u0);
+ final double d1 = d.f(u0 + h) - d.f(u0 - h);
+ final double d2 = d.f(u0 + 2 * h) - d.f(u0 - 2 * h);
+ final double d3 = d.f(u0 + 3 * h) - d.f(u0 - 3 * h);
+ final double d4 = d.f(u0 + 4 * h) - d.f(u0 - 4 * h);
+ return new DifferentialPair(ft, t.getU1() * (-3 * d4 + 32 * d3 - 168 * d2 + 672 * d1) / denominator);
+ }
+ };
+ }
+
+}
Propchange: commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/EightPointsScheme.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/FiniteDifferencesDifferentiator.java
URL: http://svn.apache.org/viewvc/commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/FiniteDifferencesDifferentiator.java?rev=648235&view=auto
==============================================================================
--- commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/FiniteDifferencesDifferentiator.java (added)
+++ commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/FiniteDifferencesDifferentiator.java Tue Apr 15 06:17:43 2008
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.nabla.differences;
+
+import java.io.Serializable;
+
+import org.apache.commons.nabla.core.UnivariateDifferentiator;
+
+/** This class represents a differentiation scheme based on finite differences.
+ * <p>The differences are based on regularly distributed points around the
+ * central test point according to a given h step.
+ * For example a four points scheme would use a linear combination of points
+ * <code>f(x-2h)</code>, <code>f(x-h)</code>, <code>f(x+h)</code> and
+ * <code>f(x+2h)</code> to compute the first differential <code>f'(x)</code>
+ * (note that <code>f(x)</code> by itself is not used).
+ * The computed differential is only an approximation, the error depends on
+ * the value of the high order differentials of the function which were not
+ * canceled by the linear combination, the number of points and the step.
+ * For example the four points scheme cancels the
+ * <code>f<sup>(2)</sup></code>, <code>f<sup>(3)</sup></code> and
+ * <code>f<sup>(4)</sup></code> high order differentials but not the
+ * <code>f<sup>(5)</sup></code> and higher order terms. The error model is
+ * <code>-2h<sup>4</sup>/5 f<sup>(5)</sup>(x) + O(h<sup>6</sup>)</code>.
+ * </p>
+ */
+public abstract class FiniteDifferencesDifferentiator
+ implements UnivariateDifferentiator, Serializable {
+
+ /** Step size. */
+ private final double stepSize;
+
+ /** Error scale factor. */
+ private final double factor;
+
+ /** Simple constructor.
+ * @param stepSize step stepSize
+ * @param factor error scale factor
+ */
+ protected FiniteDifferencesDifferentiator(final double stepSize, final double factor) {
+ this.stepSize = stepSize;
+ this.factor = factor;
+ }
+
+ /** Get the signed error scale factor for the finite differences scheme.
+ * <p>The error scale factor is the value of the h-dependent
+ * factor of the first non-canceled high order differential of the function.
+ * For example since the error model for the four points scheme is
+ * <code>-2h<sup>4</sup>/5 f<sup>(5)</sup>(x) + O(h<sup>6</sup>)</code>,
+ * the signed error scale factor is <code>-2h<sup>4</sup>/5</code>.</p>
+ * @return error signed scale factor
+ */
+ public double getSignedErrorScaleFactor() {
+ return factor;
+ }
+
+ /** Get the step size.
+ * @return step size
+ */
+ protected double getStepSize() {
+ return stepSize;
+ }
+
+}
Propchange: commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/FiniteDifferencesDifferentiator.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/FourPointsScheme.java
URL: http://svn.apache.org/viewvc/commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/FourPointsScheme.java?rev=648235&view=auto
==============================================================================
--- commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/FourPointsScheme.java (added)
+++ commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/FourPointsScheme.java Tue Apr 15 06:17:43 2008
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.nabla.differences;
+
+import org.apache.commons.nabla.core.DifferentialPair;
+import org.apache.commons.nabla.core.UnivariateDerivative;
+import org.apache.commons.nabla.core.UnivariateDifferentiable;
+
+/** Four-points finite differences scheme.
+ * The error model for the four-points scheme is
+ * <code>-2h<sup>4</sup>/5 f<sup>(5)</sup>(x) + O(h<sup>6</sup>)</code>.
+ */
+public class FourPointsScheme extends FiniteDifferencesDifferentiator {
+
+ /** Serializable UID. */
+ private static final long serialVersionUID = -4303710033273686431L;
+
+ /** Scheme denominator. */
+ private final double denominator;
+
+ /** Build a 4-points finite differences scheme.
+ * @param h differences step size
+ */
+ public FourPointsScheme(final double h) {
+ super(h, -2 * h * h * h * h / 5);
+ denominator = 12 * h;
+ }
+
+ /** {@inheritDoc} */
+ public UnivariateDerivative differentiate(final UnivariateDifferentiable d) {
+ return new UnivariateDerivative() {
+ public UnivariateDifferentiable getPrimitive() {
+ return d;
+ }
+ public DifferentialPair f(final DifferentialPair t) {
+ final double h = getStepSize();
+ final double u0 = t.getU0();
+ final double ft = d.f(u0);
+ final double d1 = d.f(u0 + h) - d.f(u0 - h);
+ final double d2 = d.f(u0 + 2 * h) - d.f(u0 - 2 * h);
+ return new DifferentialPair(ft, t.getU1() * (8 * d1 - d2) / denominator);
+ }
+ };
+ }
+
+}
Propchange: commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/FourPointsScheme.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/SixPointsScheme.java
URL: http://svn.apache.org/viewvc/commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/SixPointsScheme.java?rev=648235&view=auto
==============================================================================
--- commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/SixPointsScheme.java (added)
+++ commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/SixPointsScheme.java Tue Apr 15 06:17:43 2008
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.nabla.differences;
+
+import org.apache.commons.nabla.core.DifferentialPair;
+import org.apache.commons.nabla.core.UnivariateDerivative;
+import org.apache.commons.nabla.core.UnivariateDifferentiable;
+
+/** Six-points finite differences scheme.
+ * The error model for the six-points scheme is
+ * <code>h<sup>6</sup>/140 f<sup>(7)</sup>(x) + O(h<sup>8</sup>)</code>.
+ */
+public class SixPointsScheme extends FiniteDifferencesDifferentiator {
+
+ /** Serializable UID. */
+ private static final long serialVersionUID = -3054982833938680212L;
+
+ /** Scheme denominator. */
+ private final double denominator;
+
+ /** Build a 6-points finite differences scheme.
+ * @param h differences step size
+ */
+ public SixPointsScheme(final double h) {
+ super(h, h * h * h * h * h * h / 140);
+ denominator = 60 * h;
+ }
+
+ /** {@inheritDoc} */
+ public UnivariateDerivative differentiate(final UnivariateDifferentiable d) {
+ return new UnivariateDerivative() {
+ public UnivariateDifferentiable getPrimitive() {
+ return d;
+ }
+ public DifferentialPair f(final DifferentialPair t) {
+ final double h = getStepSize();
+ final double u0 = t.getU0();
+ final double ft = d.f(u0);
+ final double d1 = d.f(u0 + h) - d.f(u0 - h);
+ final double d2 = d.f(u0 + 2 * h) - d.f(u0 - 2 * h);
+ final double d3 = d.f(u0 + 3 * h) - d.f(u0 - 3 * h);
+ return new DifferentialPair(ft, t.getU1() * (d3 - 9 * d2 + 45 * d1) / denominator);
+ }
+ };
+ }
+
+}
Propchange: commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/SixPointsScheme.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/TwoPointsScheme.java
URL: http://svn.apache.org/viewvc/commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/TwoPointsScheme.java?rev=648235&view=auto
==============================================================================
--- commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/TwoPointsScheme.java (added)
+++ commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/TwoPointsScheme.java Tue Apr 15 06:17:43 2008
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.nabla.differences;
+
+import org.apache.commons.nabla.core.DifferentialPair;
+import org.apache.commons.nabla.core.UnivariateDerivative;
+import org.apache.commons.nabla.core.UnivariateDifferentiable;
+
+/** Two-points finite differences scheme.
+ * The error model for the two-points scheme is
+ * <code>h<sup>2</sup>/6 f<sup>(3)</sup>(x) + O(h<sup>4</sup>)</code>.
+ */
+public class TwoPointsScheme extends FiniteDifferencesDifferentiator {
+
+ /** Serializable UID. */
+ private static final long serialVersionUID = -3932881915416655202L;
+
+ /** Scheme denominator. */
+ private final double denominator;
+
+ /** Build a 2-points finite differences scheme.
+ * @param h differences step size
+ */
+ public TwoPointsScheme(final double h) {
+ super(h, h * h / 6);
+ denominator = 2 * h;
+ }
+
+ /** {@inheritDoc} */
+ public UnivariateDerivative differentiate(final UnivariateDifferentiable d) {
+ return new UnivariateDerivative() {
+ public UnivariateDifferentiable getPrimitive() {
+ return d;
+ }
+ public DifferentialPair f(final DifferentialPair t) {
+ final double h = getStepSize();
+ final double u0 = t.getU0();
+ final double ft = d.f(u0);
+ final double d1 = d.f(u0 + h) - d.f(u0 - h);
+ return new DifferentialPair(ft, t.getU1() * d1 / denominator);
+ }
+ };
+ }
+
+}
Propchange: commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/differences/TwoPointsScheme.java
------------------------------------------------------------------------------
svn:eol-style = native