Coverage Report - org.apache.commons.nabla.differences.FourPointsScheme
 
Classes in this File Line Coverage Branch Coverage Complexity
FourPointsScheme
100%
5/5
N/A
1
FourPointsScheme$1
100%
8/8
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  
 /** Four-points finite differences scheme.
 24  
  * The error model for the four-points scheme is
 25  
  * <code>-2h<sup>4</sup>/5 f<sup>(5)</sup>(x) + O(h<sup>6</sup>)</code>.
 26  
  */
 27  441
 public class FourPointsScheme extends FiniteDifferencesDifferentiator {
 28  
 
 29  
     /** Serializable UID. */
 30  
     private static final long serialVersionUID = 3216911780962952859L;
 31  
 
 32  
     /** Scheme denominator. */
 33  
     private final double denominator;
 34  
 
 35  
     /** Build a 4-points finite differences scheme.
 36  
      * @param h differences step size
 37  
      */
 38  
     public FourPointsScheme(final double h) {
 39  129
         super(h, -2 * h * h * h * h / 5, 5);
 40  129
         denominator = 12 * h;
 41  129
     }
 42  
 
 43  
     /** {@inheritDoc} */
 44  
     public UnivariateDerivative differentiate(final UnivariateDifferentiable d) {
 45  129
         return new UnivariateDerivative() {
 46  
             public UnivariateDifferentiable getPrimitive() {
 47  1
                 return d;
 48  
             }
 49  
             public DifferentialPair f(final DifferentialPair t) {
 50  441
                 final double h = getStepSize();
 51  441
                 final double u0 = t.getValue();
 52  441
                 final double ft = d.f(u0);
 53  441
                 final double d1 = d.f(u0 +     h) - d.f(u0 -     h);
 54  441
                 final double d2 = d.f(u0 + 2 * h) - d.f(u0 - 2 * h);
 55  441
                 return new DifferentialPair(ft, t.getFirstDerivative() * (8 * d1 - d2) / denominator);
 56  
             }
 57  
         };
 58  
     }
 59  
 
 60  
 }