passagemath-objects 10.6.44__cp314-cp314t-macosx_13_0_arm64.whl
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.
- passagemath_objects/.dylibs/libgmp.10.dylib +0 -0
- passagemath_objects/__init__.py +3 -0
- passagemath_objects-10.6.44.dist-info/METADATA +115 -0
- passagemath_objects-10.6.44.dist-info/RECORD +280 -0
- passagemath_objects-10.6.44.dist-info/WHEEL +6 -0
- passagemath_objects-10.6.44.dist-info/top_level.txt +3 -0
- sage/all__sagemath_objects.py +37 -0
- sage/arith/all__sagemath_objects.py +5 -0
- sage/arith/long.pxd +411 -0
- sage/arith/numerical_approx.cpython-314t-darwin.so +0 -0
- sage/arith/numerical_approx.pxd +35 -0
- sage/arith/numerical_approx.pyx +75 -0
- sage/arith/power.cpython-314t-darwin.so +0 -0
- sage/arith/power.pxd +31 -0
- sage/arith/power.pyx +127 -0
- sage/categories/action.cpython-314t-darwin.so +0 -0
- sage/categories/action.pxd +29 -0
- sage/categories/action.pyx +641 -0
- sage/categories/algebra_functor.py +745 -0
- sage/categories/all__sagemath_objects.py +33 -0
- sage/categories/basic.py +62 -0
- sage/categories/cartesian_product.py +295 -0
- sage/categories/category.py +3401 -0
- sage/categories/category_cy_helper.cpython-314t-darwin.so +0 -0
- sage/categories/category_cy_helper.pxd +8 -0
- sage/categories/category_cy_helper.pyx +322 -0
- sage/categories/category_singleton.cpython-314t-darwin.so +0 -0
- sage/categories/category_singleton.pxd +3 -0
- sage/categories/category_singleton.pyx +342 -0
- sage/categories/category_types.py +637 -0
- sage/categories/category_with_axiom.py +2876 -0
- sage/categories/covariant_functorial_construction.py +703 -0
- sage/categories/facade_sets.py +228 -0
- sage/categories/functor.cpython-314t-darwin.so +0 -0
- sage/categories/functor.pxd +7 -0
- sage/categories/functor.pyx +691 -0
- sage/categories/homset.py +1338 -0
- sage/categories/homsets.py +364 -0
- sage/categories/isomorphic_objects.py +73 -0
- sage/categories/map.cpython-314t-darwin.so +0 -0
- sage/categories/map.pxd +34 -0
- sage/categories/map.pyx +2106 -0
- sage/categories/morphism.cpython-314t-darwin.so +0 -0
- sage/categories/morphism.pxd +14 -0
- sage/categories/morphism.pyx +895 -0
- sage/categories/objects.py +167 -0
- sage/categories/primer.py +1696 -0
- sage/categories/pushout.py +4834 -0
- sage/categories/quotients.py +64 -0
- sage/categories/realizations.py +200 -0
- sage/categories/sets_cat.py +3290 -0
- sage/categories/sets_with_partial_maps.py +52 -0
- sage/categories/subobjects.py +64 -0
- sage/categories/subquotients.py +21 -0
- sage/categories/with_realizations.py +311 -0
- sage/cpython/__init__.py +19 -0
- sage/cpython/_py2_random.py +619 -0
- sage/cpython/all.py +3 -0
- sage/cpython/atexit.cpython-314t-darwin.so +0 -0
- sage/cpython/atexit.pyx +269 -0
- sage/cpython/builtin_types.cpython-314t-darwin.so +0 -0
- sage/cpython/builtin_types.pyx +7 -0
- sage/cpython/cython_metaclass.cpython-314t-darwin.so +0 -0
- sage/cpython/cython_metaclass.h +117 -0
- sage/cpython/cython_metaclass.pxd +3 -0
- sage/cpython/cython_metaclass.pyx +130 -0
- sage/cpython/debug.cpython-314t-darwin.so +0 -0
- sage/cpython/debug.pyx +302 -0
- sage/cpython/dict_del_by_value.cpython-314t-darwin.so +0 -0
- sage/cpython/dict_del_by_value.pxd +9 -0
- sage/cpython/dict_del_by_value.pyx +191 -0
- sage/cpython/dict_internal.h +245 -0
- sage/cpython/getattr.cpython-314t-darwin.so +0 -0
- sage/cpython/getattr.pxd +9 -0
- sage/cpython/getattr.pyx +439 -0
- sage/cpython/pycore_long.h +97 -0
- sage/cpython/pycore_long.pxd +10 -0
- sage/cpython/python_debug.h +44 -0
- sage/cpython/python_debug.pxd +47 -0
- sage/cpython/pyx_visit.h +13 -0
- sage/cpython/string.cpython-314t-darwin.so +0 -0
- sage/cpython/string.pxd +76 -0
- sage/cpython/string.pyx +34 -0
- sage/cpython/string_impl.h +60 -0
- sage/cpython/type.cpython-314t-darwin.so +0 -0
- sage/cpython/type.pxd +2 -0
- sage/cpython/type.pyx +40 -0
- sage/cpython/wrapperdescr.pxd +67 -0
- sage/ext/all__sagemath_objects.py +3 -0
- sage/ext/ccobject.h +64 -0
- sage/ext/cplusplus.pxd +17 -0
- sage/ext/mod_int.h +30 -0
- sage/ext/mod_int.pxd +24 -0
- sage/ext/stdsage.pxd +39 -0
- sage/groups/all__sagemath_objects.py +1 -0
- sage/groups/group.cpython-314t-darwin.so +0 -0
- sage/groups/group.pxd +14 -0
- sage/groups/group.pyx +322 -0
- sage/groups/old.cpython-314t-darwin.so +0 -0
- sage/groups/old.pxd +14 -0
- sage/groups/old.pyx +219 -0
- sage/libs/all__sagemath_objects.py +3 -0
- sage/libs/gmp/__init__.py +1 -0
- sage/libs/gmp/all.pxd +6 -0
- sage/libs/gmp/binop.pxd +23 -0
- sage/libs/gmp/misc.pxd +8 -0
- sage/libs/gmp/mpf.pxd +88 -0
- sage/libs/gmp/mpn.pxd +57 -0
- sage/libs/gmp/mpq.pxd +57 -0
- sage/libs/gmp/mpz.pxd +202 -0
- sage/libs/gmp/pylong.cpython-314t-darwin.so +0 -0
- sage/libs/gmp/pylong.pxd +12 -0
- sage/libs/gmp/pylong.pyx +150 -0
- sage/libs/gmp/random.pxd +25 -0
- sage/libs/gmp/randomize.pxd +59 -0
- sage/libs/gmp/types.pxd +53 -0
- sage/libs/gmpxx.pxd +19 -0
- sage/misc/abstract_method.py +276 -0
- sage/misc/all__sagemath_objects.py +43 -0
- sage/misc/bindable_class.py +253 -0
- sage/misc/c3_controlled.cpython-314t-darwin.so +0 -0
- sage/misc/c3_controlled.pxd +2 -0
- sage/misc/c3_controlled.pyx +1402 -0
- sage/misc/cachefunc.cpython-314t-darwin.so +0 -0
- sage/misc/cachefunc.pxd +43 -0
- sage/misc/cachefunc.pyx +3781 -0
- sage/misc/call.py +188 -0
- sage/misc/classcall_metaclass.cpython-314t-darwin.so +0 -0
- sage/misc/classcall_metaclass.pxd +14 -0
- sage/misc/classcall_metaclass.pyx +599 -0
- sage/misc/constant_function.cpython-314t-darwin.so +0 -0
- sage/misc/constant_function.pyx +130 -0
- sage/misc/decorators.py +747 -0
- sage/misc/fast_methods.cpython-314t-darwin.so +0 -0
- sage/misc/fast_methods.pxd +20 -0
- sage/misc/fast_methods.pyx +351 -0
- sage/misc/flatten.py +90 -0
- sage/misc/fpickle.cpython-314t-darwin.so +0 -0
- sage/misc/fpickle.pyx +177 -0
- sage/misc/function_mangling.cpython-314t-darwin.so +0 -0
- sage/misc/function_mangling.pxd +11 -0
- sage/misc/function_mangling.pyx +308 -0
- sage/misc/inherit_comparison.cpython-314t-darwin.so +0 -0
- sage/misc/inherit_comparison.pxd +5 -0
- sage/misc/inherit_comparison.pyx +105 -0
- sage/misc/instancedoc.cpython-314t-darwin.so +0 -0
- sage/misc/instancedoc.pyx +331 -0
- sage/misc/lazy_attribute.cpython-314t-darwin.so +0 -0
- sage/misc/lazy_attribute.pyx +607 -0
- sage/misc/lazy_format.py +135 -0
- sage/misc/lazy_import.cpython-314t-darwin.so +0 -0
- sage/misc/lazy_import.pyx +1299 -0
- sage/misc/lazy_import_cache.py +36 -0
- sage/misc/lazy_list.cpython-314t-darwin.so +0 -0
- sage/misc/lazy_list.pxd +19 -0
- sage/misc/lazy_list.pyx +1187 -0
- sage/misc/lazy_string.cpython-314t-darwin.so +0 -0
- sage/misc/lazy_string.pxd +7 -0
- sage/misc/lazy_string.pyx +546 -0
- sage/misc/misc.py +1066 -0
- sage/misc/misc_c.cpython-314t-darwin.so +0 -0
- sage/misc/misc_c.pxd +3 -0
- sage/misc/misc_c.pyx +766 -0
- sage/misc/namespace_package.py +37 -0
- sage/misc/nested_class.cpython-314t-darwin.so +0 -0
- sage/misc/nested_class.pxd +3 -0
- sage/misc/nested_class.pyx +394 -0
- sage/misc/persist.cpython-314t-darwin.so +0 -0
- sage/misc/persist.pyx +1251 -0
- sage/misc/prandom.py +418 -0
- sage/misc/randstate.cpython-314t-darwin.so +0 -0
- sage/misc/randstate.pxd +30 -0
- sage/misc/randstate.pyx +1059 -0
- sage/misc/repr.py +203 -0
- sage/misc/reset.cpython-314t-darwin.so +0 -0
- sage/misc/reset.pyx +196 -0
- sage/misc/sage_ostools.cpython-314t-darwin.so +0 -0
- sage/misc/sage_ostools.pyx +323 -0
- sage/misc/sage_timeit.py +275 -0
- sage/misc/sage_timeit_class.cpython-314t-darwin.so +0 -0
- sage/misc/sage_timeit_class.pyx +120 -0
- sage/misc/sage_unittest.py +637 -0
- sage/misc/sageinspect.py +2768 -0
- sage/misc/session.cpython-314t-darwin.so +0 -0
- sage/misc/session.pyx +392 -0
- sage/misc/superseded.py +557 -0
- sage/misc/test_nested_class.py +228 -0
- sage/misc/timing.py +264 -0
- sage/misc/unknown.py +222 -0
- sage/misc/verbose.py +253 -0
- sage/misc/weak_dict.cpython-314t-darwin.so +0 -0
- sage/misc/weak_dict.pxd +15 -0
- sage/misc/weak_dict.pyx +1231 -0
- sage/modules/all__sagemath_objects.py +1 -0
- sage/modules/module.cpython-314t-darwin.so +0 -0
- sage/modules/module.pxd +5 -0
- sage/modules/module.pyx +329 -0
- sage/rings/all__sagemath_objects.py +3 -0
- sage/rings/integer_fake.h +22 -0
- sage/rings/integer_fake.pxd +55 -0
- sage/sets/all__sagemath_objects.py +3 -0
- sage/sets/pythonclass.cpython-314t-darwin.so +0 -0
- sage/sets/pythonclass.pxd +9 -0
- sage/sets/pythonclass.pyx +247 -0
- sage/structure/__init__.py +4 -0
- sage/structure/all.py +30 -0
- sage/structure/category_object.cpython-314t-darwin.so +0 -0
- sage/structure/category_object.pxd +28 -0
- sage/structure/category_object.pyx +1087 -0
- sage/structure/coerce.cpython-314t-darwin.so +0 -0
- sage/structure/coerce.pxd +44 -0
- sage/structure/coerce.pyx +2107 -0
- sage/structure/coerce_actions.cpython-314t-darwin.so +0 -0
- sage/structure/coerce_actions.pxd +27 -0
- sage/structure/coerce_actions.pyx +988 -0
- sage/structure/coerce_dict.cpython-314t-darwin.so +0 -0
- sage/structure/coerce_dict.pxd +51 -0
- sage/structure/coerce_dict.pyx +1557 -0
- sage/structure/coerce_exceptions.py +23 -0
- sage/structure/coerce_maps.cpython-314t-darwin.so +0 -0
- sage/structure/coerce_maps.pxd +28 -0
- sage/structure/coerce_maps.pyx +718 -0
- sage/structure/debug_options.cpython-314t-darwin.so +0 -0
- sage/structure/debug_options.pxd +6 -0
- sage/structure/debug_options.pyx +54 -0
- sage/structure/dynamic_class.py +541 -0
- sage/structure/element.cpython-314t-darwin.so +0 -0
- sage/structure/element.pxd +272 -0
- sage/structure/element.pyx +4772 -0
- sage/structure/element_wrapper.cpython-314t-darwin.so +0 -0
- sage/structure/element_wrapper.pxd +12 -0
- sage/structure/element_wrapper.pyx +582 -0
- sage/structure/factorization.py +1422 -0
- sage/structure/factorization_integer.py +105 -0
- sage/structure/factory.cpython-314t-darwin.so +0 -0
- sage/structure/factory.pyx +786 -0
- sage/structure/formal_sum.py +489 -0
- sage/structure/gens_py.py +73 -0
- sage/structure/global_options.py +1743 -0
- sage/structure/indexed_generators.py +863 -0
- sage/structure/list_clone.cpython-314t-darwin.so +0 -0
- sage/structure/list_clone.pxd +65 -0
- sage/structure/list_clone.pyx +1867 -0
- sage/structure/list_clone_demo.cpython-314t-darwin.so +0 -0
- sage/structure/list_clone_demo.pyx +248 -0
- sage/structure/list_clone_timings.py +179 -0
- sage/structure/list_clone_timings_cy.cpython-314t-darwin.so +0 -0
- sage/structure/list_clone_timings_cy.pyx +86 -0
- sage/structure/mutability.cpython-314t-darwin.so +0 -0
- sage/structure/mutability.pxd +21 -0
- sage/structure/mutability.pyx +348 -0
- sage/structure/nonexact.py +69 -0
- sage/structure/parent.cpython-314t-darwin.so +0 -0
- sage/structure/parent.pxd +112 -0
- sage/structure/parent.pyx +3093 -0
- sage/structure/parent_base.cpython-314t-darwin.so +0 -0
- sage/structure/parent_base.pxd +13 -0
- sage/structure/parent_base.pyx +44 -0
- sage/structure/parent_gens.cpython-314t-darwin.so +0 -0
- sage/structure/parent_gens.pxd +22 -0
- sage/structure/parent_gens.pyx +377 -0
- sage/structure/parent_old.cpython-314t-darwin.so +0 -0
- sage/structure/parent_old.pxd +25 -0
- sage/structure/parent_old.pyx +294 -0
- sage/structure/proof/__init__.py +1 -0
- sage/structure/proof/all.py +243 -0
- sage/structure/proof/proof.py +300 -0
- sage/structure/richcmp.cpython-314t-darwin.so +0 -0
- sage/structure/richcmp.pxd +213 -0
- sage/structure/richcmp.pyx +495 -0
- sage/structure/sage_object.cpython-314t-darwin.so +0 -0
- sage/structure/sage_object.pxd +3 -0
- sage/structure/sage_object.pyx +988 -0
- sage/structure/sage_object_test.py +19 -0
- sage/structure/sequence.py +937 -0
- sage/structure/set_factories.py +1178 -0
- sage/structure/set_factories_example.py +527 -0
- sage/structure/support_view.py +179 -0
- sage/structure/test_factory.py +56 -0
- sage/structure/unique_representation.py +1359 -0
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
# sage_setup: distribution = sagemath-objects
|
|
2
|
+
"""
|
|
3
|
+
Except from the `random` module from Python 2.7.14 used solely for consistency
|
|
4
|
+
in the doctest suite for random results that depend on the Python PRNG.
|
|
5
|
+
|
|
6
|
+
It should be made clear that the actual random data from the PRNG is consistent
|
|
7
|
+
between Python versions, but the difference lies in higher-level methods such
|
|
8
|
+
as `random.randint`.
|
|
9
|
+
|
|
10
|
+
See :issue:`24508`
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
# The following code was copied from
|
|
14
|
+
# https://github.com/python/cpython/blob/9b7ae96b98238627751e2f508ba5d600363555ef/Lib/random.py
|
|
15
|
+
# and was modified slightly for Python 3 compatibility.
|
|
16
|
+
#
|
|
17
|
+
# It has following copyrights; for the full Python license
|
|
18
|
+
# agreement see COPYING.txt at the root of Sage sources:
|
|
19
|
+
#
|
|
20
|
+
# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
|
|
21
|
+
# 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Python Software Foundation.
|
|
22
|
+
# All rights reserved.
|
|
23
|
+
#
|
|
24
|
+
# Copyright (c) 2000 BeOpen.com. All rights reserved.
|
|
25
|
+
#
|
|
26
|
+
# Copyright (c) 1995-2001 Corporation for National Research Initiatives. All
|
|
27
|
+
# rights reserved.
|
|
28
|
+
#
|
|
29
|
+
# Copyright (c) 1991-1995 Stichting Mathematisch Centrum. All rights reserved.
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
from warnings import warn as _warn
|
|
33
|
+
from types import MethodType as _MethodType, BuiltinMethodType as _BuiltinMethodType
|
|
34
|
+
from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
|
|
35
|
+
from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
|
|
36
|
+
from os import urandom as _urandom
|
|
37
|
+
from binascii import hexlify as _hexlify
|
|
38
|
+
import hashlib as _hashlib
|
|
39
|
+
|
|
40
|
+
NV_MAGICCONST = 4 * _exp(-0.5)/_sqrt(2.0)
|
|
41
|
+
TWOPI = 2.0*_pi
|
|
42
|
+
LOG4 = _log(4.0)
|
|
43
|
+
SG_MAGICCONST = 1.0 + _log(4.5)
|
|
44
|
+
BPF = 53 # Number of bits in a float
|
|
45
|
+
|
|
46
|
+
import _random
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class Random(_random.Random):
|
|
50
|
+
"""Random number generator base class used by bound module functions.
|
|
51
|
+
|
|
52
|
+
Used to instantiate instances of Random to get generators that don't
|
|
53
|
+
share state. Especially useful for multi-threaded programs, creating
|
|
54
|
+
a different instance of Random for each thread, and using the jumpahead()
|
|
55
|
+
method to ensure that the generated sequences seen by each thread don't
|
|
56
|
+
overlap.
|
|
57
|
+
|
|
58
|
+
Class Random can also be subclassed if you want to use a different basic
|
|
59
|
+
generator of your own devising: in that case, override the following
|
|
60
|
+
methods: random(), seed(), getstate(), setstate() and jumpahead().
|
|
61
|
+
Optionally, implement a getrandbits() method so that randrange() can cover
|
|
62
|
+
arbitrarily large ranges.
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
VERSION = 3 # used by getstate/setstate
|
|
66
|
+
|
|
67
|
+
def __init__(self, x=None):
|
|
68
|
+
"""Initialize an instance.
|
|
69
|
+
|
|
70
|
+
Optional argument x controls seeding, as for Random.seed().
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
self.seed(x)
|
|
74
|
+
self.gauss_next = None
|
|
75
|
+
|
|
76
|
+
def seed(self, a=None):
|
|
77
|
+
"""Initialize internal state of the random number generator.
|
|
78
|
+
|
|
79
|
+
None or no argument seeds from current time or from an operating
|
|
80
|
+
system specific randomness source if available.
|
|
81
|
+
|
|
82
|
+
If a is not None or is an int or long, hash(a) is used instead.
|
|
83
|
+
Hash values for some types are nondeterministic when the
|
|
84
|
+
PYTHONHASHSEED environment variable is enabled.
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
if a is None:
|
|
88
|
+
try:
|
|
89
|
+
# Seed with enough bytes to span the 19937 bit
|
|
90
|
+
# state space for the Mersenne Twister
|
|
91
|
+
a = int(_hexlify(_urandom(2500)), 16)
|
|
92
|
+
except NotImplementedError:
|
|
93
|
+
import time
|
|
94
|
+
a = int(time.time() * 256) # use fractional seconds
|
|
95
|
+
|
|
96
|
+
super().seed(a)
|
|
97
|
+
self.gauss_next = None
|
|
98
|
+
|
|
99
|
+
def getstate(self):
|
|
100
|
+
"""Return internal state; can be passed to setstate() later."""
|
|
101
|
+
return self.VERSION, super().getstate(), self.gauss_next
|
|
102
|
+
|
|
103
|
+
def setstate(self, state):
|
|
104
|
+
"""Restore internal state from object returned by getstate()."""
|
|
105
|
+
version = state[0]
|
|
106
|
+
if version == 3:
|
|
107
|
+
version, internalstate, self.gauss_next = state
|
|
108
|
+
super().setstate(internalstate)
|
|
109
|
+
elif version == 2:
|
|
110
|
+
version, internalstate, self.gauss_next = state
|
|
111
|
+
# In version 2, the state was saved as signed ints, which causes
|
|
112
|
+
# inconsistencies between 32/64-bit systems. The state is
|
|
113
|
+
# really unsigned 32-bit ints, so we convert negative ints from
|
|
114
|
+
# version 2 to positive longs for version 3.
|
|
115
|
+
try:
|
|
116
|
+
internalstate = tuple( int(x) % (2**32) for x in internalstate )
|
|
117
|
+
except ValueError as e:
|
|
118
|
+
raise TypeError(e)
|
|
119
|
+
super().setstate(internalstate)
|
|
120
|
+
else:
|
|
121
|
+
raise ValueError("state with version %s passed to "
|
|
122
|
+
"Random.setstate() of version %s" %
|
|
123
|
+
(version, self.VERSION))
|
|
124
|
+
|
|
125
|
+
def jumpahead(self, n):
|
|
126
|
+
"""Change the internal state to one that is likely far away
|
|
127
|
+
from the current state. This method will not be in Py3.x,
|
|
128
|
+
so it is better to simply reseed.
|
|
129
|
+
"""
|
|
130
|
+
# The super.jumpahead() method uses shuffling to change state,
|
|
131
|
+
# so it needs a large and "interesting" n to work with. Here,
|
|
132
|
+
# we use hashing to create a large n for the shuffle.
|
|
133
|
+
s = repr(n) + repr(self.getstate())
|
|
134
|
+
n = int(_hashlib.new('sha512', s).hexdigest(), 16)
|
|
135
|
+
super().jumpahead(n)
|
|
136
|
+
|
|
137
|
+
## ---- Methods below this point do not need to be overridden when
|
|
138
|
+
## ---- subclassing for the purpose of using a different core generator.
|
|
139
|
+
|
|
140
|
+
## -------------------- pickle support -------------------
|
|
141
|
+
|
|
142
|
+
def __getstate__(self): # for pickle
|
|
143
|
+
return self.getstate()
|
|
144
|
+
|
|
145
|
+
def __setstate__(self, state): # for pickle
|
|
146
|
+
self.setstate(state)
|
|
147
|
+
|
|
148
|
+
def __reduce__(self):
|
|
149
|
+
return self.__class__, (), self.getstate()
|
|
150
|
+
|
|
151
|
+
## -------------------- integer methods -------------------
|
|
152
|
+
|
|
153
|
+
def randrange(self, start, stop=None, step=1, _int=int, _maxwidth=1 << BPF):
|
|
154
|
+
"""Choose a random item from range(start, stop[, step]).
|
|
155
|
+
|
|
156
|
+
This fixes the problem with randint() which includes the
|
|
157
|
+
endpoint; in Python this is usually not what you want.
|
|
158
|
+
"""
|
|
159
|
+
|
|
160
|
+
# This code is a bit messy to make it fast for the
|
|
161
|
+
# common case while still doing adequate error checking.
|
|
162
|
+
istart = _int(start)
|
|
163
|
+
if istart != start:
|
|
164
|
+
raise ValueError("non-integer arg 1 for randrange()")
|
|
165
|
+
if stop is None:
|
|
166
|
+
if istart > 0:
|
|
167
|
+
if istart >= _maxwidth:
|
|
168
|
+
return self._randbelow(istart)
|
|
169
|
+
return _int(self.random() * istart)
|
|
170
|
+
raise ValueError("empty range for randrange()")
|
|
171
|
+
|
|
172
|
+
# stop argument supplied.
|
|
173
|
+
istop = _int(stop)
|
|
174
|
+
if istop != stop:
|
|
175
|
+
raise ValueError("non-integer stop for randrange()")
|
|
176
|
+
width = istop - istart
|
|
177
|
+
if step == 1 and width > 0:
|
|
178
|
+
# Note that
|
|
179
|
+
# int(istart + self.random()*width)
|
|
180
|
+
# instead would be incorrect. For example, consider istart
|
|
181
|
+
# = -2 and istop = 0. Then the guts would be in
|
|
182
|
+
# -2.0 to 0.0 exclusive on both ends (ignoring that random()
|
|
183
|
+
# might return 0.0), and because int() truncates toward 0, the
|
|
184
|
+
# final result would be -1 or 0 (instead of -2 or -1).
|
|
185
|
+
# istart + int(self.random()*width)
|
|
186
|
+
# would also be incorrect, for a subtler reason: the RHS
|
|
187
|
+
# can return a long, and then randrange() would also return
|
|
188
|
+
# a long, but we're supposed to return an int (for backward
|
|
189
|
+
# compatibility).
|
|
190
|
+
|
|
191
|
+
if width >= _maxwidth:
|
|
192
|
+
return _int(istart + self._randbelow(width))
|
|
193
|
+
return _int(istart + _int(self.random()*width))
|
|
194
|
+
if step == 1:
|
|
195
|
+
raise ValueError("empty range for randrange() (%d,%d, %d)" %
|
|
196
|
+
(istart, istop, width))
|
|
197
|
+
|
|
198
|
+
# Non-unit step argument supplied.
|
|
199
|
+
istep = _int(step)
|
|
200
|
+
if istep != step:
|
|
201
|
+
raise ValueError("non-integer step for randrange()")
|
|
202
|
+
if istep > 0:
|
|
203
|
+
n = (width + istep - 1) // istep
|
|
204
|
+
elif istep < 0:
|
|
205
|
+
n = (width + istep + 1) // istep
|
|
206
|
+
else:
|
|
207
|
+
raise ValueError("zero step for randrange()")
|
|
208
|
+
|
|
209
|
+
if n <= 0:
|
|
210
|
+
raise ValueError("empty range for randrange()")
|
|
211
|
+
|
|
212
|
+
if n >= _maxwidth:
|
|
213
|
+
return istart + istep*self._randbelow(n)
|
|
214
|
+
return istart + istep*_int(self.random() * n)
|
|
215
|
+
|
|
216
|
+
def randint(self, a, b):
|
|
217
|
+
"""Return random integer in range [a, b], including both end points.
|
|
218
|
+
"""
|
|
219
|
+
return self.randrange(a, b + 1)
|
|
220
|
+
|
|
221
|
+
def _randbelow(self, n, _log=_log, _int=int, _maxwidth=1 << BPF,
|
|
222
|
+
_Method=_MethodType, _BuiltinMethod=_BuiltinMethodType):
|
|
223
|
+
"""Return a random int in the range [0,n)
|
|
224
|
+
|
|
225
|
+
Handles the case where n has more bits than returned
|
|
226
|
+
by a single call to the underlying generator.
|
|
227
|
+
"""
|
|
228
|
+
|
|
229
|
+
try:
|
|
230
|
+
getrandbits = self.getrandbits
|
|
231
|
+
except AttributeError:
|
|
232
|
+
pass
|
|
233
|
+
else:
|
|
234
|
+
# Only call self.getrandbits if the original random() builtin method
|
|
235
|
+
# has not been overridden or if a new getrandbits() was supplied.
|
|
236
|
+
# This assures that the two methods correspond.
|
|
237
|
+
if type(self.random) is _BuiltinMethod or type(getrandbits) is _Method:
|
|
238
|
+
k = _int(1.00001 + _log(n-1, 2.0)) # 2**k > n-1 > 2**(k-2)
|
|
239
|
+
r = getrandbits(k)
|
|
240
|
+
while r >= n:
|
|
241
|
+
r = getrandbits(k)
|
|
242
|
+
return r
|
|
243
|
+
if n >= _maxwidth:
|
|
244
|
+
_warn("Underlying random() generator does not supply \n"
|
|
245
|
+
"enough bits to choose from a population range this large")
|
|
246
|
+
return _int(self.random() * n)
|
|
247
|
+
|
|
248
|
+
## -------------------- sequence methods -------------------
|
|
249
|
+
|
|
250
|
+
def choice(self, seq):
|
|
251
|
+
"""Choose a random element from a non-empty sequence."""
|
|
252
|
+
return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty
|
|
253
|
+
|
|
254
|
+
def shuffle(self, x, random=None):
|
|
255
|
+
"""x, random=random.random -> shuffle list x in place; return None.
|
|
256
|
+
|
|
257
|
+
Optional arg random is a 0-argument function returning a random
|
|
258
|
+
float in [0.0, 1.0); by default, the standard random.random.
|
|
259
|
+
"""
|
|
260
|
+
|
|
261
|
+
if random is None:
|
|
262
|
+
random = self.random
|
|
263
|
+
_int = int
|
|
264
|
+
for i in reversed(range(1, len(x))):
|
|
265
|
+
# pick an element in x[:i+1] with which to exchange x[i]
|
|
266
|
+
j = _int(random() * (i+1))
|
|
267
|
+
x[i], x[j] = x[j], x[i]
|
|
268
|
+
|
|
269
|
+
def sample(self, population, k):
|
|
270
|
+
"""
|
|
271
|
+
Choose k unique random elements from a population sequence.
|
|
272
|
+
|
|
273
|
+
Return a new list containing elements from the population while
|
|
274
|
+
leaving the original population unchanged. The resulting list is
|
|
275
|
+
in selection order so that all sub-slices will also be valid random
|
|
276
|
+
samples. This allows raffle winners (the sample) to be partitioned
|
|
277
|
+
into grand prize and second place winners (the subslices).
|
|
278
|
+
|
|
279
|
+
Members of the population need not be hashable or unique. If the
|
|
280
|
+
population contains repeats, then each occurrence is a possible
|
|
281
|
+
selection in the sample.
|
|
282
|
+
|
|
283
|
+
To choose a sample in a range of integers, use range as an argument.
|
|
284
|
+
This is especially fast and space efficient for sampling from a
|
|
285
|
+
large population: sample(range(10000000), 60)
|
|
286
|
+
"""
|
|
287
|
+
|
|
288
|
+
# Sampling without replacement entails tracking either potential
|
|
289
|
+
# selections (the pool) in a list or previous selections in a set.
|
|
290
|
+
|
|
291
|
+
# When the number of selections is small compared to the
|
|
292
|
+
# population, then tracking selections is efficient, requiring
|
|
293
|
+
# only a small set and an occasional reselection. For
|
|
294
|
+
# a larger number of selections, the pool tracking method is
|
|
295
|
+
# preferred since the list takes less space than the
|
|
296
|
+
# set and it doesn't suffer from frequent reselections.
|
|
297
|
+
|
|
298
|
+
n = len(population)
|
|
299
|
+
if not 0 <= k <= n:
|
|
300
|
+
raise ValueError("sample larger than population")
|
|
301
|
+
random = self.random
|
|
302
|
+
_int = int
|
|
303
|
+
result = [None] * k
|
|
304
|
+
setsize = 21 # size of a small set minus size of an empty list
|
|
305
|
+
if k > 5:
|
|
306
|
+
setsize += 4 ** _ceil(_log(k * 3, 4)) # table size for big sets
|
|
307
|
+
if n <= setsize or hasattr(population, "keys"):
|
|
308
|
+
# An n-length list is smaller than a k-length set, or this is a
|
|
309
|
+
# mapping type so the other algorithm wouldn't work.
|
|
310
|
+
pool = list(population)
|
|
311
|
+
for i in range(k): # invariant: non-selected at [0,n-i)
|
|
312
|
+
j = _int(random() * (n-i))
|
|
313
|
+
result[i] = pool[j]
|
|
314
|
+
pool[j] = pool[n-i-1] # move non-selected item into vacancy
|
|
315
|
+
else:
|
|
316
|
+
try:
|
|
317
|
+
selected = set()
|
|
318
|
+
selected_add = selected.add
|
|
319
|
+
for i in range(k):
|
|
320
|
+
j = _int(random() * n)
|
|
321
|
+
while j in selected:
|
|
322
|
+
j = _int(random() * n)
|
|
323
|
+
selected_add(j)
|
|
324
|
+
result[i] = population[j]
|
|
325
|
+
except (TypeError, KeyError): # handle (at least) sets
|
|
326
|
+
if isinstance(population, list):
|
|
327
|
+
raise
|
|
328
|
+
return self.sample(tuple(population), k)
|
|
329
|
+
return result
|
|
330
|
+
|
|
331
|
+
## -------------------- real-valued distributions -------------------
|
|
332
|
+
|
|
333
|
+
## -------------------- uniform distribution -------------------
|
|
334
|
+
|
|
335
|
+
def uniform(self, a, b):
|
|
336
|
+
"Get a random number in the range [a, b) or [a, b] depending on rounding."
|
|
337
|
+
return a + (b-a) * self.random()
|
|
338
|
+
|
|
339
|
+
## -------------------- triangular --------------------
|
|
340
|
+
|
|
341
|
+
def triangular(self, low=0.0, high=1.0, mode=None):
|
|
342
|
+
"""Triangular distribution.
|
|
343
|
+
|
|
344
|
+
Continuous distribution bounded by given lower and upper limits,
|
|
345
|
+
and having a given mode value in-between.
|
|
346
|
+
|
|
347
|
+
http://en.wikipedia.org/wiki/Triangular_distribution
|
|
348
|
+
"""
|
|
349
|
+
u = self.random()
|
|
350
|
+
try:
|
|
351
|
+
c = 0.5 if mode is None else (mode - low) / (high - low)
|
|
352
|
+
except ZeroDivisionError:
|
|
353
|
+
return low
|
|
354
|
+
if u > c:
|
|
355
|
+
u = 1.0 - u
|
|
356
|
+
c = 1.0 - c
|
|
357
|
+
low, high = high, low
|
|
358
|
+
return low + (high - low) * (u * c) ** 0.5
|
|
359
|
+
|
|
360
|
+
## -------------------- normal distribution --------------------
|
|
361
|
+
|
|
362
|
+
def normalvariate(self, mu, sigma):
|
|
363
|
+
"""Normal distribution.
|
|
364
|
+
|
|
365
|
+
mu is the mean, and sigma is the standard deviation.
|
|
366
|
+
"""
|
|
367
|
+
# mu = mean, sigma = standard deviation
|
|
368
|
+
|
|
369
|
+
# Uses Kinderman and Monahan method. Reference: Kinderman,
|
|
370
|
+
# A.J. and Monahan, J.F., "Computer generation of random
|
|
371
|
+
# variables using the ratio of uniform deviates", ACM Trans
|
|
372
|
+
# Math Software, 3, (1977), pp257-260.
|
|
373
|
+
|
|
374
|
+
random = self.random
|
|
375
|
+
while 1:
|
|
376
|
+
u1 = random()
|
|
377
|
+
u2 = 1.0 - random()
|
|
378
|
+
z = NV_MAGICCONST*(u1-0.5)/u2
|
|
379
|
+
zz = z*z/4.0
|
|
380
|
+
if zz <= -_log(u2):
|
|
381
|
+
break
|
|
382
|
+
return mu + z*sigma
|
|
383
|
+
|
|
384
|
+
## -------------------- lognormal distribution --------------------
|
|
385
|
+
|
|
386
|
+
def lognormvariate(self, mu, sigma):
|
|
387
|
+
"""Log normal distribution.
|
|
388
|
+
|
|
389
|
+
If you take the natural logarithm of this distribution, you'll get a
|
|
390
|
+
normal distribution with mean mu and standard deviation sigma.
|
|
391
|
+
mu can have any value, and sigma must be greater than zero.
|
|
392
|
+
"""
|
|
393
|
+
return _exp(self.normalvariate(mu, sigma))
|
|
394
|
+
|
|
395
|
+
## -------------------- exponential distribution --------------------
|
|
396
|
+
|
|
397
|
+
def expovariate(self, lambd):
|
|
398
|
+
"""Exponential distribution.
|
|
399
|
+
|
|
400
|
+
lambd is 1.0 divided by the desired mean. It should be
|
|
401
|
+
nonzero. (The parameter would be called "lambda", but that is
|
|
402
|
+
a reserved word in Python.) Returned values range from 0 to
|
|
403
|
+
positive infinity if lambd is positive, and from negative
|
|
404
|
+
infinity to 0 if lambd is negative.
|
|
405
|
+
"""
|
|
406
|
+
# lambd: rate lambd = 1/mean
|
|
407
|
+
# ('lambda' is a Python reserved word)
|
|
408
|
+
|
|
409
|
+
# we use 1-random() instead of random() to preclude the
|
|
410
|
+
# possibility of taking the log of zero.
|
|
411
|
+
return -_log(1.0 - self.random())/lambd
|
|
412
|
+
|
|
413
|
+
## -------------------- von Mises distribution --------------------
|
|
414
|
+
|
|
415
|
+
def vonmisesvariate(self, mu, kappa):
|
|
416
|
+
"""Circular data distribution.
|
|
417
|
+
|
|
418
|
+
mu is the mean angle, expressed in radians between 0 and 2*pi, and
|
|
419
|
+
kappa is the concentration parameter, which must be greater than or
|
|
420
|
+
equal to zero. If kappa is equal to zero, this distribution reduces
|
|
421
|
+
to a uniform random angle over the range 0 to 2*pi.
|
|
422
|
+
"""
|
|
423
|
+
# mu: mean angle (in radians between 0 and 2*pi)
|
|
424
|
+
# kappa: concentration parameter kappa (>= 0)
|
|
425
|
+
# if kappa = 0 generate uniform random angle
|
|
426
|
+
|
|
427
|
+
# Based upon an algorithm published in: Fisher, N.I.,
|
|
428
|
+
# "Statistical Analysis of Circular Data", Cambridge
|
|
429
|
+
# University Press, 1993.
|
|
430
|
+
|
|
431
|
+
# Thanks to Magnus Kessler for a correction to the
|
|
432
|
+
# implementation of step 4.
|
|
433
|
+
|
|
434
|
+
random = self.random
|
|
435
|
+
if kappa <= 1e-6:
|
|
436
|
+
return TWOPI * random()
|
|
437
|
+
|
|
438
|
+
s = 0.5 / kappa
|
|
439
|
+
r = s + _sqrt(1.0 + s * s)
|
|
440
|
+
|
|
441
|
+
while 1:
|
|
442
|
+
u1 = random()
|
|
443
|
+
z = _cos(_pi * u1)
|
|
444
|
+
|
|
445
|
+
d = z / (r + z)
|
|
446
|
+
u2 = random()
|
|
447
|
+
if u2 < 1.0 - d * d or u2 <= (1.0 - d) * _exp(d):
|
|
448
|
+
break
|
|
449
|
+
|
|
450
|
+
q = 1.0 / r
|
|
451
|
+
f = (q + z) / (1.0 + q * z)
|
|
452
|
+
u3 = random()
|
|
453
|
+
if u3 > 0.5:
|
|
454
|
+
theta = (mu + _acos(f)) % TWOPI
|
|
455
|
+
else:
|
|
456
|
+
theta = (mu - _acos(f)) % TWOPI
|
|
457
|
+
|
|
458
|
+
return theta
|
|
459
|
+
|
|
460
|
+
## -------------------- gamma distribution --------------------
|
|
461
|
+
|
|
462
|
+
def gammavariate(self, alpha, beta):
|
|
463
|
+
"""Gamma distribution. Not the gamma function!
|
|
464
|
+
|
|
465
|
+
Conditions on the parameters are alpha > 0 and beta > 0.
|
|
466
|
+
|
|
467
|
+
The probability distribution function is::
|
|
468
|
+
|
|
469
|
+
x ** (alpha - 1) * math.exp(-x / beta)
|
|
470
|
+
pdf(x) = --------------------------------------
|
|
471
|
+
math.gamma(alpha) * beta ** alpha
|
|
472
|
+
"""
|
|
473
|
+
|
|
474
|
+
# alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2
|
|
475
|
+
|
|
476
|
+
# Warning: a few older sources define the gamma distribution in terms
|
|
477
|
+
# of alpha > -1.0
|
|
478
|
+
if alpha <= 0.0 or beta <= 0.0:
|
|
479
|
+
raise ValueError('gammavariate: alpha and beta must be > 0.0')
|
|
480
|
+
|
|
481
|
+
random = self.random
|
|
482
|
+
if alpha > 1.0:
|
|
483
|
+
|
|
484
|
+
# Uses R.C.H. Cheng, "The generation of Gamma
|
|
485
|
+
# variables with non-integral shape parameters",
|
|
486
|
+
# Applied Statistics, (1977), 26, No. 1, p71-74
|
|
487
|
+
|
|
488
|
+
ainv = _sqrt(2.0 * alpha - 1.0)
|
|
489
|
+
bbb = alpha - LOG4
|
|
490
|
+
ccc = alpha + ainv
|
|
491
|
+
|
|
492
|
+
while 1:
|
|
493
|
+
u1 = random()
|
|
494
|
+
if not 1e-7 < u1 < .9999999:
|
|
495
|
+
continue
|
|
496
|
+
u2 = 1.0 - random()
|
|
497
|
+
v = _log(u1/(1.0-u1))/ainv
|
|
498
|
+
x = alpha*_exp(v)
|
|
499
|
+
z = u1*u1*u2
|
|
500
|
+
r = bbb+ccc*v-x
|
|
501
|
+
if r + SG_MAGICCONST - 4.5*z >= 0.0 or r >= _log(z):
|
|
502
|
+
return x * beta
|
|
503
|
+
|
|
504
|
+
elif alpha == 1.0:
|
|
505
|
+
# expovariate(1)
|
|
506
|
+
u = random()
|
|
507
|
+
while u <= 1e-7:
|
|
508
|
+
u = random()
|
|
509
|
+
return -_log(u) * beta
|
|
510
|
+
|
|
511
|
+
else: # alpha is between 0 and 1 (exclusive)
|
|
512
|
+
|
|
513
|
+
# Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
|
|
514
|
+
|
|
515
|
+
while 1:
|
|
516
|
+
u = random()
|
|
517
|
+
b = (_e + alpha)/_e
|
|
518
|
+
p = b*u
|
|
519
|
+
if p <= 1.0:
|
|
520
|
+
x = p ** (1.0/alpha)
|
|
521
|
+
else:
|
|
522
|
+
x = -_log((b-p)/alpha)
|
|
523
|
+
u1 = random()
|
|
524
|
+
if p > 1.0:
|
|
525
|
+
if u1 <= x ** (alpha - 1.0):
|
|
526
|
+
break
|
|
527
|
+
elif u1 <= _exp(-x):
|
|
528
|
+
break
|
|
529
|
+
return x * beta
|
|
530
|
+
|
|
531
|
+
## -------------------- Gauss (faster alternative) --------------------
|
|
532
|
+
|
|
533
|
+
def gauss(self, mu, sigma):
|
|
534
|
+
"""Gaussian distribution.
|
|
535
|
+
|
|
536
|
+
mu is the mean, and sigma is the standard deviation. This is
|
|
537
|
+
slightly faster than the normalvariate() function.
|
|
538
|
+
|
|
539
|
+
Not thread-safe without a lock around calls.
|
|
540
|
+
"""
|
|
541
|
+
|
|
542
|
+
# When x and y are two variables from [0, 1), uniformly
|
|
543
|
+
# distributed, then
|
|
544
|
+
#
|
|
545
|
+
# cos(2*pi*x)*sqrt(-2*log(1-y))
|
|
546
|
+
# sin(2*pi*x)*sqrt(-2*log(1-y))
|
|
547
|
+
#
|
|
548
|
+
# are two *independent* variables with normal distribution
|
|
549
|
+
# (mu = 0, sigma = 1).
|
|
550
|
+
# (Lambert Meertens)
|
|
551
|
+
# (corrected version; bug discovered by Mike Miller, fixed by LM)
|
|
552
|
+
|
|
553
|
+
# Multithreading note: When two threads call this function
|
|
554
|
+
# simultaneously, it is possible that they will receive the
|
|
555
|
+
# same return value. The window is very small though. To
|
|
556
|
+
# avoid this, you have to use a lock around all calls. (I
|
|
557
|
+
# didn't want to slow this down in the serial case by using a
|
|
558
|
+
# lock here.)
|
|
559
|
+
|
|
560
|
+
random = self.random
|
|
561
|
+
z = self.gauss_next
|
|
562
|
+
self.gauss_next = None
|
|
563
|
+
if z is None:
|
|
564
|
+
x2pi = random() * TWOPI
|
|
565
|
+
g2rad = _sqrt(-2.0 * _log(1.0 - random()))
|
|
566
|
+
z = _cos(x2pi) * g2rad
|
|
567
|
+
self.gauss_next = _sin(x2pi) * g2rad
|
|
568
|
+
|
|
569
|
+
return mu + z*sigma
|
|
570
|
+
|
|
571
|
+
## -------------------- beta --------------------
|
|
572
|
+
## See
|
|
573
|
+
## http://mail.python.org/pipermail/python-bugs-list/2001-January/003752.html
|
|
574
|
+
## for Ivan Frohne's insightful analysis of why the original implementation:
|
|
575
|
+
##
|
|
576
|
+
## def betavariate(self, alpha, beta):
|
|
577
|
+
## # Discrete Event Simulation in C, pp 87-88.
|
|
578
|
+
##
|
|
579
|
+
## y = self.expovariate(alpha)
|
|
580
|
+
## z = self.expovariate(1.0/beta)
|
|
581
|
+
## return z/(y+z)
|
|
582
|
+
##
|
|
583
|
+
## was dead wrong, and how it probably got that way.
|
|
584
|
+
|
|
585
|
+
def betavariate(self, alpha, beta):
|
|
586
|
+
"""Beta distribution.
|
|
587
|
+
|
|
588
|
+
Conditions on the parameters are alpha > 0 and beta > 0.
|
|
589
|
+
Returned values range between 0 and 1.
|
|
590
|
+
"""
|
|
591
|
+
|
|
592
|
+
# This version due to Janne Sinkkonen, and matches all the std
|
|
593
|
+
# texts (e.g., Knuth Vol 2 Ed 3 pg 134 "the beta distribution").
|
|
594
|
+
y = self.gammavariate(alpha, 1.)
|
|
595
|
+
if y == 0:
|
|
596
|
+
return 0.0
|
|
597
|
+
else:
|
|
598
|
+
return y / (y + self.gammavariate(beta, 1.))
|
|
599
|
+
|
|
600
|
+
## -------------------- Pareto --------------------
|
|
601
|
+
|
|
602
|
+
def paretovariate(self, alpha):
|
|
603
|
+
"""Pareto distribution. alpha is the shape parameter."""
|
|
604
|
+
# Jain, pg. 495
|
|
605
|
+
|
|
606
|
+
u = 1.0 - self.random()
|
|
607
|
+
return 1.0 / pow(u, 1.0/alpha)
|
|
608
|
+
|
|
609
|
+
## -------------------- Weibull --------------------
|
|
610
|
+
|
|
611
|
+
def weibullvariate(self, alpha, beta):
|
|
612
|
+
"""Weibull distribution.
|
|
613
|
+
|
|
614
|
+
alpha is the scale parameter and beta is the shape parameter.
|
|
615
|
+
"""
|
|
616
|
+
# Jain, pg. 499; bug fix courtesy Bill Arms
|
|
617
|
+
|
|
618
|
+
u = 1.0 - self.random()
|
|
619
|
+
return alpha * pow(-_log(u), 1.0/beta)
|
sage/cpython/all.py
ADDED
|
Binary file
|