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