numpy-ts 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +447 -0
- package/dist/numpy-ts.browser.js +1365 -0
- package/dist/numpy-ts.esm.js +1365 -0
- package/dist/numpy-ts.node.cjs +1366 -0
- package/dist/types/core/broadcasting.d.ts +62 -0
- package/dist/types/core/dtype.d.ts +66 -0
- package/dist/types/core/ndarray.d.ts +387 -0
- package/dist/types/core/slicing.d.ts +91 -0
- package/dist/types/core/storage.d.ts +92 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/internal/compute.d.ts +25 -0
- package/dist/types/internal/indexing.d.ts +24 -0
- package/dist/types/ops/arithmetic.d.ts +49 -0
- package/dist/types/ops/comparison.d.ts +43 -0
- package/dist/types/ops/linalg.d.ts +16 -0
- package/dist/types/ops/reduction.d.ts +61 -0
- package/dist/types/ops/shape.d.ts +39 -0
- package/package.json +121 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Broadcasting utilities for NumPy-compatible array operations
|
|
3
|
+
*
|
|
4
|
+
* Wraps @stdlib broadcasting functions with NumPy-compatible API
|
|
5
|
+
*/
|
|
6
|
+
type StdlibNDArray = any;
|
|
7
|
+
/**
|
|
8
|
+
* Check if two or more shapes are broadcast-compatible
|
|
9
|
+
* and compute the resulting output shape
|
|
10
|
+
*
|
|
11
|
+
* @param shapes - Array of shapes to broadcast
|
|
12
|
+
* @returns The broadcast output shape, or null if incompatible
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* computeBroadcastShape([[3, 4], [4]]); // [3, 4]
|
|
17
|
+
* computeBroadcastShape([[3, 4], [3, 1]]); // [3, 4]
|
|
18
|
+
* computeBroadcastShape([[3, 4], [5]]); // null (incompatible)
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function computeBroadcastShape(shapes: readonly number[][]): number[] | null;
|
|
22
|
+
/**
|
|
23
|
+
* Check if two shapes are broadcast-compatible
|
|
24
|
+
*
|
|
25
|
+
* @param shape1 - First shape
|
|
26
|
+
* @param shape2 - Second shape
|
|
27
|
+
* @returns true if shapes can be broadcast together, false otherwise
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* areBroadcastable([3, 4], [4]); // true
|
|
32
|
+
* areBroadcastable([3, 4], [3, 1]); // true
|
|
33
|
+
* areBroadcastable([3, 4], [5]); // false
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function areBroadcastable(shape1: readonly number[], shape2: readonly number[]): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Broadcast multiple stdlib ndarrays to a common shape
|
|
39
|
+
*
|
|
40
|
+
* Returns read-only views of the input arrays broadcast to the same shape.
|
|
41
|
+
* Views share memory with the original arrays.
|
|
42
|
+
*
|
|
43
|
+
* @param arrays - Stdlib ndarrays to broadcast
|
|
44
|
+
* @returns Array of broadcast stdlib ndarrays
|
|
45
|
+
* @throws Error if arrays have incompatible shapes
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const [a, b] = broadcastStdlibArrays([arr1._data, arr2._data]);
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export declare function broadcastStdlibArrays(arrays: StdlibNDArray[]): StdlibNDArray[];
|
|
53
|
+
/**
|
|
54
|
+
* Generate a descriptive error message for broadcasting failures
|
|
55
|
+
*
|
|
56
|
+
* @param shapes - The incompatible shapes
|
|
57
|
+
* @param operation - The operation being attempted (e.g., 'add', 'multiply')
|
|
58
|
+
* @returns Error message string
|
|
59
|
+
*/
|
|
60
|
+
export declare function broadcastErrorMessage(shapes: readonly number[][], operation?: string): string;
|
|
61
|
+
export {};
|
|
62
|
+
//# sourceMappingURL=broadcasting.d.ts.map
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DType (Data Type) system for numpy-ts
|
|
3
|
+
*
|
|
4
|
+
* Supports NumPy numeric types:
|
|
5
|
+
* - Floating point: float32, float64
|
|
6
|
+
* - Signed integers: int8, int16, int32, int64
|
|
7
|
+
* - Unsigned integers: uint8, uint16, uint32, uint64
|
|
8
|
+
* - Boolean: bool
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* All supported dtypes
|
|
12
|
+
*/
|
|
13
|
+
export type DType = 'float64' | 'float32' | 'int64' | 'int32' | 'int16' | 'int8' | 'uint64' | 'uint32' | 'uint16' | 'uint8' | 'bool';
|
|
14
|
+
/**
|
|
15
|
+
* TypedArray types for each dtype
|
|
16
|
+
*/
|
|
17
|
+
export type TypedArray = Float64Array | Float32Array | BigInt64Array | Int32Array | Int16Array | Int8Array | BigUint64Array | Uint32Array | Uint16Array | Uint8Array;
|
|
18
|
+
/**
|
|
19
|
+
* Default dtype (matches NumPy)
|
|
20
|
+
*/
|
|
21
|
+
export declare const DEFAULT_DTYPE: DType;
|
|
22
|
+
/**
|
|
23
|
+
* Get the TypedArray constructor for a given dtype
|
|
24
|
+
*/
|
|
25
|
+
export declare function getTypedArrayConstructor(dtype: DType): TypedArrayConstructor | null;
|
|
26
|
+
type TypedArrayConstructor = Float64ArrayConstructor | Float32ArrayConstructor | BigInt64ArrayConstructor | Int32ArrayConstructor | Int16ArrayConstructor | Int8ArrayConstructor | BigUint64ArrayConstructor | Uint32ArrayConstructor | Uint16ArrayConstructor | Uint8ArrayConstructor;
|
|
27
|
+
/**
|
|
28
|
+
* Get the element size in bytes for a given dtype
|
|
29
|
+
*/
|
|
30
|
+
export declare function getDTypeSize(dtype: DType): number;
|
|
31
|
+
/**
|
|
32
|
+
* Check if dtype is integer
|
|
33
|
+
*/
|
|
34
|
+
export declare function isIntegerDType(dtype: DType): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Check if dtype is floating point
|
|
37
|
+
*/
|
|
38
|
+
export declare function isFloatDType(dtype: DType): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Check if dtype uses BigInt
|
|
41
|
+
*/
|
|
42
|
+
export declare function isBigIntDType(dtype: DType): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Infer dtype from JavaScript value
|
|
45
|
+
*/
|
|
46
|
+
export declare function inferDType(value: unknown): DType;
|
|
47
|
+
/**
|
|
48
|
+
* Promote two dtypes to a common dtype
|
|
49
|
+
* Follows NumPy's type promotion rules
|
|
50
|
+
*/
|
|
51
|
+
export declare function promoteDTypes(dtype1: DType, dtype2: DType): DType;
|
|
52
|
+
/**
|
|
53
|
+
* Validate dtype string
|
|
54
|
+
*/
|
|
55
|
+
export declare function isValidDType(dtype: string): dtype is DType;
|
|
56
|
+
/**
|
|
57
|
+
* Convert value to the appropriate type for the given dtype
|
|
58
|
+
*/
|
|
59
|
+
export declare function castValue(value: number | bigint | boolean, dtype: DType): number | bigint;
|
|
60
|
+
/**
|
|
61
|
+
* Map our dtype to @stdlib's supported dtype
|
|
62
|
+
* @stdlib doesn't support int64, uint64 natively, but does support bool
|
|
63
|
+
*/
|
|
64
|
+
export declare function toStdlibDType(dtype: DType): string;
|
|
65
|
+
export {};
|
|
66
|
+
//# sourceMappingURL=dtype.d.ts.map
|
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NDArray - NumPy-compatible multidimensional array
|
|
3
|
+
*
|
|
4
|
+
* Thin wrapper around @stdlib/ndarray providing NumPy-like API
|
|
5
|
+
*/
|
|
6
|
+
import { type DType, type TypedArray } from './dtype';
|
|
7
|
+
import { ArrayStorage } from './storage';
|
|
8
|
+
type StdlibNDArray = any;
|
|
9
|
+
export declare class NDArray {
|
|
10
|
+
private _data;
|
|
11
|
+
private _dtype?;
|
|
12
|
+
private _base?;
|
|
13
|
+
constructor(stdlibArray: StdlibNDArray, dtype?: DType, base?: NDArray);
|
|
14
|
+
/**
|
|
15
|
+
* Get internal storage (for ops modules)
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
get _storage(): ArrayStorage;
|
|
19
|
+
/**
|
|
20
|
+
* Create NDArray from storage (for ops modules)
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
static _fromStorage(storage: ArrayStorage, base?: NDArray): NDArray;
|
|
24
|
+
get shape(): readonly number[];
|
|
25
|
+
get ndim(): number;
|
|
26
|
+
get size(): number;
|
|
27
|
+
get dtype(): string;
|
|
28
|
+
get data(): TypedArray;
|
|
29
|
+
get strides(): readonly number[];
|
|
30
|
+
/**
|
|
31
|
+
* Array flags (similar to NumPy's flags)
|
|
32
|
+
* Provides information about memory layout
|
|
33
|
+
*/
|
|
34
|
+
get flags(): {
|
|
35
|
+
C_CONTIGUOUS: boolean;
|
|
36
|
+
F_CONTIGUOUS: boolean;
|
|
37
|
+
OWNDATA: boolean;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Base array if this is a view, null if this array owns its data
|
|
41
|
+
* Similar to NumPy's base attribute
|
|
42
|
+
*/
|
|
43
|
+
get base(): NDArray | null;
|
|
44
|
+
/**
|
|
45
|
+
* Get a single element from the array
|
|
46
|
+
* @param indices - Array of indices, one per dimension (e.g., [0, 1] for 2D array)
|
|
47
|
+
* @returns The element value (BigInt for int64/uint64, number otherwise)
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* const arr = array([[1, 2], [3, 4]]);
|
|
52
|
+
* arr.get([0, 1]); // Returns 2
|
|
53
|
+
* arr.get([-1, -1]); // Returns 4 (negative indexing supported)
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
get(indices: number[]): number | bigint;
|
|
57
|
+
/**
|
|
58
|
+
* Set a single element in the array
|
|
59
|
+
* @param indices - Array of indices, one per dimension (e.g., [0, 1] for 2D array)
|
|
60
|
+
* @param value - Value to set (will be converted to array's dtype)
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const arr = zeros([2, 3]);
|
|
65
|
+
* arr.set([0, 1], 42); // Set element at position [0, 1] to 42
|
|
66
|
+
* arr.set([-1, -1], 99); // Set last element to 99 (negative indexing supported)
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
set(indices: number[], value: number | bigint): void;
|
|
70
|
+
/**
|
|
71
|
+
* Return a deep copy of the array
|
|
72
|
+
* Creates a new array with copied data (not a view)
|
|
73
|
+
*
|
|
74
|
+
* @returns Deep copy of the array
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const arr = array([[1, 2], [3, 4]]);
|
|
79
|
+
* const copied = arr.copy();
|
|
80
|
+
* copied.set([0, 0], 99); // Doesn't affect original
|
|
81
|
+
* console.log(arr.get([0, 0])); // Still 1
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
copy(): NDArray;
|
|
85
|
+
/**
|
|
86
|
+
* Cast array to a different dtype
|
|
87
|
+
* @param dtype - Target dtype
|
|
88
|
+
* @param copy - If false and dtype matches, return self; otherwise create copy (default: true)
|
|
89
|
+
* @returns Array with specified dtype
|
|
90
|
+
*/
|
|
91
|
+
astype(dtype: DType, copy?: boolean): NDArray;
|
|
92
|
+
/**
|
|
93
|
+
* Helper method for element-wise operations with broadcasting
|
|
94
|
+
* @private
|
|
95
|
+
*/
|
|
96
|
+
add(other: NDArray | number): NDArray;
|
|
97
|
+
subtract(other: NDArray | number): NDArray;
|
|
98
|
+
multiply(other: NDArray | number): NDArray;
|
|
99
|
+
divide(other: NDArray | number): NDArray;
|
|
100
|
+
/**
|
|
101
|
+
* Element-wise greater than comparison
|
|
102
|
+
* @param other - Value or array to compare with
|
|
103
|
+
* @returns Boolean array (represented as uint8: 1=true, 0=false)
|
|
104
|
+
*/
|
|
105
|
+
greater(other: NDArray | number): NDArray;
|
|
106
|
+
/**
|
|
107
|
+
* Element-wise greater than or equal comparison
|
|
108
|
+
* @param other - Value or array to compare with
|
|
109
|
+
* @returns Boolean array (represented as uint8: 1=true, 0=false)
|
|
110
|
+
*/
|
|
111
|
+
greater_equal(other: NDArray | number): NDArray;
|
|
112
|
+
/**
|
|
113
|
+
* Element-wise less than comparison
|
|
114
|
+
* @param other - Value or array to compare with
|
|
115
|
+
* @returns Boolean array (represented as uint8: 1=true, 0=false)
|
|
116
|
+
*/
|
|
117
|
+
less(other: NDArray | number): NDArray;
|
|
118
|
+
/**
|
|
119
|
+
* Element-wise less than or equal comparison
|
|
120
|
+
* @param other - Value or array to compare with
|
|
121
|
+
* @returns Boolean array (represented as uint8: 1=true, 0=false)
|
|
122
|
+
*/
|
|
123
|
+
less_equal(other: NDArray | number): NDArray;
|
|
124
|
+
/**
|
|
125
|
+
* Element-wise equality comparison
|
|
126
|
+
* @param other - Value or array to compare with
|
|
127
|
+
* @returns Boolean array (represented as uint8: 1=true, 0=false)
|
|
128
|
+
*/
|
|
129
|
+
equal(other: NDArray | number): NDArray;
|
|
130
|
+
/**
|
|
131
|
+
* Element-wise not equal comparison
|
|
132
|
+
* @param other - Value or array to compare with
|
|
133
|
+
* @returns Boolean array (represented as uint8: 1=true, 0=false)
|
|
134
|
+
*/
|
|
135
|
+
not_equal(other: NDArray | number): NDArray;
|
|
136
|
+
/**
|
|
137
|
+
* Element-wise comparison with tolerance
|
|
138
|
+
* Returns True where |a - b| <= (atol + rtol * |b|)
|
|
139
|
+
* @param other - Value or array to compare with
|
|
140
|
+
* @param rtol - Relative tolerance (default: 1e-5)
|
|
141
|
+
* @param atol - Absolute tolerance (default: 1e-8)
|
|
142
|
+
* @returns Boolean array (represented as uint8: 1=true, 0=false)
|
|
143
|
+
*/
|
|
144
|
+
isclose(other: NDArray | number, rtol?: number, atol?: number): NDArray;
|
|
145
|
+
allclose(other: NDArray | number, rtol?: number, atol?: number): boolean;
|
|
146
|
+
/**
|
|
147
|
+
* Helper method for isclose operation with broadcasting
|
|
148
|
+
* @private
|
|
149
|
+
*/
|
|
150
|
+
/**
|
|
151
|
+
* Helper method for comparison operations with broadcasting
|
|
152
|
+
* @private
|
|
153
|
+
*/
|
|
154
|
+
/**
|
|
155
|
+
* Sum array elements over a given axis
|
|
156
|
+
* @param axis - Axis along which to sum. If undefined, sum all elements
|
|
157
|
+
* @param keepdims - If true, reduced axes are left as dimensions with size 1
|
|
158
|
+
* @returns Sum of array elements, or array of sums along axis
|
|
159
|
+
*/
|
|
160
|
+
sum(axis?: number, keepdims?: boolean): NDArray | number;
|
|
161
|
+
/**
|
|
162
|
+
* Compute the arithmetic mean along the specified axis
|
|
163
|
+
* @param axis - Axis along which to compute mean. If undefined, compute mean of all elements
|
|
164
|
+
* @param keepdims - If true, reduced axes are left as dimensions with size 1
|
|
165
|
+
* @returns Mean of array elements, or array of means along axis
|
|
166
|
+
*
|
|
167
|
+
* Note: mean() returns float64 for integer dtypes, matching NumPy behavior
|
|
168
|
+
*/
|
|
169
|
+
mean(axis?: number, keepdims?: boolean): NDArray | number;
|
|
170
|
+
/**
|
|
171
|
+
* Return the maximum along a given axis
|
|
172
|
+
* @param axis - Axis along which to compute maximum. If undefined, compute maximum of all elements
|
|
173
|
+
* @param keepdims - If true, reduced axes are left as dimensions with size 1
|
|
174
|
+
* @returns Maximum of array elements, or array of maximums along axis
|
|
175
|
+
*/
|
|
176
|
+
max(axis?: number, keepdims?: boolean): NDArray | number;
|
|
177
|
+
/**
|
|
178
|
+
* Return the minimum along a given axis
|
|
179
|
+
* @param axis - Axis along which to compute minimum. If undefined, compute minimum of all elements
|
|
180
|
+
* @param keepdims - If true, reduced axes are left as dimensions with size 1
|
|
181
|
+
* @returns Minimum of array elements, or array of minimums along axis
|
|
182
|
+
*/
|
|
183
|
+
min(axis?: number, keepdims?: boolean): NDArray | number;
|
|
184
|
+
/**
|
|
185
|
+
* Product of array elements over a given axis
|
|
186
|
+
* @param axis - Axis along which to compute the product. If undefined, product of all elements.
|
|
187
|
+
* @param keepdims - If true, reduced axes are left as dimensions with size 1
|
|
188
|
+
* @returns Product of array elements, or array of products along axis
|
|
189
|
+
*/
|
|
190
|
+
prod(axis?: number, keepdims?: boolean): NDArray | number;
|
|
191
|
+
/**
|
|
192
|
+
* Indices of the minimum values along an axis
|
|
193
|
+
* @param axis - Axis along which to find minimum indices. If undefined, index of global minimum.
|
|
194
|
+
* @returns Indices of minimum values
|
|
195
|
+
*/
|
|
196
|
+
argmin(axis?: number): NDArray | number;
|
|
197
|
+
/**
|
|
198
|
+
* Indices of the maximum values along an axis
|
|
199
|
+
* @param axis - Axis along which to find maximum indices. If undefined, index of global maximum.
|
|
200
|
+
* @returns Indices of maximum values
|
|
201
|
+
*/
|
|
202
|
+
argmax(axis?: number): NDArray | number;
|
|
203
|
+
/**
|
|
204
|
+
* Compute variance along the specified axis
|
|
205
|
+
* @param axis - Axis along which to compute variance. If undefined, variance of all elements.
|
|
206
|
+
* @param ddof - Delta degrees of freedom (default: 0)
|
|
207
|
+
* @param keepdims - If true, reduced axes are left as dimensions with size 1
|
|
208
|
+
* @returns Variance of array elements
|
|
209
|
+
*/
|
|
210
|
+
var(axis?: number, ddof?: number, keepdims?: boolean): NDArray | number;
|
|
211
|
+
/**
|
|
212
|
+
* Compute standard deviation along the specified axis
|
|
213
|
+
* @param axis - Axis along which to compute std. If undefined, std of all elements.
|
|
214
|
+
* @param ddof - Delta degrees of freedom (default: 0)
|
|
215
|
+
* @param keepdims - If true, reduced axes are left as dimensions with size 1
|
|
216
|
+
* @returns Standard deviation of array elements
|
|
217
|
+
*/
|
|
218
|
+
std(axis?: number, ddof?: number, keepdims?: boolean): NDArray | number;
|
|
219
|
+
/**
|
|
220
|
+
* Test whether all array elements along a given axis evaluate to True
|
|
221
|
+
* @param axis - Axis along which to perform logical AND. If undefined, test all elements.
|
|
222
|
+
* @param keepdims - If true, reduced axes are left as dimensions with size 1
|
|
223
|
+
* @returns Boolean or array of booleans
|
|
224
|
+
*/
|
|
225
|
+
all(axis?: number, keepdims?: boolean): NDArray | boolean;
|
|
226
|
+
/**
|
|
227
|
+
* Test whether any array elements along a given axis evaluate to True
|
|
228
|
+
* @param axis - Axis along which to perform logical OR. If undefined, test all elements.
|
|
229
|
+
* @param keepdims - If true, reduced axes are left as dimensions with size 1
|
|
230
|
+
* @returns Boolean or array of booleans
|
|
231
|
+
*/
|
|
232
|
+
any(axis?: number, keepdims?: boolean): NDArray | boolean;
|
|
233
|
+
/**
|
|
234
|
+
* Reshape array to a new shape
|
|
235
|
+
* Returns a new array with the specified shape
|
|
236
|
+
* @param shape - New shape (must be compatible with current size)
|
|
237
|
+
* @returns Reshaped array
|
|
238
|
+
*/
|
|
239
|
+
reshape(...shape: number[]): NDArray;
|
|
240
|
+
/**
|
|
241
|
+
* Return a flattened copy of the array
|
|
242
|
+
* @returns 1D array containing all elements
|
|
243
|
+
*/
|
|
244
|
+
flatten(): NDArray;
|
|
245
|
+
/**
|
|
246
|
+
* Return a flattened array (view when possible, otherwise copy)
|
|
247
|
+
* @returns 1D array containing all elements
|
|
248
|
+
*/
|
|
249
|
+
ravel(): NDArray;
|
|
250
|
+
/**
|
|
251
|
+
* Transpose array (permute dimensions)
|
|
252
|
+
* @param axes - Permutation of axes. If undefined, reverse the dimensions
|
|
253
|
+
* @returns Transposed array (always a view)
|
|
254
|
+
*/
|
|
255
|
+
transpose(axes?: number[]): NDArray;
|
|
256
|
+
/**
|
|
257
|
+
* Remove axes of length 1
|
|
258
|
+
* @param axis - Axis to squeeze. If undefined, squeeze all axes of length 1
|
|
259
|
+
* @returns Array with specified dimensions removed (always a view)
|
|
260
|
+
*/
|
|
261
|
+
squeeze(axis?: number): NDArray;
|
|
262
|
+
/**
|
|
263
|
+
* Expand the shape by inserting a new axis of length 1
|
|
264
|
+
* @param axis - Position where new axis is placed
|
|
265
|
+
* @returns Array with additional dimension (always a view)
|
|
266
|
+
*/
|
|
267
|
+
expand_dims(axis: number): NDArray;
|
|
268
|
+
matmul(other: NDArray): NDArray;
|
|
269
|
+
/**
|
|
270
|
+
* Slice the array using NumPy-style string syntax
|
|
271
|
+
*
|
|
272
|
+
* @param sliceStrs - Slice specifications, one per dimension
|
|
273
|
+
* @returns Sliced view of the array
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* ```typescript
|
|
277
|
+
* const arr = array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
|
|
278
|
+
* const row = arr.slice('0', ':'); // First row: [1, 2, 3]
|
|
279
|
+
* const col = arr.slice(':', '1'); // Second column: [2, 5, 8]
|
|
280
|
+
* const sub = arr.slice('0:2', '1:3'); // Top-right 2x2: [[2, 3], [5, 6]]
|
|
281
|
+
* ```
|
|
282
|
+
*/
|
|
283
|
+
slice(...sliceStrs: string[]): NDArray;
|
|
284
|
+
/**
|
|
285
|
+
* Get a single row (convenience method)
|
|
286
|
+
* @param i - Row index
|
|
287
|
+
* @returns Row as 1D or (n-1)D array
|
|
288
|
+
*/
|
|
289
|
+
row(i: number): NDArray;
|
|
290
|
+
/**
|
|
291
|
+
* Get a single column (convenience method)
|
|
292
|
+
* @param j - Column index
|
|
293
|
+
* @returns Column as 1D or (n-1)D array
|
|
294
|
+
*/
|
|
295
|
+
col(j: number): NDArray;
|
|
296
|
+
/**
|
|
297
|
+
* Get a range of rows (convenience method)
|
|
298
|
+
* @param start - Start row index
|
|
299
|
+
* @param stop - Stop row index (exclusive)
|
|
300
|
+
* @returns Rows as array
|
|
301
|
+
*/
|
|
302
|
+
rows(start: number, stop: number): NDArray;
|
|
303
|
+
/**
|
|
304
|
+
* Get a range of columns (convenience method)
|
|
305
|
+
* @param start - Start column index
|
|
306
|
+
* @param stop - Stop column index (exclusive)
|
|
307
|
+
* @returns Columns as array
|
|
308
|
+
*/
|
|
309
|
+
cols(start: number, stop: number): NDArray;
|
|
310
|
+
toString(): string;
|
|
311
|
+
toArray(): any;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Create array of zeros
|
|
315
|
+
*/
|
|
316
|
+
export declare function zeros(shape: number[], dtype?: DType): NDArray;
|
|
317
|
+
/**
|
|
318
|
+
* Create array of ones
|
|
319
|
+
*/
|
|
320
|
+
export declare function ones(shape: number[], dtype?: DType): NDArray;
|
|
321
|
+
/**
|
|
322
|
+
* Create array from nested JavaScript arrays
|
|
323
|
+
*/
|
|
324
|
+
export declare function array(data: any, dtype?: DType): NDArray;
|
|
325
|
+
/**
|
|
326
|
+
* Create array with evenly spaced values within a given interval
|
|
327
|
+
* Similar to Python's range() but returns floats
|
|
328
|
+
*/
|
|
329
|
+
export declare function arange(start: number, stop?: number, step?: number, dtype?: DType): NDArray;
|
|
330
|
+
/**
|
|
331
|
+
* Create array with evenly spaced values over a specified interval
|
|
332
|
+
*/
|
|
333
|
+
export declare function linspace(start: number, stop: number, num?: number, dtype?: DType): NDArray;
|
|
334
|
+
/**
|
|
335
|
+
* Create array with logarithmically spaced values
|
|
336
|
+
* Returns num samples, equally spaced on a log scale from base^start to base^stop
|
|
337
|
+
*/
|
|
338
|
+
export declare function logspace(start: number, stop: number, num?: number, base?: number, dtype?: DType): NDArray;
|
|
339
|
+
/**
|
|
340
|
+
* Create array with geometrically spaced values
|
|
341
|
+
* Returns num samples, equally spaced on a log scale (geometric progression)
|
|
342
|
+
*/
|
|
343
|
+
export declare function geomspace(start: number, stop: number, num?: number, dtype?: DType): NDArray;
|
|
344
|
+
/**
|
|
345
|
+
* Create identity matrix
|
|
346
|
+
*/
|
|
347
|
+
export declare function eye(n: number, m?: number, k?: number, dtype?: DType): NDArray;
|
|
348
|
+
/**
|
|
349
|
+
* Create an uninitialized array
|
|
350
|
+
* Note: Unlike NumPy, TypedArrays are zero-initialized by default in JavaScript
|
|
351
|
+
*/
|
|
352
|
+
export declare function empty(shape: number[], dtype?: DType): NDArray;
|
|
353
|
+
/**
|
|
354
|
+
* Create array filled with a constant value
|
|
355
|
+
*/
|
|
356
|
+
export declare function full(shape: number[], fill_value: number | bigint | boolean, dtype?: DType): NDArray;
|
|
357
|
+
/**
|
|
358
|
+
* Create a square identity matrix
|
|
359
|
+
*/
|
|
360
|
+
export declare function identity(n: number, dtype?: DType): NDArray;
|
|
361
|
+
/**
|
|
362
|
+
* Convert input to an ndarray
|
|
363
|
+
* If input is already an NDArray, optionally convert dtype
|
|
364
|
+
*/
|
|
365
|
+
export declare function asarray(a: NDArray | any, dtype?: DType): NDArray;
|
|
366
|
+
/**
|
|
367
|
+
* Create a deep copy of an array
|
|
368
|
+
*/
|
|
369
|
+
export declare function copy(a: NDArray): NDArray;
|
|
370
|
+
/**
|
|
371
|
+
* Create array of zeros with the same shape as another array
|
|
372
|
+
*/
|
|
373
|
+
export declare function zeros_like(a: NDArray, dtype?: DType): NDArray;
|
|
374
|
+
/**
|
|
375
|
+
* Create array of ones with the same shape as another array
|
|
376
|
+
*/
|
|
377
|
+
export declare function ones_like(a: NDArray, dtype?: DType): NDArray;
|
|
378
|
+
/**
|
|
379
|
+
* Create uninitialized array with the same shape as another array
|
|
380
|
+
*/
|
|
381
|
+
export declare function empty_like(a: NDArray, dtype?: DType): NDArray;
|
|
382
|
+
/**
|
|
383
|
+
* Create array filled with a constant value, same shape as another array
|
|
384
|
+
*/
|
|
385
|
+
export declare function full_like(a: NDArray, fill_value: number | bigint | boolean, dtype?: DType): NDArray;
|
|
386
|
+
export {};
|
|
387
|
+
//# sourceMappingURL=ndarray.d.ts.map
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slicing utilities for NumPy-compatible array indexing
|
|
3
|
+
*
|
|
4
|
+
* Supports Python-style slice syntax via strings: "0:5", ":", "::2", "-1"
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Represents a parsed slice specification
|
|
8
|
+
*/
|
|
9
|
+
export interface SliceSpec {
|
|
10
|
+
start: number | null;
|
|
11
|
+
stop: number | null;
|
|
12
|
+
step: number;
|
|
13
|
+
isIndex: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Parse a slice string into a SliceSpec
|
|
17
|
+
*
|
|
18
|
+
* Supports:
|
|
19
|
+
* - Single index: "5", "-1"
|
|
20
|
+
* - Full slice: "0:5", "2:8"
|
|
21
|
+
* - With step: "0:10:2", "::2"
|
|
22
|
+
* - Partial: "5:", ":10", ":"
|
|
23
|
+
* - Negative: "-5:", ":-2", "::-1"
|
|
24
|
+
*
|
|
25
|
+
* @param sliceStr - String representation of slice (e.g., "0:5", ":", "::2")
|
|
26
|
+
* @returns Parsed slice specification
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* parseSlice("0:5") // {start: 0, stop: 5, step: 1, isIndex: false}
|
|
31
|
+
* parseSlice(":") // {start: null, stop: null, step: 1, isIndex: false}
|
|
32
|
+
* parseSlice("::2") // {start: null, stop: null, step: 2, isIndex: false}
|
|
33
|
+
* parseSlice("-1") // {start: -1, stop: null, step: 1, isIndex: true}
|
|
34
|
+
* parseSlice("5") // {start: 5, stop: null, step: 1, isIndex: true}
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare function parseSlice(sliceStr: string): SliceSpec;
|
|
38
|
+
/**
|
|
39
|
+
* Normalize a slice specification to absolute indices
|
|
40
|
+
*
|
|
41
|
+
* Handles negative indices and defaults:
|
|
42
|
+
* - Negative indices count from the end
|
|
43
|
+
* - null start becomes 0 (or size-1 for negative step)
|
|
44
|
+
* - null stop becomes size (or -1 for negative step)
|
|
45
|
+
*
|
|
46
|
+
* @param spec - Parsed slice specification
|
|
47
|
+
* @param size - Size of the dimension being sliced
|
|
48
|
+
* @returns Normalized slice with absolute start, stop, step
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* normalizeSlice({start: -1, stop: null, step: 1, isIndex: true}, 10)
|
|
53
|
+
* // {start: 9, stop: 10, step: 1, isIndex: true}
|
|
54
|
+
*
|
|
55
|
+
* normalizeSlice({start: null, stop: -2, step: 1, isIndex: false}, 10)
|
|
56
|
+
* // {start: 0, stop: 8, step: 1, isIndex: false}
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export declare function normalizeSlice(spec: SliceSpec, size: number): {
|
|
60
|
+
start: number;
|
|
61
|
+
stop: number;
|
|
62
|
+
step: number;
|
|
63
|
+
isIndex: boolean;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Compute the length of a slice result
|
|
67
|
+
*
|
|
68
|
+
* @param start - Normalized start index
|
|
69
|
+
* @param stop - Normalized stop index
|
|
70
|
+
* @param step - Step value
|
|
71
|
+
* @returns Number of elements in the slice
|
|
72
|
+
*/
|
|
73
|
+
export declare function computeSliceLength(start: number, stop: number, step: number): number;
|
|
74
|
+
/**
|
|
75
|
+
* Parse multiple slice specifications for multi-dimensional indexing
|
|
76
|
+
*
|
|
77
|
+
* @param sliceStrs - Array of slice strings, one per dimension
|
|
78
|
+
* @returns Array of parsed slice specifications
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* parseSlices(["0:5", ":", "::2"])
|
|
83
|
+
* // [
|
|
84
|
+
* // {start: 0, stop: 5, step: 1, isIndex: false},
|
|
85
|
+
* // {start: null, stop: null, step: 1, isIndex: false},
|
|
86
|
+
* // {start: null, stop: null, step: 2, isIndex: false}
|
|
87
|
+
* // ]
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export declare function parseSlices(sliceStrs: string[]): SliceSpec[];
|
|
91
|
+
//# sourceMappingURL=slicing.d.ts.map
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ArrayStorage - Internal storage abstraction
|
|
3
|
+
*
|
|
4
|
+
* Wraps @stdlib/ndarray to provide a clean internal interface
|
|
5
|
+
* while keeping stdlib as an implementation detail.
|
|
6
|
+
*
|
|
7
|
+
* @internal - This is not part of the public API
|
|
8
|
+
*/
|
|
9
|
+
import type { ndarray as StdlibNDArray } from '@stdlib/types/ndarray';
|
|
10
|
+
import { type DType, type TypedArray } from './dtype';
|
|
11
|
+
/**
|
|
12
|
+
* Internal storage for NDArray data
|
|
13
|
+
* Manages the underlying TypedArray and metadata
|
|
14
|
+
*/
|
|
15
|
+
export declare class ArrayStorage {
|
|
16
|
+
private _stdlib;
|
|
17
|
+
private _dtype;
|
|
18
|
+
constructor(stdlibArray: StdlibNDArray, dt?: DType);
|
|
19
|
+
/**
|
|
20
|
+
* Shape of the array
|
|
21
|
+
*/
|
|
22
|
+
get shape(): readonly number[];
|
|
23
|
+
/**
|
|
24
|
+
* Number of dimensions
|
|
25
|
+
*/
|
|
26
|
+
get ndim(): number;
|
|
27
|
+
/**
|
|
28
|
+
* Total number of elements
|
|
29
|
+
*/
|
|
30
|
+
get size(): number;
|
|
31
|
+
/**
|
|
32
|
+
* Data type
|
|
33
|
+
*/
|
|
34
|
+
get dtype(): DType;
|
|
35
|
+
/**
|
|
36
|
+
* Underlying data buffer
|
|
37
|
+
*/
|
|
38
|
+
get data(): TypedArray;
|
|
39
|
+
/**
|
|
40
|
+
* Strides (steps in each dimension)
|
|
41
|
+
*/
|
|
42
|
+
get strides(): readonly number[];
|
|
43
|
+
/**
|
|
44
|
+
* Direct access to stdlib ndarray (for internal use)
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
get stdlib(): StdlibNDArray;
|
|
48
|
+
/**
|
|
49
|
+
* Check if array is C-contiguous (row-major, no gaps)
|
|
50
|
+
*/
|
|
51
|
+
get isCContiguous(): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Check if array is F-contiguous (column-major, no gaps)
|
|
54
|
+
*/
|
|
55
|
+
get isFContiguous(): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Create a deep copy of this storage
|
|
58
|
+
*/
|
|
59
|
+
copy(): ArrayStorage;
|
|
60
|
+
/**
|
|
61
|
+
* Create storage from stdlib ndarray
|
|
62
|
+
*/
|
|
63
|
+
static fromStdlib(stdlibArray: StdlibNDArray, dtype?: DType): ArrayStorage;
|
|
64
|
+
/**
|
|
65
|
+
* Create storage from TypedArray data
|
|
66
|
+
*/
|
|
67
|
+
static fromData(data: TypedArray, shape: number[], dtype: DType): ArrayStorage;
|
|
68
|
+
/**
|
|
69
|
+
* Create storage with zeros
|
|
70
|
+
*/
|
|
71
|
+
static zeros(shape: number[], dtype?: DType): ArrayStorage;
|
|
72
|
+
/**
|
|
73
|
+
* Create storage with ones
|
|
74
|
+
*/
|
|
75
|
+
static ones(shape: number[], dtype?: DType): ArrayStorage;
|
|
76
|
+
/**
|
|
77
|
+
* Compute strides for row-major (C-order) layout
|
|
78
|
+
* @private
|
|
79
|
+
*/
|
|
80
|
+
private _computeStrides;
|
|
81
|
+
/**
|
|
82
|
+
* Compute strides for row-major (C-order) layout
|
|
83
|
+
* @private
|
|
84
|
+
*/
|
|
85
|
+
private static _computeStrides;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Compute strides for a given shape (row-major order)
|
|
89
|
+
* @internal
|
|
90
|
+
*/
|
|
91
|
+
export declare function computeStrides(shape: readonly number[]): number[];
|
|
92
|
+
//# sourceMappingURL=storage.d.ts.map
|