|
3 | 3 | from .compatibility import with_metaclass
|
4 | 4 | from .lib.symengine_wrapper import (sympify, sympify as S,
|
5 | 5 | SympifyError, sqrt, I, E, pi, Matrix, Derivative, exp,
|
6 |
| - factorial, gcd, lcm, factor, nextprime, mod_inverse, |
7 |
| - totient, primitive_root, Lambdify as lambdify, symarray, |
8 |
| - diff, eye, diag, ones, zeros, expand, Subs, |
| 6 | + nextprime, mod_inverse, primitive_root, Lambdify as lambdify, |
| 7 | + symarray, diff, eye, diag, ones, zeros, expand, Subs, |
9 | 8 | FunctionSymbol as AppliedUndef)
|
10 | 9 | from types import ModuleType
|
11 | 10 | import sys
|
12 | 11 |
|
| 12 | +def dps_to_prec(n): |
| 13 | + """Return the number of bits required to represent n decimals accurately.""" |
| 14 | + return max(1, int(round((int(n)+1)*3.3219280948873626))) |
13 | 15 |
|
14 | 16 | class BasicMeta(type):
|
15 | 17 | def __instancecheck__(self, instance):
|
@@ -45,6 +47,47 @@ def __new__(cls, i):
|
45 | 47 | return symengine.Integer(i)
|
46 | 48 |
|
47 | 49 |
|
| 50 | +class Float(Number): |
| 51 | + _classes = (symengine.RealDouble, symengine.RealMPFR) |
| 52 | + |
| 53 | + def __new__(cls, num, dps=None, precision=None): |
| 54 | + if dps is not None and precision is not None: |
| 55 | + raise ValueError('Both decimal and binary precision supplied. ' |
| 56 | + 'Supply only one. ') |
| 57 | + if dps is None and precision is None: |
| 58 | + dps = 15 |
| 59 | + if precision is None: |
| 60 | + precision = dps_to_prec(dps) |
| 61 | + |
| 62 | + if HAVE_SYMENGINE_MPFR: |
| 63 | + if precision > 53: |
| 64 | + if isinstance(num, symengine.RealDouble): |
| 65 | + return symengine.RealMPFR(str(num), precision) |
| 66 | + elif isinstance(num, symengine.RealMPFR): |
| 67 | + if precision == num.get_prec(): |
| 68 | + return num |
| 69 | + else: |
| 70 | + return symengine.RealMPFR(str(num), precision) |
| 71 | + else: |
| 72 | + return symengine.RealMPFR(str(num), precision) |
| 73 | + else: |
| 74 | + if isinstance(num, symengine.RealDouble): |
| 75 | + return num |
| 76 | + else: |
| 77 | + return symengine.RealDouble(float(num)) |
| 78 | + else: |
| 79 | + if precision > 53: |
| 80 | + raise ValueError('RealMPFR unavailable for high precision numerical values.') |
| 81 | + else: |
| 82 | + if isinstance(num, symengine.RealDouble): |
| 83 | + return num |
| 84 | + else: |
| 85 | + return symengine.RealDouble(float(str(num))) |
| 86 | + |
| 87 | + |
| 88 | +RealNumber = Float |
| 89 | + |
| 90 | + |
48 | 91 | class Add(Basic):
|
49 | 92 | _classes = (symengine.Add,)
|
50 | 93 |
|
|
0 commit comments