ningshuxia
2022-12-12 e78f5936fee9ab4fff600515bb20a41a28f329c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// <copyright file="ZeroCrossingBracketing.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
 
using System;
using System.Collections.Generic;
 
namespace IStation.Numerics.RootFinding
{
    public static class ZeroCrossingBracketing
    {
        public static IEnumerable<Tuple<double, double>> FindIntervalsWithin(Func<double, double> f, double lowerBound, double upperBound, int subdivisions)
        {
            // TODO: Consider binary-style search instead of linear scan
            double fmin = f(lowerBound);
            double fmax = f(upperBound);
 
            if (Math.Sign(fmin) != Math.Sign(fmax))
            {
                yield return new Tuple<double, double>(lowerBound, upperBound);
                yield break;
            }
 
            double subdiv = (upperBound - lowerBound)/subdivisions;
            double smin = lowerBound;
            int sign = Math.Sign(fmin);
 
            for (int k = 0; k < subdivisions; k++)
            {
                double smax = smin + subdiv;
                double sfmax = f(smax);
                if (double.IsInfinity(sfmax))
                {
                    // expand interval to include pole
                    smin = smax;
                    continue;
                }
 
                if (Math.Sign(sfmax) != sign)
                {
                    yield return new Tuple<double, double>(smin, smax);
                    sign = Math.Sign(sfmax);
                }
 
                smin = smax;
            }
        }
 
        /// <summary>Detect a range containing at least one root.</summary>
        /// <param name="f">The function to detect roots from.</param>
        /// <param name="lowerBound">Lower value of the range.</param>
        /// <param name="upperBound">Upper value of the range</param>
        /// <param name="factor">The growing factor of research. Usually 1.6.</param>
        /// <param name="maxIterations">Maximum number of iterations. Usually 50.</param>
        /// <returns>True if the bracketing operation succeeded, false otherwise.</returns>
        /// <remarks>This iterative methods stops when two values with opposite signs are found.</remarks>
        public static bool Expand(Func<double, double> f, ref double lowerBound, ref double upperBound, double factor = 1.6, int maxIterations = 50)
        {
            double originalLowerBound = lowerBound;
            double originalUpperBound = upperBound;
 
            if (lowerBound >= upperBound)
            {
                throw new ArgumentOutOfRangeException(nameof(upperBound), "xmax must be greater than xmin.");
            }
 
            double fmin = f(lowerBound);
            double fmax = f(upperBound);
 
            for (int i = 0; i < maxIterations; i++)
            {
                if (Math.Sign(fmin) != Math.Sign(fmax))
                {
                    return true;
                }
 
                if (Math.Abs(fmin) < Math.Abs(fmax))
                {
                    lowerBound += factor*(lowerBound - upperBound);
                    fmin = f(lowerBound);
                }
                else
                {
                    upperBound += factor*(upperBound - lowerBound);
                    fmax = f(upperBound);
                }
            }
 
            lowerBound = originalLowerBound;
            upperBound = originalUpperBound;
            return false;
        }
 
        public static bool Reduce(Func<double, double> f, ref double lowerBound, ref double upperBound, int subdivisions = 1000)
        {
            double originalLowerBound = lowerBound;
            double originalUpperBound = upperBound;
 
            if (lowerBound >= upperBound)
            {
                throw new ArgumentOutOfRangeException(nameof(upperBound), "xmax must be greater than xmin.");
            }
 
            // TODO: Consider binary-style search instead of linear scan
            double fmin = f(lowerBound);
            double fmax = f(upperBound);
 
            if (Math.Sign(fmin) != Math.Sign(fmax))
            {
                return true;
            }
 
            double subdiv = (upperBound - lowerBound) / subdivisions;
            double smin = lowerBound;
            int sign = Math.Sign(fmin);
 
            for (int k = 0; k < subdivisions; k++)
            {
                double smax = smin + subdiv;
                double sfmax = f(smax);
                if (double.IsInfinity(sfmax))
                {
                    // expand interval to include pole
                    smin = smax;
                    continue;
                }
 
                if (Math.Sign(sfmax) != sign)
                {
                    lowerBound = smin;
                    upperBound = smax;
                    return true;
                }
 
                smin = smax;
            }
 
            lowerBound = originalLowerBound;
            upperBound = originalUpperBound;
            return false;
        }
 
        public static bool ExpandReduce(Func<double, double> f, ref double lowerBound, ref double upperBound, double expansionFactor = 1.6, int expansionMaxIterations = 50, int reduceSubdivisions = 100)
        {
            return Expand(f, ref lowerBound, ref upperBound, expansionFactor, expansionMaxIterations) || Reduce(f, ref lowerBound, ref upperBound, reduceSubdivisions);
        }
    }
}