rsarray 1.2.10__cp315-cp315-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rsarray/__init__.py +12028 -0
- rsarray/_core/__init__.py +213 -0
- rsarray/_core/_exceptions.py +83 -0
- rsarray/_core/_internal.py +331 -0
- rsarray/_core/_multiarray_tests.py +129 -0
- rsarray/_core/_multiarray_umath.py +121 -0
- rsarray/_core/_rational_tests.py +52 -0
- rsarray/_core/arrayprint.py +36 -0
- rsarray/_core/fromnumeric.py +31 -0
- rsarray/_core/function_base.py +84 -0
- rsarray/_core/multiarray.py +385 -0
- rsarray/_core/numeric.py +28 -0
- rsarray/_core/numerictypes.py +233 -0
- rsarray/_core/records.py +14 -0
- rsarray/_core/shape_base.py +23 -0
- rsarray/_core/strings.py +78 -0
- rsarray/_core/umath.py +176 -0
- rsarray/_dtypes.py +1229 -0
- rsarray/_extra.py +6621 -0
- rsarray/_typing.py +24 -0
- rsarray/array_methods.py +1224 -0
- rsarray/array_ops.py +1585 -0
- rsarray/char.py +1590 -0
- rsarray/exceptions.py +45 -0
- rsarray/fft/__init__.py +231 -0
- rsarray/io.py +453 -0
- rsarray/lib/__init__.py +102 -0
- rsarray/lib/_function_base_impl.py +37 -0
- rsarray/lib/_index_tricks_impl.py +98 -0
- rsarray/lib/_nanfunctions_impl.py +1637 -0
- rsarray/lib/_stride_tricks_impl.py +110 -0
- rsarray/lib/recfunctions.py +1222 -0
- rsarray/lib/stride_tricks.py +106 -0
- rsarray/linalg/__init__.py +2603 -0
- rsarray/linalg/_linalg.py +91 -0
- rsarray/linalg/_pylinalg.py +1008 -0
- rsarray/linalg/lapack_lite.py +13 -0
- rsarray/ma/__init__.py +92 -0
- rsarray/ma/core.py +1385 -0
- rsarray/ma/extras.py +711 -0
- rsarray/ma/mrecords.py +144 -0
- rsarray/math_functions.py +965 -0
- rsarray/matlib.py +67 -0
- rsarray/num_core.cp315-win_amd64.pyd +0 -0
- rsarray/polynomial/__init__.py +148 -0
- rsarray/polynomial/_npcompat.py +721 -0
- rsarray/polynomial/polynomial.py +1082 -0
- rsarray/random/__init__.py +2266 -0
- rsarray/random/_distributions.py +773 -0
- rsarray/rec.py +207 -0
- rsarray/statistics.py +1360 -0
- rsarray/strings.py +349 -0
- rsarray/typing/__init__.py +26 -0
- rsarray-1.2.10.dist-info/METADATA +280 -0
- rsarray-1.2.10.dist-info/RECORD +59 -0
- rsarray-1.2.10.dist-info/WHEEL +4 -0
- rsarray-1.2.10.dist-info/licenses/LICENSE +21 -0
- rsarray-1.2.10.dist-info/licenses/NOTICE +7 -0
- rsarray-1.2.10.dist-info/sboms/rsarray.cyclonedx.json +2789 -0
rsarray/matlib.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
"""rsarray.matlib - 矩阵库模块"""
|
|
3
|
+
|
|
4
|
+
import rsarray.num_core as _core
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def empty(shape, dtype=None, order='C'):
|
|
8
|
+
"""返回一个新的未初始化矩阵。"""
|
|
9
|
+
from .__init__ import ndarray
|
|
10
|
+
if isinstance(shape, int):
|
|
11
|
+
shape = (shape, shape)
|
|
12
|
+
raw = _core.random.uniform(0.0, 1e-300, shape)
|
|
13
|
+
return ndarray(raw, _dtype=dtype or 'float64')
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def zeros(shape, dtype=None, order='C'):
|
|
17
|
+
"""创建一个以 0 填充的矩阵。"""
|
|
18
|
+
from .__init__ import ndarray
|
|
19
|
+
if isinstance(shape, int):
|
|
20
|
+
shape = (shape, shape)
|
|
21
|
+
return ndarray(_core.zeros(shape), _dtype=dtype or 'float64')
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def ones(shape, dtype=None, order='C'):
|
|
25
|
+
"""创建一个以 1 填充的矩阵。"""
|
|
26
|
+
from .__init__ import ndarray
|
|
27
|
+
if isinstance(shape, int):
|
|
28
|
+
shape = (shape, shape)
|
|
29
|
+
return ndarray(_core.ones(shape), _dtype=dtype or 'float64')
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def eye(n, M=None, k=0, dtype=None, order='C'):
|
|
33
|
+
"""返回一个矩阵,对角线元素为 1,其他位置为零。"""
|
|
34
|
+
from .__init__ import ndarray
|
|
35
|
+
if M is None:
|
|
36
|
+
M = n
|
|
37
|
+
return ndarray(_core.eye(n, M, k), _dtype=dtype or 'float64')
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def identity(n, dtype=None):
|
|
41
|
+
"""返回给定大小的单位矩阵。"""
|
|
42
|
+
return eye(n, n, 0, dtype)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def rand(*args):
|
|
46
|
+
"""创建一个给定大小的矩阵,数据是随机填充的。"""
|
|
47
|
+
from .__init__ import ndarray
|
|
48
|
+
if len(args) == 1:
|
|
49
|
+
shape = (args[0], args[0])
|
|
50
|
+
else:
|
|
51
|
+
shape = args
|
|
52
|
+
raw = _core.random.rand(*shape)
|
|
53
|
+
return ndarray(raw, _dtype='float64')
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def randn(*args):
|
|
57
|
+
"""生成指定形状的标准正态分布随机矩阵。"""
|
|
58
|
+
from .__init__ import ndarray
|
|
59
|
+
if len(args) == 1:
|
|
60
|
+
shape = (args[0], args[0])
|
|
61
|
+
else:
|
|
62
|
+
shape = args
|
|
63
|
+
raw = _core.random.randn(*shape)
|
|
64
|
+
return ndarray(raw, _dtype='float64')
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
__all__ = ['empty', 'zeros', 'ones', 'eye', 'identity', 'rand', 'randn']
|
|
Binary file
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
"""多项式模块 - 所有实现位于 Rust,这里仅保留薄包装。"""
|
|
3
|
+
|
|
4
|
+
import rsarray.num_core as _core
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _nd():
|
|
8
|
+
from ..__init__ import ndarray as _n
|
|
9
|
+
return _n
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _wrap(result):
|
|
13
|
+
return _nd()(result)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _ensure_raw(a):
|
|
17
|
+
if hasattr(a, '_array'):
|
|
18
|
+
return a._array
|
|
19
|
+
return _core.ndarray(a)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Poly:
|
|
23
|
+
"""多项式类(系数为 标准兼容的高到低排列)。"""
|
|
24
|
+
|
|
25
|
+
def __init__(self, coef):
|
|
26
|
+
nd = _nd()
|
|
27
|
+
arr = nd(coef)
|
|
28
|
+
self.coef = arr.ravel().tolist()
|
|
29
|
+
self._coef_raw = _ensure_raw(arr)
|
|
30
|
+
|
|
31
|
+
def __call__(self, x):
|
|
32
|
+
"""计算多项式在 x 处的值。"""
|
|
33
|
+
nd = _nd()
|
|
34
|
+
x_arr = nd(x) if not hasattr(x, '_array') else x
|
|
35
|
+
return _wrap(_core.polyval_rs(self._coef_raw, x_arr._array))
|
|
36
|
+
|
|
37
|
+
def __repr__(self):
|
|
38
|
+
deg = len(self.coef) - 1
|
|
39
|
+
|
|
40
|
+
# 抽取单项式格式化逻辑为闭包,便于列表推导复用
|
|
41
|
+
def _term(i, c):
|
|
42
|
+
if c == 0:
|
|
43
|
+
return None
|
|
44
|
+
p = deg - i
|
|
45
|
+
if p == 0:
|
|
46
|
+
return str(c)
|
|
47
|
+
if p == 1:
|
|
48
|
+
return str(c) + "x"
|
|
49
|
+
return str(c) + "x^" + str(p)
|
|
50
|
+
|
|
51
|
+
# 列表推导 + 过滤 None
|
|
52
|
+
terms = [t for t in (_term(i, c) for i, c in enumerate(self.coef)) if t is not None]
|
|
53
|
+
if not terms:
|
|
54
|
+
return "Poly([0.])"
|
|
55
|
+
return "Poly([" + ", ".join(terms) + "])"
|
|
56
|
+
|
|
57
|
+
def __add__(self, other):
|
|
58
|
+
if isinstance(other, Poly):
|
|
59
|
+
return Poly(_wrap(_core.polyadd(self._coef_raw, other._coef_raw)))
|
|
60
|
+
return Poly(_wrap(_core.polyadd(self._coef_raw, _core.ndarray([float(other)]))))
|
|
61
|
+
|
|
62
|
+
def __sub__(self, other):
|
|
63
|
+
if isinstance(other, Poly):
|
|
64
|
+
return Poly(_wrap(_core.polysub(self._coef_raw, other._coef_raw)))
|
|
65
|
+
return Poly(_wrap(_core.polysub(self._coef_raw, _core.ndarray([float(other)]))))
|
|
66
|
+
|
|
67
|
+
def __mul__(self, other):
|
|
68
|
+
if isinstance(other, Poly):
|
|
69
|
+
return Poly(_wrap(_core.polymul(self._coef_raw, other._coef_raw)))
|
|
70
|
+
return Poly(_wrap(_core.polymul(self._coef_raw, _core.ndarray([float(other)]))))
|
|
71
|
+
|
|
72
|
+
def __getitem__(self, index):
|
|
73
|
+
"""支持索引访问系数。"""
|
|
74
|
+
return self.coef[index]
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def degree(self):
|
|
78
|
+
"""多项式的次数。"""
|
|
79
|
+
return len(self.coef) - 1
|
|
80
|
+
|
|
81
|
+
def deriv(self, m=1):
|
|
82
|
+
"""计算多项式的 m 阶导数。"""
|
|
83
|
+
return Poly(_wrap(_core.polyder_rs(self._coef_raw, m)).ravel().tolist())
|
|
84
|
+
|
|
85
|
+
def integ(self, m=1, k=0):
|
|
86
|
+
"""计算多项式的不定积分。"""
|
|
87
|
+
return Poly(_wrap(_core.polyint_rs(self._coef_raw, m, k)).ravel().tolist())
|
|
88
|
+
|
|
89
|
+
def roots(self):
|
|
90
|
+
"""计算多项式的根。"""
|
|
91
|
+
return _wrap(_core.polyroots_rs(self._coef_raw))
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def polyval(p, x):
|
|
95
|
+
"""计算多项式在 x 处的值。"""
|
|
96
|
+
if isinstance(p, Poly):
|
|
97
|
+
return p(x)
|
|
98
|
+
return Poly(p)(x)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def polyfit(x, y, deg):
|
|
102
|
+
"""多项式拟合(最小二乘法)。"""
|
|
103
|
+
nd = _nd()
|
|
104
|
+
x_arr = nd(x) if not hasattr(x, '_array') else x
|
|
105
|
+
y_arr = nd(y) if not hasattr(y, '_array') else y
|
|
106
|
+
coef = _wrap(_core.polyfit_rs(x_arr._array, y_arr._array, deg))
|
|
107
|
+
return Poly(coef)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def polyder(p, m=1):
|
|
111
|
+
"""计算多项式的导数。"""
|
|
112
|
+
if isinstance(p, Poly):
|
|
113
|
+
return p.deriv(m)
|
|
114
|
+
return Poly(p).deriv(m)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def polyint(p, m=1, k=0):
|
|
118
|
+
"""计算多项式的不定积分。"""
|
|
119
|
+
if isinstance(p, Poly):
|
|
120
|
+
return p.integ(m, k)
|
|
121
|
+
return Poly(p).integ(m, k)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def polyroots(p):
|
|
125
|
+
"""计算多项式的根。"""
|
|
126
|
+
# 系数含 NaN:伴矩阵特征值未定义,与标准语义一致抛 LinAlgError
|
|
127
|
+
import math
|
|
128
|
+
_c = (p.coef if isinstance(p, Poly)
|
|
129
|
+
else (p.tolist() if hasattr(p, 'tolist') else list(p)))
|
|
130
|
+
def _has_nan(v):
|
|
131
|
+
if isinstance(v, (list, tuple)):
|
|
132
|
+
return any(_has_nan(x) for x in v)
|
|
133
|
+
try:
|
|
134
|
+
return math.isnan(float(v))
|
|
135
|
+
except (TypeError, ValueError, OverflowError):
|
|
136
|
+
return False
|
|
137
|
+
if any(_has_nan(v) for v in _c):
|
|
138
|
+
import rsarray as _np
|
|
139
|
+
raise _np.linalg.LinAlgError(
|
|
140
|
+
"Eigenvalues did not converge: coefficients contain NaN")
|
|
141
|
+
if isinstance(p, Poly):
|
|
142
|
+
return p.roots()
|
|
143
|
+
return Poly(p).roots()
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
# 2.x:np.polynomial.Polynomial 为新 API 类(升序系数);
|
|
147
|
+
# 旧 Poly(降序)保留供顶层旧 API 使用
|
|
148
|
+
from .polynomial import Polynomial # noqa: E402,F401
|