numpy-ts 0.1.0 → 0.3.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/README.md +63 -138
- package/dist/numpy-ts.browser.js +99 -1067
- package/dist/numpy-ts.esm.js +99 -1067
- package/dist/numpy-ts.node.cjs +125 -1078
- package/dist/types/core/broadcasting.d.ts +16 -13
- package/dist/types/core/ndarray.d.ts +213 -52
- package/dist/types/core/storage.d.ts +25 -18
- package/dist/types/index.d.ts +1 -1
- package/dist/types/internal/compute.d.ts +18 -0
- package/dist/types/ops/arithmetic.d.ts +59 -0
- package/dist/types/ops/exponential.d.ts +28 -0
- package/package.json +8 -6
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Broadcasting utilities for NumPy-compatible array operations
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Implements NumPy broadcasting rules without external dependencies
|
|
5
5
|
*/
|
|
6
|
-
|
|
6
|
+
import { ArrayStorage } from './storage';
|
|
7
7
|
/**
|
|
8
8
|
* Check if two or more shapes are broadcast-compatible
|
|
9
9
|
* and compute the resulting output shape
|
|
@@ -35,21 +35,25 @@ export declare function computeBroadcastShape(shapes: readonly number[][]): numb
|
|
|
35
35
|
*/
|
|
36
36
|
export declare function areBroadcastable(shape1: readonly number[], shape2: readonly number[]): boolean;
|
|
37
37
|
/**
|
|
38
|
-
* Broadcast
|
|
38
|
+
* Broadcast an ArrayStorage to a target shape
|
|
39
|
+
* Returns a view with modified strides for broadcasting
|
|
39
40
|
*
|
|
40
|
-
*
|
|
41
|
+
* @param storage - The storage to broadcast
|
|
42
|
+
* @param targetShape - The target shape to broadcast to
|
|
43
|
+
* @returns A new ArrayStorage view with broadcasting strides
|
|
44
|
+
*/
|
|
45
|
+
export declare function broadcastTo(storage: ArrayStorage, targetShape: readonly number[]): ArrayStorage;
|
|
46
|
+
/**
|
|
47
|
+
* Broadcast multiple ArrayStorage objects to a common shape
|
|
48
|
+
*
|
|
49
|
+
* Returns views of the input arrays broadcast to the same shape.
|
|
41
50
|
* Views share memory with the original arrays.
|
|
42
51
|
*
|
|
43
|
-
* @param
|
|
44
|
-
* @returns Array of broadcast
|
|
52
|
+
* @param storages - ArrayStorage objects to broadcast
|
|
53
|
+
* @returns Array of broadcast ArrayStorage views
|
|
45
54
|
* @throws Error if arrays have incompatible shapes
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* ```typescript
|
|
49
|
-
* const [a, b] = broadcastStdlibArrays([arr1._data, arr2._data]);
|
|
50
|
-
* ```
|
|
51
55
|
*/
|
|
52
|
-
export declare function
|
|
56
|
+
export declare function broadcastArrays(storages: ArrayStorage[]): ArrayStorage[];
|
|
53
57
|
/**
|
|
54
58
|
* Generate a descriptive error message for broadcasting failures
|
|
55
59
|
*
|
|
@@ -58,5 +62,4 @@ export declare function broadcastStdlibArrays(arrays: StdlibNDArray[]): StdlibND
|
|
|
58
62
|
* @returns Error message string
|
|
59
63
|
*/
|
|
60
64
|
export declare function broadcastErrorMessage(shapes: readonly number[][], operation?: string): string;
|
|
61
|
-
export {};
|
|
62
65
|
//# sourceMappingURL=broadcasting.d.ts.map
|
|
@@ -1,21 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* NDArray - NumPy-compatible multidimensional array
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Core array class providing NumPy-like API
|
|
5
5
|
*/
|
|
6
6
|
import { type DType, type TypedArray } from './dtype';
|
|
7
7
|
import { ArrayStorage } from './storage';
|
|
8
|
-
type StdlibNDArray = any;
|
|
9
8
|
export declare class NDArray {
|
|
10
|
-
private
|
|
11
|
-
private _dtype?;
|
|
9
|
+
private _storage;
|
|
12
10
|
private _base?;
|
|
13
|
-
constructor(
|
|
11
|
+
constructor(storage: ArrayStorage, base?: NDArray);
|
|
14
12
|
/**
|
|
15
13
|
* Get internal storage (for ops modules)
|
|
16
14
|
* @internal
|
|
17
15
|
*/
|
|
18
|
-
get
|
|
16
|
+
get storage(): ArrayStorage;
|
|
19
17
|
/**
|
|
20
18
|
* Create NDArray from storage (for ops modules)
|
|
21
19
|
* @internal
|
|
@@ -45,41 +43,16 @@ export declare class NDArray {
|
|
|
45
43
|
* Get a single element from the array
|
|
46
44
|
* @param indices - Array of indices, one per dimension (e.g., [0, 1] for 2D array)
|
|
47
45
|
* @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
46
|
*/
|
|
56
47
|
get(indices: number[]): number | bigint;
|
|
57
48
|
/**
|
|
58
49
|
* Set a single element in the array
|
|
59
50
|
* @param indices - Array of indices, one per dimension (e.g., [0, 1] for 2D array)
|
|
60
51
|
* @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
52
|
*/
|
|
69
53
|
set(indices: number[], value: number | bigint): void;
|
|
70
54
|
/**
|
|
71
55
|
* 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
56
|
*/
|
|
84
57
|
copy(): NDArray;
|
|
85
58
|
/**
|
|
@@ -90,13 +63,78 @@ export declare class NDArray {
|
|
|
90
63
|
*/
|
|
91
64
|
astype(dtype: DType, copy?: boolean): NDArray;
|
|
92
65
|
/**
|
|
93
|
-
*
|
|
94
|
-
* @
|
|
66
|
+
* Element-wise addition
|
|
67
|
+
* @param other - Array or scalar to add
|
|
68
|
+
* @returns Result of addition with broadcasting
|
|
95
69
|
*/
|
|
96
70
|
add(other: NDArray | number): NDArray;
|
|
71
|
+
/**
|
|
72
|
+
* Element-wise subtraction
|
|
73
|
+
* @param other - Array or scalar to subtract
|
|
74
|
+
* @returns Result of subtraction with broadcasting
|
|
75
|
+
*/
|
|
97
76
|
subtract(other: NDArray | number): NDArray;
|
|
77
|
+
/**
|
|
78
|
+
* Element-wise multiplication
|
|
79
|
+
* @param other - Array or scalar to multiply
|
|
80
|
+
* @returns Result of multiplication with broadcasting
|
|
81
|
+
*/
|
|
98
82
|
multiply(other: NDArray | number): NDArray;
|
|
83
|
+
/**
|
|
84
|
+
* Element-wise division
|
|
85
|
+
* @param other - Array or scalar to divide by
|
|
86
|
+
* @returns Result of division with broadcasting
|
|
87
|
+
*/
|
|
99
88
|
divide(other: NDArray | number): NDArray;
|
|
89
|
+
/**
|
|
90
|
+
* Element-wise modulo operation
|
|
91
|
+
* @param other - Array or scalar divisor
|
|
92
|
+
* @returns Remainder after division
|
|
93
|
+
*/
|
|
94
|
+
mod(other: NDArray | number): NDArray;
|
|
95
|
+
/**
|
|
96
|
+
* Element-wise floor division
|
|
97
|
+
* @param other - Array or scalar to divide by
|
|
98
|
+
* @returns Floor of the quotient
|
|
99
|
+
*/
|
|
100
|
+
floor_divide(other: NDArray | number): NDArray;
|
|
101
|
+
/**
|
|
102
|
+
* Numerical positive (element-wise +x)
|
|
103
|
+
* @returns Copy of the array
|
|
104
|
+
*/
|
|
105
|
+
positive(): NDArray;
|
|
106
|
+
/**
|
|
107
|
+
* Element-wise reciprocal (1/x)
|
|
108
|
+
* @returns New array with reciprocals
|
|
109
|
+
*/
|
|
110
|
+
reciprocal(): NDArray;
|
|
111
|
+
/**
|
|
112
|
+
* Square root of each element
|
|
113
|
+
* Promotes integer types to float64
|
|
114
|
+
* @returns New array with square roots
|
|
115
|
+
*/
|
|
116
|
+
sqrt(): NDArray;
|
|
117
|
+
/**
|
|
118
|
+
* Raise elements to power
|
|
119
|
+
* @param exponent - Power to raise to (array or scalar)
|
|
120
|
+
* @returns New array with powered values
|
|
121
|
+
*/
|
|
122
|
+
power(exponent: NDArray | number): NDArray;
|
|
123
|
+
/**
|
|
124
|
+
* Absolute value of each element
|
|
125
|
+
* @returns New array with absolute values
|
|
126
|
+
*/
|
|
127
|
+
absolute(): NDArray;
|
|
128
|
+
/**
|
|
129
|
+
* Numerical negative (element-wise negation)
|
|
130
|
+
* @returns New array with negated values
|
|
131
|
+
*/
|
|
132
|
+
negative(): NDArray;
|
|
133
|
+
/**
|
|
134
|
+
* Sign of each element (-1, 0, or 1)
|
|
135
|
+
* @returns New array with signs
|
|
136
|
+
*/
|
|
137
|
+
sign(): NDArray;
|
|
100
138
|
/**
|
|
101
139
|
* Element-wise greater than comparison
|
|
102
140
|
* @param other - Value or array to compare with
|
|
@@ -142,15 +180,15 @@ export declare class NDArray {
|
|
|
142
180
|
* @returns Boolean array (represented as uint8: 1=true, 0=false)
|
|
143
181
|
*/
|
|
144
182
|
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
183
|
/**
|
|
151
|
-
*
|
|
152
|
-
*
|
|
184
|
+
* Element-wise comparison with tolerance
|
|
185
|
+
* Returns True where |a - b| <= (atol + rtol * |b|)
|
|
186
|
+
* @param other - Value or array to compare with
|
|
187
|
+
* @param rtol - Relative tolerance (default: 1e-5)
|
|
188
|
+
* @param atol - Absolute tolerance (default: 1e-8)
|
|
189
|
+
* @returns Boolean array (represented as uint8: 1=true, 0=false)
|
|
153
190
|
*/
|
|
191
|
+
allclose(other: NDArray | number, rtol?: number, atol?: number): boolean;
|
|
154
192
|
/**
|
|
155
193
|
* Sum array elements over a given axis
|
|
156
194
|
* @param axis - Axis along which to sum. If undefined, sum all elements
|
|
@@ -265,20 +303,17 @@ export declare class NDArray {
|
|
|
265
303
|
* @returns Array with additional dimension (always a view)
|
|
266
304
|
*/
|
|
267
305
|
expand_dims(axis: number): NDArray;
|
|
306
|
+
/**
|
|
307
|
+
* Matrix multiplication
|
|
308
|
+
* @param other - Array to multiply with
|
|
309
|
+
* @returns Result of matrix multiplication
|
|
310
|
+
*/
|
|
268
311
|
matmul(other: NDArray): NDArray;
|
|
269
312
|
/**
|
|
270
313
|
* Slice the array using NumPy-style string syntax
|
|
271
314
|
*
|
|
272
315
|
* @param sliceStrs - Slice specifications, one per dimension
|
|
273
316
|
* @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
317
|
*/
|
|
283
318
|
slice(...sliceStrs: string[]): NDArray;
|
|
284
319
|
/**
|
|
@@ -307,81 +342,207 @@ export declare class NDArray {
|
|
|
307
342
|
* @returns Columns as array
|
|
308
343
|
*/
|
|
309
344
|
cols(start: number, stop: number): NDArray;
|
|
345
|
+
/**
|
|
346
|
+
* String representation of the array
|
|
347
|
+
* @returns String describing the array shape and dtype
|
|
348
|
+
*/
|
|
310
349
|
toString(): string;
|
|
350
|
+
/**
|
|
351
|
+
* Convert to nested JavaScript array
|
|
352
|
+
* @returns Nested JavaScript array representation
|
|
353
|
+
*/
|
|
311
354
|
toArray(): any;
|
|
312
355
|
}
|
|
313
356
|
/**
|
|
314
357
|
* Create array of zeros
|
|
358
|
+
* @param shape - Shape of the array
|
|
359
|
+
* @param dtype - Data type (default: float64)
|
|
360
|
+
* @returns Array filled with zeros
|
|
315
361
|
*/
|
|
316
362
|
export declare function zeros(shape: number[], dtype?: DType): NDArray;
|
|
317
363
|
/**
|
|
318
364
|
* Create array of ones
|
|
365
|
+
* @param shape - Shape of the array
|
|
366
|
+
* @param dtype - Data type (default: float64)
|
|
367
|
+
* @returns Array filled with ones
|
|
319
368
|
*/
|
|
320
369
|
export declare function ones(shape: number[], dtype?: DType): NDArray;
|
|
321
370
|
/**
|
|
322
371
|
* Create array from nested JavaScript arrays
|
|
372
|
+
* @param data - Nested arrays or existing NDArray
|
|
373
|
+
* @param dtype - Data type (optional, will be inferred if not provided)
|
|
374
|
+
* @returns New NDArray
|
|
323
375
|
*/
|
|
324
376
|
export declare function array(data: any, dtype?: DType): NDArray;
|
|
325
377
|
/**
|
|
326
378
|
* Create array with evenly spaced values within a given interval
|
|
327
|
-
* Similar to Python's range() but returns
|
|
379
|
+
* Similar to Python's range() but returns array
|
|
380
|
+
* @param start - Start value (or stop if only one argument)
|
|
381
|
+
* @param stop - Stop value (exclusive)
|
|
382
|
+
* @param step - Step between values (default: 1)
|
|
383
|
+
* @param dtype - Data type (default: float64)
|
|
384
|
+
* @returns Array of evenly spaced values
|
|
328
385
|
*/
|
|
329
386
|
export declare function arange(start: number, stop?: number, step?: number, dtype?: DType): NDArray;
|
|
330
387
|
/**
|
|
331
388
|
* Create array with evenly spaced values over a specified interval
|
|
389
|
+
* @param start - Starting value
|
|
390
|
+
* @param stop - Ending value (inclusive)
|
|
391
|
+
* @param num - Number of samples (default: 50)
|
|
392
|
+
* @param dtype - Data type (default: float64)
|
|
393
|
+
* @returns Array of evenly spaced values
|
|
332
394
|
*/
|
|
333
395
|
export declare function linspace(start: number, stop: number, num?: number, dtype?: DType): NDArray;
|
|
334
396
|
/**
|
|
335
397
|
* Create array with logarithmically spaced values
|
|
336
398
|
* Returns num samples, equally spaced on a log scale from base^start to base^stop
|
|
399
|
+
* @param start - base^start is the starting value
|
|
400
|
+
* @param stop - base^stop is the ending value
|
|
401
|
+
* @param num - Number of samples (default: 50)
|
|
402
|
+
* @param base - Base of the log space (default: 10.0)
|
|
403
|
+
* @param dtype - Data type (default: float64)
|
|
404
|
+
* @returns Array of logarithmically spaced values
|
|
337
405
|
*/
|
|
338
406
|
export declare function logspace(start: number, stop: number, num?: number, base?: number, dtype?: DType): NDArray;
|
|
339
407
|
/**
|
|
340
408
|
* Create array with geometrically spaced values
|
|
341
409
|
* Returns num samples, equally spaced on a log scale (geometric progression)
|
|
410
|
+
* @param start - Starting value
|
|
411
|
+
* @param stop - Ending value
|
|
412
|
+
* @param num - Number of samples (default: 50)
|
|
413
|
+
* @param dtype - Data type (default: float64)
|
|
414
|
+
* @returns Array of geometrically spaced values
|
|
342
415
|
*/
|
|
343
416
|
export declare function geomspace(start: number, stop: number, num?: number, dtype?: DType): NDArray;
|
|
344
417
|
/**
|
|
345
418
|
* Create identity matrix
|
|
419
|
+
* @param n - Number of rows
|
|
420
|
+
* @param m - Number of columns (default: n)
|
|
421
|
+
* @param k - Index of diagonal (0 for main diagonal, positive for upper, negative for lower)
|
|
422
|
+
* @param dtype - Data type (default: float64)
|
|
423
|
+
* @returns Identity matrix
|
|
346
424
|
*/
|
|
347
425
|
export declare function eye(n: number, m?: number, k?: number, dtype?: DType): NDArray;
|
|
348
426
|
/**
|
|
349
427
|
* Create an uninitialized array
|
|
350
|
-
* Note:
|
|
428
|
+
* Note: TypedArrays are zero-initialized by default in JavaScript
|
|
429
|
+
* @param shape - Shape of the array
|
|
430
|
+
* @param dtype - Data type (default: float64)
|
|
431
|
+
* @returns Uninitialized array
|
|
351
432
|
*/
|
|
352
433
|
export declare function empty(shape: number[], dtype?: DType): NDArray;
|
|
353
434
|
/**
|
|
354
435
|
* Create array filled with a constant value
|
|
436
|
+
* @param shape - Shape of the array
|
|
437
|
+
* @param fill_value - Value to fill the array with
|
|
438
|
+
* @param dtype - Data type (optional, inferred from fill_value if not provided)
|
|
439
|
+
* @returns Array filled with the constant value
|
|
355
440
|
*/
|
|
356
441
|
export declare function full(shape: number[], fill_value: number | bigint | boolean, dtype?: DType): NDArray;
|
|
357
442
|
/**
|
|
358
443
|
* Create a square identity matrix
|
|
444
|
+
* @param n - Size of the square matrix
|
|
445
|
+
* @param dtype - Data type (default: float64)
|
|
446
|
+
* @returns n×n identity matrix
|
|
359
447
|
*/
|
|
360
448
|
export declare function identity(n: number, dtype?: DType): NDArray;
|
|
361
449
|
/**
|
|
362
450
|
* Convert input to an ndarray
|
|
363
|
-
*
|
|
451
|
+
* @param a - Input data (array-like or NDArray)
|
|
452
|
+
* @param dtype - Data type (optional)
|
|
453
|
+
* @returns NDArray representation of the input
|
|
364
454
|
*/
|
|
365
455
|
export declare function asarray(a: NDArray | any, dtype?: DType): NDArray;
|
|
366
456
|
/**
|
|
367
457
|
* Create a deep copy of an array
|
|
458
|
+
* @param a - Array to copy
|
|
459
|
+
* @returns Deep copy of the array
|
|
368
460
|
*/
|
|
369
461
|
export declare function copy(a: NDArray): NDArray;
|
|
370
462
|
/**
|
|
371
463
|
* Create array of zeros with the same shape as another array
|
|
464
|
+
* @param a - Array to match shape from
|
|
465
|
+
* @param dtype - Data type (optional, uses a's dtype if not provided)
|
|
466
|
+
* @returns Array of zeros
|
|
372
467
|
*/
|
|
373
468
|
export declare function zeros_like(a: NDArray, dtype?: DType): NDArray;
|
|
374
469
|
/**
|
|
375
470
|
* Create array of ones with the same shape as another array
|
|
471
|
+
* @param a - Array to match shape from
|
|
472
|
+
* @param dtype - Data type (optional, uses a's dtype if not provided)
|
|
473
|
+
* @returns Array of ones
|
|
376
474
|
*/
|
|
377
475
|
export declare function ones_like(a: NDArray, dtype?: DType): NDArray;
|
|
378
476
|
/**
|
|
379
477
|
* Create uninitialized array with the same shape as another array
|
|
478
|
+
* @param a - Array to match shape from
|
|
479
|
+
* @param dtype - Data type (optional, uses a's dtype if not provided)
|
|
480
|
+
* @returns Uninitialized array
|
|
380
481
|
*/
|
|
381
482
|
export declare function empty_like(a: NDArray, dtype?: DType): NDArray;
|
|
382
483
|
/**
|
|
383
484
|
* Create array filled with a constant value, same shape as another array
|
|
485
|
+
* @param a - Array to match shape from
|
|
486
|
+
* @param fill_value - Value to fill with
|
|
487
|
+
* @param dtype - Data type (optional, uses a's dtype if not provided)
|
|
488
|
+
* @returns Filled array
|
|
384
489
|
*/
|
|
385
490
|
export declare function full_like(a: NDArray, fill_value: number | bigint | boolean, dtype?: DType): NDArray;
|
|
386
|
-
|
|
491
|
+
/**
|
|
492
|
+
* Element-wise square root
|
|
493
|
+
* @param x - Input array
|
|
494
|
+
* @returns Array of square roots
|
|
495
|
+
*/
|
|
496
|
+
export declare function sqrt(x: NDArray): NDArray;
|
|
497
|
+
/**
|
|
498
|
+
* Element-wise power
|
|
499
|
+
* @param x - Base array
|
|
500
|
+
* @param exponent - Exponent (array or scalar)
|
|
501
|
+
* @returns Array of x raised to exponent
|
|
502
|
+
*/
|
|
503
|
+
export declare function power(x: NDArray, exponent: NDArray | number): NDArray;
|
|
504
|
+
/**
|
|
505
|
+
* Element-wise absolute value
|
|
506
|
+
* @param x - Input array
|
|
507
|
+
* @returns Array of absolute values
|
|
508
|
+
*/
|
|
509
|
+
export declare function absolute(x: NDArray): NDArray;
|
|
510
|
+
/**
|
|
511
|
+
* Element-wise negation
|
|
512
|
+
* @param x - Input array
|
|
513
|
+
* @returns Array of negated values
|
|
514
|
+
*/
|
|
515
|
+
export declare function negative(x: NDArray): NDArray;
|
|
516
|
+
/**
|
|
517
|
+
* Element-wise sign (-1, 0, or 1)
|
|
518
|
+
* @param x - Input array
|
|
519
|
+
* @returns Array of signs
|
|
520
|
+
*/
|
|
521
|
+
export declare function sign(x: NDArray): NDArray;
|
|
522
|
+
/**
|
|
523
|
+
* Element-wise modulo
|
|
524
|
+
* @param x - Dividend array
|
|
525
|
+
* @param divisor - Divisor (array or scalar)
|
|
526
|
+
* @returns Remainder after division
|
|
527
|
+
*/
|
|
528
|
+
export declare function mod(x: NDArray, divisor: NDArray | number): NDArray;
|
|
529
|
+
/**
|
|
530
|
+
* Element-wise floor division
|
|
531
|
+
* @param x - Dividend array
|
|
532
|
+
* @param divisor - Divisor (array or scalar)
|
|
533
|
+
* @returns Floor of the quotient
|
|
534
|
+
*/
|
|
535
|
+
export declare function floor_divide(x: NDArray, divisor: NDArray | number): NDArray;
|
|
536
|
+
/**
|
|
537
|
+
* Element-wise positive (unary +)
|
|
538
|
+
* @param x - Input array
|
|
539
|
+
* @returns Copy of the array
|
|
540
|
+
*/
|
|
541
|
+
export declare function positive(x: NDArray): NDArray;
|
|
542
|
+
/**
|
|
543
|
+
* Element-wise reciprocal (1/x)
|
|
544
|
+
* @param x - Input array
|
|
545
|
+
* @returns Array of reciprocals
|
|
546
|
+
*/
|
|
547
|
+
export declare function reciprocal(x: NDArray): NDArray;
|
|
387
548
|
//# sourceMappingURL=ndarray.d.ts.map
|
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* ArrayStorage - Internal storage abstraction
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* while keeping stdlib as an implementation detail.
|
|
4
|
+
* Stores array data directly using TypedArrays without external dependencies.
|
|
6
5
|
*
|
|
7
6
|
* @internal - This is not part of the public API
|
|
8
7
|
*/
|
|
9
|
-
import type { ndarray as StdlibNDArray } from '@stdlib/types/ndarray';
|
|
10
8
|
import { type DType, type TypedArray } from './dtype';
|
|
11
9
|
/**
|
|
12
10
|
* Internal storage for NDArray data
|
|
13
11
|
* Manages the underlying TypedArray and metadata
|
|
14
12
|
*/
|
|
15
13
|
export declare class ArrayStorage {
|
|
16
|
-
private
|
|
14
|
+
private _data;
|
|
15
|
+
private _shape;
|
|
16
|
+
private _strides;
|
|
17
|
+
private _offset;
|
|
17
18
|
private _dtype;
|
|
18
|
-
constructor(
|
|
19
|
+
constructor(data: TypedArray, shape: readonly number[], strides: readonly number[], offset: number, dtype: DType);
|
|
19
20
|
/**
|
|
20
21
|
* Shape of the array
|
|
21
22
|
*/
|
|
@@ -41,10 +42,9 @@ export declare class ArrayStorage {
|
|
|
41
42
|
*/
|
|
42
43
|
get strides(): readonly number[];
|
|
43
44
|
/**
|
|
44
|
-
*
|
|
45
|
-
* @internal
|
|
45
|
+
* Offset into the data buffer
|
|
46
46
|
*/
|
|
47
|
-
get
|
|
47
|
+
get offset(): number;
|
|
48
48
|
/**
|
|
49
49
|
* Check if array is C-contiguous (row-major, no gaps)
|
|
50
50
|
*/
|
|
@@ -54,17 +54,29 @@ export declare class ArrayStorage {
|
|
|
54
54
|
*/
|
|
55
55
|
get isFContiguous(): boolean;
|
|
56
56
|
/**
|
|
57
|
-
*
|
|
57
|
+
* Get element at linear index (respects strides and offset)
|
|
58
58
|
*/
|
|
59
|
-
|
|
59
|
+
iget(linearIndex: number): number | bigint;
|
|
60
|
+
/**
|
|
61
|
+
* Set element at linear index (respects strides and offset)
|
|
62
|
+
*/
|
|
63
|
+
iset(linearIndex: number, value: number | bigint): void;
|
|
64
|
+
/**
|
|
65
|
+
* Get element at multi-index position
|
|
66
|
+
*/
|
|
67
|
+
get(...indices: number[]): number | bigint;
|
|
68
|
+
/**
|
|
69
|
+
* Set element at multi-index position
|
|
70
|
+
*/
|
|
71
|
+
set(indices: number[], value: number | bigint): void;
|
|
60
72
|
/**
|
|
61
|
-
* Create
|
|
73
|
+
* Create a deep copy of this storage
|
|
62
74
|
*/
|
|
63
|
-
|
|
75
|
+
copy(): ArrayStorage;
|
|
64
76
|
/**
|
|
65
77
|
* Create storage from TypedArray data
|
|
66
78
|
*/
|
|
67
|
-
static fromData(data: TypedArray, shape: number[], dtype: DType): ArrayStorage;
|
|
79
|
+
static fromData(data: TypedArray, shape: number[], dtype: DType, strides?: number[], offset?: number): ArrayStorage;
|
|
68
80
|
/**
|
|
69
81
|
* Create storage with zeros
|
|
70
82
|
*/
|
|
@@ -73,11 +85,6 @@ export declare class ArrayStorage {
|
|
|
73
85
|
* Create storage with ones
|
|
74
86
|
*/
|
|
75
87
|
static ones(shape: number[], dtype?: DType): ArrayStorage;
|
|
76
|
-
/**
|
|
77
|
-
* Compute strides for row-major (C-order) layout
|
|
78
|
-
* @private
|
|
79
|
-
*/
|
|
80
|
-
private _computeStrides;
|
|
81
88
|
/**
|
|
82
89
|
* Compute strides for row-major (C-order) layout
|
|
83
90
|
* @private
|
package/dist/types/index.d.ts
CHANGED
|
@@ -3,6 +3,6 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module numpy-ts
|
|
5
5
|
*/
|
|
6
|
-
export { NDArray, zeros, ones, array, arange, linspace, logspace, geomspace, eye, empty, full, identity, asarray, copy, zeros_like, ones_like, empty_like, full_like, } from './core/ndarray';
|
|
6
|
+
export { NDArray, zeros, ones, array, arange, linspace, logspace, geomspace, eye, empty, full, identity, asarray, copy, zeros_like, ones_like, empty_like, full_like, sqrt, power, absolute, negative, sign, mod, floor_divide, positive, reciprocal, } from './core/ndarray';
|
|
7
7
|
export declare const __version__: string;
|
|
8
8
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -7,9 +7,18 @@
|
|
|
7
7
|
* @internal
|
|
8
8
|
*/
|
|
9
9
|
import { ArrayStorage } from '../core/storage';
|
|
10
|
+
/**
|
|
11
|
+
* Compute the broadcast shape of two arrays
|
|
12
|
+
* Returns the shape that results from broadcasting a and b together
|
|
13
|
+
* Throws if shapes are not compatible for broadcasting
|
|
14
|
+
*/
|
|
15
|
+
export declare function broadcastShapes(shapeA: readonly number[], shapeB: readonly number[]): number[];
|
|
10
16
|
/**
|
|
11
17
|
* Perform element-wise operation with broadcasting
|
|
12
18
|
*
|
|
19
|
+
* NOTE: This is the slow path for broadcasting/non-contiguous arrays.
|
|
20
|
+
* Fast paths for contiguous arrays are implemented directly in ops/arithmetic.ts
|
|
21
|
+
*
|
|
13
22
|
* @param a - First array storage
|
|
14
23
|
* @param b - Second array storage
|
|
15
24
|
* @param op - Operation to perform (a, b) => result
|
|
@@ -22,4 +31,13 @@ export declare function elementwiseBinaryOp(a: ArrayStorage, b: ArrayStorage, op
|
|
|
22
31
|
* Returns boolean array (dtype: 'bool', stored as Uint8Array)
|
|
23
32
|
*/
|
|
24
33
|
export declare function elementwiseComparisonOp(a: ArrayStorage, b: ArrayStorage, op: (a: number, b: number) => boolean): ArrayStorage;
|
|
34
|
+
/**
|
|
35
|
+
* Perform element-wise unary operation
|
|
36
|
+
*
|
|
37
|
+
* @param a - Input array storage
|
|
38
|
+
* @param op - Operation to perform (x) => result
|
|
39
|
+
* @param preserveDtype - If true, preserve input dtype; if false, promote to float64 (default: true)
|
|
40
|
+
* @returns Result storage
|
|
41
|
+
*/
|
|
42
|
+
export declare function elementwiseUnaryOp(a: ArrayStorage, op: (x: number) => number, preserveDtype?: boolean): ArrayStorage;
|
|
25
43
|
//# sourceMappingURL=compute.d.ts.map
|
|
@@ -46,4 +46,63 @@ export declare function multiply(a: ArrayStorage, b: ArrayStorage | number): Arr
|
|
|
46
46
|
* @returns Result storage with promoted float dtype
|
|
47
47
|
*/
|
|
48
48
|
export declare function divide(a: ArrayStorage, b: ArrayStorage | number): ArrayStorage;
|
|
49
|
+
/**
|
|
50
|
+
* Absolute value of each element
|
|
51
|
+
* Preserves dtype
|
|
52
|
+
*
|
|
53
|
+
* @param a - Input array storage
|
|
54
|
+
* @returns Result storage with absolute values
|
|
55
|
+
*/
|
|
56
|
+
export declare function absolute(a: ArrayStorage): ArrayStorage;
|
|
57
|
+
/**
|
|
58
|
+
* Numerical negative (element-wise negation)
|
|
59
|
+
* Preserves dtype
|
|
60
|
+
*
|
|
61
|
+
* @param a - Input array storage
|
|
62
|
+
* @returns Result storage with negated values
|
|
63
|
+
*/
|
|
64
|
+
export declare function negative(a: ArrayStorage): ArrayStorage;
|
|
65
|
+
/**
|
|
66
|
+
* Sign of each element (-1, 0, or 1)
|
|
67
|
+
* Preserves dtype
|
|
68
|
+
*
|
|
69
|
+
* @param a - Input array storage
|
|
70
|
+
* @returns Result storage with signs
|
|
71
|
+
*/
|
|
72
|
+
export declare function sign(a: ArrayStorage): ArrayStorage;
|
|
73
|
+
/**
|
|
74
|
+
* Modulo operation (remainder after division)
|
|
75
|
+
* NumPy behavior: Uses floor modulo (sign follows divisor), not JavaScript's truncate modulo
|
|
76
|
+
* Preserves dtype for integer types
|
|
77
|
+
*
|
|
78
|
+
* @param a - Dividend array storage
|
|
79
|
+
* @param b - Divisor (array storage or scalar)
|
|
80
|
+
* @returns Result storage with modulo values
|
|
81
|
+
*/
|
|
82
|
+
export declare function mod(a: ArrayStorage, b: ArrayStorage | number): ArrayStorage;
|
|
83
|
+
/**
|
|
84
|
+
* Floor division (division with result rounded down)
|
|
85
|
+
* NumPy behavior: Preserves integer types
|
|
86
|
+
*
|
|
87
|
+
* @param a - Dividend array storage
|
|
88
|
+
* @param b - Divisor (array storage or scalar)
|
|
89
|
+
* @returns Result storage with floor division values
|
|
90
|
+
*/
|
|
91
|
+
export declare function floorDivide(a: ArrayStorage, b: ArrayStorage | number): ArrayStorage;
|
|
92
|
+
/**
|
|
93
|
+
* Unary positive (returns a copy of the array)
|
|
94
|
+
* Preserves dtype
|
|
95
|
+
*
|
|
96
|
+
* @param a - Input array storage
|
|
97
|
+
* @returns Result storage (copy of input)
|
|
98
|
+
*/
|
|
99
|
+
export declare function positive(a: ArrayStorage): ArrayStorage;
|
|
100
|
+
/**
|
|
101
|
+
* Reciprocal (1/x) of each element
|
|
102
|
+
* NumPy behavior: Always promotes to float64 for integer types
|
|
103
|
+
*
|
|
104
|
+
* @param a - Input array storage
|
|
105
|
+
* @returns Result storage with reciprocal values
|
|
106
|
+
*/
|
|
107
|
+
export declare function reciprocal(a: ArrayStorage): ArrayStorage;
|
|
49
108
|
//# sourceMappingURL=arithmetic.d.ts.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exponential, logarithmic, and power operations
|
|
3
|
+
*
|
|
4
|
+
* Pure functions for element-wise exponential operations:
|
|
5
|
+
* exp, log, sqrt, power, etc.
|
|
6
|
+
*
|
|
7
|
+
* These functions are used by NDArray methods but are separated
|
|
8
|
+
* to keep the codebase modular and testable.
|
|
9
|
+
*/
|
|
10
|
+
import { ArrayStorage } from '../core/storage';
|
|
11
|
+
/**
|
|
12
|
+
* Square root of each element
|
|
13
|
+
* NumPy behavior: Always promotes to float64 for integer types
|
|
14
|
+
*
|
|
15
|
+
* @param a - Input array storage
|
|
16
|
+
* @returns Result storage with sqrt applied
|
|
17
|
+
*/
|
|
18
|
+
export declare function sqrt(a: ArrayStorage): ArrayStorage;
|
|
19
|
+
/**
|
|
20
|
+
* Raise elements to power
|
|
21
|
+
* NumPy behavior: Promotes to float64 for integer types with non-integer exponents
|
|
22
|
+
*
|
|
23
|
+
* @param a - Base array storage
|
|
24
|
+
* @param b - Exponent (array storage or scalar)
|
|
25
|
+
* @returns Result storage with power applied
|
|
26
|
+
*/
|
|
27
|
+
export declare function power(a: ArrayStorage, b: ArrayStorage | number): ArrayStorage;
|
|
28
|
+
//# sourceMappingURL=exponential.d.ts.map
|