mpmath 0.2__zip → 0.4__zip

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,199 @@
1
+ """
2
+ Check that the output from irrational functions is accurate for
3
+ high-precision input, from 5 to 200 digits. The reference values were
4
+ verified with Mathematica.
5
+ """
6
+
7
+ import time
8
+ from mpmath import *
9
+
10
+ precs = [5, 15, 28, 35, 57, 80, 100, 150, 200]
11
+
12
+ # sqrt(3) + pi/2
13
+ a = \
14
+ "3.302847134363773912758768033145623809041389953497933538543279275605"\
15
+ "841220051904536395163599428307109666700184672047856353516867399774243594"\
16
+ "67433521615861420725323528325327484262075464241255915238845599752675"
17
+
18
+ # e + 1/cgamma**2
19
+ b = \
20
+ "5.719681166601007617111261398629939965860873957353320734275716220045750"\
21
+ "31474116300529519620938123730851145473473708966080207482581266469342214"\
22
+ "824842256999042984813905047895479210702109260221361437411947323431"
23
+
24
+ # sqrt(a)
25
+ sqrt_a = \
26
+ "1.817373691447021556327498239690365674922395036495564333152483422755"\
27
+ "144321726165582817927383239308173567921345318453306994746434073691275094"\
28
+ "484777905906961689902608644112196725896908619756404253109722911487"
29
+
30
+ # sqrt(a+b*i).real
31
+ sqrt_abi_real = \
32
+ "2.225720098415113027729407777066107959851146508557282707197601407276"\
33
+ "89160998185797504198062911768240808839104987021515555650875977724230130"\
34
+ "3584116233925658621288393930286871862273400475179312570274423840384"
35
+
36
+ # sqrt(a+b*i).imag
37
+ sqrt_abi_imag = \
38
+ "1.2849057639084690902371581529110949983261182430040898147672052833653668"\
39
+ "0629534491275114877090834296831373498336559849050755848611854282001250"\
40
+ "1924311019152914021365263161630765255610885489295778894976075186"
41
+
42
+ # log(a)
43
+ log_a = \
44
+ "1.194784864491089550288313512105715261520511949410072046160598707069"\
45
+ "4336653155025770546309137440687056366757650909754708302115204338077595203"\
46
+ "83005773986664564927027147084436553262269459110211221152925732612"
47
+
48
+ # log(a+b*i).real
49
+ log_abi_real = \
50
+ "1.8877985921697018111624077550443297276844736840853590212962006811663"\
51
+ "04949387789489704203167470111267581371396245317618589339274243008242708"\
52
+ "014251531496104028712866224020066439049377679709216784954509456421"
53
+
54
+ # log(a+b*i).imag
55
+ log_abi_imag = \
56
+ "1.0471204952840802663567714297078763189256357109769672185219334169734948"\
57
+ "4265809854092437285294686651806426649541504240470168212723133326542181"\
58
+ "8300136462287639956713914482701017346851009323172531601894918640"
59
+
60
+ # exp(a)
61
+ exp_a = \
62
+ "27.18994224087168661137253262213293847994194869430518354305430976149"\
63
+ "382792035050358791398632888885200049857986258414049540376323785711941636"\
64
+ "100358982497583832083513086941635049329804685212200507288797531143"
65
+
66
+ # exp(a+b*i).real
67
+ exp_abi_real = \
68
+ "22.98606617170543596386921087657586890620262522816912505151109385026"\
69
+ "40160179326569526152851983847133513990281518417211964710397233157168852"\
70
+ "4963130831190142571659948419307628119985383887599493378056639916701"
71
+
72
+ # exp(a+b*i).imag
73
+ exp_abi_imag = \
74
+ "-14.523557450291489727214750571590272774669907424478129280902375851196283"\
75
+ "3377162379031724734050088565710975758824441845278120105728824497308303"\
76
+ "6065619788140201636218705414429933685889542661364184694108251449"
77
+
78
+ # a**b
79
+ pow_a_b = \
80
+ "928.7025342285568142947391505837660251004990092821305668257284426997"\
81
+ "361966028275685583421197860603126498884545336686124793155581311527995550"\
82
+ "580229264427202446131740932666832138634013168125809402143796691154"
83
+
84
+ # (a**(a+b*i)).real
85
+ pow_a_abi_real = \
86
+ "44.09156071394489511956058111704382592976814280267142206420038656267"\
87
+ "67707916510652790502399193109819563864568986234654864462095231138500505"\
88
+ "8197456514795059492120303477512711977915544927440682508821426093455"
89
+
90
+ # (a**(a+b*i)).imag
91
+ pow_a_abi_imag = \
92
+ "27.069371511573224750478105146737852141664955461266218367212527612279886"\
93
+ "9322304536553254659049205414427707675802193810711302947536332040474573"\
94
+ "8166261217563960235014674118610092944307893857862518964990092301"
95
+
96
+ # ((a+b*i)**(a+b*i)).real
97
+ pow_abi_abi_real = \
98
+ "-0.15171310677859590091001057734676423076527145052787388589334350524"\
99
+ "8084195882019497779202452975350579073716811284169068082670778986235179"\
100
+ "0813026562962084477640470612184016755250592698408112493759742219150452"\
101
+
102
+ # ((a+b*i)**(a+b*i)).imag
103
+ pow_abi_abi_imag = \
104
+ "1.2697592504953448936553147870155987153192995316950583150964099070426"\
105
+ "4736837932577176947632535475040521749162383347758827307504526525647759"\
106
+ "97547638617201824468382194146854367480471892602963428122896045019902"
107
+
108
+ # sin(a)
109
+ sin_a = \
110
+ "-0.16055653857469062740274792907968048154164433772938156243509084009"\
111
+ "38437090841460493108570147191289893388608611542655654723437248152535114"\
112
+ "528368009465836614227575701220612124204622383149391870684288862269631"
113
+
114
+ # sin(1000*a)
115
+ sin_1000a = \
116
+ "-0.85897040577443833776358106803777589664322997794126153477060795801"\
117
+ "09151695416961724733492511852267067419573754315098042850381158563024337"\
118
+ "216458577140500488715469780315833217177634490142748614625281171216863"
119
+
120
+ # sin(a+b*i)
121
+ sin_abi_real = \
122
+ "-24.4696999681556977743346798696005278716053366404081910969773939630"\
123
+ "7149215135459794473448465734589287491880563183624997435193637389884206"\
124
+ "02151395451271809790360963144464736839412254746645151672423256977064"
125
+
126
+ sin_abi_imag = \
127
+ "-150.42505378241784671801405965872972765595073690984080160750785565810981"\
128
+ "8314482499135443827055399655645954830931316357243750839088113122816583"\
129
+ "7169201254329464271121058839499197583056427233866320456505060735"
130
+
131
+ # cos
132
+ cos_a = \
133
+ "-0.98702664499035378399332439243967038895709261414476495730788864004"\
134
+ "05406821549361039745258003422386169330787395654908532996287293003581554"\
135
+ "257037193284199198069707141161341820684198547572456183525659969145501"
136
+
137
+ cos_1000a = \
138
+ "-0.51202523570982001856195696460663971099692261342827540426136215533"\
139
+ "52686662667660613179619804463250686852463876088694806607652218586060613"\
140
+ "951310588158830695735537073667299449753951774916401887657320950496820"
141
+
142
+ # tan
143
+ tan_a = \
144
+ "0.162666873675188117341401059858835168007137819495998960250142156848"\
145
+ "639654718809412181543343168174807985559916643549174530459883826451064966"\
146
+ "7996119428949951351938178809444268785629011625179962457123195557310"
147
+
148
+ tan_abi_real = \
149
+ "6.822696615947538488826586186310162599974827139564433912601918442911"\
150
+ "1026830824380070400102213741875804368044342309515353631134074491271890"\
151
+ "467615882710035471686578162073677173148647065131872116479947620E-6"
152
+
153
+ tan_abi_imag = \
154
+ "0.9999795833048243692245661011298447587046967777739649018690797625964167"\
155
+ "1446419978852235960862841608081413169601038230073129482874832053357571"\
156
+ "62702259309150715669026865777947502665936317953101462202542168429"
157
+
158
+
159
+ def test_hp():
160
+ for dps in precs:
161
+ mpf.dps = dps + 8
162
+ aa = mpf(a)
163
+ bb = mpf(b)
164
+ a1000 = 1000*mpf(a)
165
+ abi = mpc(aa, bb)
166
+ mpf.dps = dps
167
+ assert (sqrt(3) + pi/2).ae(aa)
168
+ assert (e + 1/cgamma**2).ae(bb)
169
+
170
+ assert sqrt(aa).ae(mpf(sqrt_a))
171
+ assert sqrt(abi).ae(mpc(sqrt_abi_real, sqrt_abi_imag))
172
+
173
+ assert log(aa).ae(mpf(log_a))
174
+ assert log(abi).ae(mpc(log_abi_real, log_abi_imag))
175
+
176
+ assert exp(aa).ae(mpf(exp_a))
177
+ assert exp(abi).ae(mpc(exp_abi_real, exp_abi_imag))
178
+
179
+ assert (aa**bb).ae(mpf(pow_a_b))
180
+ assert (aa**abi).ae(mpc(pow_a_abi_real, pow_a_abi_imag))
181
+ assert (abi**abi).ae(mpc(pow_abi_abi_real, pow_abi_abi_imag))
182
+
183
+ assert sin(a).ae(mpf(sin_a))
184
+ assert sin(a1000).ae(mpf(sin_1000a))
185
+ assert sin(abi).ae(mpc(sin_abi_real, sin_abi_imag))
186
+
187
+ assert cos(a).ae(mpf(cos_a))
188
+ assert cos(a1000).ae(mpf(cos_1000a))
189
+
190
+ assert tan(a).ae(mpf(tan_a))
191
+ assert tan(abi).ae(mpc(tan_abi_real, tan_abi_imag))
192
+
193
+ # check that complex cancellation is avoided so that both
194
+ # real and imaginary parts have high relative accuracy.
195
+ # abs_eps should be 0, but has to be set to 1e-205 to pass the
196
+ # 200-digit case, probably due to slight inaccuracy in the
197
+ # precomputed input
198
+ assert (tan(abi).real).ae(mpf(tan_abi_real), abs_eps=1e-205)
199
+ assert (tan(abi).imag).ae(mpf(tan_abi_imag), abs_eps=1e-205)
@@ -9,112 +9,6 @@ import cmath
9
9
  # Low-level tests
10
10
  #
11
11
 
12
- def test_bitcount():
13
- assert bitcount(0) == 0
14
- assert bitcount(1) == 1
15
- assert bitcount(7) == 3
16
- assert bitcount(8) == 4
17
- assert bitcount(2**100) == 101
18
- assert bitcount(2**100-1) == 100
19
- assert bitcount(-(2**100)) == 101
20
- assert bitcount(-(2**100-1)) == 100
21
-
22
- def test_trailing_zeros():
23
- assert trailing_zeros(0) == 0
24
- assert trailing_zeros(1) == 0
25
- assert trailing_zeros(2) == 1
26
- assert trailing_zeros(7) == 0
27
- assert trailing_zeros(8) == 3
28
- assert trailing_zeros(2**100) == 100
29
- assert trailing_zeros(2**100-1) == 0
30
-
31
- def test_round_down():
32
- assert normalize(0, -4, 4, ROUND_DOWN)[:2] == (0, 0)
33
- assert normalize(0xf0, -4, 4, ROUND_DOWN)[:2] == (15, 0)
34
- assert normalize(0xf1, -4, 4, ROUND_DOWN)[:2] == (15, 0)
35
- assert normalize(0xff, -4, 4, ROUND_DOWN)[:2] == (15, 0)
36
- assert normalize(-0xf0, -4, 4, ROUND_DOWN)[:2] == (-15, 0)
37
- assert normalize(-0xf1, -4, 4, ROUND_DOWN)[:2] == (-15, 0)
38
- assert normalize(-0xff, -4, 4, ROUND_DOWN)[:2] == (-15, 0)
39
-
40
- def test_round_up():
41
- assert normalize(0, -4, 4, ROUND_UP)[:2] == (0, 0)
42
- assert normalize(0xf0, -4, 4, ROUND_UP)[:2] == (15, 0)
43
- assert normalize(0xf1, -4, 4, ROUND_UP)[:2] == (1, 4)
44
- assert normalize(0xff, -4, 4, ROUND_UP)[:2] == (1, 4)
45
- assert normalize(-0xf0, -4, 4, ROUND_UP)[:2] == (-15, 0)
46
- assert normalize(-0xf1, -4, 4, ROUND_UP)[:2] == (-1, 4)
47
- assert normalize(-0xff, -4, 4, ROUND_UP)[:2] == (-1, 4)
48
-
49
- def test_round_floor():
50
- assert normalize(0, -4, 4, ROUND_FLOOR)[:2] == (0, 0)
51
- assert normalize(0xf0, -4, 4, ROUND_FLOOR)[:2] == (15, 0)
52
- assert normalize(0xf1, -4, 4, ROUND_FLOOR)[:2] == (15, 0)
53
- assert normalize(0xff, -4, 4, ROUND_FLOOR)[:2] == (15, 0)
54
- assert normalize(-0xf0, -4, 4, ROUND_FLOOR)[:2] == (-15, 0)
55
- assert normalize(-0xf1, -4, 4, ROUND_FLOOR)[:2] == (-1, 4)
56
- assert normalize(-0xff, -4, 4, ROUND_FLOOR)[:2] == (-1, 4)
57
-
58
- def test_round_ceiling():
59
- assert normalize(0, -4, 4, ROUND_CEILING)[:2] == (0, 0)
60
- assert normalize(0xf0, -4, 4, ROUND_CEILING)[:2] == (15, 0)
61
- assert normalize(0xf1, -4, 4, ROUND_CEILING)[:2] == (1, 4)
62
- assert normalize(0xff, -4, 4, ROUND_CEILING)[:2] == (1, 4)
63
- assert normalize(-0xf0, -4, 4, ROUND_CEILING)[:2] == (-15, 0)
64
- assert normalize(-0xf1, -4, 4, ROUND_CEILING)[:2] == (-15, 0)
65
- assert normalize(-0xff, -4, 4, ROUND_CEILING)[:2] == (-15, 0)
66
-
67
- def test_round_half_up():
68
- assert normalize(0, -4, 4, ROUND_HALF_UP)[:2] == (0, 0)
69
- assert normalize(0xf0, -4, 4, ROUND_HALF_UP)[:2] == (15, 0)
70
- assert normalize(0xf7, -4, 4, ROUND_HALF_UP)[:2] == (15, 0)
71
- assert normalize(0xf8, -4, 4, ROUND_HALF_UP)[:2] == (1, 4)
72
- assert normalize(0xf9, -4, 4, ROUND_HALF_UP)[:2] == (1, 4)
73
- assert normalize(0xff, -4, 4, ROUND_HALF_UP)[:2] == (1, 4)
74
- assert normalize(-0xf0, -4, 4, ROUND_HALF_UP)[:2] == (-15, 0)
75
- assert normalize(-0xf7, -4, 4, ROUND_HALF_UP)[:2] == (-15, 0)
76
- assert normalize(-0xf8, -4, 4, ROUND_HALF_UP)[:2] == (-1, 4)
77
- assert normalize(-0xf9, -4, 4, ROUND_HALF_UP)[:2] == (-1, 4)
78
- assert normalize(-0xff, -4, 4, ROUND_HALF_UP)[:2] == (-1, 4)
79
-
80
- def test_round_half_down():
81
- assert normalize(0, -4, 4, ROUND_HALF_DOWN)[:2] == (0, 0)
82
- assert normalize(0xf0, -4, 4, ROUND_HALF_DOWN)[:2] == (15, 0)
83
- assert normalize(0xf7, -4, 4, ROUND_HALF_DOWN)[:2] == (15, 0)
84
- assert normalize(0xf8, -4, 4, ROUND_HALF_DOWN)[:2] == (15, 0)
85
- assert normalize(0xf9, -4, 4, ROUND_HALF_DOWN)[:2] == (1, 4)
86
- assert normalize(0xff, -4, 4, ROUND_HALF_DOWN)[:2] == (1, 4)
87
- assert normalize(-0xf0, -4, 4, ROUND_HALF_DOWN)[:2] == (-15, 0)
88
- assert normalize(-0xf7, -4, 4, ROUND_HALF_DOWN)[:2] == (-15, 0)
89
- assert normalize(-0xf8, -4, 4, ROUND_HALF_DOWN)[:2] == (-15, 0)
90
- assert normalize(-0xf9, -4, 4, ROUND_HALF_DOWN)[:2] == (-1, 4)
91
- assert normalize(-0xff, -4, 4, ROUND_HALF_DOWN)[:2] == (-1, 4)
92
-
93
- def test_round_half_even():
94
- assert normalize(0, -4, 4, ROUND_HALF_EVEN)[:2] == (0, 0)
95
- assert normalize(0xf0, -4, 4, ROUND_HALF_EVEN)[:2] == (15, 0)
96
- assert normalize(0xf7, -4, 4, ROUND_HALF_EVEN)[:2] == (15, 0)
97
- assert normalize(0xf8, -4, 4, ROUND_HALF_EVEN)[:2] == (1, 4) # 1111.1000 -> 10000.0
98
- assert normalize(0xf9, -4, 4, ROUND_HALF_EVEN)[:2] == (1, 4) # 1111.1001 -> 10000.0
99
- assert normalize(0xe8, -4, 4, ROUND_HALF_EVEN)[:2] == (7, 1) # 1110.1000 -> 1110.0
100
- assert normalize(0xe9, -4, 4, ROUND_HALF_EVEN)[:2] == (15, 0) # 1110.1001 -> 1111.0
101
- assert normalize(-0xf0, -4, 4, ROUND_HALF_EVEN)[:2] == (-15, 0)
102
- assert normalize(-0xf7, -4, 4, ROUND_HALF_EVEN)[:2] == (-15, 0)
103
- assert normalize(-0xf8, -4, 4, ROUND_HALF_EVEN)[:2] == (-1, 4)
104
- assert normalize(-0xf9, -4, 4, ROUND_HALF_EVEN)[:2] == (-1, 4)
105
- assert normalize(-0xe8, -4, 4, ROUND_HALF_EVEN)[:2] == (-7, 1)
106
- assert normalize(-0xe9, -4, 4, ROUND_HALF_EVEN)[:2] == (-15, 0)
107
-
108
-
109
- def test_rounding_bugs():
110
- # 1 less than power-of-two cases
111
- assert normalize(72057594037927935, -56, 53, ROUND_UP) == (1, 0, 1)
112
- assert normalize(31, 0, 4, ROUND_UP) == (1, 5, 1)
113
- assert normalize(-31, 0, 4, ROUND_FLOOR) == (-1, 5, 1)
114
- assert normalize(255, 0, 7, ROUND_UP) == (1, 8, 1)
115
- assert normalize(-255, 0, 7, ROUND_FLOOR) == (-1, 8, 1)
116
-
117
-
118
12
  # Advanced rounding test
119
13
  def test_add_rounding():
120
14
  mpf.dps = 15
@@ -154,33 +48,6 @@ def test_complex():
154
48
  assert not mpc(2).ae(2 + 1e-13)
155
49
  assert mpc(2+1e-15j).ae(2)
156
50
 
157
- # Results should agree with those of small floats at standard precision
158
- def test_standard_float():
159
- random.seed(0)
160
- sqrtfail = 0
161
- for i in range(5000):
162
- x = (random.random()*1000 - 1000) * 2.0**random.randint(-20, 20)
163
- y = (random.random()*1000 - 1000) * 2.0**random.randint(-20, 20)
164
- assert mpf(x) == x
165
- assert (mpf(x) < mpf(y)) == (x < y)
166
- assert (mpf(x) > mpf(y)) == (x > y)
167
- assert (mpf(x) == mpf(y)) == (x == y)
168
- assert (mpf(x) != mpf(y)) == (x != y)
169
- assert (mpf(x) <= mpf(y)) == (x <= y)
170
- assert (mpf(x) >= mpf(y)) == (x >= y)
171
- assert mpf(x) == mpf(x)
172
- assert mpf(x) + mpf(y) == x + y
173
- assert mpf(x) * mpf(y) == x * y
174
- assert mpf(x) / mpf(y) == x / y
175
- assert abs(mpf(x)) == abs(x)
176
- # this fails roughly 1 time out of 1000. not sure whether mpf
177
- # or float is rounding the wrong way
178
- sqrtfail += (abs(mpf(x))**0.5 != abs(x)**0.5)
179
- assert sqrtfail < 5
180
- # particular bugs
181
- assert mpf(4.4408920985006262E-16) < mpf(1.7763568394002505E-15)
182
- assert mpf(-4.4408920985006262E-16) > mpf(-1.7763568394002505E-15)
183
-
184
51
 
185
52
  def test_mixed_types():
186
53
  assert 1 + mpf(3) == mpf(3) + 1 == 4
@@ -192,11 +59,13 @@ def test_mixed_types():
192
59
  assert 3.0 * mpf(2) == mpf(2) * 3.0 == 6
193
60
  assert 6.0 / mpf(2) == mpf(6) / 2.0 == 3
194
61
 
195
-
196
62
  # Test that integer arithmetic is exact
197
- def test_integers():
63
+ def test_aintegers():
198
64
  random.seed(0)
199
65
  for prec in [6, 10, 25, 40, 100, 250, 725]:
66
+ for rounding in [ROUND_DOWN, ROUND_UP, ROUND_FLOOR, ROUND_CEILING,
67
+ ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN]:
68
+ mpf._rounding = rounding
200
69
  mpf.dps = prec
201
70
  M = 10**(prec-2)
202
71
  M2 = 10**(prec//2-2)
@@ -212,6 +81,7 @@ def test_integers():
212
81
  a = random.randint(-M2, M2)
213
82
  b = random.randint(-M2, M2)
214
83
  assert mpf(a) * mpf(b) == a*b
84
+ mpf.round_default()
215
85
  mpf.dps = 15
216
86
 
217
87
  def test_exact_sqrts():
@@ -229,9 +99,21 @@ def test_exact_sqrts():
229
99
  assert sqrt(mpf((a*a, 2*i))) == mpf((a, i))
230
100
  assert sqrt(mpf((a*a, -2*i))) == mpf((a, -i))
231
101
 
102
+ def test_sqrt_rounding():
103
+ for i in [2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15]:
104
+ for dps in [7, 15, 83, 106, 2000]:
105
+ mpf.dps = dps
106
+ mpf.round_down()
107
+ assert (mpf(i)**0.5)**2 < i
108
+ mpf.round_up()
109
+ assert (mpf(i)**0.5)**2 > i
110
+ mpf.dps = 15
111
+ mpf.round_default()
112
+
113
+
232
114
 
233
115
  #----------------------------------------------------------------------------
234
- # Type comparison
116
+ # Type comparison and conversion
235
117
  #
236
118
 
237
119
  def test_type_compare():
@@ -245,6 +127,7 @@ def test_type_compare():
245
127
  assert mpc(2) != '2.0'
246
128
 
247
129
 
130
+
248
131
  #----------------------------------------------------------------------------
249
132
  # Constants and functions
250
133
  #
@@ -273,14 +156,16 @@ def test_constants():
273
156
 
274
157
  def test_str_1000_digits():
275
158
  mpf.dps = 1001
276
- assert str(mpf(2)**0.5)[-10:] == '9518488472'
277
- assert str(pi)[-10:] == '2164201989'
159
+ # last digit may be wrong
160
+ assert str(mpf(2)**0.5)[-10:-1] == '9518488472'[:9]
161
+ assert str(pi)[-10:-1] == '2164201989'[:9]
278
162
  mpf.dps = 15
279
163
 
280
164
  def test_str_10000_digits():
281
165
  mpf.dps = 10001
282
- assert str(mpf(2)**0.5)[-10:] == '5873258352'
283
- assert str(pi)[-10:] == '5256375679'
166
+ # last digit may be wrong
167
+ assert str(mpf(2)**0.5)[-10:-1] == '5873258351'[:9]
168
+ assert str(pi)[-10:-1] == '5256375678'[:9]
284
169
  mpf.dps = 15
285
170
 
286
171
  def test_float_sqrt():
@@ -319,50 +204,78 @@ def test_log():
319
204
  assert log(10**1234, 10) == 1234
320
205
  assert log(2+2j).ae(cmath.log(2+2j))
321
206
 
322
- def test_trig_basic():
207
+ def test_trig_hyperb_basic():
323
208
  for x in (range(100) + range(-100,0)):
324
209
  t = x / 4.1
325
210
  assert cos(mpf(t)).ae(math.cos(t))
326
211
  assert sin(mpf(t)).ae(math.sin(t))
327
212
  assert tan(mpf(t)).ae(math.tan(t))
213
+ assert cosh(mpf(t)).ae(math.cosh(t))
214
+ assert sinh(mpf(t)).ae(math.sinh(t))
215
+ assert tanh(mpf(t)).ae(math.tanh(t))
328
216
  assert sin(1+1j).ae(cmath.sin(1+1j))
329
217
  assert sin(-4-3.6j).ae(cmath.sin(-4-3.6j))
330
218
  assert cos(1+1j).ae(cmath.cos(1+1j))
331
219
  assert cos(-4-3.6j).ae(cmath.cos(-4-3.6j))
332
220
 
333
- def test_complex_functions():
334
- for x in (range(10) + range(-10,0)):
335
- for y in (range(10) + range(-10,0)):
336
- z = complex(x, y)/4.3 + 0.01j
337
- assert exp(mpc(z)).ae(cmath.exp(z))
338
- assert log(mpc(z)).ae(cmath.log(z))
339
- assert cos(mpc(z)).ae(cmath.cos(z))
340
- assert sin(mpc(z)).ae(cmath.sin(z))
341
- assert tan(mpc(z)).ae(cmath.tan(z))
342
-
343
- def test_complex_powers():
221
+ def test_degrees():
222
+ assert cos(0*degree) == 1
223
+ assert cos(90*degree).ae(0)
224
+ assert cos(180*degree).ae(-1)
225
+ assert cos(270*degree).ae(0)
226
+ assert cos(360*degree).ae(1)
227
+ assert sin(0*degree) == 0
228
+ assert sin(90*degree).ae(1)
229
+ assert sin(180*degree).ae(0)
230
+ assert sin(270*degree).ae(-1)
231
+ assert sin(360*degree).ae(0)
232
+
233
+ def random_complexes(N):
344
234
  random.seed(1)
345
- for i in range(100):
235
+ a = []
236
+ for i in range(N):
346
237
  x1 = random.uniform(-10, 10)
347
238
  y1 = random.uniform(-10, 10)
348
239
  x2 = random.uniform(-10, 10)
349
240
  y2 = random.uniform(-10, 10)
350
241
  z1 = complex(x1, y1)
351
242
  z2 = complex(x2, y2)
243
+ a.append((z1, z2))
244
+ return a
245
+
246
+ def test_complex_powers():
247
+ for dps in [15, 30, 100]:
248
+ # Check accuracy for complex square root
249
+ mpf.dps = dps
250
+ a = mpc(1j)**0.5
251
+ assert a.real == a.imag == mpf(2)**0.5 / 2
252
+ mpf.dps = 15
253
+ random.seed(1)
254
+ for (z1, z2) in random_complexes(100):
352
255
  assert (mpc(z1)**mpc(z2)).ae(z1**z2, 1e-12)
353
256
  assert (e**(-pi*1j)).ae(-1)
354
257
  mpf.dps = 50
355
258
  assert (e**(-pi*1j)).ae(-1)
356
259
  mpf.dps = 15
357
260
 
358
- def test_trig_hard():
261
+ def test_atrig_hard():
359
262
  mpf.prec = 150
360
263
  a = mpf(10**50)
361
264
  mpf.prec = 53
362
265
  assert sin(a).ae(-0.7896724934293100827)
363
266
  assert cos(a).ae(-0.6135286082336635622)
364
- assert sin(1e-6).ae(9.999999999998333e-007)
267
+ # Check relative accuracy close to x = zero
268
+ assert sin(1e-100) == 1e-100 # when rounding to nearest
269
+ assert sin(1e-6).ae(9.999999999998333e-007, rel_eps=2e-15, abs_eps=0)
270
+ assert sin(1e-6j).ae(1.0000000000001666e-006j, rel_eps=2e-15, abs_eps=0)
271
+ assert sin(-1e-6j).ae(-1.0000000000001666e-006j, rel_eps=2e-15, abs_eps=0)
272
+ assert cos(1e-100) == 1
365
273
  assert cos(1e-6).ae(0.9999999999995)
274
+ assert cos(-1e-6j).ae(1.0000000000005)
275
+ assert tan(1e-100) == 1e-100
276
+ assert tan(1e-6).ae(1.0000000000003335e-006, rel_eps=2e-15, abs_eps=0)
277
+ assert tan(1e-6j).ae(9.9999999999966644e-007j, rel_eps=2e-15, abs_eps=0)
278
+ assert tan(-1e-6j).ae(-9.9999999999966644e-007j, rel_eps=2e-15, abs_eps=0)
366
279
 
367
280
  def test_atan():
368
281
  assert atan(-2.3).ae(math.atan(-2.3))
@@ -372,18 +285,76 @@ def test_atan():
372
285
  assert atan2(-1,1).ae(math.atan2(-1,1))
373
286
  assert atan2(-1,0).ae(math.atan2(-1,0))
374
287
  assert atan2(1,0).ae(math.atan2(1,0))
375
- assert atan(1e-50).ae(1e-50, abs_eps=0)
288
+ assert atan2(0,0) == 0
289
+ assert atan(1e-50) == 1e-50
376
290
  assert atan(1e50).ae(pi/2)
377
- assert atan(-1e-50).ae(-1e-50, abs_eps=0)
291
+ assert atan(-1e-50) == -1e-50
378
292
  assert atan(-1e50).ae(-pi/2)
293
+ for dps in [25, 70, 100, 300, 1000]:
294
+ mpf.dps = dps
295
+ assert (4*atan(1)).ae(pi)
296
+ mpf.dps = 15
297
+
298
+ def test_areal_inverses():
299
+ assert asin(mpf(0)) == 0
300
+ assert asinh(mpf(0)) == 0
301
+ assert acosh(mpf(1)) == 0
302
+ assert isinstance(asin(mpf(0.5)), mpf)
303
+ assert isinstance(asin(mpf(2.0)), mpc)
304
+ assert isinstance(acos(mpf(0.5)), mpf)
305
+ assert isinstance(acos(mpf(2.0)), mpc)
306
+ assert isinstance(atanh(mpf(0.1)), mpf)
307
+ assert isinstance(atanh(mpf(1.1)), mpc)
308
+
309
+ random.seed(1)
310
+ for i in range(50):
311
+ x = random.uniform(0, 1)
312
+ assert asin(mpf(x)).ae(math.asin(x))
313
+ assert acos(mpf(x)).ae(math.acos(x))
314
+
315
+ x = random.uniform(-10, 10)
316
+ assert asinh(mpf(x)).ae(cmath.asinh(x).real)
317
+ assert isinstance(asinh(mpf(x)), mpf)
318
+ x = random.uniform(1, 10)
319
+ assert acosh(mpf(x)).ae(cmath.acosh(x).real)
320
+ assert isinstance(acosh(mpf(x)), mpf)
321
+ x = random.uniform(-10, 0.999)
322
+ assert isinstance(acosh(mpf(x)), mpc)
323
+
324
+ x = random.uniform(-1, 1)
325
+ assert atanh(mpf(x)).ae(cmath.atanh(x).real)
326
+ assert isinstance(atanh(mpf(x)), mpf)
379
327
 
380
328
 
381
- if __name__ == "__main__":
382
- globals_ = globals().keys()
383
- t1 = time.time()
384
- for f in globals_:
385
- if f.startswith("test_"):
386
- print "running", f, "..."
387
- globals()[f]()
388
- t2 = time.time()
389
- print "passed all tests in", (t2-t1), "seconds"
329
+ def test_complex_functions():
330
+ for x in (range(10) + range(-10,0)):
331
+ for y in (range(10) + range(-10,0)):
332
+ z = complex(x, y)/4.3 + 0.01j
333
+ assert exp(mpc(z)).ae(cmath.exp(z))
334
+ assert log(mpc(z)).ae(cmath.log(z))
335
+ assert cos(mpc(z)).ae(cmath.cos(z))
336
+ assert sin(mpc(z)).ae(cmath.sin(z))
337
+ assert tan(mpc(z)).ae(cmath.tan(z))
338
+ assert sinh(mpc(z)).ae(cmath.sinh(z))
339
+ assert cosh(mpc(z)).ae(cmath.cosh(z))
340
+ assert tanh(mpc(z)).ae(cmath.tanh(z))
341
+
342
+ def test_complex_inverse_functions():
343
+ for (z1, z2) in random_complexes(30):
344
+ # apparently cmath uses a different branch, so we
345
+ # can't use it for comparison
346
+ assert sinh(asinh(z1)).ae(z1)
347
+ #
348
+ assert acosh(z1).ae(cmath.acosh(z1))
349
+ assert atanh(z1).ae(cmath.atanh(z1))
350
+ assert atan(z1).ae(cmath.atan(z1))
351
+ # the reason we set a big eps here is that the cmath
352
+ # functions are inaccurate
353
+ assert asin(z1).ae(cmath.asin(z1), rel_eps=1e-12)
354
+ assert acos(z1).ae(cmath.acos(z1), rel_eps=1e-12)
355
+
356
+ def test_misc_bugs():
357
+ # test that this doesn't raise an exception
358
+ mpf.dps = 1000
359
+ log(1302)
360
+ mpf.dps = 15
mpmath-0.2/CHANGES DELETED
@@ -1,23 +0,0 @@
1
- -- 0.2 --
2
- Released October 2, 2007
3
-
4
- * 50% faster exponential function
5
- * faster mpf <-> int ops
6
- * fixed import error in pidigits.py; added demos to source distribution
7
- * __rmul__ was missing on mpf
8
- * fixed bitcount bug also for -(2**n-1)
9
- * more docstrings
10
- * more tests; tests included in source distribution
11
- * approximate equality testing (.ae method) supported
12
- * implemented atan and atan2 functions
13
- * tan works for both mpfs and mpcs
14
- * complex logarithms and complex powers supported
15
- * more details in README
16
-
17
- -- 0.1 --
18
- Released September 27, 2007
19
-
20
- * imported code from SymPy's numerics module
21
- * renamed functions and restructured various parts of the code
22
- * fixed erroneous bitcount for 2**n-1 mantissa with directed rounding
23
- * various small speed improvements and bug fixes
@@ -1,2 +0,0 @@
1
- import lib
2
- from mpmath import *