| 1 | /* |
| 2 | * NewtonZeroFinder.java |
| 3 | * |
| 4 | * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard |
| 5 | * |
| 6 | * This file is part of BEAST. |
| 7 | * See the NOTICE file distributed with this work for additional |
| 8 | * information regarding copyright ownership and licensing. |
| 9 | * |
| 10 | * BEAST is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Lesser General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 |
| 13 | * of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * BEAST is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with BEAST; if not, write to the |
| 22 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 23 | * Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | package dr.math.iterations; |
| 27 | |
| 28 | import dr.math.MathUtils; |
| 29 | import dr.math.functionEval.DrMath; |
| 30 | import dr.math.functionEval.FunctionDerivative; |
| 31 | import dr.math.interfaces.OneVariableFunction; |
| 32 | /** |
| 33 | * Finds the zeroes of a function using Newton approximation. |
| 34 | * Note: the zero of a function if the value at which the function's |
| 35 | * value is zero. |
| 36 | * |
| 37 | * @author Didier H. Besset |
| 38 | */ |
| 39 | public class NewtonZeroFinder extends FunctionalIterator |
| 40 | { |
| 41 | /** |
| 42 | * Derivative of the function for which the zero will be found. |
| 43 | */ |
| 44 | private OneVariableFunction df; |
| 45 | /** |
| 46 | * Constructor method. |
| 47 | * @param func the function for which the zero will be found. |
| 48 | * @param start the initial value for the search. |
| 49 | */ |
| 50 | public NewtonZeroFinder( OneVariableFunction func, double start) |
| 51 | { |
| 52 | super( func); |
| 53 | setStartingValue( start); |
| 54 | } |
| 55 | /** |
| 56 | * Constructor method. |
| 57 | * @param func the function for which the zero will be found. |
| 58 | * @param dFunc derivative of func. |
| 59 | * @param start the initial value for the search. |
| 60 | */ |
| 61 | public NewtonZeroFinder( OneVariableFunction func, |
| 62 | OneVariableFunction dFunc, double start) |
| 63 | throws IllegalArgumentException |
| 64 | { |
| 65 | this( func, start); |
| 66 | setDerivative( dFunc); |
| 67 | } |
| 68 | /** |
| 69 | * Evaluate the result of the current interation. |
| 70 | * @return the estimated precision of the result. |
| 71 | */ |
| 72 | public double evaluateIteration() |
| 73 | { |
| 74 | double delta = f.value( result) / df.value( result); |
| 75 | result -= delta; |
| 76 | return relativePrecision( Math.abs( delta)); |
| 77 | } |
| 78 | /** |
| 79 | * Initializes internal parameters to start the iterative process. |
| 80 | * Assigns default derivative if necessary. |
| 81 | */ |
| 82 | public void initializeIterations() |
| 83 | { |
| 84 | if ( df == null) |
| 85 | df = new FunctionDerivative( f); |
| 86 | if ( Double.isNaN( result) ) |
| 87 | result = 0; |
| 88 | int n = 0; |
| 89 | while ( DrMath.equal( df.value( result), 0) ) |
| 90 | { |
| 91 | if ( ++n > getMaximumIterations() ) |
| 92 | break; |
| 93 | result += MathUtils.nextDouble(); |
| 94 | } |
| 95 | } |
| 96 | /** |
| 97 | * (c) Copyrights Didier BESSET, 1999, all rights reserved. |
| 98 | * @param dFunc DhbInterfaces.OneVariableFunction |
| 99 | * @exception java.lang.IllegalArgumentException |
| 100 | * if the derivative is not accurate. |
| 101 | */ |
| 102 | public void setDerivative( OneVariableFunction dFunc) |
| 103 | throws IllegalArgumentException |
| 104 | { |
| 105 | df = new FunctionDerivative( f); |
| 106 | if ( !DrMath.equal( df.value( result), dFunc.value( result), 0.001) ) |
| 107 | throw new IllegalArgumentException |
| 108 | ( "Supplied derative function is inaccurate"); |
| 109 | df = dFunc; |
| 110 | } |
| 111 | /** |
| 112 | * (c) Copyrights Didier BESSET, 1999, all rights reserved. |
| 113 | */ |
| 114 | public void setFunction( OneVariableFunction func) |
| 115 | { |
| 116 | super.setFunction( func); |
| 117 | df = null; |
| 118 | } |
| 119 | /** |
| 120 | * Defines the initial value for the search. |
| 121 | */ |
| 122 | public void setStartingValue( double start) |
| 123 | { |
| 124 | result = start; |
| 125 | } |
| 126 | } |