Skip to content

Commit 16f8f92

Browse files
authored
Merge pull request #3 from SimpleArt/bug-double-imprecision-errors
Added bounds checking
2 parents 1f1ac6a + d994d82 commit 16f8f92

File tree

3 files changed

+193
-1
lines changed

3 files changed

+193
-1
lines changed

pyroot/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@
4949
# Things for export.
5050
__all__ = ["root_in", "root_iter"]
5151

52-
__version__ = "0.3.0"
52+
__version__ = "0.3.1"

pyroot/_pyroot_new.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,12 @@ def chandrupatla_in(
542542
x2 = x
543543
y2 = y
544544
x = secant(x1, x2, y1, y2)
545+
if x1 < x2:
546+
if x < x1:
547+
x = x1
548+
else:
549+
if x > x2:
550+
x = x2
545551
# Use the secant method on the final iteration for high precision.
546552
return secant(x1, x2, y1, y2)
547553

@@ -687,6 +693,12 @@ def chandrupatla_iter(
687693
x2 = x
688694
y2 = y
689695
x = secant(x1, x2, y1, y2)
696+
if x1 < x2:
697+
if x < x1:
698+
x = x1
699+
else:
700+
if x > x2:
701+
x = x2
690702
# Use the secant method on the final iteration for high precision.
691703
yield secant(x1, x2, y1, y2)
692704

@@ -799,6 +811,12 @@ def heun_ode_in(
799811
# Fall-back to the secant method if convergence fails.
800812
if not is_between(x1, x, x2):
801813
x = secant(x1, x2, y1, y2)
814+
if x1 < x2:
815+
if x < x1:
816+
x = x1
817+
else:
818+
if x > x2:
819+
x = x2
802820
# Use the secant method on the final iteration for high precision.
803821
return secant(x1, x2, y1, y2)
804822

@@ -914,6 +932,12 @@ def heun_ode_iter(
914932
# Fall-back to the secant method if convergence fails.
915933
if not is_between(x1, x, x2):
916934
x = secant(x1, x2, y1, y2)
935+
if x1 < x2:
936+
if x < x1:
937+
x = x1
938+
else:
939+
if x > x2:
940+
x = x2
917941
# Use the secant method on the final iteration for high precision.
918942
yield secant(x1, x2, y1, y2)
919943

@@ -1028,6 +1052,12 @@ def midpoint_ode_in(
10281052
# Fall-back to the secant method if convergence fails.
10291053
if not is_between(x1, x, x2):
10301054
x = secant(x1, x2, y1, y2)
1055+
if x1 < x2:
1056+
if x < x1:
1057+
x = x1
1058+
else:
1059+
if x > x2:
1060+
x = x2
10311061
# Use the secant method on the final iteration for high precision.
10321062
return secant(x1, x2, y1, y2)
10331063

@@ -1149,6 +1179,12 @@ def midpoint_ode_iter(
11491179
# Fall-back to the secant method if convergence fails.
11501180
if not is_between(x1, x, x2):
11511181
x = secant(x1, x2, y1, y2)
1182+
if x1 < x2:
1183+
if x < x1:
1184+
x = x1
1185+
else:
1186+
if x > x2:
1187+
x = x2
11521188
# Use the secant method on the final iteration for high precision.
11531189
yield secant(x1, x2, y1, y2)
11541190

@@ -1246,6 +1282,12 @@ def newt_ode_in(
12461282
# Fall-back to the secant method if convergence fails.
12471283
if not is_between(x1, x, x2):
12481284
x = secant(x1, x2, y1, y2)
1285+
if x1 < x2:
1286+
if x < x1:
1287+
x = x1
1288+
else:
1289+
if x > x2:
1290+
x = x2
12491291
# Use the secant method on the final iteration for high precision.
12501292
return secant(x1, x2, y1, y2)
12511293

@@ -1346,6 +1388,12 @@ def newt_ode_iter(
13461388
# Fall-back to the secant method if convergence fails.
13471389
if not is_between(x1, x, x2):
13481390
x = secant(x1, x2, y1, y2)
1391+
if x1 < x2:
1392+
if x < x1:
1393+
x = x1
1394+
else:
1395+
if x > x2:
1396+
x = x2
13491397
# Use the secant method on the final iteration for high precision.
13501398
yield secant(x1, x2, y1, y2)
13511399

@@ -1433,6 +1481,12 @@ def newt_safe_in(
14331481
# Fall-back to the secant method if convergence fails.
14341482
if not is_between(x1, x, x2):
14351483
x = secant(x1, x2, y1, y2)
1484+
if x1 < x2:
1485+
if x < x1:
1486+
x = x1
1487+
else:
1488+
if x > x2:
1489+
x = x2
14361490
# Use the secant method on the final iteration for high precision.
14371491
return secant(x1, x2, y1, y2)
14381492

@@ -1523,6 +1577,12 @@ def newt_safe_iter(
15231577
# Fall-back to the secant method if convergence fails.
15241578
if not is_between(x1, x, x2):
15251579
x = secant(x1, x2, y1, y2)
1580+
if x1 < x2:
1581+
if x < x1:
1582+
x = x1
1583+
else:
1584+
if x > x2:
1585+
x = x2
15261586
# Use the secant method on the final iteration for high precision.
15271587
yield secant(x1, x2, y1, y2)
15281588

@@ -1643,6 +1703,12 @@ def nonsimple_in(
16431703
x2 = x
16441704
y2 = y
16451705
x = secant(x1, x2, y1, y2, power)
1706+
if x1 < x2:
1707+
if x < x1:
1708+
x = x1
1709+
else:
1710+
if x > x2:
1711+
x = x2
16461712
# Use the secant method on the final iteration for high precision.
16471713
if 0.6 < power < 1.4:
16481714
return secant(x1, x2, y1, y2)
@@ -1769,6 +1835,12 @@ def nonsimple_iter(
17691835
x2 = x
17701836
y2 = y
17711837
x = secant(x1, x2, y1, y2, power)
1838+
if x1 < x2:
1839+
if x < x1:
1840+
x = x1
1841+
else:
1842+
if x > x2:
1843+
x = x2
17721844
# Use the secant method on the final iteration for high precision.
17731845
if 0.6 < power < 1.4:
17741846
yield secant(x1, x2, y1, y2)
@@ -1873,6 +1945,12 @@ def rk45_ode_in(
18731945
# Fall-back to the secant method if convergence fails.
18741946
if not is_between(x1, x, x2):
18751947
x = secant(x1, x2, y1, y2)
1948+
if x1 < x2:
1949+
if x < x1:
1950+
x = x1
1951+
else:
1952+
if x > x2:
1953+
x = x2
18761954
# Use the secant method on the final iteration for high precision.
18771955
return secant(x1, x2, y1, y2)
18781956

@@ -1977,6 +2055,12 @@ def rk45_ode_iter(
19772055
# Fall-back to the secant method if convergence fails.
19782056
if not is_between(x1, x, x2):
19792057
x = secant(x1, x2, y1, y2)
2058+
if x1 < x2:
2059+
if x < x1:
2060+
x = x1
2061+
else:
2062+
if x > x2:
2063+
x = x2
19802064
# Use the secant method on the final iteration for high precision.
19812065
yield secant(x1, x2, y1, y2)
19822066

@@ -2080,6 +2164,12 @@ def secant_in(
20802164
x2 = x
20812165
y2 = y
20822166
x = secant(x1, x2, y1, y2)
2167+
if x1 < x2:
2168+
if x < x1:
2169+
x = x1
2170+
else:
2171+
if x > x2:
2172+
x = x2
20832173
# Use the secant method on the final iteration for high precision.
20842174
return secant(x1, x2, y1, y2)
20852175

@@ -2186,6 +2276,12 @@ def secant_iter(
21862276
x2 = x
21872277
y2 = y
21882278
x = secant(x1, x2, y1, y2)
2279+
if x1 < x2:
2280+
if x < x1:
2281+
x = x1
2282+
else:
2283+
if x > x2:
2284+
x = x2
21892285
# Use the secant method on the final iteration for high precision.
21902286
yield secant(x1, x2, y1, y2)
21912287

0 commit comments

Comments
 (0)