rsarray 1.2.10__cp313-cp313-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.cp313-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/strings.py
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
"""strings 风格字符串 API(np.strings.*)。
|
|
3
|
+
|
|
4
|
+
与 2.x 的 ``np.strings`` 命名空间对齐:逐元素作用于
|
|
5
|
+
定宽字符串数组(U/S dtype),多数实现复用 ``rsarray.char`` 的
|
|
6
|
+
底层算子,其余为等价的 Python 实现。
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import builtins
|
|
10
|
+
|
|
11
|
+
from . import char as _char
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _py_str(fn, a, *args):
|
|
15
|
+
"""逐元素字符串映射(Python 实现兜底)。"""
|
|
16
|
+
vals = _char._decode(_char._get_strings(a))
|
|
17
|
+
out = []
|
|
18
|
+
for v in vals:
|
|
19
|
+
try:
|
|
20
|
+
out.append(fn(v, *args))
|
|
21
|
+
except Exception:
|
|
22
|
+
out.append('')
|
|
23
|
+
shape = _char._shape_of(a)
|
|
24
|
+
if isinstance(a, (str, bytes)) or shape == ():
|
|
25
|
+
return out[0] if out else ''
|
|
26
|
+
return _char._wrap_str(out, shape, _char._a_was_bytes(a))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _py_int(fn, a):
|
|
30
|
+
"""逐元素整数值(数组语义:标量输入 → 0-d int 数组)。"""
|
|
31
|
+
vals = _char._decode(_char._get_strings(a))
|
|
32
|
+
out = []
|
|
33
|
+
for v in vals:
|
|
34
|
+
try:
|
|
35
|
+
out.append(fn(v))
|
|
36
|
+
except Exception:
|
|
37
|
+
out.append(-1)
|
|
38
|
+
shape = _char._shape_of(a)
|
|
39
|
+
from .__init__ import ndarray
|
|
40
|
+
if isinstance(a, (str, bytes)) or shape == ():
|
|
41
|
+
res = ndarray([float(out[0]) if out else -1], _dtype='int64')
|
|
42
|
+
return res.reshape(())
|
|
43
|
+
return ndarray([float(v) for v in out], _dtype='int64').reshape(shape)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _py_bool(fn, a):
|
|
47
|
+
"""逐元素布尔(数组语义:标量输入 → 0-d bool 数组)。"""
|
|
48
|
+
vals = _char._decode(_char._get_strings(a))
|
|
49
|
+
out = []
|
|
50
|
+
for v in vals:
|
|
51
|
+
try:
|
|
52
|
+
out.append(bool(fn(v)))
|
|
53
|
+
except Exception:
|
|
54
|
+
out.append(False)
|
|
55
|
+
shape = _char._shape_of(a)
|
|
56
|
+
from .__init__ import ndarray
|
|
57
|
+
if isinstance(a, (str, bytes)) or shape == ():
|
|
58
|
+
res = ndarray([1.0 if (out and out[0]) else 0.0])
|
|
59
|
+
res._dtype = 'bool'
|
|
60
|
+
return res.reshape(())
|
|
61
|
+
res = ndarray([1.0 if v else 0.0 for v in out])
|
|
62
|
+
res._dtype = 'bool'
|
|
63
|
+
return res.reshape(shape)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# ==================== 委托 np.char(同语义) ====================
|
|
67
|
+
|
|
68
|
+
add = _char.add
|
|
69
|
+
multiply = _char.multiply
|
|
70
|
+
capitalize = _char.capitalize
|
|
71
|
+
title = _char.title
|
|
72
|
+
lower = _char.lower
|
|
73
|
+
upper = _char.upper
|
|
74
|
+
swapcase = _char.swapcase
|
|
75
|
+
center = _char.center
|
|
76
|
+
ljust = _char.ljust
|
|
77
|
+
rjust = _char.rjust
|
|
78
|
+
zfill = _char.zfill
|
|
79
|
+
strip = _char.strip
|
|
80
|
+
lstrip = _char.lstrip
|
|
81
|
+
rstrip = _char.rstrip
|
|
82
|
+
replace = _char.replace
|
|
83
|
+
expandtabs = _char.expandtabs
|
|
84
|
+
find = _char.find
|
|
85
|
+
rfind = _char.rfind
|
|
86
|
+
index = _char.index
|
|
87
|
+
rindex = _char.rindex
|
|
88
|
+
count = _char.count
|
|
89
|
+
startswith = _char.startswith
|
|
90
|
+
endswith = _char.endswith
|
|
91
|
+
equal = _char.equal
|
|
92
|
+
not_equal = _char.not_equal
|
|
93
|
+
greater = _char.greater
|
|
94
|
+
greater_equal = _char.greater_equal
|
|
95
|
+
less = _char.less
|
|
96
|
+
less_equal = _char.less_equal
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
# ==================== Python 实现 ====================
|
|
100
|
+
|
|
101
|
+
def str_len(a):
|
|
102
|
+
"""逐元素字符串长度(strings.str_len 兼容)。"""
|
|
103
|
+
return _py_int(len, a)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def decode(a, encoding='utf-8', errors='strict'):
|
|
107
|
+
"""bytes 数组 → unicode 数组(strings.decode 兼容)。"""
|
|
108
|
+
vals = _char._get_strings(a)
|
|
109
|
+
was_bytes = _char._a_was_bytes(a)
|
|
110
|
+
if was_bytes:
|
|
111
|
+
out = []
|
|
112
|
+
for v in vals:
|
|
113
|
+
try:
|
|
114
|
+
out.append(v.decode(encoding, errors) if isinstance(v, (bytes, bytearray))
|
|
115
|
+
else str(v))
|
|
116
|
+
except Exception:
|
|
117
|
+
out.append('')
|
|
118
|
+
else:
|
|
119
|
+
out = _char._decode(vals)
|
|
120
|
+
shape = _char._shape_of(a)
|
|
121
|
+
if isinstance(a, (str, bytes)) or shape == ():
|
|
122
|
+
return out[0] if out else ''
|
|
123
|
+
return _char._wrap_str(out, shape, False)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def encode(a, encoding='utf-8', errors='strict'):
|
|
127
|
+
"""unicode 数组 → bytes 数组(strings.encode 兼容)。"""
|
|
128
|
+
vals = _char._decode(_char._get_strings(a))
|
|
129
|
+
out = [v.encode(encoding, errors) for v in vals]
|
|
130
|
+
shape = _char._shape_of(a)
|
|
131
|
+
if isinstance(a, (str, bytes)) or shape == ():
|
|
132
|
+
return out[0] if out else b''
|
|
133
|
+
return _char._wrap_str(out, shape, True)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _partition_impl(a, sep, right=False):
|
|
137
|
+
"""partition/rpartition 共用实现。
|
|
138
|
+
|
|
139
|
+
数组语义:sep 支持标量/数组(逐元素广播);ArrayLike 输入经
|
|
140
|
+
__array__ 转换(恰好一次);S dtype 按 bytes 语义往返。"""
|
|
141
|
+
from rsarray import asarray as _asarray
|
|
142
|
+
_a = _asarray(a) if not hasattr(a, '_raw_data') else a
|
|
143
|
+
_s = _asarray(sep) if not hasattr(sep, '_raw_data') else sep
|
|
144
|
+
vals = _char._decode(_char._get_strings(_a))
|
|
145
|
+
a_shape = _char._shape_of(_a)
|
|
146
|
+
seps_flat = _char._decode(_char._get_strings(_s))
|
|
147
|
+
s_shape = _char._shape_of(_s)
|
|
148
|
+
bshape = _char._bc_shapes([a_shape, s_shape])
|
|
149
|
+
wb = _char._a_was_bytes(_a)
|
|
150
|
+
heads, seps, tails = [], [], []
|
|
151
|
+
for idx in _char._iter_indices(bshape):
|
|
152
|
+
v = _char._bc_get(vals, a_shape, idx)
|
|
153
|
+
s = _char._bc_get(seps_flat, s_shape, idx)
|
|
154
|
+
h, m, t = v.rpartition(s) if right else v.partition(s)
|
|
155
|
+
heads.append(h)
|
|
156
|
+
seps.append(m)
|
|
157
|
+
tails.append(t)
|
|
158
|
+
if isinstance(a, (str, bytes)):
|
|
159
|
+
# 仅裸标量输入返回裸字符串;0-d 数组输入返回数组(数组语义)
|
|
160
|
+
return (heads[0] if heads else '', seps[0] if seps else '',
|
|
161
|
+
tails[0] if tails else '')
|
|
162
|
+
_r1 = _char._wrap_str(heads, bshape, wb)
|
|
163
|
+
_r2 = _char._wrap_str(seps, bshape, wb)
|
|
164
|
+
_r3 = _char._wrap_str(tails, bshape, wb)
|
|
165
|
+
# T(StringDType 变长)标记传播:输入为 T 时结果保持 T dtype
|
|
166
|
+
# (ArrayLike 经 __array__ 转换后才是 T 数组,须查转换后的 _a)
|
|
167
|
+
if getattr(_a, '_strdtype_T', False):
|
|
168
|
+
for _r in (_r1, _r2, _r3):
|
|
169
|
+
if hasattr(_r, '_str_dtype'):
|
|
170
|
+
_r._strdtype_T = True
|
|
171
|
+
return (_r1, _r2, _r3)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def partition(a, sep=' '):
|
|
175
|
+
"""按 sep 拆三段,返回 (head, sep, tail) 数组元组。"""
|
|
176
|
+
return _partition_impl(a, sep, right=False)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def rpartition(a, sep=' '):
|
|
180
|
+
"""从右侧按 sep 拆三段(strings.rpartition 兼容)。"""
|
|
181
|
+
return _partition_impl(a, sep, right=True)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
class _NoValue:
|
|
185
|
+
"""_NoValue 等价哨兵:区分 stop 缺省与 None。"""
|
|
186
|
+
def __repr__(self):
|
|
187
|
+
return '_NoValue'
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
_NoValue = _NoValue()
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _slice_kind_of(a):
|
|
194
|
+
"""字符串输入种类('S'/'U');非字符串输入返回 None(—no-loop)。"""
|
|
195
|
+
if isinstance(a, bytes):
|
|
196
|
+
return 'S'
|
|
197
|
+
if isinstance(a, str):
|
|
198
|
+
return 'U'
|
|
199
|
+
if isinstance(a, (builtins.list, builtins.tuple)):
|
|
200
|
+
flat = _char._flatten(builtins.list(a), [])
|
|
201
|
+
if not flat:
|
|
202
|
+
return None
|
|
203
|
+
if builtins.all(isinstance(v, bytes) for v in flat):
|
|
204
|
+
return 'S'
|
|
205
|
+
if builtins.all(isinstance(v, str) for v in flat):
|
|
206
|
+
return 'U'
|
|
207
|
+
return None
|
|
208
|
+
k = getattr(getattr(a, 'dtype', None), 'kind', None)
|
|
209
|
+
# T(StringDType 变长)按字符串输入处理
|
|
210
|
+
return k if k in ('S', 'U', 'T') else None
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def slice(a, start=None, stop=_NoValue, step=None, /): # noqa: A001
|
|
214
|
+
"""逐元素切片(strings.slice 语义):
|
|
215
|
+
|
|
216
|
+
- 仅给一个参数时该参解释为 stop(同 slice 对象构造规则);
|
|
217
|
+
- start/stop/step 支持整型标量/数组(广播到 a),字符串输入 →
|
|
218
|
+
"Cannot cast ufunc '_slice' input ..."(TypeError);
|
|
219
|
+
- None 按 step 符号逐元素展开为 intp 极值;step=0 → ValueError;
|
|
220
|
+
- 非字符串输入 → no-loop 错误(TypeError)。
|
|
221
|
+
"""
|
|
222
|
+
if _slice_kind_of(a) is None:
|
|
223
|
+
from rsarray._core._exceptions import _UFuncNoLoopError
|
|
224
|
+
raise _UFuncNoLoopError(
|
|
225
|
+
"'_slice' ufunc did not contain a loop with signature matching "
|
|
226
|
+
"types")
|
|
227
|
+
if stop is _NoValue:
|
|
228
|
+
stop = start
|
|
229
|
+
start = None
|
|
230
|
+
from rsarray._core import umath as _um
|
|
231
|
+
return _um._slice(a, start, stop, step)
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def translate(a, table):
|
|
235
|
+
"""逐元素字符映射(strings.translate 兼容)。"""
|
|
236
|
+
def _t(v):
|
|
237
|
+
try:
|
|
238
|
+
return v.translate(table)
|
|
239
|
+
except Exception:
|
|
240
|
+
return v
|
|
241
|
+
return _py_str(_t, a)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def mod(a, values):
|
|
245
|
+
"""逐元素 printf 风格格式化(strings.mod 兼容)。"""
|
|
246
|
+
vals = _char._decode(_char._get_strings(a))
|
|
247
|
+
if isinstance(values, (tuple, list)):
|
|
248
|
+
vlist = list(values)
|
|
249
|
+
else:
|
|
250
|
+
try:
|
|
251
|
+
vlist = list(values.tolist())
|
|
252
|
+
except Exception:
|
|
253
|
+
vlist = [values] * builtins_max(len(vals), 1)
|
|
254
|
+
out = []
|
|
255
|
+
for i, v in enumerate(vals):
|
|
256
|
+
vv = vlist[i % len(vlist)] if vlist else ''
|
|
257
|
+
try:
|
|
258
|
+
out.append(v % (vv if isinstance(vv, tuple) else (vv,)))
|
|
259
|
+
except Exception:
|
|
260
|
+
out.append('')
|
|
261
|
+
shape = _char._shape_of(a)
|
|
262
|
+
if isinstance(a, (str, bytes)) or shape == ():
|
|
263
|
+
return out[0] if out else ''
|
|
264
|
+
return _char._wrap_str(out, shape, _char._a_was_bytes(a))
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def builtins_max(x, y):
|
|
268
|
+
return x if x > y else y
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def _is(a, method):
|
|
272
|
+
return _py_bool(getattr(str, method), a)
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
isalnum = lambda a: _is(a, 'isalnum') # noqa: E731
|
|
276
|
+
isalpha = lambda a: _is(a, 'isalpha') # noqa: E731
|
|
277
|
+
def isdecimal(a):
|
|
278
|
+
# unicode-only:S(bytes)输入无 loop → no-loop 错误
|
|
279
|
+
_char._unicode_only_guard(a, 'isdecimal')
|
|
280
|
+
return _is(a, 'isdecimal')
|
|
281
|
+
isdigit = lambda a: _is(a, 'isdigit') # noqa: E731
|
|
282
|
+
islower = lambda a: _is(a, 'islower') # noqa: E731
|
|
283
|
+
def isnumeric(a):
|
|
284
|
+
# unicode-only:S(bytes)输入无 loop → no-loop 错误
|
|
285
|
+
_char._unicode_only_guard(a, 'isnumeric')
|
|
286
|
+
return _is(a, 'isnumeric')
|
|
287
|
+
isspace = lambda a: _is(a, 'isspace') # noqa: E731
|
|
288
|
+
istitle = lambda a: _is(a, 'istitle') # noqa: E731
|
|
289
|
+
isupper = lambda a: _is(a, 'isupper') # noqa: E731
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
__all__ = [
|
|
293
|
+
'add', 'capitalize', 'center', 'count', 'decode', 'encode',
|
|
294
|
+
'endswith', 'equal', 'expandtabs', 'find', 'greater',
|
|
295
|
+
'greater_equal', 'index', 'isalnum', 'isalpha', 'isdecimal',
|
|
296
|
+
'isdigit', 'islower', 'isnumeric', 'isspace', 'istitle',
|
|
297
|
+
'isupper', 'less', 'less_equal', 'ljust', 'lower', 'lstrip',
|
|
298
|
+
'mod', 'multiply', 'not_equal', 'partition', 'replace',
|
|
299
|
+
'rfind', 'rindex', 'rjust', 'rpartition', 'rstrip', 'slice',
|
|
300
|
+
'startswith', 'str_len', 'strip', 'swapcase', 'title',
|
|
301
|
+
'translate', 'upper', 'zfill',
|
|
302
|
+
]
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
# ==================== 协议分派(TestOverride) ====================
|
|
306
|
+
# strings 函数对实现 __array_function__ / __array_ufunc__ 的对象
|
|
307
|
+
# 直接分派:function 组查 __array_function__,ufunc 组查 __array_ufunc__,
|
|
308
|
+
# 返回协议提供的值。ndarray 自带 __array_ufunc__(数值 ufunc 协议),
|
|
309
|
+
# 必须排除在分派之外,否则常规输入会被劫持。
|
|
310
|
+
|
|
311
|
+
def _protocol_wrap(orig, use_ufunc):
|
|
312
|
+
def wrapper(a, *args, **kwargs):
|
|
313
|
+
# ndarray 自带 __array_ufunc__(数值 ufunc 协议):排除在分派之外
|
|
314
|
+
_is_arr = hasattr(a, '_raw_data') or hasattr(a, '_array')
|
|
315
|
+
if not _is_arr:
|
|
316
|
+
_hook_name = '__array_ufunc__' if use_ufunc else '__array_function__'
|
|
317
|
+
_hook = getattr(type(a), _hook_name, None)
|
|
318
|
+
if _hook is not None:
|
|
319
|
+
if use_ufunc:
|
|
320
|
+
return _hook(a, orig, 'call', a, *args, **kwargs)
|
|
321
|
+
return _hook(a, orig, (a,) + args, kwargs)
|
|
322
|
+
return orig(a, *args, **kwargs)
|
|
323
|
+
wrapper.__name__ = getattr(orig, '__name__', 'wrapper')
|
|
324
|
+
wrapper.__doc__ = getattr(orig, '__doc__', None)
|
|
325
|
+
wrapper.__wrapped__ = orig
|
|
326
|
+
return wrapper
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def _apply_protocol_wraps():
|
|
330
|
+
_func_names = (
|
|
331
|
+
'center', 'capitalize', 'decode', 'encode', 'expandtabs', 'ljust',
|
|
332
|
+
'lower', 'mod', 'multiply', 'partition', 'rjust', 'rpartition',
|
|
333
|
+
'swapcase', 'title', 'translate', 'upper', 'zfill')
|
|
334
|
+
_ufunc_names = (
|
|
335
|
+
'add', 'lstrip', 'rstrip', 'strip', 'equal', 'not_equal',
|
|
336
|
+
'greater_equal', 'less_equal', 'greater', 'less', 'count',
|
|
337
|
+
'endswith', 'find', 'index', 'isalnum', 'isalpha', 'isdecimal',
|
|
338
|
+
'isdigit', 'islower', 'isnumeric', 'isspace', 'istitle', 'isupper',
|
|
339
|
+
'rfind', 'rindex', 'startswith', 'str_len')
|
|
340
|
+
g = globals()
|
|
341
|
+
for _nm in _func_names:
|
|
342
|
+
if g.get(_nm) is not None:
|
|
343
|
+
g[_nm] = _protocol_wrap(g[_nm], use_ufunc=False)
|
|
344
|
+
for _nm in _ufunc_names:
|
|
345
|
+
if g.get(_nm) is not None:
|
|
346
|
+
g[_nm] = _protocol_wrap(g[_nm], use_ufunc=True)
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
_apply_protocol_wraps()
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
"""rsarray.typing - 类型注解支持,对齐 rsarray.typing。
|
|
3
|
+
|
|
4
|
+
提供 `NDArray`、`ArrayLike`、`DTypeLike`,主要用于类型注解,
|
|
5
|
+
支持 `NDArray[complex]`、`NDArray[float]` 等下标写法。
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import typing as _t
|
|
9
|
+
|
|
10
|
+
__all__ = ['NDArray', 'ArrayLike', 'DTypeLike']
|
|
11
|
+
|
|
12
|
+
_ScalarType = _t.TypeVar('_ScalarType')
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class NDArray(_t.Generic[_ScalarType]):
|
|
16
|
+
"""rsarray.typing.NDArray 的轻量替代,支持下标用于类型注解。"""
|
|
17
|
+
|
|
18
|
+
def __class_getitem__(cls, item):
|
|
19
|
+
return _t.cast(type, cls)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# 任意可转换为数组的对象;用于函数签名注解。
|
|
23
|
+
ArrayLike = _t.Any
|
|
24
|
+
|
|
25
|
+
# 任意可解释为 dtype 的对象。
|
|
26
|
+
DTypeLike = _t.Any
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rsarray
|
|
3
|
+
Version: 1.2.10
|
|
4
|
+
Classifier: Programming Language :: Rust
|
|
5
|
+
Classifier: Programming Language :: Python :: 3
|
|
6
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
7
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
8
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
13
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
14
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
15
|
+
Classifier: Operating System :: MacOS
|
|
16
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
17
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Visualization
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
License-File: NOTICE
|
|
21
|
+
Summary: A high-performance array library powered by Rust.
|
|
22
|
+
Author: YJ-Niu
|
|
23
|
+
License-Expression: MIT
|
|
24
|
+
Requires-Python: >=3.10
|
|
25
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
26
|
+
|
|
27
|
+
# rsarray
|
|
28
|
+
|
|
29
|
+
A high-performance array library where every compute-heavy
|
|
30
|
+
operator is implemented in Rust and dispatched through PyO3 with the GIL
|
|
31
|
+
released. The Python layer is a thin parameter-forwarding shell, so
|
|
32
|
+
multi-threaded Python code actually runs in parallel.
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Highlights
|
|
37
|
+
|
|
38
|
+
- **Familiar array API.** Drop-in for typical array workloads. Covers array
|
|
39
|
+
creation, math, statistics, `linalg`, `fft`, `random`, polynomial, file I/O
|
|
40
|
+
(`.npy` / `.npz` / text), `datetime64` / `timedelta64`, masked and
|
|
41
|
+
structured arrays. Aligned with the 2.5 semantic baseline.
|
|
42
|
+
- **Rust-powered performance.** On macOS (Apple Silicon) for `n = 1,000,000`
|
|
43
|
+
`float64`, versus a typical C/Python array stack:
|
|
44
|
+
- `sin` / `exp` — **5.2×–5.4×** faster
|
|
45
|
+
- `sort` — **~32×** faster
|
|
46
|
+
- `cumsum` — **~1.9×** faster
|
|
47
|
+
- `add` / `mul` / `div` / `sqrt` — **~1.6×–1.7×** faster
|
|
48
|
+
- `matmul` 1024×1024 — system Accelerate BLAS level via Rust dispatch
|
|
49
|
+
- **GIL-free parallelism.** All heavy operators (`sort`, `cumsum`, `matmul`,
|
|
50
|
+
`inv`, element-wise, reductions) release the GIL via `py.detach` and are
|
|
51
|
+
parallelized in Rust with rayon, behind cost-tiered thresholds so small
|
|
52
|
+
arrays don't pay a thread-scheduling tax.
|
|
53
|
+
- **Zero-copy buffer protocol.** Implements PEP 3118 (`__getbuffer__` /
|
|
54
|
+
`__releasebuffer__` in Rust) plus PEP 688 (`__buffer__` for Python 3.12+).
|
|
55
|
+
Downstream libraries like `matplotlib` or `rsplotlib` can read `float64` data
|
|
56
|
+
without copying. Non-`float64` dtypes fall back to `__array_interface__`
|
|
57
|
+
with dtype-precise `bytes`.
|
|
58
|
+
- **Variable-length elements.** `VarStringArray` is a faster drop-in for
|
|
59
|
+
variable-length string data that would otherwise degrade to `object`
|
|
60
|
+
dtype. `BigIntArray` uses Python `int` to hold integers that exceed `2^53`
|
|
61
|
+
without losing precision.
|
|
62
|
+
- **Large-array tools.** `chunked_apply` for axis-chunked user functions,
|
|
63
|
+
`mmap_array` for reading part of a binary file by `dtype` + `shape` +
|
|
64
|
+
`offset`, `LazyArray` for deferred chained operations, and
|
|
65
|
+
`memory_usage()` for inspecting array footprint.
|
|
66
|
+
- **Thread-safety helpers.** `threadsafe_copy` / `threadsafe_view` and a
|
|
67
|
+
`thread_safe=True` switch on `ndarray.copy()` and `np.copy()` to make
|
|
68
|
+
multi-threaded sharing safe without paying for a global lock on the hot
|
|
69
|
+
path.
|
|
70
|
+
- **Smart dtype inference.** `array(['hello', 'world'])` infers `U5` instead
|
|
71
|
+
of `object`; mixed-type and integer-overflow paths emit warnings and
|
|
72
|
+
recommend better dtypes or `BigIntArray`.
|
|
73
|
+
- **Compact binary & fast import.** Release build uses `lto="fat"`,
|
|
74
|
+
`codegen-units=1`, `panic="abort"`, and `strip="symbols"`, plus a
|
|
75
|
+
`num_cpus`-free dependency tree — `num_core.cpython-*.so` is about **5.1 MB**.
|
|
76
|
+
Circular imports are broken with lazy `_get_deps()`, so the first
|
|
77
|
+
`import rsarray` finishes in roughly **7 ms**.
|
|
78
|
+
- **Thread-pool control.** `set_num_threads()`, `get_num_threads()`,
|
|
79
|
+
`parallel_context()`, and `set_parallel_thresholds()` /
|
|
80
|
+
`get_parallel_thresholds()` let callers steer the underlying rayon pool
|
|
81
|
+
and per-operator cost thresholds at runtime.
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
## Installation
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
pip install rsarray
|
|
89
|
+
# or
|
|
90
|
+
uv pip install rsarray
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Prebuilt wheels are published for **Python 3.10–3.14** on:
|
|
94
|
+
|
|
95
|
+
- Linux `x86_64` / `aarch64`
|
|
96
|
+
- macOS Apple Silicon / Intel
|
|
97
|
+
- Windows `x64`
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## Quick Start
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
import rsarray as np
|
|
105
|
+
|
|
106
|
+
# Basics
|
|
107
|
+
a = np.array([1.0, 2.0, 3.0, 4.0])
|
|
108
|
+
b = np.arange(12).reshape(3, 4)
|
|
109
|
+
print(np.sin(a).tolist())
|
|
110
|
+
print(np.exp(a).tolist())
|
|
111
|
+
print(np.sum(a), np.mean(a))
|
|
112
|
+
|
|
113
|
+
# Linear algebra
|
|
114
|
+
A = np.array([[1.0, 2.0], [3.0, 4.0]])
|
|
115
|
+
print(np.linalg.inv(A).tolist())
|
|
116
|
+
print(np.linalg.det(A))
|
|
117
|
+
|
|
118
|
+
# FFT
|
|
119
|
+
spectrum = np.fft.fft(a)
|
|
120
|
+
|
|
121
|
+
# Reproducible parallel random sampling
|
|
122
|
+
rng = np.random.default_rng(42)
|
|
123
|
+
print(rng.random(5).tolist())
|
|
124
|
+
|
|
125
|
+
# Variable-length strings (faster than object dtype)
|
|
126
|
+
vsa = np.VarStringArray(['hello', 'world', 'foo'])
|
|
127
|
+
print((vsa + '!').to_list())
|
|
128
|
+
|
|
129
|
+
# Big integers (no precision loss past 2^53)
|
|
130
|
+
big = np.BigIntArray([2**60, 2**70, 2**80])
|
|
131
|
+
print(big[0], big.sum())
|
|
132
|
+
|
|
133
|
+
# Large-array tools
|
|
134
|
+
large = np.arange(10_000_000)
|
|
135
|
+
np.chunked_apply(np.sum, large, chunk_size=1_000_000)
|
|
136
|
+
|
|
137
|
+
lazy = np.LazyArray(np.arange(1000.0))
|
|
138
|
+
result = lazy.sqrt().multiply(2).add(1).compute()
|
|
139
|
+
|
|
140
|
+
# Zero-copy interop with third-party libraries:
|
|
141
|
+
# e.g. matplotlib consumes the float64 buffer directly, no copy
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
## Project Layout
|
|
147
|
+
|
|
148
|
+
```
|
|
149
|
+
rsarray/
|
|
150
|
+
├── src/ # Rust source (22,000+ lines)
|
|
151
|
+
│ ├── lib.rs # core ndarray + PyO3 module
|
|
152
|
+
│ ├── indexing.rs # multi-dim indexing & slicing
|
|
153
|
+
│ ├── fft.rs # FFT
|
|
154
|
+
│ ├── linalg.rs # linear algebra
|
|
155
|
+
│ ├── random.rs # random sampling
|
|
156
|
+
│ ├── threadpool.rs # rayon pool + parallel thresholds
|
|
157
|
+
│ ├── arithmetic/ # + - * / % **
|
|
158
|
+
│ ├── bitwise/ # & | ^ ~
|
|
159
|
+
│ ├── buffer/ # bytes ⇄ array interop
|
|
160
|
+
│ ├── creation/ # zeros, ones, arange, linspace, ...
|
|
161
|
+
│ ├── formatting/ # repr / str formatting
|
|
162
|
+
│ ├── io/ # .npy / .npz
|
|
163
|
+
│ ├── logic/ # comparison & logical ops
|
|
164
|
+
│ ├── manipulation/ # reshape, transpose, concatenate, ...
|
|
165
|
+
│ ├── mathematics/ # sin, cos, exp, log, ...
|
|
166
|
+
│ ├── poly/ # polynomial primitives
|
|
167
|
+
│ ├── searching/ # argmax, argmin, searchsorted, ...
|
|
168
|
+
│ ├── setops/ # unique, intersect1d, ...
|
|
169
|
+
│ ├── sorting/ # sort, argsort, partition, ...
|
|
170
|
+
│ └── statistics/ # sum, mean, std, var, ...
|
|
171
|
+
├── python/rsarray/ # thin Python wrappers (26,000+ lines)
|
|
172
|
+
│ ├── __init__.py
|
|
173
|
+
│ ├── array_methods.py
|
|
174
|
+
│ ├── array_ops.py
|
|
175
|
+
│ ├── _extra.py # ~140 array-library helpers
|
|
176
|
+
│ ├── _dtypes.py
|
|
177
|
+
│ ├── char.py, io.py, ma.py, rec.py
|
|
178
|
+
│ ├── math_functions.py, statistics.py
|
|
179
|
+
│ ├── linalg/, polynomial/, random/, fft/, lib/, typing/
|
|
180
|
+
├── test/ # regression & compat tests
|
|
181
|
+
│ ├── run_test.py
|
|
182
|
+
│ └── test_rf/ # networkx / scipy compatibility
|
|
183
|
+
├── debug/ # benchmarks & debug scripts
|
|
184
|
+
├── Cargo.toml # Rust manifest & release profile
|
|
185
|
+
├── pyproject.toml # Python build config (maturin)
|
|
186
|
+
├── build_wheel.sh # one-shot build & install
|
|
187
|
+
└── README.md / RELEASE_NOTES.md / PYPI.md
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
## Building from Source
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
git clone https://github.com/YJ-Niu/rsarray.git
|
|
196
|
+
cd rsarray
|
|
197
|
+
|
|
198
|
+
# Create a virtualenv (uv recommended)
|
|
199
|
+
uv venv .venv --python 3.11
|
|
200
|
+
source .venv/bin/activate
|
|
201
|
+
uv pip install maturin
|
|
202
|
+
|
|
203
|
+
# Build & install (release)
|
|
204
|
+
bash build_wheel.sh
|
|
205
|
+
# bash build_wheel.sh --debug # debug build with symbols
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
The script detects the active Python, runs `maturin build --release`,
|
|
209
|
+
places the wheel under `wheelhouse/`, and installs it with
|
|
210
|
+
`uv pip install --no-deps`. For a development build that picks up source
|
|
211
|
+
edits immediately:
|
|
212
|
+
|
|
213
|
+
```bash
|
|
214
|
+
maturin develop --release
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
---
|
|
218
|
+
|
|
219
|
+
## Dependencies
|
|
220
|
+
|
|
221
|
+
`rsarray` is **self-contained** at the Python level — there are no runtime
|
|
222
|
+
Python dependencies at all. All computation lives in the Rust extension.
|
|
223
|
+
|
|
224
|
+
Build-time only:
|
|
225
|
+
|
|
226
|
+
- `maturin >= 1.13, < 2.0`
|
|
227
|
+
- A Rust toolchain ≥ 1.75 (pinned via `rust-toolchain.toml`)
|
|
228
|
+
|
|
229
|
+
Runtime Rust crates bundled into the wheel:
|
|
230
|
+
|
|
231
|
+
- `pyo3 0.29` (extension-module)
|
|
232
|
+
- `ndarray 0.17.2` (with `rayon` and `matrixmultiply-threading`; on macOS
|
|
233
|
+
also `blas` to dispatch to Accelerate)
|
|
234
|
+
- `rayon 1.12`
|
|
235
|
+
- `rand 0.10.2` / `rand_distr 0.6.0`
|
|
236
|
+
- `num-traits 0.2.19` / `num-complex 0.4.6`
|
|
237
|
+
- `rustfft 6.4.1`
|
|
238
|
+
- `zip 8.6.0` (with only `deflate-flate2-zlib-rs` enabled, for `.npz`
|
|
239
|
+
read/write)
|
|
240
|
+
- `thiserror 2.0.19`
|
|
241
|
+
- `blas-src 0.14.0` (macOS, with `accelerate`)
|
|
242
|
+
|
|
243
|
+
`num_cpus` was **removed** in v1.2.3 — the project now uses
|
|
244
|
+
`std::thread::available_parallelism()` to keep the binary small.
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
## Version
|
|
249
|
+
|
|
250
|
+
**v1.2.3** — see [RELEASE_NOTES.md](RELEASE_NOTES.md) for the full changelog
|
|
251
|
+
(binary size −25%, import time −130×, dynamic thread pool, variable-length
|
|
252
|
+
elements, large-array tools, smart dtype inference, thread-safety helpers,
|
|
253
|
+
and several `sort` / `indexing` / `statistics` bug fixes).
|
|
254
|
+
|
|
255
|
+
### Previous Highlights
|
|
256
|
+
|
|
257
|
+
- **v1.2.1** — 2×2 batched `linalg.inv` parallel with rayon; 2-port S-parameter
|
|
258
|
+
renormalization ~700× faster; closed-form S-parameter inversion ~8× faster.
|
|
259
|
+
- **v1.1.6** — GIL release for compute-heavy operators; cost-tiered parallel
|
|
260
|
+
thresholds; in-place parallel `cumsum` / `cumprod`; macOS Accelerate BLAS
|
|
261
|
+
backend.
|
|
262
|
+
- **v1.1.0** — ~140 array API functions added; aligned with the 2.5 baseline.
|
|
263
|
+
- **v1.0.7** — `__array__` interface for direct matplotlib consumption.
|
|
264
|
+
- **v1.0.0** — Initial release with arrays, math, statistics, linalg, FFT,
|
|
265
|
+
random, polynomial, and file I/O.
|
|
266
|
+
|
|
267
|
+
---
|
|
268
|
+
|
|
269
|
+
## License
|
|
270
|
+
|
|
271
|
+
MIT — see [LICENSE](LICENSE). Original code throughout; no third-party
|
|
272
|
+
attribution required (see [NOTICE](https://github.com/YJ-Niu/rsarray/blob/main/NOTICE)).
|
|
273
|
+
|
|
274
|
+
## Links
|
|
275
|
+
|
|
276
|
+
- [GitHub repository](https://github.com/YJ-Niu/rsarray)
|
|
277
|
+
- [README](https://github.com/YJ-Niu/rsarray/blob/main/README.md)
|
|
278
|
+
- [Release notes](https://github.com/YJ-Niu/rsarray/blob/main/RELEASE_NOTES.md)
|
|
279
|
+
- [Issue tracker](https://github.com/YJ-Niu/rsarray/issues)
|
|
280
|
+
|