Chaste Commit::baa90ac2819b962188b7562f2326be23c47859a7
MathsCustomFunctions.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
37
38#include <cassert>
39#include <cmath>
40#include <iostream>
41
42double SmallPow(double x, unsigned exponent)
43{
44 switch (exponent)
45 {
46 case 0:
47 {
48 return 1.0;
49 }
50 case 1:
51 {
52 return x;
53 }
54 case 2:
55 {
56 return x * x;
57 }
58 case 3:
59 {
60 return x * x * x;
61 }
62 case 4:
63 {
64 return x * x * x * x;
65 }
66 default:
67 {
68 return std::pow(x, static_cast<double>(exponent));
69 }
70 }
71}
72unsigned SmallPow(unsigned x, unsigned exponent)
73{
74 switch (exponent)
75 {
76 case 0:
77 {
78 return 1u;
79 }
80 case 1:
81 {
82 return x;
83 }
84 case 2:
85 {
86 return x*x;
87 }
88 case 3:
89 {
90 return x*x*x;
91 }
92 default:
93 {
94 if (exponent % 2 == 0)
95 {
96 // Even power
97 unsigned partial_answer = SmallPow(x, exponent/2);
98 return partial_answer*partial_answer;
99 }
100 else
101 {
102 // Odd power
103 return SmallPow(x, exponent-1)*x;
104 }
105 }
106 }
107}
108
109bool Divides(double smallerNumber, double largerNumber)
110{
111 double remainder = fmod(largerNumber, smallerNumber);
112 /*
113 * Is the remainder close to zero? Note that the comparison is scaled
114 * with respect to the larger of the numbers.
115 */
116 if (remainder < DBL_EPSILON*largerNumber)
117 {
118 return true;
119 }
120 /*
121 * Is the remainder close to smallerNumber? Note that the comparison
122 * is scaled with respect to the larger of the numbers.
123 */
124 if (fabs(remainder-smallerNumber) < DBL_EPSILON*largerNumber)
125 {
126 return true;
127 }
128
129 return false;
130}
131
132unsigned CeilDivide(unsigned numerator, unsigned denominator)
133{
134 if (numerator == 0u)
135 {
136 return 0u;
137 }
138 else
139 {
140 // Overflow-safe for large numbers, but not valid for numerator==0.
141 return ((numerator - 1u) / denominator) + 1u;
142 }
143}
144
145double Signum(double value)
146{
147 return (0.0 < value) - (value < 0.0);
148}
149
150bool CompareDoubles::IsNearZero(double number, double tolerance)
151{
152 return fabs(number) <= fabs(tolerance);
153}
154
155double SafeDivide(double numerator, double divisor)
156{
157 // Avoid overflow
158 if (divisor < 1.0 && numerator > divisor*DBL_MAX)
159 {
160 return DBL_MAX;
161 }
162
163 // Avoid underflow
164 if (numerator == 0.0 || (divisor > 1.0 && numerator < divisor*DBL_MIN))
165 {
166 return 0.0;
167 }
168
169 return numerator/divisor;
170}
171
172unsigned AdvanceMod(const unsigned currentLocation, const int increment, const std::size_t range) noexcept
173{
174 int new_pos = currentLocation + increment;
175
176 while (new_pos < 0)
177 {
178 new_pos += range;
179 }
180 while (new_pos >= static_cast<int>(range))
181 {
182 new_pos -= range;
183 }
184
185 return static_cast<unsigned>(new_pos);
186}
187
188unsigned SmallDifferenceMod(const unsigned idxA, const unsigned idxB, const std::size_t range) noexcept
189{
190 assert(idxA < range);
191 assert(idxB < range);
192
193 const unsigned min = idxA < idxB ? idxA : idxB;
194 const unsigned max = idxA < idxB ? idxB : idxA;
195
196 return std::min<unsigned>(max - min, (range + min) - max);
197}
198
199bool CompareDoubles::WithinRelativeTolerance(double number1, double number2, double tolerance)
200{
201 double difference = fabs(number1 - number2);
202 double d1 = SafeDivide(difference, fabs(number1));
203 double d2 = SafeDivide(difference, fabs(number2));
204
205 return d1 <= tolerance && d2 <= tolerance;
206}
207
208bool CompareDoubles::WithinAbsoluteTolerance(double number1, double number2, double tolerance)
209{
210 return fabs(number1 - number2) <= tolerance;
211}
212
213bool CompareDoubles::WithinAnyTolerance(double number1, double number2, double relTol, double absTol, bool printError)
214{
215 bool ok = WithinAbsoluteTolerance(number1, number2, absTol) || WithinRelativeTolerance(number1, number2, relTol);
216 if (printError && !ok)
217 {
218 std::cout << "CompareDoubles::WithinAnyTolerance: " << number1 << " and " << number2
219 << " differ by more than relative tolerance of " << relTol
220 << " and absolute tolerance of " << absTol << std::endl;
221 }
222 return ok;
223}
224
225bool CompareDoubles::WithinTolerance(double number1, double number2, double tolerance, bool toleranceIsAbsolute)
226{
227 bool ok;
228 if (toleranceIsAbsolute)
229 {
230 ok = WithinAbsoluteTolerance(number1, number2, tolerance);
231 }
232 else
233 {
234 ok = WithinRelativeTolerance(number1, number2, tolerance);
235 }
236 if (!ok)
237 {
238 std::cout << "CompareDoubles::WithinTolerance: " << number1 << " and " << number2
239 << " differ by more than " << (toleranceIsAbsolute ? "absolute" : "relative")
240 << " tolerance of " << tolerance << std::endl;
241 }
242 return ok;
243}
244
245double CompareDoubles::Difference(double number1, double number2, bool toleranceIsAbsolute)
246{
247 if (toleranceIsAbsolute)
248 {
249 return fabs(number1 - number2);
250 }
251 else
252 {
253 double difference = fabs(number1 - number2);
254 double d1 = SafeDivide(difference, fabs(number1));
255 double d2 = SafeDivide(difference, fabs(number2));
256 return d1 > d2 ? d1 : d2;
257 }
258}
double SafeDivide(double numerator, double divisor)
static bool IsNearZero(double number, double tolerance)
static bool WithinAnyTolerance(double number1, double number2, double relTol=DBL_EPSILON, double absTol=DBL_EPSILON, bool printError=false)
static bool WithinTolerance(double number1, double number2, double tolerance, bool toleranceIsAbsolute)
static bool WithinRelativeTolerance(double number1, double number2, double tolerance)
static double Difference(double number1, double number2, bool toleranceIsAbsolute)
static bool WithinAbsoluteTolerance(double number1, double number2, double tolerance)