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.
Files changed (59) hide show
  1. rsarray/__init__.py +12028 -0
  2. rsarray/_core/__init__.py +213 -0
  3. rsarray/_core/_exceptions.py +83 -0
  4. rsarray/_core/_internal.py +331 -0
  5. rsarray/_core/_multiarray_tests.py +129 -0
  6. rsarray/_core/_multiarray_umath.py +121 -0
  7. rsarray/_core/_rational_tests.py +52 -0
  8. rsarray/_core/arrayprint.py +36 -0
  9. rsarray/_core/fromnumeric.py +31 -0
  10. rsarray/_core/function_base.py +84 -0
  11. rsarray/_core/multiarray.py +385 -0
  12. rsarray/_core/numeric.py +28 -0
  13. rsarray/_core/numerictypes.py +233 -0
  14. rsarray/_core/records.py +14 -0
  15. rsarray/_core/shape_base.py +23 -0
  16. rsarray/_core/strings.py +78 -0
  17. rsarray/_core/umath.py +176 -0
  18. rsarray/_dtypes.py +1229 -0
  19. rsarray/_extra.py +6621 -0
  20. rsarray/_typing.py +24 -0
  21. rsarray/array_methods.py +1224 -0
  22. rsarray/array_ops.py +1585 -0
  23. rsarray/char.py +1590 -0
  24. rsarray/exceptions.py +45 -0
  25. rsarray/fft/__init__.py +231 -0
  26. rsarray/io.py +453 -0
  27. rsarray/lib/__init__.py +102 -0
  28. rsarray/lib/_function_base_impl.py +37 -0
  29. rsarray/lib/_index_tricks_impl.py +98 -0
  30. rsarray/lib/_nanfunctions_impl.py +1637 -0
  31. rsarray/lib/_stride_tricks_impl.py +110 -0
  32. rsarray/lib/recfunctions.py +1222 -0
  33. rsarray/lib/stride_tricks.py +106 -0
  34. rsarray/linalg/__init__.py +2603 -0
  35. rsarray/linalg/_linalg.py +91 -0
  36. rsarray/linalg/_pylinalg.py +1008 -0
  37. rsarray/linalg/lapack_lite.py +13 -0
  38. rsarray/ma/__init__.py +92 -0
  39. rsarray/ma/core.py +1385 -0
  40. rsarray/ma/extras.py +711 -0
  41. rsarray/ma/mrecords.py +144 -0
  42. rsarray/math_functions.py +965 -0
  43. rsarray/matlib.py +67 -0
  44. rsarray/num_core.cp315-win_amd64.pyd +0 -0
  45. rsarray/polynomial/__init__.py +148 -0
  46. rsarray/polynomial/_npcompat.py +721 -0
  47. rsarray/polynomial/polynomial.py +1082 -0
  48. rsarray/random/__init__.py +2266 -0
  49. rsarray/random/_distributions.py +773 -0
  50. rsarray/rec.py +207 -0
  51. rsarray/statistics.py +1360 -0
  52. rsarray/strings.py +349 -0
  53. rsarray/typing/__init__.py +26 -0
  54. rsarray-1.2.10.dist-info/METADATA +280 -0
  55. rsarray-1.2.10.dist-info/RECORD +59 -0
  56. rsarray-1.2.10.dist-info/WHEEL +4 -0
  57. rsarray-1.2.10.dist-info/licenses/LICENSE +21 -0
  58. rsarray-1.2.10.dist-info/licenses/NOTICE +7 -0
  59. rsarray-1.2.10.dist-info/sboms/rsarray.cyclonedx.json +2789 -0
rsarray/exceptions.py ADDED
@@ -0,0 +1,45 @@
1
+ # SPDX-License-Identifier: MIT
2
+ class AxisError(ValueError, IndexError):
3
+ def __init__(self, axis, ndim=None, msg_prefix=None):
4
+ # 数组语义:单参数调用时 axis 就是完整消息(此时 axis/ndim 属性为 None)
5
+ self.axis = None if (ndim is None and msg_prefix is None) else axis
6
+ self.ndim = ndim
7
+ if ndim is None and msg_prefix is None:
8
+ super().__init__(axis)
9
+ elif msg_prefix is not None:
10
+ super().__init__(f"{msg_prefix}: axis {axis} is out of bounds for array of dimension {ndim}")
11
+ else:
12
+ super().__init__(f"axis {axis} is out of bounds for array of dimension {ndim}")
13
+ self._init_args = (axis, ndim, msg_prefix)
14
+
15
+ def __reduce__(self):
16
+ # pickle 后 axis/ndim/args 保持一致(数组语义)
17
+ return (self.__class__, self._init_args)
18
+
19
+
20
+ class ComplexWarning(Warning):
21
+ pass
22
+
23
+
24
+ class DTypePromotionError(TypeError):
25
+ pass
26
+
27
+
28
+ class ModuleDeprecationWarning(DeprecationWarning):
29
+ pass
30
+
31
+
32
+ class RankWarning(Warning):
33
+ pass
34
+
35
+
36
+ class TooHardError(Exception):
37
+ pass
38
+
39
+
40
+ class VisibleDeprecationWarning(Warning):
41
+ pass
42
+
43
+
44
+ __all__ = ['AxisError', 'ComplexWarning', 'DTypePromotionError', 'ModuleDeprecationWarning',
45
+ 'RankWarning', 'TooHardError', 'VisibleDeprecationWarning']
@@ -0,0 +1,231 @@
1
+ # SPDX-License-Identifier: MIT
2
+ """rsarray.fft - 快速傅里叶变换模块。
3
+
4
+ 与 fft 对齐:既支持 `import rsarray as np; np.fft.fftshift(...)`,
5
+ 也支持 `from rsarray.fft import fft, ifft, rfft, irfft, fftshift, ifftshift`。
6
+
7
+ 底层一维变换由 Rust (`rsarray.num_core`) 实现,复数结果以形状 ``[n, 2]``
8
+ 的实数数组表示(每行为 ``[实部, 虚部]``)。fftshift/ifftshift/fftfreq/rfftfreq
9
+ 则参考 数组的实现用纯 Python 组合现有原语完成。
10
+ """
11
+
12
+ import math as _math
13
+
14
+ import rsarray.num_core as _core
15
+
16
+
17
+
18
+ __all__ = [
19
+ 'fft', 'ifft', 'rfft', 'irfft', 'irfftn',
20
+ 'fftshift', 'ifftshift', 'fftfreq', 'rfftfreq',
21
+ ]
22
+
23
+
24
+ def _np():
25
+ """惰性获取父包,避免导入期循环依赖。"""
26
+ import rsarray as _rsnp
27
+ return _rsnp
28
+
29
+
30
+ def _raw(a):
31
+ """取得底层 `_core.ndarray`。"""
32
+ if hasattr(a, '_array'):
33
+ return a._array
34
+ return _core.ndarray(a)
35
+
36
+
37
+ def _scale(result, factor):
38
+ """对(可能是复数 [n, 2] 表示的)结果整体乘以标量。"""
39
+ if factor == 1.0:
40
+ return result
41
+ return result * factor
42
+
43
+
44
+ def _norm_forward_factor(n, norm):
45
+ """正变换(fft/rfft)在给定 norm 下相对 默认(无缩放)的系数。"""
46
+ if not n:
47
+ return 1.0
48
+ if norm == 'ortho':
49
+ return 1.0 / _math.sqrt(n)
50
+ if norm == 'forward':
51
+ return 1.0 / n
52
+ return 1.0
53
+
54
+
55
+ def _norm_inverse_factor(n, norm):
56
+ """逆变换(ifft/irfft)相对 `_core` 默认(已含 1/n)的补偿系数。"""
57
+ if not n:
58
+ return 1.0
59
+ if norm == 'ortho':
60
+ return _math.sqrt(n)
61
+ if norm == 'forward':
62
+ return float(n)
63
+ return 1.0
64
+
65
+
66
+ def _fft_1d(arr_core, n, kind):
67
+ """对一维核心数组执行 FFT。
68
+
69
+ kind: 'fft', 'ifft', 'rfft', 'irfft'
70
+ """
71
+ if kind == 'fft':
72
+ return _core.py_fft_ndarray(arr_core)
73
+ elif kind == 'ifft':
74
+ return _core.py_ifft_ndarray(arr_core)
75
+ elif kind == 'rfft':
76
+ return _core.py_rfft_ndarray(arr_core)
77
+ elif kind == 'irfft':
78
+ return _core.py_irfft_ndarray(arr_core, n)
79
+ raise ValueError(f"未知 FFT 类型: {kind}")
80
+
81
+
82
+ def _fft_nd(arr, n, axis, norm, kind, is_inverse):
83
+ """沿指定轴对多维数组执行 FFT。
84
+
85
+ kind: 'fft', 'ifft', 'rfft', 'irfft'
86
+ is_inverse: True 表示逆变换(使用 _norm_inverse_factor)
87
+ """
88
+ np = _np()
89
+ nd = np.ndarray
90
+ arr = arr if hasattr(arr, '_array') else nd(arr)
91
+
92
+ # 一维数组直接处理
93
+ if arr.ndim <= 1:
94
+ result = nd._wrap(_fft_1d(arr._array, n, kind))
95
+ if is_inverse:
96
+ return _scale(result, _norm_inverse_factor(result.shape[0], norm))
97
+ else:
98
+ return _scale(result, _norm_forward_factor(arr.size, norm))
99
+
100
+ # 多维数组:沿 axis 处理
101
+ # 标准化 axis
102
+ ax = axis % arr.ndim if axis is not None else -1
103
+ ax = ax if ax >= 0 else arr.ndim + ax
104
+
105
+ # 将 axis 交换到最后一维
106
+ if ax != arr.ndim - 1:
107
+ arr_swapped = np.swapaxes(arr, ax, arr.ndim - 1)
108
+ else:
109
+ arr_swapped = arr
110
+
111
+ # 展平前面的维度
112
+ original_shape = arr_swapped.shape
113
+ last_dim = original_shape[-1]
114
+ arr_2d = arr_swapped.reshape(-1, last_dim)
115
+
116
+ # 对每行做 1D FFT,使用列表推导代替显式 for 循环
117
+ rows = [nd._wrap(_fft_1d(arr_2d[i]._array, n, kind)) for i in range(arr_2d.shape[0])]
118
+
119
+ # 合并结果
120
+ if not rows:
121
+ result_2d = np.array([])
122
+ else:
123
+ result_2d = np.stack(rows)
124
+
125
+ # 计算新形状
126
+ new_last_dim = result_2d.shape[1] if result_2d.ndim >= 2 else result_2d.shape[0]
127
+ new_shape = original_shape[:-1] + (new_last_dim,)
128
+ result = result_2d.reshape(new_shape)
129
+
130
+ # 将 axis 交换回原位
131
+ if ax != arr.ndim - 1:
132
+ result = np.swapaxes(result, arr.ndim - 1, ax)
133
+
134
+ # 应用归一化
135
+ transform_len = result.shape[ax]
136
+ if is_inverse:
137
+ return _scale(result, _norm_inverse_factor(transform_len, norm))
138
+ else:
139
+ return _scale(result, _norm_forward_factor(arr.size // last_dim * transform_len if transform_len else arr.size, norm))
140
+
141
+
142
+ def fft(a, n=None, axis=-1, norm=None):
143
+ """计算一维离散傅里叶变换。"""
144
+ return _fft_nd(a, n, axis, norm, 'fft', is_inverse=False)
145
+
146
+
147
+ def ifft(a, n=None, axis=-1, norm=None):
148
+ """计算一维逆离散傅里叶变换。"""
149
+ return _fft_nd(a, n, axis, norm, 'ifft', is_inverse=True)
150
+
151
+
152
+ def rfft(a, n=None, axis=-1, norm=None):
153
+ """计算实输入的一维离散傅里叶变换。"""
154
+ return _fft_nd(a, n, axis, norm, 'rfft', is_inverse=False)
155
+
156
+
157
+ def irfft(a, n=None, axis=-1, norm=None):
158
+ """计算 rfft 的逆变换(返回实数数组)。"""
159
+ return _fft_nd(a, n, axis, norm, 'irfft', is_inverse=True)
160
+
161
+
162
+ def irfftn(a, s=None, axes=None, norm=None):
163
+ """计算 rfftn 的逆变换(实数输出)。
164
+
165
+ 对 axes[-1] 做 irfft,对其余 axes 做 ifft(与 数组语义一致)。
166
+ """
167
+ np = _np()
168
+ arr = a if hasattr(a, '_array') else np.ndarray(a)
169
+ ndim = arr.ndim
170
+ if axes is None:
171
+ axes = list(range(ndim))
172
+ if isinstance(axes, int):
173
+ axes = (axes,)
174
+ axes = tuple(ax if ax >= 0 else ax + ndim for ax in axes)
175
+ axes = tuple(dict.fromkeys(axes))
176
+ if not axes:
177
+ raise ValueError("irfftn: at least one axis must be provided")
178
+ _s = list(s) if s is not None else [None] * len(axes)
179
+ if len(_s) < len(axes):
180
+ _s = _s + [None] * (len(axes) - len(_s))
181
+ out = arr
182
+ for ax, n_ax in zip(axes[:-1], _s[:-1]):
183
+ out = ifft(out, n=n_ax, axis=ax, norm=norm)
184
+ out = irfft(out, n=_s[-1], axis=axes[-1], norm=norm)
185
+ return out
186
+
187
+
188
+ def _shift(x, axes, inverse):
189
+ np = _np()
190
+ x = np.asarray(x)
191
+ ndim = len(x.shape)
192
+ if axes is None:
193
+ axis_list = range(ndim)
194
+ elif isinstance(axes, int):
195
+ axis_list = (axes,)
196
+ else:
197
+ axis_list = axes
198
+ for ax in axis_list:
199
+ n = x.shape[ax]
200
+ half = n // 2
201
+ shift = -half if inverse else half
202
+ x = np.roll(x, shift, axis=ax)
203
+ return x
204
+
205
+
206
+ def fftshift(x, axes=None):
207
+ """将零频分量移到频谱中心。"""
208
+ return _shift(x, axes, inverse=False)
209
+
210
+
211
+ def ifftshift(x, axes=None):
212
+ """fftshift 的逆操作。"""
213
+ return _shift(x, axes, inverse=True)
214
+
215
+
216
+ def fftfreq(n, d=1.0):
217
+ """返回离散傅里叶变换的采样频率。"""
218
+ np = _np()
219
+ val = 1.0 / (n * d)
220
+ half = (n - 1) // 2 + 1
221
+ p1 = np.arange(0, half)
222
+ p2 = np.arange(-(n // 2), 0)
223
+ return np.concatenate([p1, p2]) * val
224
+
225
+
226
+ def rfftfreq(n, d=1.0):
227
+ """返回 rfft 的采样频率(仅非负频率)。"""
228
+ np = _np()
229
+ val = 1.0 / (n * d)
230
+ half = n // 2 + 1
231
+ return np.arange(0, half) * val