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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
'''Chemical Engineering Design Library (ChEDL). Utilities for process modeling.
Copyright (C) 2021, 2023 Caleb Bell <Caleb.Andrew.Bell@gmail.com>
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.
'''
import pytest
from fluids.numerics import assert_close, assert_close1d
from fluids.numerics.doubledouble import (
add_dd,
add_imag_dd,
cbrt_dd,
cbrt_imag_dd,
cube_dd,
div_dd,
div_imag_dd,
eq_dd,
exp_dd,
ge_dd,
gt_dd,
imag_inv_dd,
intpow_dd,
le_dd,
log_dd,
lt_dd,
mul_dd,
mul_imag_dd,
mul_imag_noerrors_dd,
mul_noerrors_dd,
neq_dd,
pow_dd,
sqrt_dd,
sqrt_imag_dd,
square_dd,
)
try:
import mpmath as mp
has_mpmath = True
mp.mp.dps=100
except:
has_mpmath = False
def mark_mpmath(f):
f = pytest.mark.mpmath(f)
f = pytest.mark.skipif(not has_mpmath, reason='missing mpmath')(f)
return f
def test_cube_dd():
ans = cube_dd(2.718281828459045, 1.4456468917292502e-16)
assert_close1d(ans, (20.085536923187668, -1.8275625525512858e-16), rtol=1e-14)
def test_div_imag_dd():
mp_ans = (-6221711.561975023 - 2.5074521914349666e-10j)/(1247.1686953729213 +2160.157819988352j)
ans = div_imag_dd(-6221711.561975023, 0.0, - 2.5074521914349666e-10, 0.0,
1247.1686953729213, 0.0, 2160.157819988352, 0.0)
assert_close1d(ans, (-1247.168695372921, -7.085386715133776e-14, 2160.1578199883515, -1.7404258749820416e-14), rtol=1e-13)
def test_add_dd():
ans = add_dd(2.718281828459045, 1.4456468917292502e-16, -3.141592653589793, -1.2246467991473532e-16)
assert_close1d(ans, (-0.423310825130748, 2.2100009258189695e-17), rtol=1e-13)
@mark_mpmath
def test_add_dd_mp():
ans = add_dd(2.718281828459045, 1.4456468917292502e-16, -3.141592653589793, -1.2246467991473532e-16)
assert abs(mp.e-mp.pi - (mp.mpf(ans[0]) + mp.mpf(ans[1]))) < 1e-30
@mark_mpmath
def test_mul_dd_mp():
ans = mul_dd(2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16)
assert_close1d(ans, (8.539734222673568, -6.773815290502423e-16), rtol=1e-13)
assert abs(mp.e*mp.pi - (mp.mpf(ans[0]) + mp.mpf(ans[1]))) < 1e-30
def test_div_dd():
ans = div_dd(2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16)
assert_close1d(ans, (0.8652559794322651, 2.1741459631779752e-17), rtol=1e-13)
@mark_mpmath
def test_div_dd_mp():
ans = div_dd(2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16)
assert abs(mp.e/mp.pi - (mp.mpf(ans[0]) + mp.mpf(ans[1]))) < 1e-30
def test_sqrt_dd():
ans = sqrt_dd(2.718281828459045, 1.4456468917292502e-16)
assert_close1d(ans, (1.6487212707001282, -4.731568479435833e-17), rtol=1e-13)
@mark_mpmath
def test_sqrt_dd_mp():
ans = sqrt_dd(2.718281828459045, 1.4456468917292502e-16)
assert abs(mp.sqrt(mp.e) - (mp.mpf(ans[0]) + mp.mpf(ans[1]))) < 1e-30
def test_mul_noerrors_dd():
ans = mul_noerrors_dd(2.718281828, 3.1415926)
assert_close1d(ans, (8.539734075559272, 6.111502792870851e-16), rtol=1e-13)
assert_close1d(ans, mul_dd(2.718281828, 0.0, 3.1415926, 0.0), rtol=1e-13)
def test_square_dd():
ans = square_dd(2.718281828459045, 1.4456468917292502e-16)
assert_close1d(ans, (7.38905609893065, -1.7971139497839148e-16), rtol=1e-13)
@mark_mpmath
def test_square_dd_mp():
ans = square_dd(2.718281828459045, 1.4456468917292502e-16)
assert abs(mp.e**2 - (mp.mpf(ans[0]) + mp.mpf(ans[1]))) < 1e-30
def test_mul_imag_dd():
ans = mul_imag_dd(2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16,
2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16)
assert_close1d(ans, (-2.4805483021587085, 8.193747384776261e-17, 17.079468445347135, -1.3547630581004847e-15), rtol=1e-14)
ans = mul_imag_dd(1.11, 0.00, .027, 0.0, -1.2, 0.0, .2995, 0.0)
assert_close1d(ans, (-1.3400865, -1.0926552718171222e-16, 0.300045, 1.1863843241144421e-17), rtol=1e-14)
@mark_mpmath
def test_mul_imag_dd_mp():
ans = mul_imag_dd(2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16,
2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16)
mp_ans = (mp.e + mp.pi*1j)*(mp.e + mp.pi*1j)
assert abs(mp_ans.real - (mp.mpf(ans[0]) + mp.mpf(ans[1]) )) < 1e-30
assert abs(mp_ans.imag - (mp.mpf(ans[2]) + mp.mpf(ans[3]) )) < 1e-30
def test_mul_imag_noerrors_dd():
ans = mul_imag_noerrors_dd(1.11, .027, -1.2, .2995)
assert_close1d(ans, (-1.3400865, -1.0926552718171222e-16, 0.300045, 1.1863843241144421e-17), rtol=1e-14)
def test_sqrt_imag_dd():
ans = sqrt_imag_dd(2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16)
assert_close1d(ans, (1.853730863795006, 1.8197550334075816e-17, 0.8473702183385572, 4.942630438677856e-17))
ans = sqrt_imag_dd(2.718281828459045, 1.4456468917292502e-16, 0, 0)
assert_close1d(ans, (1.6487212707001282, -4.731568479435833e-17, 0.0, 0.0))
@mark_mpmath
def test_sqrt_imag_dd():
ans = sqrt_imag_dd(2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16)
mp_ans = mp.sqrt(mp.e + mp.pi*1j)
assert abs(mp_ans.real - (mp.mpf(ans[0]) + mp.mpf(ans[1]) )) < 1e-30
assert abs(mp_ans.imag - (mp.mpf(ans[2]) + mp.mpf(ans[3]) )) < 1e-30
ans = sqrt_imag_dd(2.718281828459045, 1.4456468917292502e-16, 0, 0)
mp_ans = mp.sqrt(mp.e)
assert abs(mp_ans.real - (mp.mpf(ans[0]) + mp.mpf(ans[1]) )) < 1e-30
assert abs(mp_ans.imag - (mp.mpf(ans[2]) + mp.mpf(ans[3]) )) < 1e-30
def test_add_imag_dd():
ans = add_imag_dd(2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16,
3.141592653589793, 1.2246467991473532e-16, 2.718281828459045, 1.4456468917292502e-16)
assert_close1d(ans, (5.859874482048839, -1.770598407624023e-16, 5.859874482048839, -1.770598407624023e-16))
@mark_mpmath
def test_add_imag_dd_mp():
ans = add_imag_dd(2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16,
3.141592653589793, 1.2246467991473532e-16, 2.718281828459045, 1.4456468917292502e-16)
mp_ans = (mp.e + mp.pi*1j) + (mp.pi + mp.e*1j)
assert abs(mp_ans.real - (mp.mpf(ans[0]) + mp.mpf(ans[1]) )) < 1e-30
assert abs(mp_ans.imag - (mp.mpf(ans[2]) + mp.mpf(ans[3]) )) < 1e-30
def test_div_imag_dd():
ans = div_imag_dd(2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16,
3.141592653589793, 1.2246467991473532e-16, 2.718281828459045, 1.4456468917292502e-16)
assert_close1d(ans, (0.9896172675351794, -2.57625629153772e-17, 0.14372774191576643, 1.174077497180584e-17), rtol=1e-13)
@mark_mpmath
def test_div_imag_dd_mp():
ans = div_imag_dd(2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16,
3.141592653589793, 1.2246467991473532e-16, 2.718281828459045, 1.4456468917292502e-16)
mp_ans = (mp.e + mp.pi*1j)/(mp.pi + mp.e*1j)
assert abs(mp_ans.real - (mp.mpf(ans[0]) + mp.mpf(ans[1]) )) < 1e-30
assert abs(mp_ans.imag - (mp.mpf(ans[2]) + mp.mpf(ans[3]) )) < 1e-30
def test_imag_inv_dd():
ans = imag_inv_dd(2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16)
assert_close1d(ans, (0.15750247989731844, -3.9415361951446925e-18, -0.18202992367722576, -5.408691854671257e-18), rtol=1e-13)
@mark_mpmath
def test_imag_inv_dd_mp():
ans = imag_inv_dd(2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16)
mp_ans = 1/(mp.e + mp.pi*1j)
assert abs(mp_ans.real - (mp.mpf(ans[0]) + mp.mpf(ans[1]) )) < 1e-30
assert abs(mp_ans.imag - (mp.mpf(ans[2]) + mp.mpf(ans[3]) )) < 1e-30
def test_cbrt_imag_dd():
ans = cbrt_imag_dd(2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16)
assert_close1d(ans, (1.5423370526780396, 8.764992916723807e-17, 0.45326965450036827, 2.105896617565284e-17), rtol=1e-14)
@mark_mpmath
def test_cbrt_imag_dd_mp():
ans = cbrt_imag_dd(2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16)
mp_ans = mp.cbrt(mp.e + mp.pi*1j)
assert abs(mp_ans.real - (mp.mpf(ans[0]) + mp.mpf(ans[1]) )) < 1e-30
assert abs(mp_ans.imag - (mp.mpf(ans[2]) + mp.mpf(ans[3]) )) < 1e-30
def test_cbrt_dd():
ans = cbrt_dd(3, 0.0)
assert_close1d(ans, (1.4422495703074083, 8.054912676113696e-17), rtol=1e-13)
ans = cbrt_dd(1.4422495703074083, 8.054912676113696e-17)
assert_close1d(ans, (1.129830963909753, 1.601807086137469e-17), rtol=1e-13)
@mark_mpmath
def test_cbrt_dd_mp():
ans = cbrt_dd(3, 0.0)
assert abs(mp.mpf(3)**(mp.mpf(1)/mp.mpf(3)) - (mp.mpf(ans[0]) + mp.mpf(ans[1]))) < 1e-30
ans = cbrt_dd(1.4422495703074083, 8.054912676113696e-17)
assert abs((mp.mpf(3)**(mp.mpf(1)/mp.mpf(3)))**(mp.mpf(1)/mp.mpf(3)) - (mp.mpf(ans[0]) + mp.mpf(ans[1]))) < 1e-30
def test_eq_dd():
assert eq_dd(1, 0, 1, 0)
assert not eq_dd(1, 0, 1, .1)
assert not eq_dd(1, 0, 0, 0)
assert not eq_dd(1, 1, 1, 0)
assert not eq_dd(0, 0, 1, 0)
def test_neq_dd():
assert not neq_dd(1, 0, 1, 0)
assert neq_dd(1, 0, 1, .1)
assert neq_dd(1, 0, 0, 0)
assert neq_dd(1, 1, 1, 0)
assert neq_dd(0, 0, 1, 0)
def test_lt_dd():
assert not lt_dd(1, 0, 1, 0)
assert not lt_dd(1, 1e-10, 1, 1e-10)
assert lt_dd(.9, 0, 1, 0)
assert not lt_dd(1, 1e-10, 1, 0)
assert lt_dd(1, 0, 1, 1e-10)
def test_gt_dd():
assert not gt_dd(1, 0, 1, 0)
assert not gt_dd(1, 1e-10, 1, 1e-10)
assert not gt_dd(.9, 0, 1, 0)
assert gt_dd(1, 1e-10, 1, 0)
assert not gt_dd(1, 0, 1, 1e-10)
def test_le_dd():
assert le_dd(1, 0, 1, 0)
assert le_dd(1, 1e-10, 1, 1e-10)
assert le_dd(.9, 0, 1, 0)
assert not le_dd(1, 1e-10, 1, 0)
assert le_dd(1, 0, 1, 1e-10)
def test_ge_dd():
assert ge_dd(1, 0, 1, 0)
assert ge_dd(1, 1e-10, 1, 1e-10)
assert not ge_dd(.9, 0, 1, 0)
assert ge_dd(1, 1e-10, 1, 0)
assert not ge_dd(1, 0, 1, 1e-10)
@mark_mpmath
def test_intpow_dd():
ar, ae = 0.6931471805599453, 2.3190468138462996e-17
for n in range(-7, 4):
outr, oute = intpow_dd(ar, ae, n=n)
mp_ans = (mp.mpf(ar) + mp.mpf(ae))**n
ans_as_mp = mp.mpf(outr) + mp.mpf(oute)
err = abs(1.0 - ans_as_mp/mp_ans)
assert err < 1e-30
def test_exp_dd():
ar, ae = 0.6931471805599453, 2.3190468138462996e-17
outr, oute = exp_dd(ar, ae)
assert outr == 2
assert_close(oute, 0, atol=1e-30)
def test_log_dd():
a, b = log_dd(0.6931471805599453, 2.3190468138462996e-17)
assert_close(a, -0.36651292058166435, rtol=1e-14)
assert_close(b, 2.0606571710351483e-17, rtol=1e-14)
def test_pow_dd():
for n in range(-3, 4):
assert pow_dd(0.6931471805599453, 2.3190468138462996e-17, n, 0) == intpow_dd(0.6931471805599453, 2.3190468138462996e-17, n)
a, b = pow_dd(0.6931471805599453, 2.3190468138462996e-17, 0.6931471805599453, 2.3190468138462996e-17)
assert_close(a, 0.7756550370345752, rtol=1e-14)
assert_close(b, 3.1024007879879916e-17, rtol=1e-14)
|