Coverage Report - org.apache.commons.nabla.differences.EightPointsScheme
 
Classes in this File Line Coverage Branch Coverage Complexity
EightPointsScheme
100%
5/5
N/A
1
EightPointsScheme$1
100%
10/10
N/A
1
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.nabla.differences;
 18  
 
 19  
 import org.apache.commons.nabla.core.DifferentialPair;
 20  
 import org.apache.commons.nabla.core.UnivariateDerivative;
 21  
 import org.apache.commons.nabla.core.UnivariateDifferentiable;
 22  
 
 23  
 /** Eight-points finite differences scheme.
 24  
  * The error model for the eight-points scheme is
 25  
  * <code>-h<sup>8</sup>/630 f<sup>(9)</sup>(x) + O(h<sup>10</sup>)</code>.
 26  
  */
 27  3291
 public class EightPointsScheme extends FiniteDifferencesDifferentiator {
 28  
 
 29  
     /** Serializable UID. */
 30  
     private static final long serialVersionUID = 3498050520014763473L;
 31  
 
 32  
     /** Scheme denominator. */
 33  
     private final double denominator;
 34  
 
 35  
     /** Build an 8-points finite differences scheme.
 36  
      * @param h differences step size
 37  
      */
 38  
     public EightPointsScheme(final double h) {
 39  2971
         super(h, -h * h * h * h * h * h * h * h / 630, 9);
 40  2971
         denominator = 840 * h;
 41  2971
     }
 42  
 
 43  
     /** {@inheritDoc} */
 44  
     public UnivariateDerivative differentiate(final UnivariateDifferentiable d) {
 45  2971
         return new UnivariateDerivative() {
 46  
             public UnivariateDifferentiable getPrimitive() {
 47  1
                 return d;
 48  
             }
 49  
             public DifferentialPair f(final DifferentialPair t) {
 50  3291
                 final double h = getStepSize();
 51  3291
                 final double u0 = t.getValue();
 52  3291
                 final double ft = d.f(u0);
 53  3291
                 final double d1 = d.f(u0 +     h) - d.f(u0 -     h);
 54  3291
                 final double d2 = d.f(u0 + 2 * h) - d.f(u0 - 2 * h);
 55  3291
                 final double d3 = d.f(u0 + 3 * h) - d.f(u0 - 3 * h);
 56  3291
                 final double d4 = d.f(u0 + 4 * h) - d.f(u0 - 4 * h);
 57  3291
                 return new DifferentialPair(ft, t.getFirstDerivative() * (-3 * d4 + 32 * d3 - 168 * d2 + 672 * d1) / denominator);
 58  
             }
 59  
         };
 60  
     }
 61  
 
 62  
 }