-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibreal.py
322 lines (275 loc) · 10.7 KB
/
ibreal.py
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
from collections import namedtuple
from math import log
from os import environ
# the internal integer pair representing a real by (number, offset)
# where number is an integer representing all the digits in a real
# and offset is the number of places from the right where the decimal
# point should be i.e (12345, 2) represents 123.45
Ival = namedtuple('Ival', 'num off')
# arbitrary-precision real number
class IBReal:
"""
IBReal represents a memory-limited, arbitrary precision, integer-based implementation of a real number.
All math operations are available in in-place mode (i.e. +=).
Usage:
realnum = IBReal(raw, prec=None, trim_on=True, rep=None)
raw: IBReal object, number, Ival object, ascii repr of a real number, or tuple (integer, offset) -- where integer
is the integer after multiplying the real number by 10^offset. If using an IBReal instance, the precision
will always be inherited from the object argument in spite of passing a prec val.
prec: precision -- length limit of internal integer (self.ival.num)
trim_on: allow trimming or not
rep: any special name for this number
$ export IBR_DEF_PREC=450 :: set environment var to pick up global precision
"""
def __init__(self, raw, prec=None, trim_on=True, rep=None):
if prec is not None:
self.prec = prec
elif 'IBR_DEF_PREC' in environ:
# precision from environment
self.prec = int(environ['IBR_DEF_PREC'])
else:
# default without environment var
self.prec = 50
self.trim_on = trim_on
self.rep = rep
try:
# Ival instance
if isinstance(raw, Ival):
self.ival = raw
# 2-tuple/list representing ival members
elif isinstance(raw, tuple) or isinstance(raw, list):
self.ival = Ival(*raw)
# another IBReal instance
elif isinstance(raw, type(self)):
self.ival = raw.ival
self.prec = prec or raw.prec
# an integer
elif isinstance(raw, int):
self.ival = Ival(raw, 0)
# something else -- text or float
else:
self.ival = self._from_raw(raw)
self.trim()
except Exception as e:
raise ValueError('Failed to coerce {}:{} to Ival'.format(type(raw), raw)) from e
# display trim -- no side effects on self
def dtrim(self, prec=None):
prec = self.prec if prec is None else prec
if not isinstance(prec, int) or prec <= 0:
raise TypeError('Only positive integers allowed')
if self.ilength > prec+1:
tval = str(self.ival.num)
if tval[0] == '-':
tval = tval[1:]
tlen = len(tval)
return type(self)(Ival(ib_sgn(self.ival.num)*int(tval[:prec]), self.ival.off-tlen+prec), **self.kwargs)
return self
# in-place trim to precision -- side effects
def trim(self, prec=None):
if not self.trim_on:
return self
prec = self.prec if prec is None else prec
self.ival = self.dtrim(prec=prec).ival
return self
@property
def kwargs(self):
return {'prec':self.prec, 'trim_on':self.trim_on}
# true if self's value is effectively an integer (i.e 2.0000000000000000000)
# concerned about int roundoff
@property
def isint(self):
return self == int(self)
@property
def _repr(self):
if self.rep is not None:
return self.rep
neg = '-' if self.ival.num < 0 else ''
txt = str(abs(self.ival.num))
if len(txt) > self.ival.off:
return '{}{}.{}'.format(neg, txt[:len(txt)-self.ival.off], txt[len(txt)-self.ival.off:] or '0')
else:
return '{}{}.{}e-{}'.format(neg, txt[0], txt[1:] or '0', self.ival.off-len(txt)+1)
# length of internal integer
@property
def ilength(self):
if self.ival.num == 0:
return 1
return int(log(abs(self.ival.num), 10)) + 1
# coercion engine
def _from_raw(self, raw):
straw = str(raw)
straw = straw.replace(' ', '')
if straw[0] == '-':
neg = -1
straw = straw[1:]
else:
neg = 1
exp = straw.find('e-')
if exp == -1:
exp = None
ev = 0
else:
ev = int(straw[exp+2:])
dot = straw.find('.')
if dot == -1:
dot = len(straw[:exp])
straw = straw[:dot] + straw[dot+1:exp]
off = len(straw) - dot + ev
return Ival(neg*int(straw), off)
# ensure decimal alignment for addition and subtraction
def _align(self, siv, oiv):
if siv.off > oiv.off:
pad = 10**(siv.off-oiv.off)
oiv = Ival(pad*oiv.num, siv.off)
else:
pad = 10**(oiv.off-siv.off)
siv = Ival(pad*siv.num, oiv.off)
return (siv, oiv)
def __mul__(self, other):
try:
if not isinstance(other, type(self)):
other = type(self)(other, **self.kwargs)
except Exception:
return other.__rmul__(self)
oiv = other.ival
siv = self.ival
ival = Ival(siv.num*oiv.num, siv.off+oiv.off)
return type(self)(ival, **self.kwargs).trim()
def __rmul__(self, other):
if not isinstance(other, type(self)):
other = type(self)(other, **self.kwargs)
return other.__mul__(self)
def __imul__(self, other):
self.ival = self.__mul__(other).ival
return self
def __truediv__(self, other):
try:
if not isinstance(other, type(self)):
other = type(self)(other, **self.kwargs)
except Exception:
return other.__rtruediv__(self)
siv = self.ival
oiv = other.ival
mlen = self.prec + other.ilength - self.ilength
num = siv.num * 10**mlen // oiv.num
off = mlen + siv.off - oiv.off
return type(self)(Ival(num, off), **self.kwargs).trim()
def __rtruediv__(self, other):
if not isinstance(other, type(self)):
other = type(self)(other, **self.kwargs)
return other.__truediv__(self)
def __itruediv__(self, other):
self.ival = self.__truediv__(other).ival
return self
def __floordiv__(self, other):
return type(self)(int(self.__truediv__(other)))
def __rfloordiv__(self, other):
if not isinstance(other, type(self)):
other = type(self)(other, **self.kwargs)
return other.__floordiv__(self)
def __ifloordiv__(self, other):
self.ival = self.__floordiv__(other).ival
return self
def __add__(self, other):
try:
if not isinstance(other, type(self)):
other = type(self)(other, **self.kwargs)
except Exception:
return other.__radd__(self)
(siv, oiv) = self._align(self.ival, other.ival)
ival = Ival(siv.num+oiv.num, siv.off)
return type(self)(ival, **self.kwargs).trim()
def __radd__(self, other):
if not isinstance(other, type(self)):
other = type(self)(other, **self.kwargs)
return other.__add__(self)
def __iadd__(self, other):
self.ival = self.__add__(other).ival
return self
def __sub__(self, other):
try:
if not isinstance(other, type(self)):
other = type(self)(other, **self.kwargs)
except Exception:
return other.__rsub__(self)
(siv, oiv) = self._align(self.ival, other.ival)
ival = Ival(siv.num-oiv.num, siv.off)
return type(self)(ival, **self.kwargs).trim()
def __rsub__(self, other):
if not isinstance(other, type(self)):
other = type(self)(other, **self.kwargs)
return other.__sub__(self)
def __isub__(self, other):
self.ival = self.__sub__(other).ival
return self
def __pow__(self, other):
try:
if not isinstance(other, type(self)):
other = type(self)(other, **self.kwargs)
except Exception:
return other.__rpow__(self)
tmp = type(self)(self.ival, **self.kwargs)
if other == 0:
tmp.ival = Ival(1, 0)
return tmp.trim()
if isinstance(other, type(self)) and other.isint:
# !! Leave int section alone -- needed for series expansions
for _ in range(1, abs(int(other))):
tmp *= self
if other < 0:
tmp = type(self)(Ival(1, 0), **self.kwargs).__truediv__(tmp)
else:
sl = ib_log(self)
tmp = ib_exp(sl*other)
return tmp.trim(other.prec)
def __rpow__(self, other):
if not isinstance(other, type(self)):
other = type(self)(other, **self.kwargs)
return other.__pow__(self)
def __ipow__(self, other):
self.ival = self.__pow__(other).ival
return self
def __int__(self):
return int(self.__float__())
def __float__(self):
return self.ival.num/10**self.ival.off
def __neg__(self):
return type(self)(Ival(-self.ival.num, self.ival.off), **self.kwargs)
def __abs__(self):
return type(self)(Ival(abs(self.ival.num), self.ival.off), **self.kwargs)
def __eq__(self, other):
if not isinstance(other, type(self)):
other = type(self)(other, **self.kwargs)
(siv, oiv) = self._align(self.ival, other.ival)
return siv.num == oiv.num
def __lt__(self, other):
if not isinstance(other, type(self)):
other = type(self)(other, **self.kwargs)
(siv, oiv) = self._align(self.ival, other.ival)
return siv.num < oiv.num
def __gt__(self, other):
if not isinstance(other, type(self)):
other = type(self)(other, **self.kwargs)
(siv, oiv) = self._align(self.ival, other.ival)
return siv.num > oiv.num
def __le__(self, other):
if not isinstance(other, type(self)):
other = type(self)(other, **self.kwargs)
(siv, oiv) = self._align(self.ival, other.ival)
return siv.num <= oiv.num
def __ge__(self, other):
if not isinstance(other, type(self)):
other = type(self)(other, **self.kwargs)
(siv, oiv) = self._align(self.ival, other.ival)
return siv.num >= oiv.num
def __ne__(self, other):
if not isinstance(other, type(self)):
other = type(self)(other, **self.kwargs)
(siv, oiv) = self._align(self.ival, other.ival)
return siv.num != oiv.num
def __str__(self):
return self.trim()._repr
def __repr__(self):
return self.trim()._repr
# here to prevent circular import
from .ibfuncs import ib_exp, ib_log, ib_sgn