Coverage Report - org.apache.commons.nabla.differences.FiniteDifferencesDifferentiator
 
Classes in this File Line Coverage Branch Coverage Complexity
FiniteDifferencesDifferentiator
88%
7/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 java.io.Serializable;
 20  
 
 21  
 import org.apache.commons.nabla.core.UnivariateDifferentiator;
 22  
 
 23  
 /** This class represents a differentiation scheme based on finite differences.
 24  
  * <p>The differences are based on regularly distributed points around the
 25  
  * central test point according to a given h step.
 26  
  * For example a four points scheme would use a linear combination of points
 27  
  * <code>f(x-2h)</code>, <code>f(x-h)</code>, <code>f(x+h)</code> and
 28  
  * <code>f(x+2h)</code> to compute the first derivative <code>f'(x)</code>
 29  
  * (note that <code>f(x)</code> by itself is not used for the evaluation of
 30  
  * <code>f'(x)</code>, but is used to compute the value of the function
 31  
  * <code>f(x)</code).</p>
 32  
  * <p>The computed differential is only an approximation. The error depends on
 33  
  * the value of the high order derivatives of the function which were not
 34  
  * canceled by the linear combination, the number of points and the step size.
 35  
  * For example the four points scheme cancels the
 36  
  * <code>f<sup>(2)</sup></code>, <code>f<sup>(3)</sup></code> and
 37  
  * <code>f<sup>(4)</sup></code> high order derivatives but not the
 38  
  * <code>f<sup>(5)</sup></code> and higher order terms. The theoretical error
 39  
  * model for this scheme is <code>-2h<sup>4</sup>/5 f<sup>(5)</sup>(x) +
 40  
  * O(h<sup>6</sup>)</code>. This model doesn't take into account numerical
 41  
  * cancellation which occur when the steps are too small.</p>
 42  
  * <p>The general expression of the theoretical error model is
 43  
  * <code>A f<sup>(k)</sup> + O(h<sup>k+1</sup>)</code> where <code>A</code>
 44  
  * is the value returned by {@link #getSignedErrorScaleFactor()} and
 45  
  * <code>k</code> is the value returned by {@link #getOrderFirstNonCanceled()}.</p>
 46  
  */
 47  
 public abstract class FiniteDifferencesDifferentiator
 48  
     implements UnivariateDifferentiator, Serializable {
 49  
 
 50  
     /** Step size. */
 51  
     private final double stepSize;
 52  
 
 53  
     /** Error scale factor. */
 54  
     private final double factor;
 55  
 
 56  
     /** Order of the first non-canceled derivative. */
 57  
     private final int order;
 58  
 
 59  
     /** Simple constructor.
 60  
      * @param stepSize step stepSize
 61  
      * @param factor error scale factor
 62  
      * @param order order of the first non-canceled derivative
 63  
      */
 64  
     protected FiniteDifferencesDifferentiator(final double stepSize,
 65  
                                               final double factor,
 66  3268
                                               final int order) {
 67  3268
         this.stepSize = stepSize;
 68  3268
         this.factor   = factor;
 69  3268
         this.order    = order;
 70  3268
     }
 71  
 
 72  
     /** Get the signed error scale factor for the finite differences scheme.
 73  
      * <p>The error scale factor is the value of the h-dependent
 74  
      * factor of the first non-canceled high order differential of the function.
 75  
      * For example since the error model for the four points scheme is
 76  
      * <code>-2h<sup>4</sup>/5 f<sup>(5)</sup>(x) + O(h<sup>6</sup>)</code>,
 77  
      * the signed error scale factor is <code>-2h<sup>4</sup>/5</code>.</p>
 78  
      * @return error signed scale factor
 79  
      * @see #getOrderFirstNonCanceled()
 80  
      */
 81  
     public double getSignedErrorScaleFactor() {
 82  394
         return factor;
 83  
     }
 84  
 
 85  
     /** Get the order of the first non-canceled derivative.
 86  
      * <p>The error model of the scheme depends on the first non-canceled
 87  
      * high order differential of the function. For example since the error
 88  
      * model for the four points scheme is <code>-2h<sup>4</sup>/5
 89  
      * f<sup>(5)</sup>(x) + O(h<sup>6</sup>)</code>, the order of the first
 90  
      * non-canceled derivative is 5.
 91  
      * @return order of the first non-canceled derivative
 92  
      * @see #getSignedErrorScaleFactor()
 93  
      */
 94  
     public int getOrderFirstNonCanceled() {
 95  0
         return order;
 96  
     }
 97  
 
 98  
     /** Get the step size.
 99  
      * @return step size
 100  
      */
 101  
     protected double getStepSize() {
 102  4524
         return stepSize;
 103  
     }
 104  
 
 105  
 }