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