shomez commited on
Commit
dc62cb2
·
verified ·
1 Parent(s): d178251

Upload backend.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. backend.py +115 -0
backend.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+
4
+ #----------------------------------------------------------------------------#
5
+ # Support GMPY for high-speed large integer arithmetic. #
6
+ # #
7
+ # To allow an external module to handle arithmetic, we need to make sure #
8
+ # that all high-precision variables are declared of the correct type. MPZ #
9
+ # is the constructor for the high-precision type. It defaults to Python's #
10
+ # long type but can be assinged another type, typically gmpy.mpz. #
11
+ # #
12
+ # MPZ must be used for the mantissa component of an mpf and must be used #
13
+ # for internal fixed-point operations. #
14
+ # #
15
+ # Side-effects #
16
+ # 1) "is" cannot be used to test for special values. Must use "==". #
17
+ # 2) There are bugs in GMPY prior to v1.02 so we must use v1.03 or later. #
18
+ #----------------------------------------------------------------------------#
19
+
20
+ # So we can import it from this module
21
+ gmpy = None
22
+ sage = None
23
+ sage_utils = None
24
+
25
+ if sys.version_info[0] < 3:
26
+ python3 = False
27
+ else:
28
+ python3 = True
29
+
30
+ BACKEND = 'python'
31
+
32
+ if not python3:
33
+ MPZ = long
34
+ xrange = xrange
35
+ basestring = basestring
36
+
37
+ def exec_(_code_, _globs_=None, _locs_=None):
38
+ """Execute code in a namespace."""
39
+ if _globs_ is None:
40
+ frame = sys._getframe(1)
41
+ _globs_ = frame.f_globals
42
+ if _locs_ is None:
43
+ _locs_ = frame.f_locals
44
+ del frame
45
+ elif _locs_ is None:
46
+ _locs_ = _globs_
47
+ exec("""exec _code_ in _globs_, _locs_""")
48
+ else:
49
+ MPZ = int
50
+ xrange = range
51
+ basestring = str
52
+
53
+ import builtins
54
+ exec_ = getattr(builtins, "exec")
55
+
56
+ # Define constants for calculating hash on Python 3.2.
57
+ if sys.version_info >= (3, 2):
58
+ HASH_MODULUS = sys.hash_info.modulus
59
+ if sys.hash_info.width == 32:
60
+ HASH_BITS = 31
61
+ else:
62
+ HASH_BITS = 61
63
+ else:
64
+ HASH_MODULUS = None
65
+ HASH_BITS = None
66
+
67
+ if 'MPMATH_NOGMPY' not in os.environ:
68
+ try:
69
+ try:
70
+ import gmpy2 as gmpy
71
+ except ImportError:
72
+ try:
73
+ import gmpy
74
+ except ImportError:
75
+ raise ImportError
76
+ if gmpy.version() >= '1.03':
77
+ BACKEND = 'gmpy'
78
+ MPZ = gmpy.mpz
79
+ except:
80
+ pass
81
+
82
+ if ('MPMATH_NOSAGE' not in os.environ and 'SAGE_ROOT' in os.environ or
83
+ 'MPMATH_SAGE' in os.environ):
84
+ try:
85
+ import sage.all
86
+ import sage.libs.mpmath.utils as _sage_utils
87
+ sage = sage.all
88
+ sage_utils = _sage_utils
89
+ BACKEND = 'sage'
90
+ MPZ = sage.Integer
91
+ except:
92
+ pass
93
+
94
+ if 'MPMATH_STRICT' in os.environ:
95
+ STRICT = True
96
+ else:
97
+ STRICT = False
98
+
99
+ MPZ_TYPE = type(MPZ(0))
100
+ MPZ_ZERO = MPZ(0)
101
+ MPZ_ONE = MPZ(1)
102
+ MPZ_TWO = MPZ(2)
103
+ MPZ_THREE = MPZ(3)
104
+ MPZ_FIVE = MPZ(5)
105
+
106
+ try:
107
+ if BACKEND == 'python':
108
+ int_types = (int, long)
109
+ else:
110
+ int_types = (int, long, MPZ_TYPE)
111
+ except NameError:
112
+ if BACKEND == 'python':
113
+ int_types = (int,)
114
+ else:
115
+ int_types = (int, MPZ_TYPE)