Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +3 -0
- aria/lib/python3.10/site-packages/nvidia/nccl/lib/libnccl.so.2 +3 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/calculus/__init__.py +25 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/calculus/__pycache__/euler.cpython-310.pyc +0 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/calculus/finite_diff.py +476 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/domainmatrix.cpython-310.pyc +3 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/__pycache__/__init__.cpython-310.pyc +0 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/__pycache__/aesaracode.cpython-310.pyc +0 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/__pycache__/cxx.cpython-310.pyc +0 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/__pycache__/dot.cpython-310.pyc +0 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/__pycache__/fortran.cpython-310.pyc +0 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/__pycache__/octave.cpython-310.pyc +0 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/__pycache__/python.cpython-310.pyc +0 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/pretty/__pycache__/pretty.cpython-310.pyc +0 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/pretty/__pycache__/pretty_symbology.cpython-310.pyc +0 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/pretty/__pycache__/stringpict.cpython-310.pyc +0 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/pretty/pretty.py +0 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/pretty/stringpict.py +537 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/pretty/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/pretty/tests/test_pretty.py +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/__init__.py +180 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/_add_newdocs_scalars.py +372 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/_dtype.py +369 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/_machar.py +356 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/_multiarray_tests.cpython-310-x86_64-linux-gnu.so +3 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/_rational_tests.cpython-310-x86_64-linux-gnu.so +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/_string_helpers.py +100 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/cversions.py +13 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/memmap.py +338 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_array_interface.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_casting_unittests.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_conversion_utils.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_cpu_dispatcher.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_cython.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_deprecations.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_dlpack.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_einsum.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_hashtable.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_longdouble.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_mem_overlap.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_mem_policy.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_scalar_ctors.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_scalarinherit.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_scalarprint.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_simd_module.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_ufunc.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_umath_complex.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_unicode.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/examples/cython/__pycache__/setup.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/examples/cython/checks.pyx +35 -0
.gitattributes
CHANGED
@@ -910,3 +910,6 @@ infer_4_47_1/lib/python3.10/site-packages/pandas/io/formats/__pycache__/style.cp
|
|
910 |
emu3/lib/python3.10/site-packages/nvidia/cufft/lib/libcufft.so.11 filter=lfs diff=lfs merge=lfs -text
|
911 |
evalkit_cambrian/lib/python3.10/site-packages/sympy/matrices/__pycache__/matrixbase.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
912 |
evalkit_cambrian/lib/python3.10/site-packages/safetensors/_safetensors_rust.abi3.so filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
910 |
emu3/lib/python3.10/site-packages/nvidia/cufft/lib/libcufft.so.11 filter=lfs diff=lfs merge=lfs -text
|
911 |
evalkit_cambrian/lib/python3.10/site-packages/sympy/matrices/__pycache__/matrixbase.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
912 |
evalkit_cambrian/lib/python3.10/site-packages/safetensors/_safetensors_rust.abi3.so filter=lfs diff=lfs merge=lfs -text
|
913 |
+
evalkit_cambrian/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/domainmatrix.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
914 |
+
aria/lib/python3.10/site-packages/nvidia/nccl/lib/libnccl.so.2 filter=lfs diff=lfs merge=lfs -text
|
915 |
+
infer_4_47_1/lib/python3.10/site-packages/numpy/core/_multiarray_tests.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
aria/lib/python3.10/site-packages/nvidia/nccl/lib/libnccl.so.2
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4de8ef6a9d27e45e0e3d067c81fdc0a7103cb318e34557da25949a4230a9caaf
|
3 |
+
size 192362072
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/calculus/__init__.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Calculus-related methods."""
|
2 |
+
|
3 |
+
from .euler import euler_equations
|
4 |
+
from .singularities import (singularities, is_increasing,
|
5 |
+
is_strictly_increasing, is_decreasing,
|
6 |
+
is_strictly_decreasing, is_monotonic)
|
7 |
+
from .finite_diff import finite_diff_weights, apply_finite_diff, differentiate_finite
|
8 |
+
from .util import (periodicity, not_empty_in, is_convex,
|
9 |
+
stationary_points, minimum, maximum)
|
10 |
+
from .accumulationbounds import AccumBounds
|
11 |
+
|
12 |
+
__all__ = [
|
13 |
+
'euler_equations',
|
14 |
+
|
15 |
+
'singularities', 'is_increasing',
|
16 |
+
'is_strictly_increasing', 'is_decreasing',
|
17 |
+
'is_strictly_decreasing', 'is_monotonic',
|
18 |
+
|
19 |
+
'finite_diff_weights', 'apply_finite_diff', 'differentiate_finite',
|
20 |
+
|
21 |
+
'periodicity', 'not_empty_in', 'is_convex', 'stationary_points',
|
22 |
+
'minimum', 'maximum',
|
23 |
+
|
24 |
+
'AccumBounds'
|
25 |
+
]
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/calculus/__pycache__/euler.cpython-310.pyc
ADDED
Binary file (3.81 kB). View file
|
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/calculus/finite_diff.py
ADDED
@@ -0,0 +1,476 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Finite difference weights
|
3 |
+
=========================
|
4 |
+
|
5 |
+
This module implements an algorithm for efficient generation of finite
|
6 |
+
difference weights for ordinary differentials of functions for
|
7 |
+
derivatives from 0 (interpolation) up to arbitrary order.
|
8 |
+
|
9 |
+
The core algorithm is provided in the finite difference weight generating
|
10 |
+
function (``finite_diff_weights``), and two convenience functions are provided
|
11 |
+
for:
|
12 |
+
|
13 |
+
- estimating a derivative (or interpolate) directly from a series of points
|
14 |
+
is also provided (``apply_finite_diff``).
|
15 |
+
- differentiating by using finite difference approximations
|
16 |
+
(``differentiate_finite``).
|
17 |
+
|
18 |
+
"""
|
19 |
+
|
20 |
+
from sympy.core.function import Derivative
|
21 |
+
from sympy.core.singleton import S
|
22 |
+
from sympy.core.function import Subs
|
23 |
+
from sympy.core.traversal import preorder_traversal
|
24 |
+
from sympy.utilities.exceptions import sympy_deprecation_warning
|
25 |
+
from sympy.utilities.iterables import iterable
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
def finite_diff_weights(order, x_list, x0=S.One):
|
30 |
+
"""
|
31 |
+
Calculates the finite difference weights for an arbitrarily spaced
|
32 |
+
one-dimensional grid (``x_list``) for derivatives at ``x0`` of order
|
33 |
+
0, 1, ..., up to ``order`` using a recursive formula. Order of accuracy
|
34 |
+
is at least ``len(x_list) - order``, if ``x_list`` is defined correctly.
|
35 |
+
|
36 |
+
Parameters
|
37 |
+
==========
|
38 |
+
|
39 |
+
order: int
|
40 |
+
Up to what derivative order weights should be calculated.
|
41 |
+
0 corresponds to interpolation.
|
42 |
+
x_list: sequence
|
43 |
+
Sequence of (unique) values for the independent variable.
|
44 |
+
It is useful (but not necessary) to order ``x_list`` from
|
45 |
+
nearest to furthest from ``x0``; see examples below.
|
46 |
+
x0: Number or Symbol
|
47 |
+
Root or value of the independent variable for which the finite
|
48 |
+
difference weights should be generated. Default is ``S.One``.
|
49 |
+
|
50 |
+
Returns
|
51 |
+
=======
|
52 |
+
|
53 |
+
list
|
54 |
+
A list of sublists, each corresponding to coefficients for
|
55 |
+
increasing derivative order, and each containing lists of
|
56 |
+
coefficients for increasing subsets of x_list.
|
57 |
+
|
58 |
+
Examples
|
59 |
+
========
|
60 |
+
|
61 |
+
>>> from sympy import finite_diff_weights, S
|
62 |
+
>>> res = finite_diff_weights(1, [-S(1)/2, S(1)/2, S(3)/2, S(5)/2], 0)
|
63 |
+
>>> res
|
64 |
+
[[[1, 0, 0, 0],
|
65 |
+
[1/2, 1/2, 0, 0],
|
66 |
+
[3/8, 3/4, -1/8, 0],
|
67 |
+
[5/16, 15/16, -5/16, 1/16]],
|
68 |
+
[[0, 0, 0, 0],
|
69 |
+
[-1, 1, 0, 0],
|
70 |
+
[-1, 1, 0, 0],
|
71 |
+
[-23/24, 7/8, 1/8, -1/24]]]
|
72 |
+
>>> res[0][-1] # FD weights for 0th derivative, using full x_list
|
73 |
+
[5/16, 15/16, -5/16, 1/16]
|
74 |
+
>>> res[1][-1] # FD weights for 1st derivative
|
75 |
+
[-23/24, 7/8, 1/8, -1/24]
|
76 |
+
>>> res[1][-2] # FD weights for 1st derivative, using x_list[:-1]
|
77 |
+
[-1, 1, 0, 0]
|
78 |
+
>>> res[1][-1][0] # FD weight for 1st deriv. for x_list[0]
|
79 |
+
-23/24
|
80 |
+
>>> res[1][-1][1] # FD weight for 1st deriv. for x_list[1], etc.
|
81 |
+
7/8
|
82 |
+
|
83 |
+
Each sublist contains the most accurate formula at the end.
|
84 |
+
Note, that in the above example ``res[1][1]`` is the same as ``res[1][2]``.
|
85 |
+
Since res[1][2] has an order of accuracy of
|
86 |
+
``len(x_list[:3]) - order = 3 - 1 = 2``, the same is true for ``res[1][1]``!
|
87 |
+
|
88 |
+
>>> res = finite_diff_weights(1, [S(0), S(1), -S(1), S(2), -S(2)], 0)[1]
|
89 |
+
>>> res
|
90 |
+
[[0, 0, 0, 0, 0],
|
91 |
+
[-1, 1, 0, 0, 0],
|
92 |
+
[0, 1/2, -1/2, 0, 0],
|
93 |
+
[-1/2, 1, -1/3, -1/6, 0],
|
94 |
+
[0, 2/3, -2/3, -1/12, 1/12]]
|
95 |
+
>>> res[0] # no approximation possible, using x_list[0] only
|
96 |
+
[0, 0, 0, 0, 0]
|
97 |
+
>>> res[1] # classic forward step approximation
|
98 |
+
[-1, 1, 0, 0, 0]
|
99 |
+
>>> res[2] # classic centered approximation
|
100 |
+
[0, 1/2, -1/2, 0, 0]
|
101 |
+
>>> res[3:] # higher order approximations
|
102 |
+
[[-1/2, 1, -1/3, -1/6, 0], [0, 2/3, -2/3, -1/12, 1/12]]
|
103 |
+
|
104 |
+
Let us compare this to a differently defined ``x_list``. Pay attention to
|
105 |
+
``foo[i][k]`` corresponding to the gridpoint defined by ``x_list[k]``.
|
106 |
+
|
107 |
+
>>> foo = finite_diff_weights(1, [-S(2), -S(1), S(0), S(1), S(2)], 0)[1]
|
108 |
+
>>> foo
|
109 |
+
[[0, 0, 0, 0, 0],
|
110 |
+
[-1, 1, 0, 0, 0],
|
111 |
+
[1/2, -2, 3/2, 0, 0],
|
112 |
+
[1/6, -1, 1/2, 1/3, 0],
|
113 |
+
[1/12, -2/3, 0, 2/3, -1/12]]
|
114 |
+
>>> foo[1] # not the same and of lower accuracy as res[1]!
|
115 |
+
[-1, 1, 0, 0, 0]
|
116 |
+
>>> foo[2] # classic double backward step approximation
|
117 |
+
[1/2, -2, 3/2, 0, 0]
|
118 |
+
>>> foo[4] # the same as res[4]
|
119 |
+
[1/12, -2/3, 0, 2/3, -1/12]
|
120 |
+
|
121 |
+
Note that, unless you plan on using approximations based on subsets of
|
122 |
+
``x_list``, the order of gridpoints does not matter.
|
123 |
+
|
124 |
+
The capability to generate weights at arbitrary points can be
|
125 |
+
used e.g. to minimize Runge's phenomenon by using Chebyshev nodes:
|
126 |
+
|
127 |
+
>>> from sympy import cos, symbols, pi, simplify
|
128 |
+
>>> N, (h, x) = 4, symbols('h x')
|
129 |
+
>>> x_list = [x+h*cos(i*pi/(N)) for i in range(N,-1,-1)] # chebyshev nodes
|
130 |
+
>>> print(x_list)
|
131 |
+
[-h + x, -sqrt(2)*h/2 + x, x, sqrt(2)*h/2 + x, h + x]
|
132 |
+
>>> mycoeffs = finite_diff_weights(1, x_list, 0)[1][4]
|
133 |
+
>>> [simplify(c) for c in mycoeffs] #doctest: +NORMALIZE_WHITESPACE
|
134 |
+
[(h**3/2 + h**2*x - 3*h*x**2 - 4*x**3)/h**4,
|
135 |
+
(-sqrt(2)*h**3 - 4*h**2*x + 3*sqrt(2)*h*x**2 + 8*x**3)/h**4,
|
136 |
+
(6*h**2*x - 8*x**3)/h**4,
|
137 |
+
(sqrt(2)*h**3 - 4*h**2*x - 3*sqrt(2)*h*x**2 + 8*x**3)/h**4,
|
138 |
+
(-h**3/2 + h**2*x + 3*h*x**2 - 4*x**3)/h**4]
|
139 |
+
|
140 |
+
Notes
|
141 |
+
=====
|
142 |
+
|
143 |
+
If weights for a finite difference approximation of 3rd order
|
144 |
+
derivative is wanted, weights for 0th, 1st and 2nd order are
|
145 |
+
calculated "for free", so are formulae using subsets of ``x_list``.
|
146 |
+
This is something one can take advantage of to save computational cost.
|
147 |
+
Be aware that one should define ``x_list`` from nearest to furthest from
|
148 |
+
``x0``. If not, subsets of ``x_list`` will yield poorer approximations,
|
149 |
+
which might not grand an order of accuracy of ``len(x_list) - order``.
|
150 |
+
|
151 |
+
See also
|
152 |
+
========
|
153 |
+
|
154 |
+
sympy.calculus.finite_diff.apply_finite_diff
|
155 |
+
|
156 |
+
References
|
157 |
+
==========
|
158 |
+
|
159 |
+
.. [1] Generation of Finite Difference Formulas on Arbitrarily Spaced
|
160 |
+
Grids, Bengt Fornberg; Mathematics of computation; 51; 184;
|
161 |
+
(1988); 699-706; doi:10.1090/S0025-5718-1988-0935077-0
|
162 |
+
|
163 |
+
"""
|
164 |
+
# The notation below closely corresponds to the one used in the paper.
|
165 |
+
order = S(order)
|
166 |
+
if not order.is_number:
|
167 |
+
raise ValueError("Cannot handle symbolic order.")
|
168 |
+
if order < 0:
|
169 |
+
raise ValueError("Negative derivative order illegal.")
|
170 |
+
if int(order) != order:
|
171 |
+
raise ValueError("Non-integer order illegal")
|
172 |
+
M = order
|
173 |
+
N = len(x_list) - 1
|
174 |
+
delta = [[[0 for nu in range(N+1)] for n in range(N+1)] for
|
175 |
+
m in range(M+1)]
|
176 |
+
delta[0][0][0] = S.One
|
177 |
+
c1 = S.One
|
178 |
+
for n in range(1, N+1):
|
179 |
+
c2 = S.One
|
180 |
+
for nu in range(n):
|
181 |
+
c3 = x_list[n] - x_list[nu]
|
182 |
+
c2 = c2 * c3
|
183 |
+
if n <= M:
|
184 |
+
delta[n][n-1][nu] = 0
|
185 |
+
for m in range(min(n, M)+1):
|
186 |
+
delta[m][n][nu] = (x_list[n]-x0)*delta[m][n-1][nu] -\
|
187 |
+
m*delta[m-1][n-1][nu]
|
188 |
+
delta[m][n][nu] /= c3
|
189 |
+
for m in range(min(n, M)+1):
|
190 |
+
delta[m][n][n] = c1/c2*(m*delta[m-1][n-1][n-1] -
|
191 |
+
(x_list[n-1]-x0)*delta[m][n-1][n-1])
|
192 |
+
c1 = c2
|
193 |
+
return delta
|
194 |
+
|
195 |
+
|
196 |
+
def apply_finite_diff(order, x_list, y_list, x0=S.Zero):
|
197 |
+
"""
|
198 |
+
Calculates the finite difference approximation of
|
199 |
+
the derivative of requested order at ``x0`` from points
|
200 |
+
provided in ``x_list`` and ``y_list``.
|
201 |
+
|
202 |
+
Parameters
|
203 |
+
==========
|
204 |
+
|
205 |
+
order: int
|
206 |
+
order of derivative to approximate. 0 corresponds to interpolation.
|
207 |
+
x_list: sequence
|
208 |
+
Sequence of (unique) values for the independent variable.
|
209 |
+
y_list: sequence
|
210 |
+
The function value at corresponding values for the independent
|
211 |
+
variable in x_list.
|
212 |
+
x0: Number or Symbol
|
213 |
+
At what value of the independent variable the derivative should be
|
214 |
+
evaluated. Defaults to 0.
|
215 |
+
|
216 |
+
Returns
|
217 |
+
=======
|
218 |
+
|
219 |
+
sympy.core.add.Add or sympy.core.numbers.Number
|
220 |
+
The finite difference expression approximating the requested
|
221 |
+
derivative order at ``x0``.
|
222 |
+
|
223 |
+
Examples
|
224 |
+
========
|
225 |
+
|
226 |
+
>>> from sympy import apply_finite_diff
|
227 |
+
>>> cube = lambda arg: (1.0*arg)**3
|
228 |
+
>>> xlist = range(-3,3+1)
|
229 |
+
>>> apply_finite_diff(2, xlist, map(cube, xlist), 2) - 12 # doctest: +SKIP
|
230 |
+
-3.55271367880050e-15
|
231 |
+
|
232 |
+
we see that the example above only contain rounding errors.
|
233 |
+
apply_finite_diff can also be used on more abstract objects:
|
234 |
+
|
235 |
+
>>> from sympy import IndexedBase, Idx
|
236 |
+
>>> x, y = map(IndexedBase, 'xy')
|
237 |
+
>>> i = Idx('i')
|
238 |
+
>>> x_list, y_list = zip(*[(x[i+j], y[i+j]) for j in range(-1,2)])
|
239 |
+
>>> apply_finite_diff(1, x_list, y_list, x[i])
|
240 |
+
((x[i + 1] - x[i])/(-x[i - 1] + x[i]) - 1)*y[i]/(x[i + 1] - x[i]) -
|
241 |
+
(x[i + 1] - x[i])*y[i - 1]/((x[i + 1] - x[i - 1])*(-x[i - 1] + x[i])) +
|
242 |
+
(-x[i - 1] + x[i])*y[i + 1]/((x[i + 1] - x[i - 1])*(x[i + 1] - x[i]))
|
243 |
+
|
244 |
+
Notes
|
245 |
+
=====
|
246 |
+
|
247 |
+
Order = 0 corresponds to interpolation.
|
248 |
+
Only supply so many points you think makes sense
|
249 |
+
to around x0 when extracting the derivative (the function
|
250 |
+
need to be well behaved within that region). Also beware
|
251 |
+
of Runge's phenomenon.
|
252 |
+
|
253 |
+
See also
|
254 |
+
========
|
255 |
+
|
256 |
+
sympy.calculus.finite_diff.finite_diff_weights
|
257 |
+
|
258 |
+
References
|
259 |
+
==========
|
260 |
+
|
261 |
+
Fortran 90 implementation with Python interface for numerics: finitediff_
|
262 |
+
|
263 |
+
.. _finitediff: https://github.com/bjodah/finitediff
|
264 |
+
|
265 |
+
"""
|
266 |
+
|
267 |
+
# In the original paper the following holds for the notation:
|
268 |
+
# M = order
|
269 |
+
# N = len(x_list) - 1
|
270 |
+
|
271 |
+
N = len(x_list) - 1
|
272 |
+
if len(x_list) != len(y_list):
|
273 |
+
raise ValueError("x_list and y_list not equal in length.")
|
274 |
+
|
275 |
+
delta = finite_diff_weights(order, x_list, x0)
|
276 |
+
|
277 |
+
derivative = 0
|
278 |
+
for nu in range(len(x_list)):
|
279 |
+
derivative += delta[order][N][nu]*y_list[nu]
|
280 |
+
return derivative
|
281 |
+
|
282 |
+
|
283 |
+
def _as_finite_diff(derivative, points=1, x0=None, wrt=None):
|
284 |
+
"""
|
285 |
+
Returns an approximation of a derivative of a function in
|
286 |
+
the form of a finite difference formula. The expression is a
|
287 |
+
weighted sum of the function at a number of discrete values of
|
288 |
+
(one of) the independent variable(s).
|
289 |
+
|
290 |
+
Parameters
|
291 |
+
==========
|
292 |
+
|
293 |
+
derivative: a Derivative instance
|
294 |
+
|
295 |
+
points: sequence or coefficient, optional
|
296 |
+
If sequence: discrete values (length >= order+1) of the
|
297 |
+
independent variable used for generating the finite
|
298 |
+
difference weights.
|
299 |
+
If it is a coefficient, it will be used as the step-size
|
300 |
+
for generating an equidistant sequence of length order+1
|
301 |
+
centered around ``x0``. default: 1 (step-size 1)
|
302 |
+
|
303 |
+
x0: number or Symbol, optional
|
304 |
+
the value of the independent variable (``wrt``) at which the
|
305 |
+
derivative is to be approximated. Default: same as ``wrt``.
|
306 |
+
|
307 |
+
wrt: Symbol, optional
|
308 |
+
"with respect to" the variable for which the (partial)
|
309 |
+
derivative is to be approximated for. If not provided it
|
310 |
+
is required that the Derivative is ordinary. Default: ``None``.
|
311 |
+
|
312 |
+
Examples
|
313 |
+
========
|
314 |
+
|
315 |
+
>>> from sympy import symbols, Function, exp, sqrt, Symbol
|
316 |
+
>>> from sympy.calculus.finite_diff import _as_finite_diff
|
317 |
+
>>> x, h = symbols('x h')
|
318 |
+
>>> f = Function('f')
|
319 |
+
>>> _as_finite_diff(f(x).diff(x))
|
320 |
+
-f(x - 1/2) + f(x + 1/2)
|
321 |
+
|
322 |
+
The default step size and number of points are 1 and ``order + 1``
|
323 |
+
respectively. We can change the step size by passing a symbol
|
324 |
+
as a parameter:
|
325 |
+
|
326 |
+
>>> _as_finite_diff(f(x).diff(x), h)
|
327 |
+
-f(-h/2 + x)/h + f(h/2 + x)/h
|
328 |
+
|
329 |
+
We can also specify the discretized values to be used in a sequence:
|
330 |
+
|
331 |
+
>>> _as_finite_diff(f(x).diff(x), [x, x+h, x+2*h])
|
332 |
+
-3*f(x)/(2*h) + 2*f(h + x)/h - f(2*h + x)/(2*h)
|
333 |
+
|
334 |
+
The algorithm is not restricted to use equidistant spacing, nor
|
335 |
+
do we need to make the approximation around ``x0``, but we can get
|
336 |
+
an expression estimating the derivative at an offset:
|
337 |
+
|
338 |
+
>>> e, sq2 = exp(1), sqrt(2)
|
339 |
+
>>> xl = [x-h, x+h, x+e*h]
|
340 |
+
>>> _as_finite_diff(f(x).diff(x, 1), xl, x+h*sq2)
|
341 |
+
2*h*((h + sqrt(2)*h)/(2*h) - (-sqrt(2)*h + h)/(2*h))*f(E*h + x)/((-h + E*h)*(h + E*h)) +
|
342 |
+
(-(-sqrt(2)*h + h)/(2*h) - (-sqrt(2)*h + E*h)/(2*h))*f(-h + x)/(h + E*h) +
|
343 |
+
(-(h + sqrt(2)*h)/(2*h) + (-sqrt(2)*h + E*h)/(2*h))*f(h + x)/(-h + E*h)
|
344 |
+
|
345 |
+
Partial derivatives are also supported:
|
346 |
+
|
347 |
+
>>> y = Symbol('y')
|
348 |
+
>>> d2fdxdy=f(x,y).diff(x,y)
|
349 |
+
>>> _as_finite_diff(d2fdxdy, wrt=x)
|
350 |
+
-Derivative(f(x - 1/2, y), y) + Derivative(f(x + 1/2, y), y)
|
351 |
+
|
352 |
+
See also
|
353 |
+
========
|
354 |
+
|
355 |
+
sympy.calculus.finite_diff.apply_finite_diff
|
356 |
+
sympy.calculus.finite_diff.finite_diff_weights
|
357 |
+
|
358 |
+
"""
|
359 |
+
if derivative.is_Derivative:
|
360 |
+
pass
|
361 |
+
elif derivative.is_Atom:
|
362 |
+
return derivative
|
363 |
+
else:
|
364 |
+
return derivative.fromiter(
|
365 |
+
[_as_finite_diff(ar, points, x0, wrt) for ar
|
366 |
+
in derivative.args], **derivative.assumptions0)
|
367 |
+
|
368 |
+
if wrt is None:
|
369 |
+
old = None
|
370 |
+
for v in derivative.variables:
|
371 |
+
if old is v:
|
372 |
+
continue
|
373 |
+
derivative = _as_finite_diff(derivative, points, x0, v)
|
374 |
+
old = v
|
375 |
+
return derivative
|
376 |
+
|
377 |
+
order = derivative.variables.count(wrt)
|
378 |
+
|
379 |
+
if x0 is None:
|
380 |
+
x0 = wrt
|
381 |
+
|
382 |
+
if not iterable(points):
|
383 |
+
if getattr(points, 'is_Function', False) and wrt in points.args:
|
384 |
+
points = points.subs(wrt, x0)
|
385 |
+
# points is simply the step-size, let's make it a
|
386 |
+
# equidistant sequence centered around x0
|
387 |
+
if order % 2 == 0:
|
388 |
+
# even order => odd number of points, grid point included
|
389 |
+
points = [x0 + points*i for i
|
390 |
+
in range(-order//2, order//2 + 1)]
|
391 |
+
else:
|
392 |
+
# odd order => even number of points, half-way wrt grid point
|
393 |
+
points = [x0 + points*S(i)/2 for i
|
394 |
+
in range(-order, order + 1, 2)]
|
395 |
+
others = [wrt, 0]
|
396 |
+
for v in set(derivative.variables):
|
397 |
+
if v == wrt:
|
398 |
+
continue
|
399 |
+
others += [v, derivative.variables.count(v)]
|
400 |
+
if len(points) < order+1:
|
401 |
+
raise ValueError("Too few points for order %d" % order)
|
402 |
+
return apply_finite_diff(order, points, [
|
403 |
+
Derivative(derivative.expr.subs({wrt: x}), *others) for
|
404 |
+
x in points], x0)
|
405 |
+
|
406 |
+
|
407 |
+
def differentiate_finite(expr, *symbols,
|
408 |
+
points=1, x0=None, wrt=None, evaluate=False):
|
409 |
+
r""" Differentiate expr and replace Derivatives with finite differences.
|
410 |
+
|
411 |
+
Parameters
|
412 |
+
==========
|
413 |
+
|
414 |
+
expr : expression
|
415 |
+
\*symbols : differentiate with respect to symbols
|
416 |
+
points: sequence, coefficient or undefined function, optional
|
417 |
+
see ``Derivative.as_finite_difference``
|
418 |
+
x0: number or Symbol, optional
|
419 |
+
see ``Derivative.as_finite_difference``
|
420 |
+
wrt: Symbol, optional
|
421 |
+
see ``Derivative.as_finite_difference``
|
422 |
+
|
423 |
+
Examples
|
424 |
+
========
|
425 |
+
|
426 |
+
>>> from sympy import sin, Function, differentiate_finite
|
427 |
+
>>> from sympy.abc import x, y, h
|
428 |
+
>>> f, g = Function('f'), Function('g')
|
429 |
+
>>> differentiate_finite(f(x)*g(x), x, points=[x-h, x+h])
|
430 |
+
-f(-h + x)*g(-h + x)/(2*h) + f(h + x)*g(h + x)/(2*h)
|
431 |
+
|
432 |
+
``differentiate_finite`` works on any expression, including the expressions
|
433 |
+
with embedded derivatives:
|
434 |
+
|
435 |
+
>>> differentiate_finite(f(x) + sin(x), x, 2)
|
436 |
+
-2*f(x) + f(x - 1) + f(x + 1) - 2*sin(x) + sin(x - 1) + sin(x + 1)
|
437 |
+
>>> differentiate_finite(f(x, y), x, y)
|
438 |
+
f(x - 1/2, y - 1/2) - f(x - 1/2, y + 1/2) - f(x + 1/2, y - 1/2) + f(x + 1/2, y + 1/2)
|
439 |
+
>>> differentiate_finite(f(x)*g(x).diff(x), x)
|
440 |
+
(-g(x) + g(x + 1))*f(x + 1/2) - (g(x) - g(x - 1))*f(x - 1/2)
|
441 |
+
|
442 |
+
To make finite difference with non-constant discretization step use
|
443 |
+
undefined functions:
|
444 |
+
|
445 |
+
>>> dx = Function('dx')
|
446 |
+
>>> differentiate_finite(f(x)*g(x).diff(x), points=dx(x))
|
447 |
+
-(-g(x - dx(x)/2 - dx(x - dx(x)/2)/2)/dx(x - dx(x)/2) +
|
448 |
+
g(x - dx(x)/2 + dx(x - dx(x)/2)/2)/dx(x - dx(x)/2))*f(x - dx(x)/2)/dx(x) +
|
449 |
+
(-g(x + dx(x)/2 - dx(x + dx(x)/2)/2)/dx(x + dx(x)/2) +
|
450 |
+
g(x + dx(x)/2 + dx(x + dx(x)/2)/2)/dx(x + dx(x)/2))*f(x + dx(x)/2)/dx(x)
|
451 |
+
|
452 |
+
"""
|
453 |
+
if any(term.is_Derivative for term in list(preorder_traversal(expr))):
|
454 |
+
evaluate = False
|
455 |
+
|
456 |
+
Dexpr = expr.diff(*symbols, evaluate=evaluate)
|
457 |
+
if evaluate:
|
458 |
+
sympy_deprecation_warning("""
|
459 |
+
The evaluate flag to differentiate_finite() is deprecated.
|
460 |
+
|
461 |
+
evaluate=True expands the intermediate derivatives before computing
|
462 |
+
differences, but this usually not what you want, as it does not
|
463 |
+
satisfy the product rule.
|
464 |
+
""",
|
465 |
+
deprecated_since_version="1.5",
|
466 |
+
active_deprecations_target="deprecated-differentiate_finite-evaluate",
|
467 |
+
)
|
468 |
+
return Dexpr.replace(
|
469 |
+
lambda arg: arg.is_Derivative,
|
470 |
+
lambda arg: arg.as_finite_difference(points=points, x0=x0, wrt=wrt))
|
471 |
+
else:
|
472 |
+
DFexpr = Dexpr.as_finite_difference(points=points, x0=x0, wrt=wrt)
|
473 |
+
return DFexpr.replace(
|
474 |
+
lambda arg: isinstance(arg, Subs),
|
475 |
+
lambda arg: arg.expr.as_finite_difference(
|
476 |
+
points=points, x0=arg.point[0], wrt=arg.variables[0]))
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/domainmatrix.cpython-310.pyc
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e0d03bbd76ce913835da6670e2b35a4a9f7ecdc79cd72905b447b429feb2c9da
|
3 |
+
size 110702
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (1.66 kB). View file
|
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/__pycache__/aesaracode.cpython-310.pyc
ADDED
Binary file (19 kB). View file
|
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/__pycache__/cxx.cpython-310.pyc
ADDED
Binary file (5.93 kB). View file
|
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/__pycache__/dot.cpython-310.pyc
ADDED
Binary file (8.83 kB). View file
|
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/__pycache__/fortran.cpython-310.pyc
ADDED
Binary file (27.3 kB). View file
|
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/__pycache__/octave.cpython-310.pyc
ADDED
Binary file (26.5 kB). View file
|
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/__pycache__/python.cpython-310.pyc
ADDED
Binary file (2.63 kB). View file
|
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/pretty/__pycache__/pretty.cpython-310.pyc
ADDED
Binary file (85.4 kB). View file
|
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/pretty/__pycache__/pretty_symbology.cpython-310.pyc
ADDED
Binary file (20.5 kB). View file
|
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/pretty/__pycache__/stringpict.cpython-310.pyc
ADDED
Binary file (15.6 kB). View file
|
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/pretty/pretty.py
ADDED
The diff for this file is too large to render.
See raw diff
|
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/pretty/stringpict.py
ADDED
@@ -0,0 +1,537 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Prettyprinter by Jurjen Bos.
|
2 |
+
(I hate spammers: mail me at pietjepuk314 at the reverse of ku.oc.oohay).
|
3 |
+
All objects have a method that create a "stringPict",
|
4 |
+
that can be used in the str method for pretty printing.
|
5 |
+
|
6 |
+
Updates by Jason Gedge (email <my last name> at cs mun ca)
|
7 |
+
- terminal_string() method
|
8 |
+
- minor fixes and changes (mostly to prettyForm)
|
9 |
+
|
10 |
+
TODO:
|
11 |
+
- Allow left/center/right alignment options for above/below and
|
12 |
+
top/center/bottom alignment options for left/right
|
13 |
+
"""
|
14 |
+
|
15 |
+
import shutil
|
16 |
+
|
17 |
+
from .pretty_symbology import hobj, vobj, xsym, xobj, pretty_use_unicode, line_width, center
|
18 |
+
from sympy.utilities.exceptions import sympy_deprecation_warning
|
19 |
+
|
20 |
+
_GLOBAL_WRAP_LINE = None
|
21 |
+
|
22 |
+
class stringPict:
|
23 |
+
"""An ASCII picture.
|
24 |
+
The pictures are represented as a list of equal length strings.
|
25 |
+
"""
|
26 |
+
#special value for stringPict.below
|
27 |
+
LINE = 'line'
|
28 |
+
|
29 |
+
def __init__(self, s, baseline=0):
|
30 |
+
"""Initialize from string.
|
31 |
+
Multiline strings are centered.
|
32 |
+
"""
|
33 |
+
self.s = s
|
34 |
+
#picture is a string that just can be printed
|
35 |
+
self.picture = stringPict.equalLengths(s.splitlines())
|
36 |
+
#baseline is the line number of the "base line"
|
37 |
+
self.baseline = baseline
|
38 |
+
self.binding = None
|
39 |
+
|
40 |
+
@staticmethod
|
41 |
+
def equalLengths(lines):
|
42 |
+
# empty lines
|
43 |
+
if not lines:
|
44 |
+
return ['']
|
45 |
+
|
46 |
+
width = max(line_width(line) for line in lines)
|
47 |
+
return [center(line, width) for line in lines]
|
48 |
+
|
49 |
+
def height(self):
|
50 |
+
"""The height of the picture in characters."""
|
51 |
+
return len(self.picture)
|
52 |
+
|
53 |
+
def width(self):
|
54 |
+
"""The width of the picture in characters."""
|
55 |
+
return line_width(self.picture[0])
|
56 |
+
|
57 |
+
@staticmethod
|
58 |
+
def next(*args):
|
59 |
+
"""Put a string of stringPicts next to each other.
|
60 |
+
Returns string, baseline arguments for stringPict.
|
61 |
+
"""
|
62 |
+
#convert everything to stringPicts
|
63 |
+
objects = []
|
64 |
+
for arg in args:
|
65 |
+
if isinstance(arg, str):
|
66 |
+
arg = stringPict(arg)
|
67 |
+
objects.append(arg)
|
68 |
+
|
69 |
+
#make a list of pictures, with equal height and baseline
|
70 |
+
newBaseline = max(obj.baseline for obj in objects)
|
71 |
+
newHeightBelowBaseline = max(
|
72 |
+
obj.height() - obj.baseline
|
73 |
+
for obj in objects)
|
74 |
+
newHeight = newBaseline + newHeightBelowBaseline
|
75 |
+
|
76 |
+
pictures = []
|
77 |
+
for obj in objects:
|
78 |
+
oneEmptyLine = [' '*obj.width()]
|
79 |
+
basePadding = newBaseline - obj.baseline
|
80 |
+
totalPadding = newHeight - obj.height()
|
81 |
+
pictures.append(
|
82 |
+
oneEmptyLine * basePadding +
|
83 |
+
obj.picture +
|
84 |
+
oneEmptyLine * (totalPadding - basePadding))
|
85 |
+
|
86 |
+
result = [''.join(lines) for lines in zip(*pictures)]
|
87 |
+
return '\n'.join(result), newBaseline
|
88 |
+
|
89 |
+
def right(self, *args):
|
90 |
+
r"""Put pictures next to this one.
|
91 |
+
Returns string, baseline arguments for stringPict.
|
92 |
+
(Multiline) strings are allowed, and are given a baseline of 0.
|
93 |
+
|
94 |
+
Examples
|
95 |
+
========
|
96 |
+
|
97 |
+
>>> from sympy.printing.pretty.stringpict import stringPict
|
98 |
+
>>> print(stringPict("10").right(" + ",stringPict("1\r-\r2",1))[0])
|
99 |
+
1
|
100 |
+
10 + -
|
101 |
+
2
|
102 |
+
|
103 |
+
"""
|
104 |
+
return stringPict.next(self, *args)
|
105 |
+
|
106 |
+
def left(self, *args):
|
107 |
+
"""Put pictures (left to right) at left.
|
108 |
+
Returns string, baseline arguments for stringPict.
|
109 |
+
"""
|
110 |
+
return stringPict.next(*(args + (self,)))
|
111 |
+
|
112 |
+
@staticmethod
|
113 |
+
def stack(*args):
|
114 |
+
"""Put pictures on top of each other,
|
115 |
+
from top to bottom.
|
116 |
+
Returns string, baseline arguments for stringPict.
|
117 |
+
The baseline is the baseline of the second picture.
|
118 |
+
Everything is centered.
|
119 |
+
Baseline is the baseline of the second picture.
|
120 |
+
Strings are allowed.
|
121 |
+
The special value stringPict.LINE is a row of '-' extended to the width.
|
122 |
+
"""
|
123 |
+
#convert everything to stringPicts; keep LINE
|
124 |
+
objects = []
|
125 |
+
for arg in args:
|
126 |
+
if arg is not stringPict.LINE and isinstance(arg, str):
|
127 |
+
arg = stringPict(arg)
|
128 |
+
objects.append(arg)
|
129 |
+
|
130 |
+
#compute new width
|
131 |
+
newWidth = max(
|
132 |
+
obj.width()
|
133 |
+
for obj in objects
|
134 |
+
if obj is not stringPict.LINE)
|
135 |
+
|
136 |
+
lineObj = stringPict(hobj('-', newWidth))
|
137 |
+
|
138 |
+
#replace LINE with proper lines
|
139 |
+
for i, obj in enumerate(objects):
|
140 |
+
if obj is stringPict.LINE:
|
141 |
+
objects[i] = lineObj
|
142 |
+
|
143 |
+
#stack the pictures, and center the result
|
144 |
+
newPicture = [center(line, newWidth) for obj in objects for line in obj.picture]
|
145 |
+
newBaseline = objects[0].height() + objects[1].baseline
|
146 |
+
return '\n'.join(newPicture), newBaseline
|
147 |
+
|
148 |
+
def below(self, *args):
|
149 |
+
"""Put pictures under this picture.
|
150 |
+
Returns string, baseline arguments for stringPict.
|
151 |
+
Baseline is baseline of top picture
|
152 |
+
|
153 |
+
Examples
|
154 |
+
========
|
155 |
+
|
156 |
+
>>> from sympy.printing.pretty.stringpict import stringPict
|
157 |
+
>>> print(stringPict("x+3").below(
|
158 |
+
... stringPict.LINE, '3')[0]) #doctest: +NORMALIZE_WHITESPACE
|
159 |
+
x+3
|
160 |
+
---
|
161 |
+
3
|
162 |
+
|
163 |
+
"""
|
164 |
+
s, baseline = stringPict.stack(self, *args)
|
165 |
+
return s, self.baseline
|
166 |
+
|
167 |
+
def above(self, *args):
|
168 |
+
"""Put pictures above this picture.
|
169 |
+
Returns string, baseline arguments for stringPict.
|
170 |
+
Baseline is baseline of bottom picture.
|
171 |
+
"""
|
172 |
+
string, baseline = stringPict.stack(*(args + (self,)))
|
173 |
+
baseline = len(string.splitlines()) - self.height() + self.baseline
|
174 |
+
return string, baseline
|
175 |
+
|
176 |
+
def parens(self, left='(', right=')', ifascii_nougly=False):
|
177 |
+
"""Put parentheses around self.
|
178 |
+
Returns string, baseline arguments for stringPict.
|
179 |
+
|
180 |
+
left or right can be None or empty string which means 'no paren from
|
181 |
+
that side'
|
182 |
+
"""
|
183 |
+
h = self.height()
|
184 |
+
b = self.baseline
|
185 |
+
|
186 |
+
# XXX this is a hack -- ascii parens are ugly!
|
187 |
+
if ifascii_nougly and not pretty_use_unicode():
|
188 |
+
h = 1
|
189 |
+
b = 0
|
190 |
+
|
191 |
+
res = self
|
192 |
+
|
193 |
+
if left:
|
194 |
+
lparen = stringPict(vobj(left, h), baseline=b)
|
195 |
+
res = stringPict(*lparen.right(self))
|
196 |
+
if right:
|
197 |
+
rparen = stringPict(vobj(right, h), baseline=b)
|
198 |
+
res = stringPict(*res.right(rparen))
|
199 |
+
|
200 |
+
return ('\n'.join(res.picture), res.baseline)
|
201 |
+
|
202 |
+
def leftslash(self):
|
203 |
+
"""Precede object by a slash of the proper size.
|
204 |
+
"""
|
205 |
+
# XXX not used anywhere ?
|
206 |
+
height = max(
|
207 |
+
self.baseline,
|
208 |
+
self.height() - 1 - self.baseline)*2 + 1
|
209 |
+
slash = '\n'.join(
|
210 |
+
' '*(height - i - 1) + xobj('/', 1) + ' '*i
|
211 |
+
for i in range(height)
|
212 |
+
)
|
213 |
+
return self.left(stringPict(slash, height//2))
|
214 |
+
|
215 |
+
def root(self, n=None):
|
216 |
+
"""Produce a nice root symbol.
|
217 |
+
Produces ugly results for big n inserts.
|
218 |
+
"""
|
219 |
+
# XXX not used anywhere
|
220 |
+
# XXX duplicate of root drawing in pretty.py
|
221 |
+
#put line over expression
|
222 |
+
result = self.above('_'*self.width())
|
223 |
+
#construct right half of root symbol
|
224 |
+
height = self.height()
|
225 |
+
slash = '\n'.join(
|
226 |
+
' ' * (height - i - 1) + '/' + ' ' * i
|
227 |
+
for i in range(height)
|
228 |
+
)
|
229 |
+
slash = stringPict(slash, height - 1)
|
230 |
+
#left half of root symbol
|
231 |
+
if height > 2:
|
232 |
+
downline = stringPict('\\ \n \\', 1)
|
233 |
+
else:
|
234 |
+
downline = stringPict('\\')
|
235 |
+
#put n on top, as low as possible
|
236 |
+
if n is not None and n.width() > downline.width():
|
237 |
+
downline = downline.left(' '*(n.width() - downline.width()))
|
238 |
+
downline = downline.above(n)
|
239 |
+
#build root symbol
|
240 |
+
root = downline.right(slash)
|
241 |
+
#glue it on at the proper height
|
242 |
+
#normally, the root symbel is as high as self
|
243 |
+
#which is one less than result
|
244 |
+
#this moves the root symbol one down
|
245 |
+
#if the root became higher, the baseline has to grow too
|
246 |
+
root.baseline = result.baseline - result.height() + root.height()
|
247 |
+
return result.left(root)
|
248 |
+
|
249 |
+
def render(self, * args, **kwargs):
|
250 |
+
"""Return the string form of self.
|
251 |
+
|
252 |
+
Unless the argument line_break is set to False, it will
|
253 |
+
break the expression in a form that can be printed
|
254 |
+
on the terminal without being broken up.
|
255 |
+
"""
|
256 |
+
if _GLOBAL_WRAP_LINE is not None:
|
257 |
+
kwargs["wrap_line"] = _GLOBAL_WRAP_LINE
|
258 |
+
|
259 |
+
if kwargs["wrap_line"] is False:
|
260 |
+
return "\n".join(self.picture)
|
261 |
+
|
262 |
+
if kwargs["num_columns"] is not None:
|
263 |
+
# Read the argument num_columns if it is not None
|
264 |
+
ncols = kwargs["num_columns"]
|
265 |
+
else:
|
266 |
+
# Attempt to get a terminal width
|
267 |
+
ncols = self.terminal_width()
|
268 |
+
|
269 |
+
if ncols <= 0:
|
270 |
+
ncols = 80
|
271 |
+
|
272 |
+
# If smaller than the terminal width, no need to correct
|
273 |
+
if self.width() <= ncols:
|
274 |
+
return type(self.picture[0])(self)
|
275 |
+
|
276 |
+
"""
|
277 |
+
Break long-lines in a visually pleasing format.
|
278 |
+
without overflow indicators | with overflow indicators
|
279 |
+
| 2 2 3 | | 2 2 3 ↪|
|
280 |
+
|6*x *y + 4*x*y + | |6*x *y + 4*x*y + ↪|
|
281 |
+
| | | |
|
282 |
+
| 3 4 4 | |↪ 3 4 4 |
|
283 |
+
|4*y*x + x + y | |↪ 4*y*x + x + y |
|
284 |
+
|a*c*e + a*c*f + a*d | |a*c*e + a*c*f + a*d ↪|
|
285 |
+
|*e + a*d*f + b*c*e | | |
|
286 |
+
|+ b*c*f + b*d*e + b | |↪ *e + a*d*f + b*c* ↪|
|
287 |
+
|*d*f | | |
|
288 |
+
| | |↪ e + b*c*f + b*d*e ↪|
|
289 |
+
| | | |
|
290 |
+
| | |↪ + b*d*f |
|
291 |
+
"""
|
292 |
+
|
293 |
+
overflow_first = ""
|
294 |
+
if kwargs["use_unicode"] or pretty_use_unicode():
|
295 |
+
overflow_start = "\N{RIGHTWARDS ARROW WITH HOOK} "
|
296 |
+
overflow_end = " \N{RIGHTWARDS ARROW WITH HOOK}"
|
297 |
+
else:
|
298 |
+
overflow_start = "> "
|
299 |
+
overflow_end = " >"
|
300 |
+
|
301 |
+
def chunks(line):
|
302 |
+
"""Yields consecutive chunks of line_width ncols"""
|
303 |
+
prefix = overflow_first
|
304 |
+
width, start = line_width(prefix + overflow_end), 0
|
305 |
+
for i, x in enumerate(line):
|
306 |
+
wx = line_width(x)
|
307 |
+
# Only flush the screen when the current character overflows.
|
308 |
+
# This way, combining marks can be appended even when width == ncols.
|
309 |
+
if width + wx > ncols:
|
310 |
+
yield prefix + line[start:i] + overflow_end
|
311 |
+
prefix = overflow_start
|
312 |
+
width, start = line_width(prefix + overflow_end), i
|
313 |
+
width += wx
|
314 |
+
yield prefix + line[start:]
|
315 |
+
|
316 |
+
# Concurrently assemble chunks of all lines into individual screens
|
317 |
+
pictures = zip(*map(chunks, self.picture))
|
318 |
+
|
319 |
+
# Join lines of each screen into sub-pictures
|
320 |
+
pictures = ["\n".join(picture) for picture in pictures]
|
321 |
+
|
322 |
+
# Add spacers between sub-pictures
|
323 |
+
return "\n\n".join(pictures)
|
324 |
+
|
325 |
+
def terminal_width(self):
|
326 |
+
"""Return the terminal width if possible, otherwise return 0.
|
327 |
+
"""
|
328 |
+
size = shutil.get_terminal_size(fallback=(0, 0))
|
329 |
+
return size.columns
|
330 |
+
|
331 |
+
def __eq__(self, o):
|
332 |
+
if isinstance(o, str):
|
333 |
+
return '\n'.join(self.picture) == o
|
334 |
+
elif isinstance(o, stringPict):
|
335 |
+
return o.picture == self.picture
|
336 |
+
return False
|
337 |
+
|
338 |
+
def __hash__(self):
|
339 |
+
return super().__hash__()
|
340 |
+
|
341 |
+
def __str__(self):
|
342 |
+
return '\n'.join(self.picture)
|
343 |
+
|
344 |
+
def __repr__(self):
|
345 |
+
return "stringPict(%r,%d)" % ('\n'.join(self.picture), self.baseline)
|
346 |
+
|
347 |
+
def __getitem__(self, index):
|
348 |
+
return self.picture[index]
|
349 |
+
|
350 |
+
def __len__(self):
|
351 |
+
return len(self.s)
|
352 |
+
|
353 |
+
|
354 |
+
class prettyForm(stringPict):
|
355 |
+
"""
|
356 |
+
Extension of the stringPict class that knows about basic math applications,
|
357 |
+
optimizing double minus signs.
|
358 |
+
|
359 |
+
"Binding" is interpreted as follows::
|
360 |
+
|
361 |
+
ATOM this is an atom: never needs to be parenthesized
|
362 |
+
FUNC this is a function application: parenthesize if added (?)
|
363 |
+
DIV this is a division: make wider division if divided
|
364 |
+
POW this is a power: only parenthesize if exponent
|
365 |
+
MUL this is a multiplication: parenthesize if powered
|
366 |
+
ADD this is an addition: parenthesize if multiplied or powered
|
367 |
+
NEG this is a negative number: optimize if added, parenthesize if
|
368 |
+
multiplied or powered
|
369 |
+
OPEN this is an open object: parenthesize if added, multiplied, or
|
370 |
+
powered (example: Piecewise)
|
371 |
+
"""
|
372 |
+
ATOM, FUNC, DIV, POW, MUL, ADD, NEG, OPEN = range(8)
|
373 |
+
|
374 |
+
def __init__(self, s, baseline=0, binding=0, unicode=None):
|
375 |
+
"""Initialize from stringPict and binding power."""
|
376 |
+
stringPict.__init__(self, s, baseline)
|
377 |
+
self.binding = binding
|
378 |
+
if unicode is not None:
|
379 |
+
sympy_deprecation_warning(
|
380 |
+
"""
|
381 |
+
The unicode argument to prettyForm is deprecated. Only the s
|
382 |
+
argument (the first positional argument) should be passed.
|
383 |
+
""",
|
384 |
+
deprecated_since_version="1.7",
|
385 |
+
active_deprecations_target="deprecated-pretty-printing-functions")
|
386 |
+
self._unicode = unicode or s
|
387 |
+
|
388 |
+
@property
|
389 |
+
def unicode(self):
|
390 |
+
sympy_deprecation_warning(
|
391 |
+
"""
|
392 |
+
The prettyForm.unicode attribute is deprecated. Use the
|
393 |
+
prettyForm.s attribute instead.
|
394 |
+
""",
|
395 |
+
deprecated_since_version="1.7",
|
396 |
+
active_deprecations_target="deprecated-pretty-printing-functions")
|
397 |
+
return self._unicode
|
398 |
+
|
399 |
+
# Note: code to handle subtraction is in _print_Add
|
400 |
+
|
401 |
+
def __add__(self, *others):
|
402 |
+
"""Make a pretty addition.
|
403 |
+
Addition of negative numbers is simplified.
|
404 |
+
"""
|
405 |
+
arg = self
|
406 |
+
if arg.binding > prettyForm.NEG:
|
407 |
+
arg = stringPict(*arg.parens())
|
408 |
+
result = [arg]
|
409 |
+
for arg in others:
|
410 |
+
#add parentheses for weak binders
|
411 |
+
if arg.binding > prettyForm.NEG:
|
412 |
+
arg = stringPict(*arg.parens())
|
413 |
+
#use existing minus sign if available
|
414 |
+
if arg.binding != prettyForm.NEG:
|
415 |
+
result.append(' + ')
|
416 |
+
result.append(arg)
|
417 |
+
return prettyForm(binding=prettyForm.ADD, *stringPict.next(*result))
|
418 |
+
|
419 |
+
def __truediv__(self, den, slashed=False):
|
420 |
+
"""Make a pretty division; stacked or slashed.
|
421 |
+
"""
|
422 |
+
if slashed:
|
423 |
+
raise NotImplementedError("Can't do slashed fraction yet")
|
424 |
+
num = self
|
425 |
+
if num.binding == prettyForm.DIV:
|
426 |
+
num = stringPict(*num.parens())
|
427 |
+
if den.binding == prettyForm.DIV:
|
428 |
+
den = stringPict(*den.parens())
|
429 |
+
|
430 |
+
if num.binding==prettyForm.NEG:
|
431 |
+
num = num.right(" ")[0]
|
432 |
+
|
433 |
+
return prettyForm(binding=prettyForm.DIV, *stringPict.stack(
|
434 |
+
num,
|
435 |
+
stringPict.LINE,
|
436 |
+
den))
|
437 |
+
|
438 |
+
def __mul__(self, *others):
|
439 |
+
"""Make a pretty multiplication.
|
440 |
+
Parentheses are needed around +, - and neg.
|
441 |
+
"""
|
442 |
+
quantity = {
|
443 |
+
'degree': "\N{DEGREE SIGN}"
|
444 |
+
}
|
445 |
+
|
446 |
+
if len(others) == 0:
|
447 |
+
return self # We aren't actually multiplying... So nothing to do here.
|
448 |
+
|
449 |
+
# add parens on args that need them
|
450 |
+
arg = self
|
451 |
+
if arg.binding > prettyForm.MUL and arg.binding != prettyForm.NEG:
|
452 |
+
arg = stringPict(*arg.parens())
|
453 |
+
result = [arg]
|
454 |
+
for arg in others:
|
455 |
+
if arg.picture[0] not in quantity.values():
|
456 |
+
result.append(xsym('*'))
|
457 |
+
#add parentheses for weak binders
|
458 |
+
if arg.binding > prettyForm.MUL and arg.binding != prettyForm.NEG:
|
459 |
+
arg = stringPict(*arg.parens())
|
460 |
+
result.append(arg)
|
461 |
+
|
462 |
+
len_res = len(result)
|
463 |
+
for i in range(len_res):
|
464 |
+
if i < len_res - 1 and result[i] == '-1' and result[i + 1] == xsym('*'):
|
465 |
+
# substitute -1 by -, like in -1*x -> -x
|
466 |
+
result.pop(i)
|
467 |
+
result.pop(i)
|
468 |
+
result.insert(i, '-')
|
469 |
+
if result[0][0] == '-':
|
470 |
+
# if there is a - sign in front of all
|
471 |
+
# This test was failing to catch a prettyForm.__mul__(prettyForm("-1", 0, 6)) being negative
|
472 |
+
bin = prettyForm.NEG
|
473 |
+
if result[0] == '-':
|
474 |
+
right = result[1]
|
475 |
+
if right.picture[right.baseline][0] == '-':
|
476 |
+
result[0] = '- '
|
477 |
+
else:
|
478 |
+
bin = prettyForm.MUL
|
479 |
+
return prettyForm(binding=bin, *stringPict.next(*result))
|
480 |
+
|
481 |
+
def __repr__(self):
|
482 |
+
return "prettyForm(%r,%d,%d)" % (
|
483 |
+
'\n'.join(self.picture),
|
484 |
+
self.baseline,
|
485 |
+
self.binding)
|
486 |
+
|
487 |
+
def __pow__(self, b):
|
488 |
+
"""Make a pretty power.
|
489 |
+
"""
|
490 |
+
a = self
|
491 |
+
use_inline_func_form = False
|
492 |
+
if b.binding == prettyForm.POW:
|
493 |
+
b = stringPict(*b.parens())
|
494 |
+
if a.binding > prettyForm.FUNC:
|
495 |
+
a = stringPict(*a.parens())
|
496 |
+
elif a.binding == prettyForm.FUNC:
|
497 |
+
# heuristic for when to use inline power
|
498 |
+
if b.height() > 1:
|
499 |
+
a = stringPict(*a.parens())
|
500 |
+
else:
|
501 |
+
use_inline_func_form = True
|
502 |
+
|
503 |
+
if use_inline_func_form:
|
504 |
+
# 2
|
505 |
+
# sin + + (x)
|
506 |
+
b.baseline = a.prettyFunc.baseline + b.height()
|
507 |
+
func = stringPict(*a.prettyFunc.right(b))
|
508 |
+
return prettyForm(*func.right(a.prettyArgs))
|
509 |
+
else:
|
510 |
+
# 2 <-- top
|
511 |
+
# (x+y) <-- bot
|
512 |
+
top = stringPict(*b.left(' '*a.width()))
|
513 |
+
bot = stringPict(*a.right(' '*b.width()))
|
514 |
+
|
515 |
+
return prettyForm(binding=prettyForm.POW, *bot.above(top))
|
516 |
+
|
517 |
+
simpleFunctions = ["sin", "cos", "tan"]
|
518 |
+
|
519 |
+
@staticmethod
|
520 |
+
def apply(function, *args):
|
521 |
+
"""Functions of one or more variables.
|
522 |
+
"""
|
523 |
+
if function in prettyForm.simpleFunctions:
|
524 |
+
#simple function: use only space if possible
|
525 |
+
assert len(
|
526 |
+
args) == 1, "Simple function %s must have 1 argument" % function
|
527 |
+
arg = args[0].__pretty__()
|
528 |
+
if arg.binding <= prettyForm.DIV:
|
529 |
+
#optimization: no parentheses necessary
|
530 |
+
return prettyForm(binding=prettyForm.FUNC, *arg.left(function + ' '))
|
531 |
+
argumentList = []
|
532 |
+
for arg in args:
|
533 |
+
argumentList.append(',')
|
534 |
+
argumentList.append(arg.__pretty__())
|
535 |
+
argumentList = stringPict(*stringPict.next(*argumentList[1:]))
|
536 |
+
argumentList = stringPict(*argumentList.parens())
|
537 |
+
return prettyForm(binding=prettyForm.ATOM, *argumentList.left(function))
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/pretty/tests/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (189 Bytes). View file
|
|
evalkit_cambrian/lib/python3.10/site-packages/sympy/printing/pretty/tests/test_pretty.py
ADDED
The diff for this file is too large to render.
See raw diff
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/__init__.py
ADDED
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Contains the core of NumPy: ndarray, ufuncs, dtypes, etc.
|
3 |
+
|
4 |
+
Please note that this module is private. All functions and objects
|
5 |
+
are available in the main ``numpy`` namespace - use that instead.
|
6 |
+
|
7 |
+
"""
|
8 |
+
|
9 |
+
import os
|
10 |
+
import warnings
|
11 |
+
|
12 |
+
from numpy.version import version as __version__
|
13 |
+
|
14 |
+
|
15 |
+
# disables OpenBLAS affinity setting of the main thread that limits
|
16 |
+
# python threads or processes to one core
|
17 |
+
env_added = []
|
18 |
+
for envkey in ['OPENBLAS_MAIN_FREE', 'GOTOBLAS_MAIN_FREE']:
|
19 |
+
if envkey not in os.environ:
|
20 |
+
os.environ[envkey] = '1'
|
21 |
+
env_added.append(envkey)
|
22 |
+
|
23 |
+
try:
|
24 |
+
from . import multiarray
|
25 |
+
except ImportError as exc:
|
26 |
+
import sys
|
27 |
+
msg = """
|
28 |
+
|
29 |
+
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
|
30 |
+
|
31 |
+
Importing the numpy C-extensions failed. This error can happen for
|
32 |
+
many reasons, often due to issues with your setup or how NumPy was
|
33 |
+
installed.
|
34 |
+
|
35 |
+
We have compiled some common reasons and troubleshooting tips at:
|
36 |
+
|
37 |
+
https://numpy.org/devdocs/user/troubleshooting-importerror.html
|
38 |
+
|
39 |
+
Please note and check the following:
|
40 |
+
|
41 |
+
* The Python version is: Python%d.%d from "%s"
|
42 |
+
* The NumPy version is: "%s"
|
43 |
+
|
44 |
+
and make sure that they are the versions you expect.
|
45 |
+
Please carefully study the documentation linked above for further help.
|
46 |
+
|
47 |
+
Original error was: %s
|
48 |
+
""" % (sys.version_info[0], sys.version_info[1], sys.executable,
|
49 |
+
__version__, exc)
|
50 |
+
raise ImportError(msg)
|
51 |
+
finally:
|
52 |
+
for envkey in env_added:
|
53 |
+
del os.environ[envkey]
|
54 |
+
del envkey
|
55 |
+
del env_added
|
56 |
+
del os
|
57 |
+
|
58 |
+
from . import umath
|
59 |
+
|
60 |
+
# Check that multiarray,umath are pure python modules wrapping
|
61 |
+
# _multiarray_umath and not either of the old c-extension modules
|
62 |
+
if not (hasattr(multiarray, '_multiarray_umath') and
|
63 |
+
hasattr(umath, '_multiarray_umath')):
|
64 |
+
import sys
|
65 |
+
path = sys.modules['numpy'].__path__
|
66 |
+
msg = ("Something is wrong with the numpy installation. "
|
67 |
+
"While importing we detected an older version of "
|
68 |
+
"numpy in {}. One method of fixing this is to repeatedly uninstall "
|
69 |
+
"numpy until none is found, then reinstall this version.")
|
70 |
+
raise ImportError(msg.format(path))
|
71 |
+
|
72 |
+
from . import numerictypes as nt
|
73 |
+
multiarray.set_typeDict(nt.sctypeDict)
|
74 |
+
from . import numeric
|
75 |
+
from .numeric import *
|
76 |
+
from . import fromnumeric
|
77 |
+
from .fromnumeric import *
|
78 |
+
from . import defchararray as char
|
79 |
+
from . import records
|
80 |
+
from . import records as rec
|
81 |
+
from .records import record, recarray, format_parser
|
82 |
+
# Note: module name memmap is overwritten by a class with same name
|
83 |
+
from .memmap import *
|
84 |
+
from .defchararray import chararray
|
85 |
+
from . import function_base
|
86 |
+
from .function_base import *
|
87 |
+
from . import _machar
|
88 |
+
from . import getlimits
|
89 |
+
from .getlimits import *
|
90 |
+
from . import shape_base
|
91 |
+
from .shape_base import *
|
92 |
+
from . import einsumfunc
|
93 |
+
from .einsumfunc import *
|
94 |
+
del nt
|
95 |
+
|
96 |
+
from .numeric import absolute as abs
|
97 |
+
|
98 |
+
# do this after everything else, to minimize the chance of this misleadingly
|
99 |
+
# appearing in an import-time traceback
|
100 |
+
from . import _add_newdocs
|
101 |
+
from . import _add_newdocs_scalars
|
102 |
+
# add these for module-freeze analysis (like PyInstaller)
|
103 |
+
from . import _dtype_ctypes
|
104 |
+
from . import _internal
|
105 |
+
from . import _dtype
|
106 |
+
from . import _methods
|
107 |
+
|
108 |
+
__all__ = ['char', 'rec', 'memmap']
|
109 |
+
__all__ += numeric.__all__
|
110 |
+
__all__ += ['record', 'recarray', 'format_parser']
|
111 |
+
__all__ += ['chararray']
|
112 |
+
__all__ += function_base.__all__
|
113 |
+
__all__ += getlimits.__all__
|
114 |
+
__all__ += shape_base.__all__
|
115 |
+
__all__ += einsumfunc.__all__
|
116 |
+
|
117 |
+
# We used to use `np.core._ufunc_reconstruct` to unpickle. This is unnecessary,
|
118 |
+
# but old pickles saved before 1.20 will be using it, and there is no reason
|
119 |
+
# to break loading them.
|
120 |
+
def _ufunc_reconstruct(module, name):
|
121 |
+
# The `fromlist` kwarg is required to ensure that `mod` points to the
|
122 |
+
# inner-most module rather than the parent package when module name is
|
123 |
+
# nested. This makes it possible to pickle non-toplevel ufuncs such as
|
124 |
+
# scipy.special.expit for instance.
|
125 |
+
mod = __import__(module, fromlist=[name])
|
126 |
+
return getattr(mod, name)
|
127 |
+
|
128 |
+
|
129 |
+
def _ufunc_reduce(func):
|
130 |
+
# Report the `__name__`. pickle will try to find the module. Note that
|
131 |
+
# pickle supports for this `__name__` to be a `__qualname__`. It may
|
132 |
+
# make sense to add a `__qualname__` to ufuncs, to allow this more
|
133 |
+
# explicitly (Numba has ufuncs as attributes).
|
134 |
+
# See also: https://github.com/dask/distributed/issues/3450
|
135 |
+
return func.__name__
|
136 |
+
|
137 |
+
|
138 |
+
def _DType_reconstruct(scalar_type):
|
139 |
+
# This is a work-around to pickle type(np.dtype(np.float64)), etc.
|
140 |
+
# and it should eventually be replaced with a better solution, e.g. when
|
141 |
+
# DTypes become HeapTypes.
|
142 |
+
return type(dtype(scalar_type))
|
143 |
+
|
144 |
+
|
145 |
+
def _DType_reduce(DType):
|
146 |
+
# As types/classes, most DTypes can simply be pickled by their name:
|
147 |
+
if not DType._legacy or DType.__module__ == "numpy.dtypes":
|
148 |
+
return DType.__name__
|
149 |
+
|
150 |
+
# However, user defined legacy dtypes (like rational) do not end up in
|
151 |
+
# `numpy.dtypes` as module and do not have a public class at all.
|
152 |
+
# For these, we pickle them by reconstructing them from the scalar type:
|
153 |
+
scalar_type = DType.type
|
154 |
+
return _DType_reconstruct, (scalar_type,)
|
155 |
+
|
156 |
+
|
157 |
+
def __getattr__(name):
|
158 |
+
# Deprecated 2022-11-22, NumPy 1.25.
|
159 |
+
if name == "MachAr":
|
160 |
+
warnings.warn(
|
161 |
+
"The `np.core.MachAr` is considered private API (NumPy 1.24)",
|
162 |
+
DeprecationWarning, stacklevel=2,
|
163 |
+
)
|
164 |
+
return _machar.MachAr
|
165 |
+
raise AttributeError(f"Module {__name__!r} has no attribute {name!r}")
|
166 |
+
|
167 |
+
|
168 |
+
import copyreg
|
169 |
+
|
170 |
+
copyreg.pickle(ufunc, _ufunc_reduce)
|
171 |
+
copyreg.pickle(type(dtype), _DType_reduce, _DType_reconstruct)
|
172 |
+
|
173 |
+
# Unclutter namespace (must keep _*_reconstruct for unpickling)
|
174 |
+
del copyreg
|
175 |
+
del _ufunc_reduce
|
176 |
+
del _DType_reduce
|
177 |
+
|
178 |
+
from numpy._pytesttester import PytestTester
|
179 |
+
test = PytestTester(__name__)
|
180 |
+
del PytestTester
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/_add_newdocs_scalars.py
ADDED
@@ -0,0 +1,372 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
This file is separate from ``_add_newdocs.py`` so that it can be mocked out by
|
3 |
+
our sphinx ``conf.py`` during doc builds, where we want to avoid showing
|
4 |
+
platform-dependent information.
|
5 |
+
"""
|
6 |
+
import sys
|
7 |
+
import os
|
8 |
+
from numpy.core import dtype
|
9 |
+
from numpy.core import numerictypes as _numerictypes
|
10 |
+
from numpy.core.function_base import add_newdoc
|
11 |
+
|
12 |
+
##############################################################################
|
13 |
+
#
|
14 |
+
# Documentation for concrete scalar classes
|
15 |
+
#
|
16 |
+
##############################################################################
|
17 |
+
|
18 |
+
def numeric_type_aliases(aliases):
|
19 |
+
def type_aliases_gen():
|
20 |
+
for alias, doc in aliases:
|
21 |
+
try:
|
22 |
+
alias_type = getattr(_numerictypes, alias)
|
23 |
+
except AttributeError:
|
24 |
+
# The set of aliases that actually exist varies between platforms
|
25 |
+
pass
|
26 |
+
else:
|
27 |
+
yield (alias_type, alias, doc)
|
28 |
+
return list(type_aliases_gen())
|
29 |
+
|
30 |
+
|
31 |
+
possible_aliases = numeric_type_aliases([
|
32 |
+
('int8', '8-bit signed integer (``-128`` to ``127``)'),
|
33 |
+
('int16', '16-bit signed integer (``-32_768`` to ``32_767``)'),
|
34 |
+
('int32', '32-bit signed integer (``-2_147_483_648`` to ``2_147_483_647``)'),
|
35 |
+
('int64', '64-bit signed integer (``-9_223_372_036_854_775_808`` to ``9_223_372_036_854_775_807``)'),
|
36 |
+
('intp', 'Signed integer large enough to fit pointer, compatible with C ``intptr_t``'),
|
37 |
+
('uint8', '8-bit unsigned integer (``0`` to ``255``)'),
|
38 |
+
('uint16', '16-bit unsigned integer (``0`` to ``65_535``)'),
|
39 |
+
('uint32', '32-bit unsigned integer (``0`` to ``4_294_967_295``)'),
|
40 |
+
('uint64', '64-bit unsigned integer (``0`` to ``18_446_744_073_709_551_615``)'),
|
41 |
+
('uintp', 'Unsigned integer large enough to fit pointer, compatible with C ``uintptr_t``'),
|
42 |
+
('float16', '16-bit-precision floating-point number type: sign bit, 5 bits exponent, 10 bits mantissa'),
|
43 |
+
('float32', '32-bit-precision floating-point number type: sign bit, 8 bits exponent, 23 bits mantissa'),
|
44 |
+
('float64', '64-bit precision floating-point number type: sign bit, 11 bits exponent, 52 bits mantissa'),
|
45 |
+
('float96', '96-bit extended-precision floating-point number type'),
|
46 |
+
('float128', '128-bit extended-precision floating-point number type'),
|
47 |
+
('complex64', 'Complex number type composed of 2 32-bit-precision floating-point numbers'),
|
48 |
+
('complex128', 'Complex number type composed of 2 64-bit-precision floating-point numbers'),
|
49 |
+
('complex192', 'Complex number type composed of 2 96-bit extended-precision floating-point numbers'),
|
50 |
+
('complex256', 'Complex number type composed of 2 128-bit extended-precision floating-point numbers'),
|
51 |
+
])
|
52 |
+
|
53 |
+
|
54 |
+
def _get_platform_and_machine():
|
55 |
+
try:
|
56 |
+
system, _, _, _, machine = os.uname()
|
57 |
+
except AttributeError:
|
58 |
+
system = sys.platform
|
59 |
+
if system == 'win32':
|
60 |
+
machine = os.environ.get('PROCESSOR_ARCHITEW6432', '') \
|
61 |
+
or os.environ.get('PROCESSOR_ARCHITECTURE', '')
|
62 |
+
else:
|
63 |
+
machine = 'unknown'
|
64 |
+
return system, machine
|
65 |
+
|
66 |
+
|
67 |
+
_system, _machine = _get_platform_and_machine()
|
68 |
+
_doc_alias_string = f":Alias on this platform ({_system} {_machine}):"
|
69 |
+
|
70 |
+
|
71 |
+
def add_newdoc_for_scalar_type(obj, fixed_aliases, doc):
|
72 |
+
# note: `:field: value` is rST syntax which renders as field lists.
|
73 |
+
o = getattr(_numerictypes, obj)
|
74 |
+
|
75 |
+
character_code = dtype(o).char
|
76 |
+
canonical_name_doc = "" if obj == o.__name__ else \
|
77 |
+
f":Canonical name: `numpy.{obj}`\n "
|
78 |
+
if fixed_aliases:
|
79 |
+
alias_doc = ''.join(f":Alias: `numpy.{alias}`\n "
|
80 |
+
for alias in fixed_aliases)
|
81 |
+
else:
|
82 |
+
alias_doc = ''
|
83 |
+
alias_doc += ''.join(f"{_doc_alias_string} `numpy.{alias}`: {doc}.\n "
|
84 |
+
for (alias_type, alias, doc) in possible_aliases if alias_type is o)
|
85 |
+
|
86 |
+
docstring = f"""
|
87 |
+
{doc.strip()}
|
88 |
+
|
89 |
+
:Character code: ``'{character_code}'``
|
90 |
+
{canonical_name_doc}{alias_doc}
|
91 |
+
"""
|
92 |
+
|
93 |
+
add_newdoc('numpy.core.numerictypes', obj, docstring)
|
94 |
+
|
95 |
+
|
96 |
+
add_newdoc_for_scalar_type('bool_', [],
|
97 |
+
"""
|
98 |
+
Boolean type (True or False), stored as a byte.
|
99 |
+
|
100 |
+
.. warning::
|
101 |
+
|
102 |
+
The :class:`bool_` type is not a subclass of the :class:`int_` type
|
103 |
+
(the :class:`bool_` is not even a number type). This is different
|
104 |
+
than Python's default implementation of :class:`bool` as a
|
105 |
+
sub-class of :class:`int`.
|
106 |
+
""")
|
107 |
+
|
108 |
+
add_newdoc_for_scalar_type('byte', [],
|
109 |
+
"""
|
110 |
+
Signed integer type, compatible with C ``char``.
|
111 |
+
""")
|
112 |
+
|
113 |
+
add_newdoc_for_scalar_type('short', [],
|
114 |
+
"""
|
115 |
+
Signed integer type, compatible with C ``short``.
|
116 |
+
""")
|
117 |
+
|
118 |
+
add_newdoc_for_scalar_type('intc', [],
|
119 |
+
"""
|
120 |
+
Signed integer type, compatible with C ``int``.
|
121 |
+
""")
|
122 |
+
|
123 |
+
add_newdoc_for_scalar_type('int_', [],
|
124 |
+
"""
|
125 |
+
Signed integer type, compatible with Python `int` and C ``long``.
|
126 |
+
""")
|
127 |
+
|
128 |
+
add_newdoc_for_scalar_type('longlong', [],
|
129 |
+
"""
|
130 |
+
Signed integer type, compatible with C ``long long``.
|
131 |
+
""")
|
132 |
+
|
133 |
+
add_newdoc_for_scalar_type('ubyte', [],
|
134 |
+
"""
|
135 |
+
Unsigned integer type, compatible with C ``unsigned char``.
|
136 |
+
""")
|
137 |
+
|
138 |
+
add_newdoc_for_scalar_type('ushort', [],
|
139 |
+
"""
|
140 |
+
Unsigned integer type, compatible with C ``unsigned short``.
|
141 |
+
""")
|
142 |
+
|
143 |
+
add_newdoc_for_scalar_type('uintc', [],
|
144 |
+
"""
|
145 |
+
Unsigned integer type, compatible with C ``unsigned int``.
|
146 |
+
""")
|
147 |
+
|
148 |
+
add_newdoc_for_scalar_type('uint', [],
|
149 |
+
"""
|
150 |
+
Unsigned integer type, compatible with C ``unsigned long``.
|
151 |
+
""")
|
152 |
+
|
153 |
+
add_newdoc_for_scalar_type('ulonglong', [],
|
154 |
+
"""
|
155 |
+
Signed integer type, compatible with C ``unsigned long long``.
|
156 |
+
""")
|
157 |
+
|
158 |
+
add_newdoc_for_scalar_type('half', [],
|
159 |
+
"""
|
160 |
+
Half-precision floating-point number type.
|
161 |
+
""")
|
162 |
+
|
163 |
+
add_newdoc_for_scalar_type('single', [],
|
164 |
+
"""
|
165 |
+
Single-precision floating-point number type, compatible with C ``float``.
|
166 |
+
""")
|
167 |
+
|
168 |
+
add_newdoc_for_scalar_type('double', ['float_'],
|
169 |
+
"""
|
170 |
+
Double-precision floating-point number type, compatible with Python `float`
|
171 |
+
and C ``double``.
|
172 |
+
""")
|
173 |
+
|
174 |
+
add_newdoc_for_scalar_type('longdouble', ['longfloat'],
|
175 |
+
"""
|
176 |
+
Extended-precision floating-point number type, compatible with C
|
177 |
+
``long double`` but not necessarily with IEEE 754 quadruple-precision.
|
178 |
+
""")
|
179 |
+
|
180 |
+
add_newdoc_for_scalar_type('csingle', ['singlecomplex'],
|
181 |
+
"""
|
182 |
+
Complex number type composed of two single-precision floating-point
|
183 |
+
numbers.
|
184 |
+
""")
|
185 |
+
|
186 |
+
add_newdoc_for_scalar_type('cdouble', ['cfloat', 'complex_'],
|
187 |
+
"""
|
188 |
+
Complex number type composed of two double-precision floating-point
|
189 |
+
numbers, compatible with Python `complex`.
|
190 |
+
""")
|
191 |
+
|
192 |
+
add_newdoc_for_scalar_type('clongdouble', ['clongfloat', 'longcomplex'],
|
193 |
+
"""
|
194 |
+
Complex number type composed of two extended-precision floating-point
|
195 |
+
numbers.
|
196 |
+
""")
|
197 |
+
|
198 |
+
add_newdoc_for_scalar_type('object_', [],
|
199 |
+
"""
|
200 |
+
Any Python object.
|
201 |
+
""")
|
202 |
+
|
203 |
+
add_newdoc_for_scalar_type('str_', ['unicode_'],
|
204 |
+
r"""
|
205 |
+
A unicode string.
|
206 |
+
|
207 |
+
This type strips trailing null codepoints.
|
208 |
+
|
209 |
+
>>> s = np.str_("abc\x00")
|
210 |
+
>>> s
|
211 |
+
'abc'
|
212 |
+
|
213 |
+
Unlike the builtin `str`, this supports the :ref:`python:bufferobjects`, exposing its
|
214 |
+
contents as UCS4:
|
215 |
+
|
216 |
+
>>> m = memoryview(np.str_("abc"))
|
217 |
+
>>> m.format
|
218 |
+
'3w'
|
219 |
+
>>> m.tobytes()
|
220 |
+
b'a\x00\x00\x00b\x00\x00\x00c\x00\x00\x00'
|
221 |
+
""")
|
222 |
+
|
223 |
+
add_newdoc_for_scalar_type('bytes_', ['string_'],
|
224 |
+
r"""
|
225 |
+
A byte string.
|
226 |
+
|
227 |
+
When used in arrays, this type strips trailing null bytes.
|
228 |
+
""")
|
229 |
+
|
230 |
+
add_newdoc_for_scalar_type('void', [],
|
231 |
+
r"""
|
232 |
+
np.void(length_or_data, /, dtype=None)
|
233 |
+
|
234 |
+
Create a new structured or unstructured void scalar.
|
235 |
+
|
236 |
+
Parameters
|
237 |
+
----------
|
238 |
+
length_or_data : int, array-like, bytes-like, object
|
239 |
+
One of multiple meanings (see notes). The length or
|
240 |
+
bytes data of an unstructured void. Or alternatively,
|
241 |
+
the data to be stored in the new scalar when `dtype`
|
242 |
+
is provided.
|
243 |
+
This can be an array-like, in which case an array may
|
244 |
+
be returned.
|
245 |
+
dtype : dtype, optional
|
246 |
+
If provided the dtype of the new scalar. This dtype must
|
247 |
+
be "void" dtype (i.e. a structured or unstructured void,
|
248 |
+
see also :ref:`defining-structured-types`).
|
249 |
+
|
250 |
+
..versionadded:: 1.24
|
251 |
+
|
252 |
+
Notes
|
253 |
+
-----
|
254 |
+
For historical reasons and because void scalars can represent both
|
255 |
+
arbitrary byte data and structured dtypes, the void constructor
|
256 |
+
has three calling conventions:
|
257 |
+
|
258 |
+
1. ``np.void(5)`` creates a ``dtype="V5"`` scalar filled with five
|
259 |
+
``\0`` bytes. The 5 can be a Python or NumPy integer.
|
260 |
+
2. ``np.void(b"bytes-like")`` creates a void scalar from the byte string.
|
261 |
+
The dtype itemsize will match the byte string length, here ``"V10"``.
|
262 |
+
3. When a ``dtype=`` is passed the call is roughly the same as an
|
263 |
+
array creation. However, a void scalar rather than array is returned.
|
264 |
+
|
265 |
+
Please see the examples which show all three different conventions.
|
266 |
+
|
267 |
+
Examples
|
268 |
+
--------
|
269 |
+
>>> np.void(5)
|
270 |
+
void(b'\x00\x00\x00\x00\x00')
|
271 |
+
>>> np.void(b'abcd')
|
272 |
+
void(b'\x61\x62\x63\x64')
|
273 |
+
>>> np.void((5, 3.2, "eggs"), dtype="i,d,S5")
|
274 |
+
(5, 3.2, b'eggs') # looks like a tuple, but is `np.void`
|
275 |
+
>>> np.void(3, dtype=[('x', np.int8), ('y', np.int8)])
|
276 |
+
(3, 3) # looks like a tuple, but is `np.void`
|
277 |
+
|
278 |
+
""")
|
279 |
+
|
280 |
+
add_newdoc_for_scalar_type('datetime64', [],
|
281 |
+
"""
|
282 |
+
If created from a 64-bit integer, it represents an offset from
|
283 |
+
``1970-01-01T00:00:00``.
|
284 |
+
If created from string, the string can be in ISO 8601 date
|
285 |
+
or datetime format.
|
286 |
+
|
287 |
+
>>> np.datetime64(10, 'Y')
|
288 |
+
numpy.datetime64('1980')
|
289 |
+
>>> np.datetime64('1980', 'Y')
|
290 |
+
numpy.datetime64('1980')
|
291 |
+
>>> np.datetime64(10, 'D')
|
292 |
+
numpy.datetime64('1970-01-11')
|
293 |
+
|
294 |
+
See :ref:`arrays.datetime` for more information.
|
295 |
+
""")
|
296 |
+
|
297 |
+
add_newdoc_for_scalar_type('timedelta64', [],
|
298 |
+
"""
|
299 |
+
A timedelta stored as a 64-bit integer.
|
300 |
+
|
301 |
+
See :ref:`arrays.datetime` for more information.
|
302 |
+
""")
|
303 |
+
|
304 |
+
add_newdoc('numpy.core.numerictypes', "integer", ('is_integer',
|
305 |
+
"""
|
306 |
+
integer.is_integer() -> bool
|
307 |
+
|
308 |
+
Return ``True`` if the number is finite with integral value.
|
309 |
+
|
310 |
+
.. versionadded:: 1.22
|
311 |
+
|
312 |
+
Examples
|
313 |
+
--------
|
314 |
+
>>> np.int64(-2).is_integer()
|
315 |
+
True
|
316 |
+
>>> np.uint32(5).is_integer()
|
317 |
+
True
|
318 |
+
"""))
|
319 |
+
|
320 |
+
# TODO: work out how to put this on the base class, np.floating
|
321 |
+
for float_name in ('half', 'single', 'double', 'longdouble'):
|
322 |
+
add_newdoc('numpy.core.numerictypes', float_name, ('as_integer_ratio',
|
323 |
+
"""
|
324 |
+
{ftype}.as_integer_ratio() -> (int, int)
|
325 |
+
|
326 |
+
Return a pair of integers, whose ratio is exactly equal to the original
|
327 |
+
floating point number, and with a positive denominator.
|
328 |
+
Raise `OverflowError` on infinities and a `ValueError` on NaNs.
|
329 |
+
|
330 |
+
>>> np.{ftype}(10.0).as_integer_ratio()
|
331 |
+
(10, 1)
|
332 |
+
>>> np.{ftype}(0.0).as_integer_ratio()
|
333 |
+
(0, 1)
|
334 |
+
>>> np.{ftype}(-.25).as_integer_ratio()
|
335 |
+
(-1, 4)
|
336 |
+
""".format(ftype=float_name)))
|
337 |
+
|
338 |
+
add_newdoc('numpy.core.numerictypes', float_name, ('is_integer',
|
339 |
+
f"""
|
340 |
+
{float_name}.is_integer() -> bool
|
341 |
+
|
342 |
+
Return ``True`` if the floating point number is finite with integral
|
343 |
+
value, and ``False`` otherwise.
|
344 |
+
|
345 |
+
.. versionadded:: 1.22
|
346 |
+
|
347 |
+
Examples
|
348 |
+
--------
|
349 |
+
>>> np.{float_name}(-2.0).is_integer()
|
350 |
+
True
|
351 |
+
>>> np.{float_name}(3.2).is_integer()
|
352 |
+
False
|
353 |
+
"""))
|
354 |
+
|
355 |
+
for int_name in ('int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32',
|
356 |
+
'int64', 'uint64', 'int64', 'uint64', 'int64', 'uint64'):
|
357 |
+
# Add negative examples for signed cases by checking typecode
|
358 |
+
add_newdoc('numpy.core.numerictypes', int_name, ('bit_count',
|
359 |
+
f"""
|
360 |
+
{int_name}.bit_count() -> int
|
361 |
+
|
362 |
+
Computes the number of 1-bits in the absolute value of the input.
|
363 |
+
Analogous to the builtin `int.bit_count` or ``popcount`` in C++.
|
364 |
+
|
365 |
+
Examples
|
366 |
+
--------
|
367 |
+
>>> np.{int_name}(127).bit_count()
|
368 |
+
7""" +
|
369 |
+
(f"""
|
370 |
+
>>> np.{int_name}(-127).bit_count()
|
371 |
+
7
|
372 |
+
""" if dtype(int_name).char.islower() else "")))
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/_dtype.py
ADDED
@@ -0,0 +1,369 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
A place for code to be called from the implementation of np.dtype
|
3 |
+
|
4 |
+
String handling is much easier to do correctly in python.
|
5 |
+
"""
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
|
9 |
+
_kind_to_stem = {
|
10 |
+
'u': 'uint',
|
11 |
+
'i': 'int',
|
12 |
+
'c': 'complex',
|
13 |
+
'f': 'float',
|
14 |
+
'b': 'bool',
|
15 |
+
'V': 'void',
|
16 |
+
'O': 'object',
|
17 |
+
'M': 'datetime',
|
18 |
+
'm': 'timedelta',
|
19 |
+
'S': 'bytes',
|
20 |
+
'U': 'str',
|
21 |
+
}
|
22 |
+
|
23 |
+
|
24 |
+
def _kind_name(dtype):
|
25 |
+
try:
|
26 |
+
return _kind_to_stem[dtype.kind]
|
27 |
+
except KeyError as e:
|
28 |
+
raise RuntimeError(
|
29 |
+
"internal dtype error, unknown kind {!r}"
|
30 |
+
.format(dtype.kind)
|
31 |
+
) from None
|
32 |
+
|
33 |
+
|
34 |
+
def __str__(dtype):
|
35 |
+
if dtype.fields is not None:
|
36 |
+
return _struct_str(dtype, include_align=True)
|
37 |
+
elif dtype.subdtype:
|
38 |
+
return _subarray_str(dtype)
|
39 |
+
elif issubclass(dtype.type, np.flexible) or not dtype.isnative:
|
40 |
+
return dtype.str
|
41 |
+
else:
|
42 |
+
return dtype.name
|
43 |
+
|
44 |
+
|
45 |
+
def __repr__(dtype):
|
46 |
+
arg_str = _construction_repr(dtype, include_align=False)
|
47 |
+
if dtype.isalignedstruct:
|
48 |
+
arg_str = arg_str + ", align=True"
|
49 |
+
return "dtype({})".format(arg_str)
|
50 |
+
|
51 |
+
|
52 |
+
def _unpack_field(dtype, offset, title=None):
|
53 |
+
"""
|
54 |
+
Helper function to normalize the items in dtype.fields.
|
55 |
+
|
56 |
+
Call as:
|
57 |
+
|
58 |
+
dtype, offset, title = _unpack_field(*dtype.fields[name])
|
59 |
+
"""
|
60 |
+
return dtype, offset, title
|
61 |
+
|
62 |
+
|
63 |
+
def _isunsized(dtype):
|
64 |
+
# PyDataType_ISUNSIZED
|
65 |
+
return dtype.itemsize == 0
|
66 |
+
|
67 |
+
|
68 |
+
def _construction_repr(dtype, include_align=False, short=False):
|
69 |
+
"""
|
70 |
+
Creates a string repr of the dtype, excluding the 'dtype()' part
|
71 |
+
surrounding the object. This object may be a string, a list, or
|
72 |
+
a dict depending on the nature of the dtype. This
|
73 |
+
is the object passed as the first parameter to the dtype
|
74 |
+
constructor, and if no additional constructor parameters are
|
75 |
+
given, will reproduce the exact memory layout.
|
76 |
+
|
77 |
+
Parameters
|
78 |
+
----------
|
79 |
+
short : bool
|
80 |
+
If true, this creates a shorter repr using 'kind' and 'itemsize', instead
|
81 |
+
of the longer type name.
|
82 |
+
|
83 |
+
include_align : bool
|
84 |
+
If true, this includes the 'align=True' parameter
|
85 |
+
inside the struct dtype construction dict when needed. Use this flag
|
86 |
+
if you want a proper repr string without the 'dtype()' part around it.
|
87 |
+
|
88 |
+
If false, this does not preserve the
|
89 |
+
'align=True' parameter or sticky NPY_ALIGNED_STRUCT flag for
|
90 |
+
struct arrays like the regular repr does, because the 'align'
|
91 |
+
flag is not part of first dtype constructor parameter. This
|
92 |
+
mode is intended for a full 'repr', where the 'align=True' is
|
93 |
+
provided as the second parameter.
|
94 |
+
"""
|
95 |
+
if dtype.fields is not None:
|
96 |
+
return _struct_str(dtype, include_align=include_align)
|
97 |
+
elif dtype.subdtype:
|
98 |
+
return _subarray_str(dtype)
|
99 |
+
else:
|
100 |
+
return _scalar_str(dtype, short=short)
|
101 |
+
|
102 |
+
|
103 |
+
def _scalar_str(dtype, short):
|
104 |
+
byteorder = _byte_order_str(dtype)
|
105 |
+
|
106 |
+
if dtype.type == np.bool_:
|
107 |
+
if short:
|
108 |
+
return "'?'"
|
109 |
+
else:
|
110 |
+
return "'bool'"
|
111 |
+
|
112 |
+
elif dtype.type == np.object_:
|
113 |
+
# The object reference may be different sizes on different
|
114 |
+
# platforms, so it should never include the itemsize here.
|
115 |
+
return "'O'"
|
116 |
+
|
117 |
+
elif dtype.type == np.bytes_:
|
118 |
+
if _isunsized(dtype):
|
119 |
+
return "'S'"
|
120 |
+
else:
|
121 |
+
return "'S%d'" % dtype.itemsize
|
122 |
+
|
123 |
+
elif dtype.type == np.str_:
|
124 |
+
if _isunsized(dtype):
|
125 |
+
return "'%sU'" % byteorder
|
126 |
+
else:
|
127 |
+
return "'%sU%d'" % (byteorder, dtype.itemsize / 4)
|
128 |
+
|
129 |
+
# unlike the other types, subclasses of void are preserved - but
|
130 |
+
# historically the repr does not actually reveal the subclass
|
131 |
+
elif issubclass(dtype.type, np.void):
|
132 |
+
if _isunsized(dtype):
|
133 |
+
return "'V'"
|
134 |
+
else:
|
135 |
+
return "'V%d'" % dtype.itemsize
|
136 |
+
|
137 |
+
elif dtype.type == np.datetime64:
|
138 |
+
return "'%sM8%s'" % (byteorder, _datetime_metadata_str(dtype))
|
139 |
+
|
140 |
+
elif dtype.type == np.timedelta64:
|
141 |
+
return "'%sm8%s'" % (byteorder, _datetime_metadata_str(dtype))
|
142 |
+
|
143 |
+
elif np.issubdtype(dtype, np.number):
|
144 |
+
# Short repr with endianness, like '<f8'
|
145 |
+
if short or dtype.byteorder not in ('=', '|'):
|
146 |
+
return "'%s%c%d'" % (byteorder, dtype.kind, dtype.itemsize)
|
147 |
+
|
148 |
+
# Longer repr, like 'float64'
|
149 |
+
else:
|
150 |
+
return "'%s%d'" % (_kind_name(dtype), 8*dtype.itemsize)
|
151 |
+
|
152 |
+
elif dtype.isbuiltin == 2:
|
153 |
+
return dtype.type.__name__
|
154 |
+
|
155 |
+
else:
|
156 |
+
raise RuntimeError(
|
157 |
+
"Internal error: NumPy dtype unrecognized type number")
|
158 |
+
|
159 |
+
|
160 |
+
def _byte_order_str(dtype):
|
161 |
+
""" Normalize byteorder to '<' or '>' """
|
162 |
+
# hack to obtain the native and swapped byte order characters
|
163 |
+
swapped = np.dtype(int).newbyteorder('S')
|
164 |
+
native = swapped.newbyteorder('S')
|
165 |
+
|
166 |
+
byteorder = dtype.byteorder
|
167 |
+
if byteorder == '=':
|
168 |
+
return native.byteorder
|
169 |
+
if byteorder == 'S':
|
170 |
+
# TODO: this path can never be reached
|
171 |
+
return swapped.byteorder
|
172 |
+
elif byteorder == '|':
|
173 |
+
return ''
|
174 |
+
else:
|
175 |
+
return byteorder
|
176 |
+
|
177 |
+
|
178 |
+
def _datetime_metadata_str(dtype):
|
179 |
+
# TODO: this duplicates the C metastr_to_unicode functionality
|
180 |
+
unit, count = np.datetime_data(dtype)
|
181 |
+
if unit == 'generic':
|
182 |
+
return ''
|
183 |
+
elif count == 1:
|
184 |
+
return '[{}]'.format(unit)
|
185 |
+
else:
|
186 |
+
return '[{}{}]'.format(count, unit)
|
187 |
+
|
188 |
+
|
189 |
+
def _struct_dict_str(dtype, includealignedflag):
|
190 |
+
# unpack the fields dictionary into ls
|
191 |
+
names = dtype.names
|
192 |
+
fld_dtypes = []
|
193 |
+
offsets = []
|
194 |
+
titles = []
|
195 |
+
for name in names:
|
196 |
+
fld_dtype, offset, title = _unpack_field(*dtype.fields[name])
|
197 |
+
fld_dtypes.append(fld_dtype)
|
198 |
+
offsets.append(offset)
|
199 |
+
titles.append(title)
|
200 |
+
|
201 |
+
# Build up a string to make the dictionary
|
202 |
+
|
203 |
+
if np.core.arrayprint._get_legacy_print_mode() <= 121:
|
204 |
+
colon = ":"
|
205 |
+
fieldsep = ","
|
206 |
+
else:
|
207 |
+
colon = ": "
|
208 |
+
fieldsep = ", "
|
209 |
+
|
210 |
+
# First, the names
|
211 |
+
ret = "{'names'%s[" % colon
|
212 |
+
ret += fieldsep.join(repr(name) for name in names)
|
213 |
+
|
214 |
+
# Second, the formats
|
215 |
+
ret += "], 'formats'%s[" % colon
|
216 |
+
ret += fieldsep.join(
|
217 |
+
_construction_repr(fld_dtype, short=True) for fld_dtype in fld_dtypes)
|
218 |
+
|
219 |
+
# Third, the offsets
|
220 |
+
ret += "], 'offsets'%s[" % colon
|
221 |
+
ret += fieldsep.join("%d" % offset for offset in offsets)
|
222 |
+
|
223 |
+
# Fourth, the titles
|
224 |
+
if any(title is not None for title in titles):
|
225 |
+
ret += "], 'titles'%s[" % colon
|
226 |
+
ret += fieldsep.join(repr(title) for title in titles)
|
227 |
+
|
228 |
+
# Fifth, the itemsize
|
229 |
+
ret += "], 'itemsize'%s%d" % (colon, dtype.itemsize)
|
230 |
+
|
231 |
+
if (includealignedflag and dtype.isalignedstruct):
|
232 |
+
# Finally, the aligned flag
|
233 |
+
ret += ", 'aligned'%sTrue}" % colon
|
234 |
+
else:
|
235 |
+
ret += "}"
|
236 |
+
|
237 |
+
return ret
|
238 |
+
|
239 |
+
|
240 |
+
def _aligned_offset(offset, alignment):
|
241 |
+
# round up offset:
|
242 |
+
return - (-offset // alignment) * alignment
|
243 |
+
|
244 |
+
|
245 |
+
def _is_packed(dtype):
|
246 |
+
"""
|
247 |
+
Checks whether the structured data type in 'dtype'
|
248 |
+
has a simple layout, where all the fields are in order,
|
249 |
+
and follow each other with no alignment padding.
|
250 |
+
|
251 |
+
When this returns true, the dtype can be reconstructed
|
252 |
+
from a list of the field names and dtypes with no additional
|
253 |
+
dtype parameters.
|
254 |
+
|
255 |
+
Duplicates the C `is_dtype_struct_simple_unaligned_layout` function.
|
256 |
+
"""
|
257 |
+
align = dtype.isalignedstruct
|
258 |
+
max_alignment = 1
|
259 |
+
total_offset = 0
|
260 |
+
for name in dtype.names:
|
261 |
+
fld_dtype, fld_offset, title = _unpack_field(*dtype.fields[name])
|
262 |
+
|
263 |
+
if align:
|
264 |
+
total_offset = _aligned_offset(total_offset, fld_dtype.alignment)
|
265 |
+
max_alignment = max(max_alignment, fld_dtype.alignment)
|
266 |
+
|
267 |
+
if fld_offset != total_offset:
|
268 |
+
return False
|
269 |
+
total_offset += fld_dtype.itemsize
|
270 |
+
|
271 |
+
if align:
|
272 |
+
total_offset = _aligned_offset(total_offset, max_alignment)
|
273 |
+
|
274 |
+
if total_offset != dtype.itemsize:
|
275 |
+
return False
|
276 |
+
return True
|
277 |
+
|
278 |
+
|
279 |
+
def _struct_list_str(dtype):
|
280 |
+
items = []
|
281 |
+
for name in dtype.names:
|
282 |
+
fld_dtype, fld_offset, title = _unpack_field(*dtype.fields[name])
|
283 |
+
|
284 |
+
item = "("
|
285 |
+
if title is not None:
|
286 |
+
item += "({!r}, {!r}), ".format(title, name)
|
287 |
+
else:
|
288 |
+
item += "{!r}, ".format(name)
|
289 |
+
# Special case subarray handling here
|
290 |
+
if fld_dtype.subdtype is not None:
|
291 |
+
base, shape = fld_dtype.subdtype
|
292 |
+
item += "{}, {}".format(
|
293 |
+
_construction_repr(base, short=True),
|
294 |
+
shape
|
295 |
+
)
|
296 |
+
else:
|
297 |
+
item += _construction_repr(fld_dtype, short=True)
|
298 |
+
|
299 |
+
item += ")"
|
300 |
+
items.append(item)
|
301 |
+
|
302 |
+
return "[" + ", ".join(items) + "]"
|
303 |
+
|
304 |
+
|
305 |
+
def _struct_str(dtype, include_align):
|
306 |
+
# The list str representation can't include the 'align=' flag,
|
307 |
+
# so if it is requested and the struct has the aligned flag set,
|
308 |
+
# we must use the dict str instead.
|
309 |
+
if not (include_align and dtype.isalignedstruct) and _is_packed(dtype):
|
310 |
+
sub = _struct_list_str(dtype)
|
311 |
+
|
312 |
+
else:
|
313 |
+
sub = _struct_dict_str(dtype, include_align)
|
314 |
+
|
315 |
+
# If the data type isn't the default, void, show it
|
316 |
+
if dtype.type != np.void:
|
317 |
+
return "({t.__module__}.{t.__name__}, {f})".format(t=dtype.type, f=sub)
|
318 |
+
else:
|
319 |
+
return sub
|
320 |
+
|
321 |
+
|
322 |
+
def _subarray_str(dtype):
|
323 |
+
base, shape = dtype.subdtype
|
324 |
+
return "({}, {})".format(
|
325 |
+
_construction_repr(base, short=True),
|
326 |
+
shape
|
327 |
+
)
|
328 |
+
|
329 |
+
|
330 |
+
def _name_includes_bit_suffix(dtype):
|
331 |
+
if dtype.type == np.object_:
|
332 |
+
# pointer size varies by system, best to omit it
|
333 |
+
return False
|
334 |
+
elif dtype.type == np.bool_:
|
335 |
+
# implied
|
336 |
+
return False
|
337 |
+
elif dtype.type is None:
|
338 |
+
return True
|
339 |
+
elif np.issubdtype(dtype, np.flexible) and _isunsized(dtype):
|
340 |
+
# unspecified
|
341 |
+
return False
|
342 |
+
else:
|
343 |
+
return True
|
344 |
+
|
345 |
+
|
346 |
+
def _name_get(dtype):
|
347 |
+
# provides dtype.name.__get__, documented as returning a "bit name"
|
348 |
+
|
349 |
+
if dtype.isbuiltin == 2:
|
350 |
+
# user dtypes don't promise to do anything special
|
351 |
+
return dtype.type.__name__
|
352 |
+
|
353 |
+
if dtype.kind == '\x00':
|
354 |
+
name = type(dtype).__name__
|
355 |
+
elif issubclass(dtype.type, np.void):
|
356 |
+
# historically, void subclasses preserve their name, eg `record64`
|
357 |
+
name = dtype.type.__name__
|
358 |
+
else:
|
359 |
+
name = _kind_name(dtype)
|
360 |
+
|
361 |
+
# append bit counts
|
362 |
+
if _name_includes_bit_suffix(dtype):
|
363 |
+
name += "{}".format(dtype.itemsize * 8)
|
364 |
+
|
365 |
+
# append metadata to datetimes
|
366 |
+
if dtype.type in (np.datetime64, np.timedelta64):
|
367 |
+
name += _datetime_metadata_str(dtype)
|
368 |
+
|
369 |
+
return name
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/_machar.py
ADDED
@@ -0,0 +1,356 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Machine arithmetic - determine the parameters of the
|
3 |
+
floating-point arithmetic system
|
4 |
+
|
5 |
+
Author: Pearu Peterson, September 2003
|
6 |
+
|
7 |
+
"""
|
8 |
+
__all__ = ['MachAr']
|
9 |
+
|
10 |
+
from .fromnumeric import any
|
11 |
+
from ._ufunc_config import errstate
|
12 |
+
from .._utils import set_module
|
13 |
+
|
14 |
+
# Need to speed this up...especially for longfloat
|
15 |
+
|
16 |
+
# Deprecated 2021-10-20, NumPy 1.22
|
17 |
+
class MachAr:
|
18 |
+
"""
|
19 |
+
Diagnosing machine parameters.
|
20 |
+
|
21 |
+
Attributes
|
22 |
+
----------
|
23 |
+
ibeta : int
|
24 |
+
Radix in which numbers are represented.
|
25 |
+
it : int
|
26 |
+
Number of base-`ibeta` digits in the floating point mantissa M.
|
27 |
+
machep : int
|
28 |
+
Exponent of the smallest (most negative) power of `ibeta` that,
|
29 |
+
added to 1.0, gives something different from 1.0
|
30 |
+
eps : float
|
31 |
+
Floating-point number ``beta**machep`` (floating point precision)
|
32 |
+
negep : int
|
33 |
+
Exponent of the smallest power of `ibeta` that, subtracted
|
34 |
+
from 1.0, gives something different from 1.0.
|
35 |
+
epsneg : float
|
36 |
+
Floating-point number ``beta**negep``.
|
37 |
+
iexp : int
|
38 |
+
Number of bits in the exponent (including its sign and bias).
|
39 |
+
minexp : int
|
40 |
+
Smallest (most negative) power of `ibeta` consistent with there
|
41 |
+
being no leading zeros in the mantissa.
|
42 |
+
xmin : float
|
43 |
+
Floating-point number ``beta**minexp`` (the smallest [in
|
44 |
+
magnitude] positive floating point number with full precision).
|
45 |
+
maxexp : int
|
46 |
+
Smallest (positive) power of `ibeta` that causes overflow.
|
47 |
+
xmax : float
|
48 |
+
``(1-epsneg) * beta**maxexp`` (the largest [in magnitude]
|
49 |
+
usable floating value).
|
50 |
+
irnd : int
|
51 |
+
In ``range(6)``, information on what kind of rounding is done
|
52 |
+
in addition, and on how underflow is handled.
|
53 |
+
ngrd : int
|
54 |
+
Number of 'guard digits' used when truncating the product
|
55 |
+
of two mantissas to fit the representation.
|
56 |
+
epsilon : float
|
57 |
+
Same as `eps`.
|
58 |
+
tiny : float
|
59 |
+
An alias for `smallest_normal`, kept for backwards compatibility.
|
60 |
+
huge : float
|
61 |
+
Same as `xmax`.
|
62 |
+
precision : float
|
63 |
+
``- int(-log10(eps))``
|
64 |
+
resolution : float
|
65 |
+
``- 10**(-precision)``
|
66 |
+
smallest_normal : float
|
67 |
+
The smallest positive floating point number with 1 as leading bit in
|
68 |
+
the mantissa following IEEE-754. Same as `xmin`.
|
69 |
+
smallest_subnormal : float
|
70 |
+
The smallest positive floating point number with 0 as leading bit in
|
71 |
+
the mantissa following IEEE-754.
|
72 |
+
|
73 |
+
Parameters
|
74 |
+
----------
|
75 |
+
float_conv : function, optional
|
76 |
+
Function that converts an integer or integer array to a float
|
77 |
+
or float array. Default is `float`.
|
78 |
+
int_conv : function, optional
|
79 |
+
Function that converts a float or float array to an integer or
|
80 |
+
integer array. Default is `int`.
|
81 |
+
float_to_float : function, optional
|
82 |
+
Function that converts a float array to float. Default is `float`.
|
83 |
+
Note that this does not seem to do anything useful in the current
|
84 |
+
implementation.
|
85 |
+
float_to_str : function, optional
|
86 |
+
Function that converts a single float to a string. Default is
|
87 |
+
``lambda v:'%24.16e' %v``.
|
88 |
+
title : str, optional
|
89 |
+
Title that is printed in the string representation of `MachAr`.
|
90 |
+
|
91 |
+
See Also
|
92 |
+
--------
|
93 |
+
finfo : Machine limits for floating point types.
|
94 |
+
iinfo : Machine limits for integer types.
|
95 |
+
|
96 |
+
References
|
97 |
+
----------
|
98 |
+
.. [1] Press, Teukolsky, Vetterling and Flannery,
|
99 |
+
"Numerical Recipes in C++," 2nd ed,
|
100 |
+
Cambridge University Press, 2002, p. 31.
|
101 |
+
|
102 |
+
"""
|
103 |
+
|
104 |
+
def __init__(self, float_conv=float,int_conv=int,
|
105 |
+
float_to_float=float,
|
106 |
+
float_to_str=lambda v:'%24.16e' % v,
|
107 |
+
title='Python floating point number'):
|
108 |
+
"""
|
109 |
+
|
110 |
+
float_conv - convert integer to float (array)
|
111 |
+
int_conv - convert float (array) to integer
|
112 |
+
float_to_float - convert float array to float
|
113 |
+
float_to_str - convert array float to str
|
114 |
+
title - description of used floating point numbers
|
115 |
+
|
116 |
+
"""
|
117 |
+
# We ignore all errors here because we are purposely triggering
|
118 |
+
# underflow to detect the properties of the runninng arch.
|
119 |
+
with errstate(under='ignore'):
|
120 |
+
self._do_init(float_conv, int_conv, float_to_float, float_to_str, title)
|
121 |
+
|
122 |
+
def _do_init(self, float_conv, int_conv, float_to_float, float_to_str, title):
|
123 |
+
max_iterN = 10000
|
124 |
+
msg = "Did not converge after %d tries with %s"
|
125 |
+
one = float_conv(1)
|
126 |
+
two = one + one
|
127 |
+
zero = one - one
|
128 |
+
|
129 |
+
# Do we really need to do this? Aren't they 2 and 2.0?
|
130 |
+
# Determine ibeta and beta
|
131 |
+
a = one
|
132 |
+
for _ in range(max_iterN):
|
133 |
+
a = a + a
|
134 |
+
temp = a + one
|
135 |
+
temp1 = temp - a
|
136 |
+
if any(temp1 - one != zero):
|
137 |
+
break
|
138 |
+
else:
|
139 |
+
raise RuntimeError(msg % (_, one.dtype))
|
140 |
+
b = one
|
141 |
+
for _ in range(max_iterN):
|
142 |
+
b = b + b
|
143 |
+
temp = a + b
|
144 |
+
itemp = int_conv(temp-a)
|
145 |
+
if any(itemp != 0):
|
146 |
+
break
|
147 |
+
else:
|
148 |
+
raise RuntimeError(msg % (_, one.dtype))
|
149 |
+
ibeta = itemp
|
150 |
+
beta = float_conv(ibeta)
|
151 |
+
|
152 |
+
# Determine it and irnd
|
153 |
+
it = -1
|
154 |
+
b = one
|
155 |
+
for _ in range(max_iterN):
|
156 |
+
it = it + 1
|
157 |
+
b = b * beta
|
158 |
+
temp = b + one
|
159 |
+
temp1 = temp - b
|
160 |
+
if any(temp1 - one != zero):
|
161 |
+
break
|
162 |
+
else:
|
163 |
+
raise RuntimeError(msg % (_, one.dtype))
|
164 |
+
|
165 |
+
betah = beta / two
|
166 |
+
a = one
|
167 |
+
for _ in range(max_iterN):
|
168 |
+
a = a + a
|
169 |
+
temp = a + one
|
170 |
+
temp1 = temp - a
|
171 |
+
if any(temp1 - one != zero):
|
172 |
+
break
|
173 |
+
else:
|
174 |
+
raise RuntimeError(msg % (_, one.dtype))
|
175 |
+
temp = a + betah
|
176 |
+
irnd = 0
|
177 |
+
if any(temp-a != zero):
|
178 |
+
irnd = 1
|
179 |
+
tempa = a + beta
|
180 |
+
temp = tempa + betah
|
181 |
+
if irnd == 0 and any(temp-tempa != zero):
|
182 |
+
irnd = 2
|
183 |
+
|
184 |
+
# Determine negep and epsneg
|
185 |
+
negep = it + 3
|
186 |
+
betain = one / beta
|
187 |
+
a = one
|
188 |
+
for i in range(negep):
|
189 |
+
a = a * betain
|
190 |
+
b = a
|
191 |
+
for _ in range(max_iterN):
|
192 |
+
temp = one - a
|
193 |
+
if any(temp-one != zero):
|
194 |
+
break
|
195 |
+
a = a * beta
|
196 |
+
negep = negep - 1
|
197 |
+
# Prevent infinite loop on PPC with gcc 4.0:
|
198 |
+
if negep < 0:
|
199 |
+
raise RuntimeError("could not determine machine tolerance "
|
200 |
+
"for 'negep', locals() -> %s" % (locals()))
|
201 |
+
else:
|
202 |
+
raise RuntimeError(msg % (_, one.dtype))
|
203 |
+
negep = -negep
|
204 |
+
epsneg = a
|
205 |
+
|
206 |
+
# Determine machep and eps
|
207 |
+
machep = - it - 3
|
208 |
+
a = b
|
209 |
+
|
210 |
+
for _ in range(max_iterN):
|
211 |
+
temp = one + a
|
212 |
+
if any(temp-one != zero):
|
213 |
+
break
|
214 |
+
a = a * beta
|
215 |
+
machep = machep + 1
|
216 |
+
else:
|
217 |
+
raise RuntimeError(msg % (_, one.dtype))
|
218 |
+
eps = a
|
219 |
+
|
220 |
+
# Determine ngrd
|
221 |
+
ngrd = 0
|
222 |
+
temp = one + eps
|
223 |
+
if irnd == 0 and any(temp*one - one != zero):
|
224 |
+
ngrd = 1
|
225 |
+
|
226 |
+
# Determine iexp
|
227 |
+
i = 0
|
228 |
+
k = 1
|
229 |
+
z = betain
|
230 |
+
t = one + eps
|
231 |
+
nxres = 0
|
232 |
+
for _ in range(max_iterN):
|
233 |
+
y = z
|
234 |
+
z = y*y
|
235 |
+
a = z*one # Check here for underflow
|
236 |
+
temp = z*t
|
237 |
+
if any(a+a == zero) or any(abs(z) >= y):
|
238 |
+
break
|
239 |
+
temp1 = temp * betain
|
240 |
+
if any(temp1*beta == z):
|
241 |
+
break
|
242 |
+
i = i + 1
|
243 |
+
k = k + k
|
244 |
+
else:
|
245 |
+
raise RuntimeError(msg % (_, one.dtype))
|
246 |
+
if ibeta != 10:
|
247 |
+
iexp = i + 1
|
248 |
+
mx = k + k
|
249 |
+
else:
|
250 |
+
iexp = 2
|
251 |
+
iz = ibeta
|
252 |
+
while k >= iz:
|
253 |
+
iz = iz * ibeta
|
254 |
+
iexp = iexp + 1
|
255 |
+
mx = iz + iz - 1
|
256 |
+
|
257 |
+
# Determine minexp and xmin
|
258 |
+
for _ in range(max_iterN):
|
259 |
+
xmin = y
|
260 |
+
y = y * betain
|
261 |
+
a = y * one
|
262 |
+
temp = y * t
|
263 |
+
if any((a + a) != zero) and any(abs(y) < xmin):
|
264 |
+
k = k + 1
|
265 |
+
temp1 = temp * betain
|
266 |
+
if any(temp1*beta == y) and any(temp != y):
|
267 |
+
nxres = 3
|
268 |
+
xmin = y
|
269 |
+
break
|
270 |
+
else:
|
271 |
+
break
|
272 |
+
else:
|
273 |
+
raise RuntimeError(msg % (_, one.dtype))
|
274 |
+
minexp = -k
|
275 |
+
|
276 |
+
# Determine maxexp, xmax
|
277 |
+
if mx <= k + k - 3 and ibeta != 10:
|
278 |
+
mx = mx + mx
|
279 |
+
iexp = iexp + 1
|
280 |
+
maxexp = mx + minexp
|
281 |
+
irnd = irnd + nxres
|
282 |
+
if irnd >= 2:
|
283 |
+
maxexp = maxexp - 2
|
284 |
+
i = maxexp + minexp
|
285 |
+
if ibeta == 2 and not i:
|
286 |
+
maxexp = maxexp - 1
|
287 |
+
if i > 20:
|
288 |
+
maxexp = maxexp - 1
|
289 |
+
if any(a != y):
|
290 |
+
maxexp = maxexp - 2
|
291 |
+
xmax = one - epsneg
|
292 |
+
if any(xmax*one != xmax):
|
293 |
+
xmax = one - beta*epsneg
|
294 |
+
xmax = xmax / (xmin*beta*beta*beta)
|
295 |
+
i = maxexp + minexp + 3
|
296 |
+
for j in range(i):
|
297 |
+
if ibeta == 2:
|
298 |
+
xmax = xmax + xmax
|
299 |
+
else:
|
300 |
+
xmax = xmax * beta
|
301 |
+
|
302 |
+
smallest_subnormal = abs(xmin / beta ** (it))
|
303 |
+
|
304 |
+
self.ibeta = ibeta
|
305 |
+
self.it = it
|
306 |
+
self.negep = negep
|
307 |
+
self.epsneg = float_to_float(epsneg)
|
308 |
+
self._str_epsneg = float_to_str(epsneg)
|
309 |
+
self.machep = machep
|
310 |
+
self.eps = float_to_float(eps)
|
311 |
+
self._str_eps = float_to_str(eps)
|
312 |
+
self.ngrd = ngrd
|
313 |
+
self.iexp = iexp
|
314 |
+
self.minexp = minexp
|
315 |
+
self.xmin = float_to_float(xmin)
|
316 |
+
self._str_xmin = float_to_str(xmin)
|
317 |
+
self.maxexp = maxexp
|
318 |
+
self.xmax = float_to_float(xmax)
|
319 |
+
self._str_xmax = float_to_str(xmax)
|
320 |
+
self.irnd = irnd
|
321 |
+
|
322 |
+
self.title = title
|
323 |
+
# Commonly used parameters
|
324 |
+
self.epsilon = self.eps
|
325 |
+
self.tiny = self.xmin
|
326 |
+
self.huge = self.xmax
|
327 |
+
self.smallest_normal = self.xmin
|
328 |
+
self._str_smallest_normal = float_to_str(self.xmin)
|
329 |
+
self.smallest_subnormal = float_to_float(smallest_subnormal)
|
330 |
+
self._str_smallest_subnormal = float_to_str(smallest_subnormal)
|
331 |
+
|
332 |
+
import math
|
333 |
+
self.precision = int(-math.log10(float_to_float(self.eps)))
|
334 |
+
ten = two + two + two + two + two
|
335 |
+
resolution = ten ** (-self.precision)
|
336 |
+
self.resolution = float_to_float(resolution)
|
337 |
+
self._str_resolution = float_to_str(resolution)
|
338 |
+
|
339 |
+
def __str__(self):
|
340 |
+
fmt = (
|
341 |
+
'Machine parameters for %(title)s\n'
|
342 |
+
'---------------------------------------------------------------------\n'
|
343 |
+
'ibeta=%(ibeta)s it=%(it)s iexp=%(iexp)s ngrd=%(ngrd)s irnd=%(irnd)s\n'
|
344 |
+
'machep=%(machep)s eps=%(_str_eps)s (beta**machep == epsilon)\n'
|
345 |
+
'negep =%(negep)s epsneg=%(_str_epsneg)s (beta**epsneg)\n'
|
346 |
+
'minexp=%(minexp)s xmin=%(_str_xmin)s (beta**minexp == tiny)\n'
|
347 |
+
'maxexp=%(maxexp)s xmax=%(_str_xmax)s ((1-epsneg)*beta**maxexp == huge)\n'
|
348 |
+
'smallest_normal=%(smallest_normal)s '
|
349 |
+
'smallest_subnormal=%(smallest_subnormal)s\n'
|
350 |
+
'---------------------------------------------------------------------\n'
|
351 |
+
)
|
352 |
+
return fmt % self.__dict__
|
353 |
+
|
354 |
+
|
355 |
+
if __name__ == '__main__':
|
356 |
+
print(MachAr())
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/_multiarray_tests.cpython-310-x86_64-linux-gnu.so
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:0de46f5496fc26171572adc200ca54ac5ee4703fc4cd9f6173e38eb2ee21c20b
|
3 |
+
size 175912
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/_rational_tests.cpython-310-x86_64-linux-gnu.so
ADDED
Binary file (59.8 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/_string_helpers.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
String-handling utilities to avoid locale-dependence.
|
3 |
+
|
4 |
+
Used primarily to generate type name aliases.
|
5 |
+
"""
|
6 |
+
# "import string" is costly to import!
|
7 |
+
# Construct the translation tables directly
|
8 |
+
# "A" = chr(65), "a" = chr(97)
|
9 |
+
_all_chars = tuple(map(chr, range(256)))
|
10 |
+
_ascii_upper = _all_chars[65:65+26]
|
11 |
+
_ascii_lower = _all_chars[97:97+26]
|
12 |
+
LOWER_TABLE = "".join(_all_chars[:65] + _ascii_lower + _all_chars[65+26:])
|
13 |
+
UPPER_TABLE = "".join(_all_chars[:97] + _ascii_upper + _all_chars[97+26:])
|
14 |
+
|
15 |
+
|
16 |
+
def english_lower(s):
|
17 |
+
""" Apply English case rules to convert ASCII strings to all lower case.
|
18 |
+
|
19 |
+
This is an internal utility function to replace calls to str.lower() such
|
20 |
+
that we can avoid changing behavior with changing locales. In particular,
|
21 |
+
Turkish has distinct dotted and dotless variants of the Latin letter "I" in
|
22 |
+
both lowercase and uppercase. Thus, "I".lower() != "i" in a "tr" locale.
|
23 |
+
|
24 |
+
Parameters
|
25 |
+
----------
|
26 |
+
s : str
|
27 |
+
|
28 |
+
Returns
|
29 |
+
-------
|
30 |
+
lowered : str
|
31 |
+
|
32 |
+
Examples
|
33 |
+
--------
|
34 |
+
>>> from numpy.core.numerictypes import english_lower
|
35 |
+
>>> english_lower('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_')
|
36 |
+
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789_'
|
37 |
+
>>> english_lower('')
|
38 |
+
''
|
39 |
+
"""
|
40 |
+
lowered = s.translate(LOWER_TABLE)
|
41 |
+
return lowered
|
42 |
+
|
43 |
+
|
44 |
+
def english_upper(s):
|
45 |
+
""" Apply English case rules to convert ASCII strings to all upper case.
|
46 |
+
|
47 |
+
This is an internal utility function to replace calls to str.upper() such
|
48 |
+
that we can avoid changing behavior with changing locales. In particular,
|
49 |
+
Turkish has distinct dotted and dotless variants of the Latin letter "I" in
|
50 |
+
both lowercase and uppercase. Thus, "i".upper() != "I" in a "tr" locale.
|
51 |
+
|
52 |
+
Parameters
|
53 |
+
----------
|
54 |
+
s : str
|
55 |
+
|
56 |
+
Returns
|
57 |
+
-------
|
58 |
+
uppered : str
|
59 |
+
|
60 |
+
Examples
|
61 |
+
--------
|
62 |
+
>>> from numpy.core.numerictypes import english_upper
|
63 |
+
>>> english_upper('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_')
|
64 |
+
'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
|
65 |
+
>>> english_upper('')
|
66 |
+
''
|
67 |
+
"""
|
68 |
+
uppered = s.translate(UPPER_TABLE)
|
69 |
+
return uppered
|
70 |
+
|
71 |
+
|
72 |
+
def english_capitalize(s):
|
73 |
+
""" Apply English case rules to convert the first character of an ASCII
|
74 |
+
string to upper case.
|
75 |
+
|
76 |
+
This is an internal utility function to replace calls to str.capitalize()
|
77 |
+
such that we can avoid changing behavior with changing locales.
|
78 |
+
|
79 |
+
Parameters
|
80 |
+
----------
|
81 |
+
s : str
|
82 |
+
|
83 |
+
Returns
|
84 |
+
-------
|
85 |
+
capitalized : str
|
86 |
+
|
87 |
+
Examples
|
88 |
+
--------
|
89 |
+
>>> from numpy.core.numerictypes import english_capitalize
|
90 |
+
>>> english_capitalize('int8')
|
91 |
+
'Int8'
|
92 |
+
>>> english_capitalize('Int8')
|
93 |
+
'Int8'
|
94 |
+
>>> english_capitalize('')
|
95 |
+
''
|
96 |
+
"""
|
97 |
+
if s:
|
98 |
+
return english_upper(s[0]) + s[1:]
|
99 |
+
else:
|
100 |
+
return s
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/cversions.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Simple script to compute the api hash of the current API.
|
2 |
+
|
3 |
+
The API has is defined by numpy_api_order and ufunc_api_order.
|
4 |
+
|
5 |
+
"""
|
6 |
+
from os.path import dirname
|
7 |
+
|
8 |
+
from code_generators.genapi import fullapi_hash
|
9 |
+
from code_generators.numpy_api import full_api
|
10 |
+
|
11 |
+
if __name__ == '__main__':
|
12 |
+
curdir = dirname(__file__)
|
13 |
+
print(fullapi_hash(full_api))
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/memmap.py
ADDED
@@ -0,0 +1,338 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from contextlib import nullcontext
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
from .._utils import set_module
|
5 |
+
from .numeric import uint8, ndarray, dtype
|
6 |
+
from numpy.compat import os_fspath, is_pathlib_path
|
7 |
+
|
8 |
+
__all__ = ['memmap']
|
9 |
+
|
10 |
+
dtypedescr = dtype
|
11 |
+
valid_filemodes = ["r", "c", "r+", "w+"]
|
12 |
+
writeable_filemodes = ["r+", "w+"]
|
13 |
+
|
14 |
+
mode_equivalents = {
|
15 |
+
"readonly":"r",
|
16 |
+
"copyonwrite":"c",
|
17 |
+
"readwrite":"r+",
|
18 |
+
"write":"w+"
|
19 |
+
}
|
20 |
+
|
21 |
+
|
22 |
+
@set_module('numpy')
|
23 |
+
class memmap(ndarray):
|
24 |
+
"""Create a memory-map to an array stored in a *binary* file on disk.
|
25 |
+
|
26 |
+
Memory-mapped files are used for accessing small segments of large files
|
27 |
+
on disk, without reading the entire file into memory. NumPy's
|
28 |
+
memmap's are array-like objects. This differs from Python's ``mmap``
|
29 |
+
module, which uses file-like objects.
|
30 |
+
|
31 |
+
This subclass of ndarray has some unpleasant interactions with
|
32 |
+
some operations, because it doesn't quite fit properly as a subclass.
|
33 |
+
An alternative to using this subclass is to create the ``mmap``
|
34 |
+
object yourself, then create an ndarray with ndarray.__new__ directly,
|
35 |
+
passing the object created in its 'buffer=' parameter.
|
36 |
+
|
37 |
+
This class may at some point be turned into a factory function
|
38 |
+
which returns a view into an mmap buffer.
|
39 |
+
|
40 |
+
Flush the memmap instance to write the changes to the file. Currently there
|
41 |
+
is no API to close the underlying ``mmap``. It is tricky to ensure the
|
42 |
+
resource is actually closed, since it may be shared between different
|
43 |
+
memmap instances.
|
44 |
+
|
45 |
+
|
46 |
+
Parameters
|
47 |
+
----------
|
48 |
+
filename : str, file-like object, or pathlib.Path instance
|
49 |
+
The file name or file object to be used as the array data buffer.
|
50 |
+
dtype : data-type, optional
|
51 |
+
The data-type used to interpret the file contents.
|
52 |
+
Default is `uint8`.
|
53 |
+
mode : {'r+', 'r', 'w+', 'c'}, optional
|
54 |
+
The file is opened in this mode:
|
55 |
+
|
56 |
+
+------+-------------------------------------------------------------+
|
57 |
+
| 'r' | Open existing file for reading only. |
|
58 |
+
+------+-------------------------------------------------------------+
|
59 |
+
| 'r+' | Open existing file for reading and writing. |
|
60 |
+
+------+-------------------------------------------------------------+
|
61 |
+
| 'w+' | Create or overwrite existing file for reading and writing. |
|
62 |
+
| | If ``mode == 'w+'`` then `shape` must also be specified. |
|
63 |
+
+------+-------------------------------------------------------------+
|
64 |
+
| 'c' | Copy-on-write: assignments affect data in memory, but |
|
65 |
+
| | changes are not saved to disk. The file on disk is |
|
66 |
+
| | read-only. |
|
67 |
+
+------+-------------------------------------------------------------+
|
68 |
+
|
69 |
+
Default is 'r+'.
|
70 |
+
offset : int, optional
|
71 |
+
In the file, array data starts at this offset. Since `offset` is
|
72 |
+
measured in bytes, it should normally be a multiple of the byte-size
|
73 |
+
of `dtype`. When ``mode != 'r'``, even positive offsets beyond end of
|
74 |
+
file are valid; The file will be extended to accommodate the
|
75 |
+
additional data. By default, ``memmap`` will start at the beginning of
|
76 |
+
the file, even if ``filename`` is a file pointer ``fp`` and
|
77 |
+
``fp.tell() != 0``.
|
78 |
+
shape : tuple, optional
|
79 |
+
The desired shape of the array. If ``mode == 'r'`` and the number
|
80 |
+
of remaining bytes after `offset` is not a multiple of the byte-size
|
81 |
+
of `dtype`, you must specify `shape`. By default, the returned array
|
82 |
+
will be 1-D with the number of elements determined by file size
|
83 |
+
and data-type.
|
84 |
+
order : {'C', 'F'}, optional
|
85 |
+
Specify the order of the ndarray memory layout:
|
86 |
+
:term:`row-major`, C-style or :term:`column-major`,
|
87 |
+
Fortran-style. This only has an effect if the shape is
|
88 |
+
greater than 1-D. The default order is 'C'.
|
89 |
+
|
90 |
+
Attributes
|
91 |
+
----------
|
92 |
+
filename : str or pathlib.Path instance
|
93 |
+
Path to the mapped file.
|
94 |
+
offset : int
|
95 |
+
Offset position in the file.
|
96 |
+
mode : str
|
97 |
+
File mode.
|
98 |
+
|
99 |
+
Methods
|
100 |
+
-------
|
101 |
+
flush
|
102 |
+
Flush any changes in memory to file on disk.
|
103 |
+
When you delete a memmap object, flush is called first to write
|
104 |
+
changes to disk.
|
105 |
+
|
106 |
+
|
107 |
+
See also
|
108 |
+
--------
|
109 |
+
lib.format.open_memmap : Create or load a memory-mapped ``.npy`` file.
|
110 |
+
|
111 |
+
Notes
|
112 |
+
-----
|
113 |
+
The memmap object can be used anywhere an ndarray is accepted.
|
114 |
+
Given a memmap ``fp``, ``isinstance(fp, numpy.ndarray)`` returns
|
115 |
+
``True``.
|
116 |
+
|
117 |
+
Memory-mapped files cannot be larger than 2GB on 32-bit systems.
|
118 |
+
|
119 |
+
When a memmap causes a file to be created or extended beyond its
|
120 |
+
current size in the filesystem, the contents of the new part are
|
121 |
+
unspecified. On systems with POSIX filesystem semantics, the extended
|
122 |
+
part will be filled with zero bytes.
|
123 |
+
|
124 |
+
Examples
|
125 |
+
--------
|
126 |
+
>>> data = np.arange(12, dtype='float32')
|
127 |
+
>>> data.resize((3,4))
|
128 |
+
|
129 |
+
This example uses a temporary file so that doctest doesn't write
|
130 |
+
files to your directory. You would use a 'normal' filename.
|
131 |
+
|
132 |
+
>>> from tempfile import mkdtemp
|
133 |
+
>>> import os.path as path
|
134 |
+
>>> filename = path.join(mkdtemp(), 'newfile.dat')
|
135 |
+
|
136 |
+
Create a memmap with dtype and shape that matches our data:
|
137 |
+
|
138 |
+
>>> fp = np.memmap(filename, dtype='float32', mode='w+', shape=(3,4))
|
139 |
+
>>> fp
|
140 |
+
memmap([[0., 0., 0., 0.],
|
141 |
+
[0., 0., 0., 0.],
|
142 |
+
[0., 0., 0., 0.]], dtype=float32)
|
143 |
+
|
144 |
+
Write data to memmap array:
|
145 |
+
|
146 |
+
>>> fp[:] = data[:]
|
147 |
+
>>> fp
|
148 |
+
memmap([[ 0., 1., 2., 3.],
|
149 |
+
[ 4., 5., 6., 7.],
|
150 |
+
[ 8., 9., 10., 11.]], dtype=float32)
|
151 |
+
|
152 |
+
>>> fp.filename == path.abspath(filename)
|
153 |
+
True
|
154 |
+
|
155 |
+
Flushes memory changes to disk in order to read them back
|
156 |
+
|
157 |
+
>>> fp.flush()
|
158 |
+
|
159 |
+
Load the memmap and verify data was stored:
|
160 |
+
|
161 |
+
>>> newfp = np.memmap(filename, dtype='float32', mode='r', shape=(3,4))
|
162 |
+
>>> newfp
|
163 |
+
memmap([[ 0., 1., 2., 3.],
|
164 |
+
[ 4., 5., 6., 7.],
|
165 |
+
[ 8., 9., 10., 11.]], dtype=float32)
|
166 |
+
|
167 |
+
Read-only memmap:
|
168 |
+
|
169 |
+
>>> fpr = np.memmap(filename, dtype='float32', mode='r', shape=(3,4))
|
170 |
+
>>> fpr.flags.writeable
|
171 |
+
False
|
172 |
+
|
173 |
+
Copy-on-write memmap:
|
174 |
+
|
175 |
+
>>> fpc = np.memmap(filename, dtype='float32', mode='c', shape=(3,4))
|
176 |
+
>>> fpc.flags.writeable
|
177 |
+
True
|
178 |
+
|
179 |
+
It's possible to assign to copy-on-write array, but values are only
|
180 |
+
written into the memory copy of the array, and not written to disk:
|
181 |
+
|
182 |
+
>>> fpc
|
183 |
+
memmap([[ 0., 1., 2., 3.],
|
184 |
+
[ 4., 5., 6., 7.],
|
185 |
+
[ 8., 9., 10., 11.]], dtype=float32)
|
186 |
+
>>> fpc[0,:] = 0
|
187 |
+
>>> fpc
|
188 |
+
memmap([[ 0., 0., 0., 0.],
|
189 |
+
[ 4., 5., 6., 7.],
|
190 |
+
[ 8., 9., 10., 11.]], dtype=float32)
|
191 |
+
|
192 |
+
File on disk is unchanged:
|
193 |
+
|
194 |
+
>>> fpr
|
195 |
+
memmap([[ 0., 1., 2., 3.],
|
196 |
+
[ 4., 5., 6., 7.],
|
197 |
+
[ 8., 9., 10., 11.]], dtype=float32)
|
198 |
+
|
199 |
+
Offset into a memmap:
|
200 |
+
|
201 |
+
>>> fpo = np.memmap(filename, dtype='float32', mode='r', offset=16)
|
202 |
+
>>> fpo
|
203 |
+
memmap([ 4., 5., 6., 7., 8., 9., 10., 11.], dtype=float32)
|
204 |
+
|
205 |
+
"""
|
206 |
+
|
207 |
+
__array_priority__ = -100.0
|
208 |
+
|
209 |
+
def __new__(subtype, filename, dtype=uint8, mode='r+', offset=0,
|
210 |
+
shape=None, order='C'):
|
211 |
+
# Import here to minimize 'import numpy' overhead
|
212 |
+
import mmap
|
213 |
+
import os.path
|
214 |
+
try:
|
215 |
+
mode = mode_equivalents[mode]
|
216 |
+
except KeyError as e:
|
217 |
+
if mode not in valid_filemodes:
|
218 |
+
raise ValueError(
|
219 |
+
"mode must be one of {!r} (got {!r})"
|
220 |
+
.format(valid_filemodes + list(mode_equivalents.keys()), mode)
|
221 |
+
) from None
|
222 |
+
|
223 |
+
if mode == 'w+' and shape is None:
|
224 |
+
raise ValueError("shape must be given if mode == 'w+'")
|
225 |
+
|
226 |
+
if hasattr(filename, 'read'):
|
227 |
+
f_ctx = nullcontext(filename)
|
228 |
+
else:
|
229 |
+
f_ctx = open(os_fspath(filename), ('r' if mode == 'c' else mode)+'b')
|
230 |
+
|
231 |
+
with f_ctx as fid:
|
232 |
+
fid.seek(0, 2)
|
233 |
+
flen = fid.tell()
|
234 |
+
descr = dtypedescr(dtype)
|
235 |
+
_dbytes = descr.itemsize
|
236 |
+
|
237 |
+
if shape is None:
|
238 |
+
bytes = flen - offset
|
239 |
+
if bytes % _dbytes:
|
240 |
+
raise ValueError("Size of available data is not a "
|
241 |
+
"multiple of the data-type size.")
|
242 |
+
size = bytes // _dbytes
|
243 |
+
shape = (size,)
|
244 |
+
else:
|
245 |
+
if not isinstance(shape, tuple):
|
246 |
+
shape = (shape,)
|
247 |
+
size = np.intp(1) # avoid default choice of np.int_, which might overflow
|
248 |
+
for k in shape:
|
249 |
+
size *= k
|
250 |
+
|
251 |
+
bytes = int(offset + size*_dbytes)
|
252 |
+
|
253 |
+
if mode in ('w+', 'r+') and flen < bytes:
|
254 |
+
fid.seek(bytes - 1, 0)
|
255 |
+
fid.write(b'\0')
|
256 |
+
fid.flush()
|
257 |
+
|
258 |
+
if mode == 'c':
|
259 |
+
acc = mmap.ACCESS_COPY
|
260 |
+
elif mode == 'r':
|
261 |
+
acc = mmap.ACCESS_READ
|
262 |
+
else:
|
263 |
+
acc = mmap.ACCESS_WRITE
|
264 |
+
|
265 |
+
start = offset - offset % mmap.ALLOCATIONGRANULARITY
|
266 |
+
bytes -= start
|
267 |
+
array_offset = offset - start
|
268 |
+
mm = mmap.mmap(fid.fileno(), bytes, access=acc, offset=start)
|
269 |
+
|
270 |
+
self = ndarray.__new__(subtype, shape, dtype=descr, buffer=mm,
|
271 |
+
offset=array_offset, order=order)
|
272 |
+
self._mmap = mm
|
273 |
+
self.offset = offset
|
274 |
+
self.mode = mode
|
275 |
+
|
276 |
+
if is_pathlib_path(filename):
|
277 |
+
# special case - if we were constructed with a pathlib.path,
|
278 |
+
# then filename is a path object, not a string
|
279 |
+
self.filename = filename.resolve()
|
280 |
+
elif hasattr(fid, "name") and isinstance(fid.name, str):
|
281 |
+
# py3 returns int for TemporaryFile().name
|
282 |
+
self.filename = os.path.abspath(fid.name)
|
283 |
+
# same as memmap copies (e.g. memmap + 1)
|
284 |
+
else:
|
285 |
+
self.filename = None
|
286 |
+
|
287 |
+
return self
|
288 |
+
|
289 |
+
def __array_finalize__(self, obj):
|
290 |
+
if hasattr(obj, '_mmap') and np.may_share_memory(self, obj):
|
291 |
+
self._mmap = obj._mmap
|
292 |
+
self.filename = obj.filename
|
293 |
+
self.offset = obj.offset
|
294 |
+
self.mode = obj.mode
|
295 |
+
else:
|
296 |
+
self._mmap = None
|
297 |
+
self.filename = None
|
298 |
+
self.offset = None
|
299 |
+
self.mode = None
|
300 |
+
|
301 |
+
def flush(self):
|
302 |
+
"""
|
303 |
+
Write any changes in the array to the file on disk.
|
304 |
+
|
305 |
+
For further information, see `memmap`.
|
306 |
+
|
307 |
+
Parameters
|
308 |
+
----------
|
309 |
+
None
|
310 |
+
|
311 |
+
See Also
|
312 |
+
--------
|
313 |
+
memmap
|
314 |
+
|
315 |
+
"""
|
316 |
+
if self.base is not None and hasattr(self.base, 'flush'):
|
317 |
+
self.base.flush()
|
318 |
+
|
319 |
+
def __array_wrap__(self, arr, context=None):
|
320 |
+
arr = super().__array_wrap__(arr, context)
|
321 |
+
|
322 |
+
# Return a memmap if a memmap was given as the output of the
|
323 |
+
# ufunc. Leave the arr class unchanged if self is not a memmap
|
324 |
+
# to keep original memmap subclasses behavior
|
325 |
+
if self is arr or type(self) is not memmap:
|
326 |
+
return arr
|
327 |
+
# Return scalar instead of 0d memmap, e.g. for np.sum with
|
328 |
+
# axis=None
|
329 |
+
if arr.shape == ():
|
330 |
+
return arr[()]
|
331 |
+
# Return ndarray otherwise
|
332 |
+
return arr.view(np.ndarray)
|
333 |
+
|
334 |
+
def __getitem__(self, index):
|
335 |
+
res = super().__getitem__(index)
|
336 |
+
if type(res) is memmap and res._mmap is None:
|
337 |
+
return res.view(type=ndarray)
|
338 |
+
return res
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_array_interface.cpython-310.pyc
ADDED
Binary file (6.03 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_casting_unittests.cpython-310.pyc
ADDED
Binary file (20.5 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_conversion_utils.cpython-310.pyc
ADDED
Binary file (8.31 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_cpu_dispatcher.cpython-310.pyc
ADDED
Binary file (1.05 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_cython.cpython-310.pyc
ADDED
Binary file (3.59 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_deprecations.cpython-310.pyc
ADDED
Binary file (39.1 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_dlpack.cpython-310.pyc
ADDED
Binary file (5.05 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_einsum.cpython-310.pyc
ADDED
Binary file (35 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_hashtable.cpython-310.pyc
ADDED
Binary file (1.1 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_longdouble.cpython-310.pyc
ADDED
Binary file (13 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_mem_overlap.cpython-310.pyc
ADDED
Binary file (25.8 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_mem_policy.cpython-310.pyc
ADDED
Binary file (13.3 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_scalar_ctors.cpython-310.pyc
ADDED
Binary file (6.8 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_scalarinherit.cpython-310.pyc
ADDED
Binary file (3.69 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_scalarprint.cpython-310.pyc
ADDED
Binary file (11.6 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_simd_module.cpython-310.pyc
ADDED
Binary file (4.02 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_ufunc.cpython-310.pyc
ADDED
Binary file (98.9 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_umath_complex.cpython-310.pyc
ADDED
Binary file (17.1 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/__pycache__/test_unicode.cpython-310.pyc
ADDED
Binary file (10.8 kB). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/examples/cython/__pycache__/setup.cpython-310.pyc
ADDED
Binary file (712 Bytes). View file
|
|
infer_4_47_1/lib/python3.10/site-packages/numpy/core/tests/examples/cython/checks.pyx
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#cython: language_level=3
|
2 |
+
|
3 |
+
"""
|
4 |
+
Functions in this module give python-space wrappers for cython functions
|
5 |
+
exposed in numpy/__init__.pxd, so they can be tested in test_cython.py
|
6 |
+
"""
|
7 |
+
cimport numpy as cnp
|
8 |
+
cnp.import_array()
|
9 |
+
|
10 |
+
|
11 |
+
def is_td64(obj):
|
12 |
+
return cnp.is_timedelta64_object(obj)
|
13 |
+
|
14 |
+
|
15 |
+
def is_dt64(obj):
|
16 |
+
return cnp.is_datetime64_object(obj)
|
17 |
+
|
18 |
+
|
19 |
+
def get_dt64_value(obj):
|
20 |
+
return cnp.get_datetime64_value(obj)
|
21 |
+
|
22 |
+
|
23 |
+
def get_td64_value(obj):
|
24 |
+
return cnp.get_timedelta64_value(obj)
|
25 |
+
|
26 |
+
|
27 |
+
def get_dt64_unit(obj):
|
28 |
+
return cnp.get_datetime64_unit(obj)
|
29 |
+
|
30 |
+
|
31 |
+
def is_integer(obj):
|
32 |
+
return isinstance(obj, (cnp.integer, int))
|
33 |
+
|
34 |
+
def conv_intp(cnp.intp_t val):
|
35 |
+
return val
|