@@ -348,60 +348,31 @@ export class Tensor {
348348 return new Tensor ( this . type , this . data . slice ( ) , this . dims . slice ( ) ) ;
349349 }
350350
351- /**
352- * Performs a vertical slice operation on a 2D Tensor.
353- * @param {number|number[]|number[][] } slices - The slice specification:
354- * - If a number is given, then a single column is selected.
355- * - If an array of two numbers is given, then a range of columns [start, end (exclusive)] is selected.
356- * - If an array of arrays is given, then those specific columns are selected.
357- * @returns {Tensor } A new Tensor containing the selected columns.
358- * @throws {Error } If the slice input is invalid.
359- */
360- vslice ( slices ) {
361- const rowDim = this . dims [ 0 ] ;
362- const colDim = this . dims [ 1 ] ;
363-
364- // Handle different slice cases (single column, range, or list of specific columns)
365- let selectedCols = [ ] ;
366- if ( typeof slices === 'number' ) {
367- // Single column slice
368- selectedCols = [ slices ] ;
369- } else if ( Array . isArray ( slices ) && slices . length === 2 && ! Array . isArray ( slices [ 0 ] ) && ! Array . isArray ( slices [ 1 ] ) ) {
370- // Range slice [start, end]
371- const [ start , end ] = slices ;
372- selectedCols = Array . from ( { length : end - start } , ( _ , i ) => i + start ) ;
373- } else if ( Array . isArray ( slices ) && Array . isArray ( slices [ 0 ] ) ) {
374- // Specific column list [[col1], [col2]]
375- selectedCols = slices . flat ( ) ;
376- } else {
377- throw new Error ( 'Invalid slice input' ) ;
378- }
379-
380- // Determine new dimensions: rows remain the same, columns are based on selection
381- const newTensorDims = [ rowDim , selectedCols . length ] ;
382- const newBufferSize = newTensorDims [ 0 ] * newTensorDims [ 1 ] ;
383- // Allocate memory
384- // @ts -ignore
385- const data = new this . data . constructor ( newBufferSize ) ;
386-
387- // Fill the new data array by selecting the correct columns
388- for ( let row = 0 ; row < rowDim ; ++ row ) {
389- for ( let i = 0 ; i < selectedCols . length ; ++ i ) {
390- const col = selectedCols [ i ] ;
391- const targetIndex = row * newTensorDims [ 1 ] + i ;
392- const originalIndex = row * colDim + col ;
393- data [ targetIndex ] = this . data [ originalIndex ] ;
394- }
395- }
396-
397- return new Tensor ( this . type , data , newTensorDims ) ;
398- }
399-
400351 /**
401352 * Performs a slice operation on the Tensor along specified dimensions.
353+ *
354+ * Consider a Tensor that has a dimension of [4, 7]:
355+ * ```
356+ * [ 1, 2, 3, 4, 5, 6, 7]
357+ * [ 8, 9, 10, 11, 12, 13, 14]
358+ * [15, 16, 17, 18, 19, 20, 21]
359+ * [22, 23, 24, 25, 26, 27, 28]
360+ * ```
361+ * We can slice against the two dims of row and column, for instance in this
362+ * case we can start at the second element, and return to the second last,
363+ * like this:
364+ * ```
365+ * tensor.slice([1, -1], [1, -1]);
366+ * ```
367+ * which would return:
368+ * ```
369+ * [ 9, 10, 11, 12, 13 ]
370+ * [ 16, 17, 18, 19, 20 ]
371+ * ```
372+ *
402373 * @param {...(number|number[]|null) } slices - The slice specifications for each dimension.
403- * - If a number is given, then a single column is selected.
404- * - If an array of two numbers is given, then a range of columns [start, end (exclusive)] is selected.
374+ * - If a number is given, then a single element is selected.
375+ * - If an array of two numbers is given, then a range of elements [start, end (exclusive)] is selected.
405376 * - If null is given, then the entire dimension is selected.
406377 * @returns {Tensor } A new Tensor containing the selected elements.
407378 * @throws {Error } If the slice input is invalid.
0 commit comments