File: test_mpc.txt

package info (click to toggle)
python-gmpy2 2.1.0~a4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,048 kB
  • sloc: ansic: 24,051; python: 325; makefile: 117
file content (220 lines) | stat: -rw-r--r-- 4,026 bytes parent folder | download
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
Test mpc elementary operations
------------------------------

>>> import gmpy2 as G
>>> from gmpy2 import mpz, mpq, mpfr, mpc
>>> from fractions import Fraction as F
>>> from decimal import Decimal as D
>>> a = mpz(123)
>>> b = mpz(456)
>>> c = 12345678901234567890
>>> af = mpfr("12.34")
>>> bf = mpfr("45.67")
>>> aq = mpq(3,11)
>>> bq = mpq(17,2)
>>> aj = mpc(1+2j)
>>> bj = mpc(4+5j)

Test addition
-------------

>>> aj + bj
mpc('5.0+7.0j')
>>> bj + aj
mpc('5.0+7.0j')
>>> aj + a
mpc('124.0+2.0j')
>>> a + aj
mpc('124.0+2.0j')
>>> aj + 1
mpc('2.0+2.0j')
>>> 1 + aj
mpc('2.0+2.0j')
>>> aj + 0
mpc('1.0+2.0j')
>>> 0 + aj
mpc('1.0+2.0j')
>>> -1 + aj
mpc('0.0+2.0j')
>>> aj - 1
mpc('0.0+2.0j')
>>> aj + 1.2 == 2.2 + 2j
True

Test subtraction
----------------

>>> aj - bj
mpc('-3.0-3.0j')
>>> bj - aj
mpc('3.0+3.0j')
>>> aj - a
mpc('-122.0+2.0j')
>>> a - aj
mpc('122.0-2.0j')
>>> aj - 1
mpc('0.0+2.0j')
>>> 1 - aj
mpc('0.0-2.0j')
>>> 0 - aj
mpc('-1.0-2.0j')
>>> aj - 0
mpc('1.0+2.0j')
>>> aj - -1
mpc('2.0+2.0j')
>>> -1 - aj
mpc('-2.0-2.0j')
>>> aj - 1.2 == (1+2j) - 1.2
True

Test multiplication
-------------------

>>> aj * bj
mpc('-6.0+13.0j')
>>> bj * aj
mpc('-6.0+13.0j')
>>> aj * a
mpc('123.0+246.0j')
>>> a * aj
mpc('123.0+246.0j')
>>> aj * -1
mpc('-1.0-2.0j')
>>> aj * (0.0+1j)
mpc('-2.0+1.0j')

Test division
-------------

>>> aj / bj
mpc('0.34146341463414637+0.073170731707317069j')
>>> aj // bj
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't take floor of complex number.
>>> aj / a
mpc('0.008130081300813009+0.016260162601626018j')
>>> a / aj
mpc('24.600000000000001-49.200000000000003j')
>>> aj / 0
mpc('inf+infj')

Test modulo
-----------

>>> aj % bj
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't mod complex numbers
>>> aj % a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't mod complex numbers
>>> a % aj
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't mod complex numbers
>>> divmod(aj, bj)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't take floor or mod of complex number.

Test operations involving NaN/Inf
---------------------------------

>>> aj + float('inf')
mpc('inf+2.0j')
>>> aj + float('-inf')
mpc('-inf+2.0j')
>>> aj + float('nan')
mpc('nan+2.0j')
>>> float('inf') - aj
mpc('inf-2.0j')
>>> aj - float('inf')
mpc('-inf+2.0j')
>>> aj - float('-inf')
mpc('inf+2.0j')
>>> aj - float('nan')
mpc('nan+2.0j')
>>> aj * float('inf')
mpc('inf+infj')
>>> aj * float('-inf')
mpc('-inf-infj')
>>> aj * float('nan')
mpc('nan+nanj')
>>> mpc(0,0) * float('inf')
mpc('nan+nanj')
>>> mpc(0,0) * float('-inf')
mpc('nan+nanj')
>>> mpc(0,0) * float('nan')
mpc('nan+nanj')
>>> aj / float('inf')
mpc('0.0+0.0j')
>>> aj / float('-inf')
mpc('-0.0-0.0j')
>>> float('inf') / aj
mpc('inf-infj')
>>> float('-inf') / aj
mpc('-inf+infj')

Test is_XXX
-----------

>>> G.is_zero(mpc("0+0j"))
True
>>> G.is_zero(mpc("1+0j"))
False
>>> G.is_zero(mpc("1+1j"))
False
>>> G.is_zero(mpc("0+1j"))
False
>>> G.is_nan(mpc("nan+1j"))
True
>>> G.is_nan(mpc("1+nanj"))
True
>>> G.is_nan(mpc("nan+nanj"))
True
>>> G.is_nan(mpc("1+1j"))
False
>>> G.is_infinite(mpc("inf+1j"))
True
>>> G.is_infinite(mpc("-inf+1j"))
True
>>> G.is_infinite(mpc("1+infj"))
True
>>> G.is_infinite(mpc("1-infj"))
True
>>> G.is_infinite(mpc("inf-infj"))
True
>>> G.is_infinite(mpc("1+1j"))
False
>>> G.is_finite(mpc("0+0j"))
True
>>> G.is_finite(mpc("nan+0j"))
False
>>> G.is_finite(mpc("0+nanj"))
False
>>> G.is_finite(mpc("0+infj"))
False
>>> G.is_finite(mpc("inf+3j"))
False

Test mpc misc
-------------
>>> c=mpc('67+87j', precision=70)
>>> c.precision
(70, 70)
>>> c.real.precision
70
>>> c.imag.precision
70
>>> c = mpc('42e56+42.909j', precision=(45,300))
>>> c.precision
(45, 300)
>>> c.real.precision
45
>>> c.imag.precision
300
>>> x = mpc("1.3142123+4.3e-1001j", precision=(70,37))
>>> mpc(x.real, x.imag, precision=(70,37)) == x
True