Debian Codegraph @

src/dr/math/iterations/BisectionZeroFinder.java

raw symbols SCIP
1 /*
2 * BisectionZeroFinder.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.interfaces.OneVariableFunction;
29
30 /**
31 * Zero finding by bisection.
32 *
33 * @author Didier H. Besset
34 */
35 public class BisectionZeroFinder extends FunctionalIterator
36 {
37 /**
38 * Value at which the function's value is negative.
39 */
40 private double xNeg;
41
42 /**
43 * Value at which the function's value is positive.
44 */
45 private double xPos;
46
47 /**
48 * @param func DhbInterfaces.OneVariableFunction
49 */
50 public BisectionZeroFinder(OneVariableFunction func) {
51 super(func);
52 }
53
54 /**
55 * @param func DhbInterfaces.OneVariableFunction
56 * @param x1 location at which the function yields a negative value
57 * @param x2 location at which the function yields a positive value
58 */
59 public BisectionZeroFinder( OneVariableFunction func, double x1, double x2)
60 throws IllegalArgumentException
61 {
62 this(func);
63 setNegativeX( x1);
64 setPositiveX( x2);
65 }
66
67 /**
68 * @return double
69 */
70 public double evaluateIteration()
71 {
72 result = ( xPos + xNeg) * 0.5;
73 if ( f.value(result) > 0 )
74 xPos = result;
75 else
76 xNeg = result;
77 return relativePrecision( Math.abs( xPos - xNeg));
78 }
79
80 /**
81 * @param x double
82 * @exception java.lang.IllegalArgumentException
83 * if the function's value is not negative
84 */
85 public void setNegativeX( double x) throws IllegalArgumentException
86 {
87 if ( f.value( x) > 0 )
88 throw new IllegalArgumentException( "f("+x+
89 ") is positive instead of negative");
90 xNeg = x;
91 }
92
93 /**
94 * (c) Copyrights Didier BESSET, 1999, all rights reserved.
95 * @param x double
96 * @exception java.lang.IllegalArgumentException
97 * if the function's value is not positive
98 */
99 public void setPositiveX( double x) throws IllegalArgumentException
100 {
101 if ( f.value( x) < 0 )
102 throw new IllegalArgumentException( "f("+x+
103 ") is negative instead of positive");
104 xPos = x;
105 }
106 }