deepbox 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.
Files changed (173) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +344 -0
  3. package/dist/CSRMatrix-CwGwQRea.d.cts +219 -0
  4. package/dist/CSRMatrix-KzNt6QpS.d.ts +219 -0
  5. package/dist/Tensor-BQLk1ltW.d.cts +147 -0
  6. package/dist/Tensor-g8mUClel.d.ts +147 -0
  7. package/dist/chunk-4S73VUBD.js +677 -0
  8. package/dist/chunk-4S73VUBD.js.map +1 -0
  9. package/dist/chunk-5R4S63PF.js +2925 -0
  10. package/dist/chunk-5R4S63PF.js.map +1 -0
  11. package/dist/chunk-6AE5FKKQ.cjs +9264 -0
  12. package/dist/chunk-6AE5FKKQ.cjs.map +1 -0
  13. package/dist/chunk-AD436M45.js +3854 -0
  14. package/dist/chunk-AD436M45.js.map +1 -0
  15. package/dist/chunk-ALS7ETWZ.cjs +4263 -0
  16. package/dist/chunk-ALS7ETWZ.cjs.map +1 -0
  17. package/dist/chunk-AU7XHGKJ.js +2092 -0
  18. package/dist/chunk-AU7XHGKJ.js.map +1 -0
  19. package/dist/chunk-B5TNKUEY.js +1481 -0
  20. package/dist/chunk-B5TNKUEY.js.map +1 -0
  21. package/dist/chunk-BCR7G3A6.js +9136 -0
  22. package/dist/chunk-BCR7G3A6.js.map +1 -0
  23. package/dist/chunk-C4PKXY74.cjs +1917 -0
  24. package/dist/chunk-C4PKXY74.cjs.map +1 -0
  25. package/dist/chunk-DWZY6PIP.cjs +6400 -0
  26. package/dist/chunk-DWZY6PIP.cjs.map +1 -0
  27. package/dist/chunk-E3EU5FZO.cjs +2113 -0
  28. package/dist/chunk-E3EU5FZO.cjs.map +1 -0
  29. package/dist/chunk-F3JWBINJ.js +1054 -0
  30. package/dist/chunk-F3JWBINJ.js.map +1 -0
  31. package/dist/chunk-FJYLIGJX.js +1940 -0
  32. package/dist/chunk-FJYLIGJX.js.map +1 -0
  33. package/dist/chunk-JSCDE774.cjs +729 -0
  34. package/dist/chunk-JSCDE774.cjs.map +1 -0
  35. package/dist/chunk-LWECRCW2.cjs +2412 -0
  36. package/dist/chunk-LWECRCW2.cjs.map +1 -0
  37. package/dist/chunk-MLBMYKCG.js +6379 -0
  38. package/dist/chunk-MLBMYKCG.js.map +1 -0
  39. package/dist/chunk-OX6QXFMV.cjs +3874 -0
  40. package/dist/chunk-OX6QXFMV.cjs.map +1 -0
  41. package/dist/chunk-PHV2DKRS.cjs +1072 -0
  42. package/dist/chunk-PHV2DKRS.cjs.map +1 -0
  43. package/dist/chunk-PL7TAYKI.js +4056 -0
  44. package/dist/chunk-PL7TAYKI.js.map +1 -0
  45. package/dist/chunk-PR647I7R.js +1898 -0
  46. package/dist/chunk-PR647I7R.js.map +1 -0
  47. package/dist/chunk-QERHVCHC.cjs +2960 -0
  48. package/dist/chunk-QERHVCHC.cjs.map +1 -0
  49. package/dist/chunk-XEG44RF6.cjs +1514 -0
  50. package/dist/chunk-XEG44RF6.cjs.map +1 -0
  51. package/dist/chunk-XMWVME2W.js +2377 -0
  52. package/dist/chunk-XMWVME2W.js.map +1 -0
  53. package/dist/chunk-ZB75FESB.cjs +1979 -0
  54. package/dist/chunk-ZB75FESB.cjs.map +1 -0
  55. package/dist/chunk-ZLW62TJG.cjs +4061 -0
  56. package/dist/chunk-ZLW62TJG.cjs.map +1 -0
  57. package/dist/chunk-ZXKBDFP3.js +4235 -0
  58. package/dist/chunk-ZXKBDFP3.js.map +1 -0
  59. package/dist/core/index.cjs +204 -0
  60. package/dist/core/index.cjs.map +1 -0
  61. package/dist/core/index.d.cts +2 -0
  62. package/dist/core/index.d.ts +2 -0
  63. package/dist/core/index.js +3 -0
  64. package/dist/core/index.js.map +1 -0
  65. package/dist/dataframe/index.cjs +22 -0
  66. package/dist/dataframe/index.cjs.map +1 -0
  67. package/dist/dataframe/index.d.cts +3 -0
  68. package/dist/dataframe/index.d.ts +3 -0
  69. package/dist/dataframe/index.js +5 -0
  70. package/dist/dataframe/index.js.map +1 -0
  71. package/dist/datasets/index.cjs +134 -0
  72. package/dist/datasets/index.cjs.map +1 -0
  73. package/dist/datasets/index.d.cts +3 -0
  74. package/dist/datasets/index.d.ts +3 -0
  75. package/dist/datasets/index.js +5 -0
  76. package/dist/datasets/index.js.map +1 -0
  77. package/dist/index-74AB8Cyh.d.cts +1126 -0
  78. package/dist/index-9oQx1HgV.d.cts +1180 -0
  79. package/dist/index-BJY2SI4i.d.ts +483 -0
  80. package/dist/index-BWGhrDlr.d.ts +733 -0
  81. package/dist/index-B_DK4FKY.d.cts +242 -0
  82. package/dist/index-BbA2Gxfl.d.ts +456 -0
  83. package/dist/index-BgHYAoSS.d.cts +837 -0
  84. package/dist/index-BndMbqsM.d.ts +1439 -0
  85. package/dist/index-C1mfVYoo.d.ts +2517 -0
  86. package/dist/index-CCvlwAmL.d.cts +809 -0
  87. package/dist/index-CDw5CnOU.d.ts +785 -0
  88. package/dist/index-Cn3SdB0O.d.ts +1126 -0
  89. package/dist/index-CrqLlS-a.d.ts +776 -0
  90. package/dist/index-D61yaSMY.d.cts +483 -0
  91. package/dist/index-D9Loo1_A.d.cts +2517 -0
  92. package/dist/index-DIT_OO9C.d.cts +785 -0
  93. package/dist/index-DIp_RrRt.d.ts +242 -0
  94. package/dist/index-DbultU6X.d.cts +1427 -0
  95. package/dist/index-DmEg_LCm.d.cts +776 -0
  96. package/dist/index-DoPWVxPo.d.cts +1439 -0
  97. package/dist/index-DuCxd-8d.d.ts +837 -0
  98. package/dist/index-Dx42TZaY.d.ts +809 -0
  99. package/dist/index-DyZ4QQf5.d.cts +456 -0
  100. package/dist/index-GFAVyOWO.d.ts +1427 -0
  101. package/dist/index-WHQLn0e8.d.cts +733 -0
  102. package/dist/index-ZtI1Iy4L.d.ts +1180 -0
  103. package/dist/index-eJgeni9c.d.cts +1911 -0
  104. package/dist/index-tk4lSYod.d.ts +1911 -0
  105. package/dist/index.cjs +72 -0
  106. package/dist/index.cjs.map +1 -0
  107. package/dist/index.d.cts +17 -0
  108. package/dist/index.d.ts +17 -0
  109. package/dist/index.js +15 -0
  110. package/dist/index.js.map +1 -0
  111. package/dist/linalg/index.cjs +86 -0
  112. package/dist/linalg/index.cjs.map +1 -0
  113. package/dist/linalg/index.d.cts +3 -0
  114. package/dist/linalg/index.d.ts +3 -0
  115. package/dist/linalg/index.js +5 -0
  116. package/dist/linalg/index.js.map +1 -0
  117. package/dist/metrics/index.cjs +158 -0
  118. package/dist/metrics/index.cjs.map +1 -0
  119. package/dist/metrics/index.d.cts +3 -0
  120. package/dist/metrics/index.d.ts +3 -0
  121. package/dist/metrics/index.js +5 -0
  122. package/dist/metrics/index.js.map +1 -0
  123. package/dist/ml/index.cjs +87 -0
  124. package/dist/ml/index.cjs.map +1 -0
  125. package/dist/ml/index.d.cts +3 -0
  126. package/dist/ml/index.d.ts +3 -0
  127. package/dist/ml/index.js +6 -0
  128. package/dist/ml/index.js.map +1 -0
  129. package/dist/ndarray/index.cjs +501 -0
  130. package/dist/ndarray/index.cjs.map +1 -0
  131. package/dist/ndarray/index.d.cts +5 -0
  132. package/dist/ndarray/index.d.ts +5 -0
  133. package/dist/ndarray/index.js +4 -0
  134. package/dist/ndarray/index.js.map +1 -0
  135. package/dist/nn/index.cjs +142 -0
  136. package/dist/nn/index.cjs.map +1 -0
  137. package/dist/nn/index.d.cts +6 -0
  138. package/dist/nn/index.d.ts +6 -0
  139. package/dist/nn/index.js +5 -0
  140. package/dist/nn/index.js.map +1 -0
  141. package/dist/optim/index.cjs +77 -0
  142. package/dist/optim/index.cjs.map +1 -0
  143. package/dist/optim/index.d.cts +4 -0
  144. package/dist/optim/index.d.ts +4 -0
  145. package/dist/optim/index.js +4 -0
  146. package/dist/optim/index.js.map +1 -0
  147. package/dist/plot/index.cjs +114 -0
  148. package/dist/plot/index.cjs.map +1 -0
  149. package/dist/plot/index.d.cts +6 -0
  150. package/dist/plot/index.d.ts +6 -0
  151. package/dist/plot/index.js +5 -0
  152. package/dist/plot/index.js.map +1 -0
  153. package/dist/preprocess/index.cjs +82 -0
  154. package/dist/preprocess/index.cjs.map +1 -0
  155. package/dist/preprocess/index.d.cts +4 -0
  156. package/dist/preprocess/index.d.ts +4 -0
  157. package/dist/preprocess/index.js +5 -0
  158. package/dist/preprocess/index.js.map +1 -0
  159. package/dist/random/index.cjs +74 -0
  160. package/dist/random/index.cjs.map +1 -0
  161. package/dist/random/index.d.cts +3 -0
  162. package/dist/random/index.d.ts +3 -0
  163. package/dist/random/index.js +5 -0
  164. package/dist/random/index.js.map +1 -0
  165. package/dist/stats/index.cjs +142 -0
  166. package/dist/stats/index.cjs.map +1 -0
  167. package/dist/stats/index.d.cts +3 -0
  168. package/dist/stats/index.d.ts +3 -0
  169. package/dist/stats/index.js +5 -0
  170. package/dist/stats/index.js.map +1 -0
  171. package/dist/tensor-B96jjJLQ.d.cts +205 -0
  172. package/dist/tensor-B96jjJLQ.d.ts +205 -0
  173. package/package.json +226 -0
@@ -0,0 +1,219 @@
1
+ import { S as Shape } from './tensor-B96jjJLQ.js';
2
+ import { T as Tensor } from './Tensor-g8mUClel.js';
3
+
4
+ type CSRMatrixInit = {
5
+ readonly data: Float64Array;
6
+ readonly indices: Int32Array;
7
+ readonly indptr: Int32Array;
8
+ readonly shape: Shape;
9
+ };
10
+ /**
11
+ * Compressed Sparse Row (CSR) matrix representation.
12
+ *
13
+ * CSR format stores a sparse matrix using three arrays:
14
+ * - `data`: Non-zero values in row-major order
15
+ * - `indices`: Column indices of non-zero values
16
+ * - `indptr`: Row pointers (indptr[i] to indptr[i+1] gives the range of data/indices for row i)
17
+ *
18
+ * This format is efficient for:
19
+ * - Row slicing
20
+ * - Matrix-vector products
21
+ * - Arithmetic operations
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * import { CSRMatrix } from 'deepbox/ndarray';
26
+ *
27
+ * // Create a 3x3 sparse matrix with values at (0,0)=1, (1,2)=2, (2,1)=3
28
+ * const sparse = CSRMatrix.fromCOO({
29
+ * rows: 3, cols: 3,
30
+ * rowIndices: new Int32Array([0, 1, 2]),
31
+ * colIndices: new Int32Array([0, 2, 1]),
32
+ * values: new Float64Array([1, 2, 3])
33
+ * });
34
+ *
35
+ * // Convert to dense for operations
36
+ * const dense = sparse.toDense();
37
+ * ```
38
+ *
39
+ * @see {@link https://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_row_(CSR,_CRS_or_Yale_format) | Wikipedia: CSR}
40
+ */
41
+ declare class CSRMatrix {
42
+ readonly data: Float64Array;
43
+ readonly indices: Int32Array;
44
+ readonly indptr: Int32Array;
45
+ readonly shape: Shape;
46
+ constructor(init: CSRMatrixInit);
47
+ /** Number of non-zero elements in the matrix */
48
+ get nnz(): number;
49
+ /** Number of rows in the matrix */
50
+ get rows(): number;
51
+ /** Number of columns in the matrix */
52
+ get cols(): number;
53
+ /**
54
+ * Convert the sparse matrix to a dense Tensor.
55
+ *
56
+ * @returns Dense 2D Tensor representation
57
+ *
58
+ * @example
59
+ * ```ts
60
+ * const dense = sparse.toDense();
61
+ * console.log(dense.shape); // [rows, cols]
62
+ * ```
63
+ */
64
+ toDense(): Tensor;
65
+ /**
66
+ * Add two sparse matrices element-wise.
67
+ *
68
+ * Both matrices must have the same shape.
69
+ *
70
+ * @param other - Matrix to add
71
+ * @returns New CSRMatrix containing the sum
72
+ * @throws {ShapeError} If shapes don't match
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * const c = a.add(b); // c = a + b
77
+ * ```
78
+ */
79
+ add(other: CSRMatrix): CSRMatrix;
80
+ /**
81
+ * Subtract another sparse matrix element-wise.
82
+ *
83
+ * Both matrices must have the same shape.
84
+ *
85
+ * @param other - Matrix to subtract
86
+ * @returns New CSRMatrix containing the difference
87
+ * @throws {ShapeError} If shapes don't match
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * const c = a.sub(b); // c = a - b
92
+ * ```
93
+ */
94
+ sub(other: CSRMatrix): CSRMatrix;
95
+ /**
96
+ * Multiply all elements by a scalar value.
97
+ *
98
+ * @param scalar - Value to multiply by
99
+ * @returns New CSRMatrix with scaled values
100
+ *
101
+ * @example
102
+ * ```ts
103
+ * const scaled = matrix.scale(2.0); // Double all values
104
+ * ```
105
+ */
106
+ scale(scalar: number): CSRMatrix;
107
+ /**
108
+ * Element-wise multiplication (Hadamard product) with another sparse matrix.
109
+ *
110
+ * Both matrices must have the same shape.
111
+ *
112
+ * @param other - Matrix to multiply with
113
+ * @returns New CSRMatrix containing the element-wise product
114
+ * @throws {ShapeError} If shapes don't match
115
+ *
116
+ * @example
117
+ * ```ts
118
+ * const c = a.multiply(b); // c[i,j] = a[i,j] * b[i,j]
119
+ * ```
120
+ */
121
+ multiply(other: CSRMatrix): CSRMatrix;
122
+ /**
123
+ * Matrix multiplication with a dense vector.
124
+ *
125
+ * Computes y = A * x where A is this sparse matrix and x is a dense vector.
126
+ *
127
+ * @param vector - Dense vector (1D Tensor or Float64Array)
128
+ * @returns Dense result vector as Tensor
129
+ * @throws {ShapeError} If vector length doesn't match matrix columns
130
+ *
131
+ * @example
132
+ * ```ts
133
+ * const x = tensor([1, 2, 3]);
134
+ * const y = sparse.matvec(x); // y = A * x
135
+ * ```
136
+ */
137
+ matvec(vector: Tensor | Float64Array): Tensor;
138
+ /**
139
+ * Matrix multiplication with a dense matrix.
140
+ *
141
+ * Computes C = A * B where A is this sparse matrix and B is a dense matrix.
142
+ *
143
+ * @param dense - Dense matrix (2D Tensor)
144
+ * @returns Dense result matrix as Tensor
145
+ * @throws {ShapeError} If inner dimensions don't match
146
+ *
147
+ * @example
148
+ * ```ts
149
+ * const B = tensor([[1, 2], [3, 4], [5, 6]]);
150
+ * const C = sparse.matmul(B); // C = A * B
151
+ * ```
152
+ */
153
+ matmul(dense: Tensor): Tensor;
154
+ /**
155
+ * Transpose the sparse matrix.
156
+ *
157
+ * @returns New CSRMatrix representing the transpose
158
+ *
159
+ * @example
160
+ * ```ts
161
+ * const At = A.transpose(); // At[i,j] = A[j,i]
162
+ * ```
163
+ */
164
+ transpose(): CSRMatrix;
165
+ /**
166
+ * Get a specific element from the matrix.
167
+ *
168
+ * @param row - Row index
169
+ * @param col - Column index
170
+ * @returns Value at the specified position (0 if not stored)
171
+ * @throws {RangeError} If indices are out of bounds
172
+ *
173
+ * @example
174
+ * ```ts
175
+ * const value = matrix.get(1, 2);
176
+ * ```
177
+ */
178
+ get(row: number, col: number): number;
179
+ /**
180
+ * Create a copy of this matrix.
181
+ *
182
+ * @returns New CSRMatrix with copied data
183
+ */
184
+ copy(): CSRMatrix;
185
+ /** Helper to convert Tensor to Float64Array */
186
+ private tensorToFloat64;
187
+ /**
188
+ * Create a sparse matrix from COO (Coordinate List) format.
189
+ *
190
+ * @param args - COO format specification
191
+ * @param args.rows - Number of rows
192
+ * @param args.cols - Number of columns
193
+ * @param args.rowIndices - Row indices of non-zero values
194
+ * @param args.colIndices - Column indices of non-zero values
195
+ * @param args.values - Non-zero values
196
+ * @param args.sort - Whether to sort entries (default: true)
197
+ * @returns New CSRMatrix
198
+ *
199
+ * @example
200
+ * ```ts
201
+ * const sparse = CSRMatrix.fromCOO({
202
+ * rows: 3, cols: 3,
203
+ * rowIndices: new Int32Array([0, 1, 2]),
204
+ * colIndices: new Int32Array([0, 2, 1]),
205
+ * values: new Float64Array([1, 2, 3])
206
+ * });
207
+ * ```
208
+ */
209
+ static fromCOO(args: {
210
+ readonly rows: number;
211
+ readonly cols: number;
212
+ readonly rowIndices: Int32Array;
213
+ readonly colIndices: Int32Array;
214
+ readonly values: Float64Array;
215
+ readonly sort?: boolean;
216
+ }): CSRMatrix;
217
+ }
218
+
219
+ export { CSRMatrix as C, type CSRMatrixInit as a };
@@ -0,0 +1,147 @@
1
+ import { S as Shape, D as DType, T as TensorLike, a as Device, b as TypedArray } from './tensor-B96jjJLQ.cjs';
2
+
3
+ type TensorOptions = {
4
+ readonly dtype: DType;
5
+ readonly device: Device;
6
+ };
7
+ type TensorData<D extends DType> = D extends "string" ? string[] : TypedArray;
8
+ /**
9
+ * Multi-dimensional array (tensor) with typed storage.
10
+ *
11
+ * Core data structure for numerical computing. Supports:
12
+ * - N-dimensional arrays with any shape
13
+ * - Multiple data types (float32, float64, int32, etc.)
14
+ * - Memory-efficient strided views
15
+ * - Device abstraction (CPU, WebGPU, WASM)
16
+ *
17
+ * Inspired by NumPy ndarray and PyTorch Tensor.
18
+ *
19
+ * @typeParam S - Shape type (readonly number array)
20
+ * @typeParam D - Data type (DType literal)
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import { tensor } from 'deepbox/ndarray';
25
+ *
26
+ * // Create from nested arrays
27
+ * const t = tensor([[1, 2, 3], [4, 5, 6]]);
28
+ * console.log(t.shape); // [2, 3]
29
+ * console.log(t.dtype); // 'float32'
30
+ *
31
+ * // Access properties
32
+ * console.log(t.size); // 6
33
+ * console.log(t.ndim); // 2
34
+ * ```
35
+ */
36
+ declare class Tensor<S extends Shape = Shape, D extends DType = DType> implements TensorLike<S, D> {
37
+ readonly shape: S;
38
+ readonly dtype: D;
39
+ readonly device: Device;
40
+ readonly data: TensorData<D>;
41
+ readonly strides: readonly number[];
42
+ readonly offset: number;
43
+ readonly size: number;
44
+ readonly ndim: number;
45
+ private constructor();
46
+ private isStringTensor;
47
+ private isNumericTensor;
48
+ static fromTypedArray<S extends Shape, D extends Exclude<DType, "string">>(args: {
49
+ readonly data: TensorData<D>;
50
+ readonly shape: S;
51
+ readonly dtype: D;
52
+ readonly device: Device;
53
+ readonly offset?: number;
54
+ readonly strides?: readonly number[];
55
+ }): Tensor<S, D>;
56
+ static fromStringArray<S extends Shape>(args: {
57
+ readonly data: string[];
58
+ readonly shape: S;
59
+ readonly device?: Device;
60
+ readonly offset?: number;
61
+ readonly strides?: readonly number[];
62
+ }): Tensor<S, "string">;
63
+ static zeros<S extends Shape>(shape: S, opts: TensorOptions & {
64
+ readonly dtype: "string";
65
+ }): Tensor<S, "string">;
66
+ static zeros<S extends Shape, D extends Exclude<DType, "string">>(shape: S, opts: TensorOptions & {
67
+ readonly dtype: D;
68
+ }): Tensor<S, D>;
69
+ /**
70
+ * Create a view sharing the same underlying data.
71
+ *
72
+ * Note: This does not copy data. Mutations (if exposed in the future) would be shared.
73
+ */
74
+ view<S2 extends Shape>(this: Tensor<S, "string">, shape: S2, strides?: readonly number[], offset?: number): Tensor<S2, "string">;
75
+ view<S2 extends Shape>(this: Tensor<S, Exclude<DType, "string">>, shape: S2, strides?: readonly number[], offset?: number): Tensor<S2, Exclude<DType, "string">>;
76
+ view<S2 extends Shape>(this: Tensor<S, DType>, shape: S2, strides?: readonly number[], offset?: number): Tensor<S2, DType>;
77
+ /**
78
+ * Reshape the tensor to a new shape without copying data.
79
+ *
80
+ * Returns a new tensor with the specified shape, sharing the same underlying data.
81
+ * The total number of elements must remain the same.
82
+ * Requires a contiguous tensor; non-contiguous views will throw.
83
+ *
84
+ * This is a convenience method that wraps the standalone `reshape` function,
85
+ * providing a more intuitive API similar to NumPy and PyTorch.
86
+ *
87
+ * @param newShape - The desired shape for the tensor
88
+ * @returns A new tensor with the specified shape
89
+ * @throws {ShapeError} If the new shape is incompatible with the tensor's size
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * const t = tensor([1, 2, 3, 4, 5, 6]);
94
+ * const reshaped = t.reshape([2, 3]);
95
+ * console.log(reshaped.shape); // [2, 3]
96
+ *
97
+ * const matrix = tensor([[1, 2], [3, 4]]);
98
+ * const flat = matrix.reshape([4]);
99
+ * console.log(flat.shape); // [4]
100
+ * ```
101
+ *
102
+ * @see {@link https://numpy.org/doc/stable/reference/generated/numpy.reshape.html | NumPy reshape}
103
+ * @see {@link https://pytorch.org/docs/stable/generated/torch.reshape.html | PyTorch reshape}
104
+ */
105
+ reshape<S2 extends Shape>(this: Tensor<S, "string">, newShape: S2): Tensor<S2, "string">;
106
+ reshape<S2 extends Shape>(this: Tensor<S, Exclude<DType, "string">>, newShape: S2): Tensor<S2, Exclude<DType, "string">>;
107
+ reshape<S2 extends Shape>(this: Tensor<S, DType>, newShape: S2): Tensor<S2, DType>;
108
+ /**
109
+ * Flatten the tensor to a 1-dimensional array.
110
+ *
111
+ * Returns a new 1D tensor containing all elements, sharing the same underlying data.
112
+ *
113
+ * @returns A 1D tensor with shape [size]
114
+ *
115
+ * @example
116
+ * ```ts
117
+ * const matrix = tensor([[1, 2, 3], [4, 5, 6]]);
118
+ * const flat = matrix.flatten();
119
+ * console.log(flat.shape); // [6]
120
+ * ```
121
+ */
122
+ flatten(this: Tensor<S, "string">): Tensor<[number], "string">;
123
+ flatten(this: Tensor<S, Exclude<DType, "string">>): Tensor<[number], Exclude<DType, "string">>;
124
+ flatten(this: Tensor<S, DType>): Tensor<[number], DType>;
125
+ at(...indices: number[]): unknown;
126
+ toArray(): unknown;
127
+ /**
128
+ * Return a human-readable string representation of this tensor.
129
+ *
130
+ * Scalars print as a bare value, 1-D tensors as a bracketed list, and
131
+ * higher-rank tensors use nested brackets with newline separators.
132
+ * Large dimensions are summarized with an ellipsis.
133
+ *
134
+ * @param maxElements - Maximum number of elements to display per
135
+ * dimension before summarizing (default: 6).
136
+ * @returns Formatted string representation
137
+ *
138
+ * @example
139
+ * ```ts
140
+ * const t = tensor([1, 2, 3]);
141
+ * t.toString(); // "tensor([1, 2, 3], dtype=float32)"
142
+ * ```
143
+ */
144
+ toString(maxElements?: number): string;
145
+ }
146
+
147
+ export { Tensor as T, type TensorOptions as a };
@@ -0,0 +1,147 @@
1
+ import { S as Shape, D as DType, T as TensorLike, a as Device, b as TypedArray } from './tensor-B96jjJLQ.js';
2
+
3
+ type TensorOptions = {
4
+ readonly dtype: DType;
5
+ readonly device: Device;
6
+ };
7
+ type TensorData<D extends DType> = D extends "string" ? string[] : TypedArray;
8
+ /**
9
+ * Multi-dimensional array (tensor) with typed storage.
10
+ *
11
+ * Core data structure for numerical computing. Supports:
12
+ * - N-dimensional arrays with any shape
13
+ * - Multiple data types (float32, float64, int32, etc.)
14
+ * - Memory-efficient strided views
15
+ * - Device abstraction (CPU, WebGPU, WASM)
16
+ *
17
+ * Inspired by NumPy ndarray and PyTorch Tensor.
18
+ *
19
+ * @typeParam S - Shape type (readonly number array)
20
+ * @typeParam D - Data type (DType literal)
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import { tensor } from 'deepbox/ndarray';
25
+ *
26
+ * // Create from nested arrays
27
+ * const t = tensor([[1, 2, 3], [4, 5, 6]]);
28
+ * console.log(t.shape); // [2, 3]
29
+ * console.log(t.dtype); // 'float32'
30
+ *
31
+ * // Access properties
32
+ * console.log(t.size); // 6
33
+ * console.log(t.ndim); // 2
34
+ * ```
35
+ */
36
+ declare class Tensor<S extends Shape = Shape, D extends DType = DType> implements TensorLike<S, D> {
37
+ readonly shape: S;
38
+ readonly dtype: D;
39
+ readonly device: Device;
40
+ readonly data: TensorData<D>;
41
+ readonly strides: readonly number[];
42
+ readonly offset: number;
43
+ readonly size: number;
44
+ readonly ndim: number;
45
+ private constructor();
46
+ private isStringTensor;
47
+ private isNumericTensor;
48
+ static fromTypedArray<S extends Shape, D extends Exclude<DType, "string">>(args: {
49
+ readonly data: TensorData<D>;
50
+ readonly shape: S;
51
+ readonly dtype: D;
52
+ readonly device: Device;
53
+ readonly offset?: number;
54
+ readonly strides?: readonly number[];
55
+ }): Tensor<S, D>;
56
+ static fromStringArray<S extends Shape>(args: {
57
+ readonly data: string[];
58
+ readonly shape: S;
59
+ readonly device?: Device;
60
+ readonly offset?: number;
61
+ readonly strides?: readonly number[];
62
+ }): Tensor<S, "string">;
63
+ static zeros<S extends Shape>(shape: S, opts: TensorOptions & {
64
+ readonly dtype: "string";
65
+ }): Tensor<S, "string">;
66
+ static zeros<S extends Shape, D extends Exclude<DType, "string">>(shape: S, opts: TensorOptions & {
67
+ readonly dtype: D;
68
+ }): Tensor<S, D>;
69
+ /**
70
+ * Create a view sharing the same underlying data.
71
+ *
72
+ * Note: This does not copy data. Mutations (if exposed in the future) would be shared.
73
+ */
74
+ view<S2 extends Shape>(this: Tensor<S, "string">, shape: S2, strides?: readonly number[], offset?: number): Tensor<S2, "string">;
75
+ view<S2 extends Shape>(this: Tensor<S, Exclude<DType, "string">>, shape: S2, strides?: readonly number[], offset?: number): Tensor<S2, Exclude<DType, "string">>;
76
+ view<S2 extends Shape>(this: Tensor<S, DType>, shape: S2, strides?: readonly number[], offset?: number): Tensor<S2, DType>;
77
+ /**
78
+ * Reshape the tensor to a new shape without copying data.
79
+ *
80
+ * Returns a new tensor with the specified shape, sharing the same underlying data.
81
+ * The total number of elements must remain the same.
82
+ * Requires a contiguous tensor; non-contiguous views will throw.
83
+ *
84
+ * This is a convenience method that wraps the standalone `reshape` function,
85
+ * providing a more intuitive API similar to NumPy and PyTorch.
86
+ *
87
+ * @param newShape - The desired shape for the tensor
88
+ * @returns A new tensor with the specified shape
89
+ * @throws {ShapeError} If the new shape is incompatible with the tensor's size
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * const t = tensor([1, 2, 3, 4, 5, 6]);
94
+ * const reshaped = t.reshape([2, 3]);
95
+ * console.log(reshaped.shape); // [2, 3]
96
+ *
97
+ * const matrix = tensor([[1, 2], [3, 4]]);
98
+ * const flat = matrix.reshape([4]);
99
+ * console.log(flat.shape); // [4]
100
+ * ```
101
+ *
102
+ * @see {@link https://numpy.org/doc/stable/reference/generated/numpy.reshape.html | NumPy reshape}
103
+ * @see {@link https://pytorch.org/docs/stable/generated/torch.reshape.html | PyTorch reshape}
104
+ */
105
+ reshape<S2 extends Shape>(this: Tensor<S, "string">, newShape: S2): Tensor<S2, "string">;
106
+ reshape<S2 extends Shape>(this: Tensor<S, Exclude<DType, "string">>, newShape: S2): Tensor<S2, Exclude<DType, "string">>;
107
+ reshape<S2 extends Shape>(this: Tensor<S, DType>, newShape: S2): Tensor<S2, DType>;
108
+ /**
109
+ * Flatten the tensor to a 1-dimensional array.
110
+ *
111
+ * Returns a new 1D tensor containing all elements, sharing the same underlying data.
112
+ *
113
+ * @returns A 1D tensor with shape [size]
114
+ *
115
+ * @example
116
+ * ```ts
117
+ * const matrix = tensor([[1, 2, 3], [4, 5, 6]]);
118
+ * const flat = matrix.flatten();
119
+ * console.log(flat.shape); // [6]
120
+ * ```
121
+ */
122
+ flatten(this: Tensor<S, "string">): Tensor<[number], "string">;
123
+ flatten(this: Tensor<S, Exclude<DType, "string">>): Tensor<[number], Exclude<DType, "string">>;
124
+ flatten(this: Tensor<S, DType>): Tensor<[number], DType>;
125
+ at(...indices: number[]): unknown;
126
+ toArray(): unknown;
127
+ /**
128
+ * Return a human-readable string representation of this tensor.
129
+ *
130
+ * Scalars print as a bare value, 1-D tensors as a bracketed list, and
131
+ * higher-rank tensors use nested brackets with newline separators.
132
+ * Large dimensions are summarized with an ellipsis.
133
+ *
134
+ * @param maxElements - Maximum number of elements to display per
135
+ * dimension before summarizing (default: 6).
136
+ * @returns Formatted string representation
137
+ *
138
+ * @example
139
+ * ```ts
140
+ * const t = tensor([1, 2, 3]);
141
+ * t.toString(); // "tensor([1, 2, 3], dtype=float32)"
142
+ * ```
143
+ */
144
+ toString(maxElements?: number): string;
145
+ }
146
+
147
+ export { Tensor as T, type TensorOptions as a };