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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
from fractions import Fraction
import pytest
import gmpy2
from gmpy2 import mpz, mpq, mpfr, mpc, get_context, square, add, digits, xmpz
from supportclasses import z, q, r, cx
def test_minus():
ctx = get_context()
assert ctx.minus(mpz(5)) == mpz(-5)
assert ctx.minus(mpq(4,5)) == mpq(-4,5)
assert ctx.minus(mpfr(4,5)) == mpfr('-4.0')
assert ctx.minus(mpfr('inf')) == mpfr('-inf')
assert ctx.minus(mpc(15,3)) == mpc('-15.0-3.0j')
assert ctx.minus(65) == mpz(-65)
assert ctx.minus(5.5) == mpfr('-5.5')
assert ctx.minus(Fraction(2,3)) == mpq(-2,3)
assert ctx.minus(complex(15,3)) == mpc('-15.0-3.0j')
assert -mpc(5,5) == mpc('-5.0-5.0j')
pytest.raises(TypeError, lambda: ctx.minus('invalid'))
pytest.raises(TypeError, lambda: ctx.minus())
def test_plus():
ctx = get_context()
assert ctx.plus(5) == mpz(5)
assert ctx.plus(-5) == mpz(-5)
assert ctx.plus(Fraction(4,5)) == mpq(4,5)
assert ctx.plus(4.5) == mpfr('4.5')
assert ctx.plus(complex(5.2,5)) == mpc('5.2000000000000002+5.0j')
assert ctx.plus(mpz(421)) == mpz(421)
pytest.raises(TypeError, lambda: ctx.plus('invalid'))
pytest.raises(TypeError, lambda: ctx.plus())
assert + mpz(421) == mpz(421)
assert + mpq('4/5') == mpq(4,5)
assert + mpfr('inf') == mpfr('inf')
assert + mpc(65.0, 45) == mpc('65.0+45.0j')
def test_square():
z = mpz(2)
assert square(z) == mpz(4)
assert square(z) == z * z
q = mpq(2,3)
assert square(q) == mpq(4,9)
assert square(q) == q * q
r = mpfr(5.3)
assert square(r) == mpfr('28.09')
assert square(r) == r * r
c = mpc(2,3)
assert square(c) == mpc('-5.0+12.0j')
assert square(c) == c * c
assert square(2) == square(z)
assert square(Fraction(2,3)) == square(q)
assert square(5.3) == square(r)
assert square(complex(2,3)) == square(c)
pytest.raises(TypeError, lambda: square('invalid'))
def test_add():
a = mpz(123)
b = mpz(456)
c = 12345678901234567890
assert a+1 == mpz(124)
assert a+(-1) == mpz(122)
assert 1+a == mpz(124)
assert (-1)+a == mpz(122)
assert a+c == mpz(12345678901234568013)
assert c+a == mpz(12345678901234568013)
assert a+(-c) == mpz(-12345678901234567767)
assert (-c)+a == mpz(-12345678901234567767)
assert a+b == mpz(579)
assert b+a == mpz(579)
assert a+(-b) == mpz(-333)
assert (-b)+a == mpz(-333)
assert a+z == mpz(125)
assert add(a,b) == a+b
assert add(c,c) == c+c
assert add(1, 1) == mpz(2)
assert add(mpfr(1), mpfr(1)) == mpfr('2.0')
assert add(a, 1) == mpz(124)
assert add(1, a) == mpz(124)
assert add(a, mpq(0)) == mpq(123,1)
assert add(a, mpfr(0)) == mpfr('123.0')
assert add(a, mpc(0)) == mpc('123.0+0.0j')
pytest.raises(TypeError, lambda: add(1))
pytest.raises(TypeError, lambda: add(1,2,3))
assert add(1,1) == mpz(2)
pytest.raises(TypeError, lambda: a+'b')
pytest.raises(TypeError, lambda: 'b'+a)
assert mpq(1,2) + Fraction(3,2) == mpq(2,1)
assert Fraction(1,2) + mpq(3,2) == mpq(2,1)
assert mpq(1,2) + mpq(3,2) == mpq(2,1)
assert mpq(1,2) + 0 == mpq(1,2)
assert mpq(1,2) + mpz(1) == mpq(3,2)
assert mpq(1,2) + (-1) == mpq(-1,2)
assert 1 + mpq(1,2) == mpq(3,2)
assert mpz(1) + mpq(1,2) == mpq(3,2)
assert mpq(1,2) + mpz(1) == mpq(3,2)
assert mpq(1,1) + mpc(0) == mpc('1.0+0.0j')
assert mpc(0) + mpq(1,1) == mpc('1.0+0.0j')
assert mpq(1,2) + z == mpq(5,2)
assert mpq(1,2) + q == mpq(2,1)
assert add(mpq(1,2), mpq(3,2)) == mpq(2,1)
assert add(mpq(1,2), Fraction(3,2)) == mpq(2,1)
assert add(Fraction(1,2), mpq(3,2)) == mpq(2,1)
assert add(Fraction(1,2), Fraction(3,2)) == mpq(2,1)
pytest.raises(TypeError, lambda: add(1, 'a'))
pytest.raises(TypeError, lambda: mpq(1,2) + 'a')
pytest.raises(TypeError, lambda: 'a' + mpq(1,2))
assert mpfr(1) + 1 == mpfr('2.0')
assert 1 + mpfr(1) == mpfr('2.0')
assert mpfr(1) + mpz(1) == mpfr('2.0')
assert mpz(1) + mpfr(1) == mpfr('2.0')
assert mpfr(1) + mpfr(1) == mpfr('2.0')
assert mpfr(1) + mpq(1,1) == mpfr('2.0')
assert mpq(1,1) + mpfr(1) == mpfr('2.0')
assert mpfr(1) + Fraction(1,1) == mpfr('2.0')
assert Fraction(1,1) + mpfr(1) == mpfr('2.0')
assert mpfr(1) + 1.0 == mpfr('2.0')
assert 1.0 + mpfr(1) == mpfr('2.0')
assert mpfr(0) + (1 << 100) == mpfr('1p100', base=2)
assert (1 << 100) + mpfr(0) == mpfr('1p100', base=2)
assert mpfr(1) + z == mpfr('3.0')
assert mpfr(0.5) + q == mpfr('2.0')
assert mpfr(1.5) + r == mpfr('3.0')
pytest.raises(TypeError, lambda: mpc(1,2) + 'a')
assert mpfr(1) + mpc(1,2) == mpc('2.0+2.0j')
assert mpc(1,2) + mpfr(1) == mpc('2.0+2.0j')
assert mpc(1,2) + 1+0j == mpc('2.0+2.0j')
assert 1+0j + mpc(1,2) == mpc('2.0+2.0j')
assert mpc(1,2) + cx == mpc('43.0+69.0j')
assert mpc(1,2) + r == mpc('2.5+2.0j')
assert mpc(1,2) + q == mpc('2.5+2.0j')
assert mpc(1,2) + z == mpc('3.0+2.0j')
def test_digits():
z2 = mpz(5)
pytest.raises(TypeError, lambda: digits())
pytest.raises(TypeError, lambda: digits(5, 5, 4, 5))
assert digits(z2) == '5'
assert digits(z2, 2) == '101'
pytest.raises(TypeError, lambda: digits(z2, 2, 5))
assert digits(mpq(3,5)) == '3/5'
assert digits(mpq(3,5), 4) == '3/11'
assert digits(mpfr(3,5), 4) == ('300', 1, 5)
assert digits(mpfr(3,5), 4, 5) == ('30000', 1, 5)
assert digits(complex(5,5), 4, 5) == (('11000', 2, 53), ('11000', 2, 53))
pytest.raises(TypeError, lambda: digits('string', 4, 5))
def test_abs():
a = mpz(123)
b = abs(a)
assert a is b
a = xmpz(123)
b = abs(a)
assert a is not b
a = mpz(-123)
b = abs(a)
assert b == mpz(123)
assert a is not b
assert a == mpz(-123)
a = mpq(12,7)
b = abs(a)
assert a is b
a = mpq(-12,7)
b = abs(a)
assert b == mpq(12,7)
assert a == mpq(-12,7)
a = xmpz(-123)
b = abs(a)
assert a == xmpz(123)
assert b is None
b = abs(a)
assert b is None
a = mpfr(1.0)
b = abs(a)
assert a is not b
assert abs(mpfr(1, precision=100)) == mpfr('1.0')
ctx = gmpy2.get_context()
ctx.clear_flags()
assert gmpy2.is_nan(abs(mpfr('nan')))
assert ctx.invalid
ctx.clear_flags()
assert abs(mpfr('inf')) == mpfr('inf')
assert ctx.overflow is False
ctx.clear_flags()
assert abs(mpfr('-inf')) == mpfr('inf')
assert ctx.overflow is False
assert abs(mpc(-1,0)) == mpfr('1.0')
assert abs(-1+0j) == 1.0
assert abs(mpc(1,1)) == mpfr('1.4142135623730951')
ctx = gmpy2.get_context()
ctx.clear_flags()
c = mpc('nan+0j')
assert gmpy2.is_nan(c.real) and c.imag == 0.0
assert ctx.invalid
assert gmpy2.is_nan(abs(c))
assert ctx.invalid
ctx.clear_flags()
assert gmpy2.is_nan(abs(mpc('nanj')))
assert ctx.invalid
ctx.clear_flags()
assert abs(mpc('inf+10j')) == mpfr('inf')
assert ctx.overflow is False
ctx.clear_flags()
assert abs(mpc('-infj')) == mpfr('inf')
assert ctx.overflow is False
a = mpc('nan+infj')
ctx.clear_flags()
assert abs(a) == mpfr('inf')
assert ctx.overflow is False
assert ctx.invalid is False
a=mpc('-inf+nanj')
ctx.clear_flags()
assert abs(a) == mpfr('inf')
assert ctx.overflow is False
assert ctx.invalid is False
ctx = gmpy2.context()
pytest.raises(TypeError, lambda: ctx.abs('a'))
assert ctx.abs(-1) == mpz(1)
assert ctx.abs(0) == mpz(0)
assert ctx.abs(1) == mpz(1)
assert ctx.abs(mpz(8)) == mpz(8)
assert ctx.abs(mpz(-8)) == mpz(8)
assert ctx.abs(-1.0) == mpfr('1.0')
assert ctx.abs(mpfr(-2)) == mpfr('2.0')
assert ctx.abs(2+3j) == mpfr('3.6055512754639891')
assert ctx.abs(mpc(2+3j)) == mpfr('3.6055512754639891')
assert ctx.abs(Fraction(1,2)) == mpq(1,2)
assert ctx.abs(Fraction(-1,2)) == mpq(1,2)
def test_muldiv_2exp():
ctx = gmpy2.get_context()
r = mpfr(7.6)
z = mpz(3)
c = mpc(4,4)
assert gmpy2.mul_2exp(r, z) == mpfr('60.799999999999997')
assert gmpy2.mul_2exp(r, 3) == mpfr('60.799999999999997')
assert gmpy2.mul_2exp(r, -5) == mpfr('0.23749999999999999')
assert gmpy2.mul_2exp(1, -5) == mpfr('0.03125') # issue 328
pytest.raises(OverflowError, lambda: gmpy2.mul_2exp(z, 10**100))
pytest.raises(OverflowError, lambda: gmpy2.mul_2exp(z, -10**100))
pytest.raises(TypeError, lambda: gmpy2.mul_2exp(z, r))
pytest.raises(TypeError, lambda: gmpy2.mul_2exp('not', 5))
pytest.raises(TypeError, lambda: ctx.mul_2exp(r, z, 45))
assert ctx.mul_2exp(c, z) == mpc('32.0+32.0j')
assert ctx.mul_2exp(c, -5) == mpc('0.125+0.125j')
pytest.raises(OverflowError, lambda: ctx.mul_2exp(c, 10**100))
pytest.raises(OverflowError, lambda: ctx.mul_2exp(c, -10**100))
assert ctx.mul_2exp(r, 0) == mpfr('7.5999999999999996')
assert gmpy2.div_2exp(r, z) == mpfr('0.94999999999999996')
assert gmpy2.div_2exp(r, 3) == mpfr('0.94999999999999996')
assert gmpy2.div_2exp(r, -5) == mpfr('243.19999999999999')
pytest.raises(OverflowError, lambda: gmpy2.div_2exp(r, 10**100))
pytest.raises(OverflowError, lambda: gmpy2.div_2exp(r, -10**100))
pytest.raises(TypeError, lambda: gmpy2.div_2exp(z, r))
pytest.raises(TypeError, lambda: gmpy2.div_2exp('not', 5))
pytest.raises(TypeError, lambda: ctx.div_2exp(r, z, 45))
assert ctx.div_2exp(c, z) == mpc('0.5+0.5j')
assert ctx.div_2exp(c, -5) == mpc('128.0+128.0j')
pytest.raises(OverflowError, lambda: ctx.div_2exp(c, 10**100))
pytest.raises(OverflowError, lambda: ctx.div_2exp(c, -10**100))
|