From 4e641d777d3cf50c3494aa959b1da4adcd42866a Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 14 Dec 2025 11:07:44 +0500 Subject: [PATCH 1/2] feat: add ndarray/flatten-from-by --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/flatten-from-by/README.md | 242 +++ .../flatten-from-by/benchmark/benchmark.js | 307 +++ .../ndarray/flatten-from-by/docs/repl.txt | 71 + .../flatten-from-by/docs/types/index.d.ts | 437 ++++ .../flatten-from-by/docs/types/test.ts | 293 +++ .../ndarray/flatten-from-by/examples/index.js | 41 + .../ndarray/flatten-from-by/lib/index.js | 52 + .../ndarray/flatten-from-by/lib/main.js | 315 +++ .../ndarray/flatten-from-by/package.json | 69 + .../ndarray/flatten-from-by/test/test.js | 1931 +++++++++++++++++ 10 files changed, 3758 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from-by/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from-by/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from-by/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from-by/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from-by/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from-by/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from-by/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from-by/README.md b/lib/node_modules/@stdlib/ndarray/flatten-from-by/README.md new file mode 100644 index 000000000000..363780a2e65a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from-by/README.md @@ -0,0 +1,242 @@ + + +# flattenFromBy + +> Flatten an [ndarray][@stdlib/ndarray/ctor] from a specified dimension while applying a callback function to each element. + +
+ +
+ + + +
+ +## Usage + +```javascript +var flattenFromBy = require( '@stdlib/ndarray/flatten-from-by' ); +``` + +#### flattenFromBy( x, dim\[, options], fcn\[, thisArg] ) + +Flattens an [ndarray][@stdlib/ndarray/ctor] from a specified dimension while applying a callback function to each element. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function scale( value ) { + return value * 2.0; +} + +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +// returns + +var y = flattenFromBy( x, 1, scale ); +// returns + +var arr = ndarray2array( y ); +// returns [ [ 2.0, 4.0 ], [ 6.0, 8.0 ], [ 10.0, 12.0 ] ] +``` + +The function accepts the following arguments: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have one or more dimensions. +- **dim**: dimension to start flattening from. If provided an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`. +- **options**: function options (_optional_). +- **fcn**: callback function. +- **thisArg**: callback execution context (_optional_). + +The function accepts the following options: + +- **order**: order in which input [ndarray][@stdlib/ndarray/ctor] elements should be flattened. Must be one of the following: + + - `'row-major'`: flatten elements in lexicographic order. For example, given a two-dimensional input [ndarray][@stdlib/ndarray/ctor] (i.e., a matrix), flattening in lexicographic order means flattening the input [ndarray][@stdlib/ndarray/ctor] row-by-row. + - `'column-major'`: flatten elements in colexicographic order. For example, given a two-dimensional input [ndarray][@stdlib/ndarray/ctor] (i.e., a matrix), flattening in colexicographic order means flattening the input [ndarray][@stdlib/ndarray/ctor] column-by-column. + - `'any'`: flatten according to the physical layout of the input [ndarray][@stdlib/ndarray/ctor] data in memory, regardless of the stated [order][@stdlib/ndarray/orders] of the input [ndarray][@stdlib/ndarray/ctor]. + - `'same'`: flatten according to the stated [order][@stdlib/ndarray/orders] of the input [ndarray][@stdlib/ndarray/ctor]. + + Default: `'row-major'`. + +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having the same [data type][@stdlib/ndarray/dtypes] as a provided input [ndarray][@stdlib/ndarray/ctor]. + +By default, the input [ndarray][@stdlib/ndarray/ctor] is flattened in lexicographic order. To flatten elements in a different order, specify the `order` option. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function scale( value ) { + return value * 2.0; +} + +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +// returns + +var opts = { + 'order': 'column-major' +}; + +var y = flattenFromBy( x, 0, opts, scale ); +// returns + +var arr = ndarray2array( y ); +// returns [ 2.0, 6.0, 10.0, 4.0, 8.0, 12.0 ] +``` + +By default, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor]. To return an ndarray with a different [data type][@stdlib/ndarray/dtypes], specify the `dtype` option. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var dtype = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function scale( value ) { + return value * 2.0; +} + +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +// returns + +var opts = { + 'dtype': 'float32' +}; + +var y = flattenFromBy( x, 0, opts, scale ); +// returns + +var dt = String( dtype( y ) ); +// returns 'float32' + +var arr = ndarray2array( y ); +// returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] +``` + +To set the callback function execution context, provide a `thisArg`. + + + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function scale( value ) { + this.count += 1; + return value * 2.0; +} + +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +// returns + +var ctx = { + 'count': 0 +}; + +var y = flattenFromBy( x, 1, scale, ctx ); +// returns + +var arr = ndarray2array( y ); +// returns [ [ 2.0, 4.0 ], [ 6.0, 8.0 ], [ 10.0, 12.0 ] ] + +var count = ctx.count; +// returns 6 +``` + +
+ + + +
+ +## Notes + +- The function **always** returns a copy of input [ndarray][@stdlib/ndarray/ctor] data, even when an input [ndarray][@stdlib/ndarray/ctor] already has the desired number of dimensions. + +- The callback function is provided the following arguments: + + - **value**: current array element. + - **indices**: current array element indices. + - **arr**: the input [ndarray][@stdlib/ndarray/ctor]. + +- The order in which array elements are traversed and passed to a provided callback function is **not** guaranteed to match the order of array elements in an [ndarray][@stdlib/ndarray/ctor] view. Accordingly, a provided callback should avoid making assumptions regarding the order of provided elements. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var flattenFromBy = require( '@stdlib/ndarray/flatten-from-by' ); + +function scale( value ) { + return value * 2.0; +} + +var xbuf = discreteUniform( 12, -100, 100, { + 'dtype': 'generic' +}); + +var x = array( xbuf, { + 'shape': [ 2, 2, 3 ], + 'dtype': 'generic' +}); +console.log( ndarray2array( x ) ); + +var y = flattenFromBy( x, 1, scale ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/flatten-from-by/benchmark/benchmark.js new file mode 100644 index 000000000000..8c95bb6f930b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from-by/benchmark/benchmark.js @@ -0,0 +1,307 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var zeros = require( '@stdlib/ndarray/base/zeros' ); +var pkg = require( './../package.json' ).name; +var flattenFromBy = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @param {number} value - current value +* @returns {number} output value +*/ +function clbk( value ) { + return value + 1; +} + + +// MAIN // + +bench( pkg+'::2d:row-major', function benchmark( b ) { + var values; + var opts; + var y; + var i; + var j; + + values = [ + zeros( 'float64', [ 10, 10 ], 'row-major' ), + zeros( 'float32', [ 10, 10 ], 'row-major' ), + zeros( 'int32', [ 10, 10 ], 'row-major' ), + zeros( 'generic', [ 10, 10 ], 'row-major' ) + ]; + opts = { + 'order': 'row-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flattenFromBy( values[ j ], 0, opts, clbk ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d:column-major', function benchmark( b ) { + var values; + var opts; + var y; + var i; + var j; + + values = [ + zeros( 'float64', [ 10, 10 ], 'row-major' ), + zeros( 'float32', [ 10, 10 ], 'row-major' ), + zeros( 'int32', [ 10, 10 ], 'row-major' ), + zeros( 'generic', [ 10, 10 ], 'row-major' ) + ]; + opts = { + 'order': 'column-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flattenFromBy( values[ j ], 0, opts, clbk ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d:row-major', function benchmark( b ) { + var values; + var opts; + var y; + var i; + var j; + + values = [ + zeros( 'float64', [ 2, 5, 10 ], 'row-major' ), + zeros( 'float32', [ 2, 5, 10 ], 'row-major' ), + zeros( 'int32', [ 2, 5, 10 ], 'row-major' ), + zeros( 'generic', [ 2, 5, 10 ], 'row-major' ) + ]; + opts = { + 'order': 'row-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flattenFromBy( values[ j ], 0, opts, clbk ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d:column-major', function benchmark( b ) { + var values; + var opts; + var y; + var i; + var j; + + values = [ + zeros( 'float64', [ 2, 5, 10 ], 'row-major' ), + zeros( 'float32', [ 2, 5, 10 ], 'row-major' ), + zeros( 'int32', [ 2, 5, 10 ], 'row-major' ), + zeros( 'generic', [ 2, 5, 10 ], 'row-major' ) + ]; + opts = { + 'order': 'column-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flattenFromBy( values[ j ], 0, opts, clbk ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d:row-major', function benchmark( b ) { + var values; + var opts; + var y; + var i; + var j; + + values = [ + zeros( 'float64', [ 2, 5, 2, 5 ], 'row-major' ), + zeros( 'float32', [ 2, 5, 2, 5 ], 'row-major' ), + zeros( 'int32', [ 2, 5, 2, 5 ], 'row-major' ), + zeros( 'generic', [ 2, 5, 2, 5 ], 'row-major' ) + ]; + opts = { + 'order': 'row-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flattenFromBy( values[ j ], 0, opts, clbk ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d:column-major', function benchmark( b ) { + var values; + var opts; + var y; + var i; + var j; + + values = [ + zeros( 'float64', [ 2, 5, 2, 5 ], 'row-major' ), + zeros( 'float32', [ 2, 5, 2, 5 ], 'row-major' ), + zeros( 'int32', [ 2, 5, 2, 5 ], 'row-major' ), + zeros( 'generic', [ 2, 5, 2, 5 ], 'row-major' ) + ]; + opts = { + 'order': 'column-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flattenFromBy( values[ j ], 0, opts, clbk ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d:row-major', function benchmark( b ) { + var values; + var opts; + var y; + var i; + var j; + + values = [ + zeros( 'float64', [ 2, 5, 2, 5, 1 ], 'row-major' ), + zeros( 'float32', [ 2, 5, 2, 5, 1 ], 'row-major' ), + zeros( 'int32', [ 2, 5, 2, 5, 1 ], 'row-major' ), + zeros( 'generic', [ 2, 5, 2, 5, 1 ], 'row-major' ) + ]; + opts = { + 'order': 'row-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flattenFromBy( values[ j ], 0, opts, clbk ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d:column-major', function benchmark( b ) { + var values; + var opts; + var y; + var i; + var j; + + values = [ + zeros( 'float64', [ 2, 5, 2, 5, 1 ], 'row-major' ), + zeros( 'float32', [ 2, 5, 2, 5, 1 ], 'row-major' ), + zeros( 'int32', [ 2, 5, 2, 5, 1 ], 'row-major' ), + zeros( 'generic', [ 2, 5, 2, 5, 1 ], 'row-major' ) + ]; + opts = { + 'order': 'column-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flattenFromBy( values[ j ], 0, opts, clbk ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/repl.txt new file mode 100644 index 000000000000..3e6da8dbb9ec --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/repl.txt @@ -0,0 +1,71 @@ + +{{alias}}( x, dim[, options], fcn[, thisArg] ) + Flattens an ndarray from a specified dimension while applying a callback + function to each element. + + Parameters + ---------- + x: ndarray + Input ndarray. Must have one or more dimensions. + + dim: integer + Dimension to start flattening from. If provided an integer less than + zero, the dimension index is resolved relative to the last dimension, + with the last dimension corresponding to the value `-1`. + + options: Object (optional) + Function options. + + options.order: string (optional) + Order in which input ndarray elements should be flattened. Must be one + of the following: + + - 'row-major': flatten elements in lexicographic order. + - 'column-major': flatten elements in colexicographic order. + - 'any': flatten according to the physical layout of the input ndarray + data in memory, regardless of the stated order of the input ndarray. + - 'same': flatten according to the stated order of the input ndarray. + + Default: 'row-major'. + + options.dtype: any (optional) + Output ndarray data type. By default, the function returns an ndarray + having the same data type as a provided input ndarray. + + fcn: Function + Callback function. + + thisArg: any (optional) + Callback execution context. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ [ 1, 2 ], [ 3, 4 ] ] ] ) + + > function scale( v ) { return v * 2.0; }; + > var y = {{alias}}( x, 1, scale ) + + > {{alias:@stdlib/ndarray/to-array}}( y ) + [ [ 2, 4, 6, 8 ] ] + + > x = {{alias:@stdlib/ndarray/array}}( [ [ [ 1, 2 ], [ 3, 4 ] ] ] ) + + > y = {{alias}}( x, 0, { 'order': 'column-major' }, scale ) + + > {{alias:@stdlib/ndarray/to-array}}( y ) + [ 2, 6, 4, 8 ] + + > x = {{alias:@stdlib/ndarray/array}}( [ [ [ 1, 2 ], [ 3, 4 ] ] ] ) + + > y = {{alias}}( x, 0, { 'dtype': 'float32' }, scale ) + + > {{alias:@stdlib/ndarray/to-array}}( y ) + [ 2, 4, 6, 8 ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/types/index.d.ts new file mode 100644 index 000000000000..e548a8491af7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/types/index.d.ts @@ -0,0 +1,437 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { typedndarray, genericndarray, Order, DataTypeMap } from '@stdlib/types/ndarray'; +import { ComplexLike } from '@stdlib/types/complex'; + +/** +* Callback invoked for each ndarray element. +* +* @returns output value +*/ +type Nullary = ( this: ThisArg ) => V; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @returns output value +*/ +type Unary = ( this: ThisArg, value: T ) => V; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param indices - current array element indices +* @returns output value +*/ +type Binary = ( this: ThisArg, value: T, indices: Array ) => V; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param indices - current array element indices +* @param arr - input array +* @returns output value +*/ +type Ternary = ( this: ThisArg, value: T, indices: Array, arr: U ) => V; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param indices - current array element indices +* @param arr - input array +* @returns output value +*/ +type Callback = Nullary | Unary | Binary | Ternary; + +/** +* Interface defining "base" function options. +*/ +interface BaseOptions { + /** + * Order in which input ndarray elements should be flattened. + * + * ## Notes + * + * - The following orders are supported: + * + * - **row-major**: flatten in lexicographic order. + * - **column-major**: flatten in colexicographic order. + * - **same**: flatten according to the stated order of the input ndarray. + * - **any**: flatten according to the physical layout of the input ndarray data in memory, regardless of the stated order of the input ndarray. + * + * - Default: 'row-major'. + */ + order?: Order | 'same' | 'any'; +} + +/** +* Function options. +*/ +type Options = BaseOptions & { + /** + * Output ndarray data type. + */ + dtype: U; +}; + +/** +* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* +* @param x - input ndarray +* @param dim - dimension to start flattening from +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = flattenFromBy( x, 1, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2.0, 4.0 ], [ 6.0, 8.0 ], [ 10.0, 12.0 ] ] +*/ +declare function flattenFromBy = typedndarray, ThisArg = unknown>( x: T, dim: number, fcn: Callback, thisArg?: ThisParameterType> ): T; + +/** +* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* +* @param x - input ndarray +* @param dim - dimension to start flattening from +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Complex64Array = require( '@stdlib/array/complex64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function identity( value ) { +* return value; +* } +* +* var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 1, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = flattenFromBy( x, 0, identity ); +* // returns +*/ +declare function flattenFromBy = typedndarray, ThisArg = unknown>( x: U, dim: number, fcn: Callback, thisArg?: ThisParameterType> ): U; + +/** +* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* +* @param x - input ndarray +* @param dim - dimension to start flattening from +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var BooleanArray = require( '@stdlib/array/bool' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function invert( value ) { +* return !value; +* } +* +* var buffer = new BooleanArray( [ true, false, true, false, true, false ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'bool', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = flattenFromBy( x, 1, invert ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ false, true ], [ false, true ], [ false, true ] ] +*/ +declare function flattenFromBy = typedndarray, ThisArg = unknown>( x: T, dim: number, fcn: Callback, thisArg?: ThisParameterType> ): T; + +/** +* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* +* @param x - input ndarray +* @param dim - dimension to start flattening from +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = flattenFromBy( x, 1, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2.0, 4.0 ], [ 6.0, 8.0 ], [ 10.0, 12.0 ] ] +*/ +declare function flattenFromBy = genericndarray, V = unknown, W extends genericndarray = genericndarray, ThisArg = unknown>( x: U, dim: number, fcn: Callback, thisArg?: ThisParameterType> ): W; + +/** +* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* +* @param x - input ndarray +* @param dim - dimension to start flattening from +* @param options - function options +* @param options.order - order in which input ndarray elements should be flattened +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'column-major' +* }; +* +* var y = flattenFromBy( x, 1, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2.0, 4.0 ], [ 6.0, 8.0 ], [ 10.0, 12.0 ] ] +*/ +declare function flattenFromBy = typedndarray, ThisArg = unknown>( x: T, dim: number, options: BaseOptions, fcn: Callback, thisArg?: ThisParameterType> ): T; + +/** +* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* +* @param x - input ndarray +* @param dim - dimension to start flattening from +* @param options - function options +* @param options.order - order in which input ndarray elements should be flattened +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Complex64Array = require( '@stdlib/array/complex64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function identity( value ) { +* return value; +* } +* +* var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 1, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'column-major' +* }; +* +* var y = flattenFromBy( x, 0, opts, identity ); +* // returns +*/ +declare function flattenFromBy = typedndarray, ThisArg = unknown>( x: U, dim: number, options: BaseOptions, fcn: Callback, thisArg?: ThisParameterType> ): U; + +/** +* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* +* @param x - input ndarray +* @param dim - dimension to start flattening from +* @param options - function options +* @param options.order - order in which input ndarray elements should be flattened +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var BooleanArray = require( '@stdlib/array/bool' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function invert( value ) { +* return !value; +* } +* +* var buffer = new BooleanArray( [ true, false, true, false, true, false ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'bool', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'column-major' +* }; +* +* var y = flattenFromBy( x, 1, opts, invert ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ false, true ], [ false, true ], [ false, true ] ] +*/ +declare function flattenFromBy = typedndarray, ThisArg = unknown>( x: T, dim: number, options: BaseOptions, fcn: Callback, thisArg?: ThisParameterType> ): T; + +/** +* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* +* @param x - input ndarray +* @param dim - dimension to start flattening from +* @param options - function options +* @param options.order - order in which input ndarray elements should be flattened +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'column-major' +* }; +* +* var y = flattenFromBy( x, 1, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2.0, 4.0 ], [ 6.0, 8.0 ], [ 10.0, 12.0 ] ] +*/ +declare function flattenFromBy = genericndarray, V = unknown, W extends genericndarray = genericndarray, ThisArg = unknown>( x: U, dim: number, options: BaseOptions, fcn: Callback, thisArg?: ThisParameterType> ): W; + +/** +* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* +* @param x - input ndarray +* @param dim - dimension to start flattening from +* @param options - function options +* @param options.order - order in which input ndarray elements should be flattened +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'dtype': 'float32' +* }; +* +* var y = flattenFromBy( x, 1, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2.0, 4.0 ], [ 6.0, 8.0 ], [ 10.0, 12.0 ] ] +*/ +declare function flattenFromBy | genericndarray = typedndarray, V = unknown, W extends keyof DataTypeMap = 'generic', ThisArg = unknown>( x: U, dim: number, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): DataTypeMap[W]; + + +// EXPORTS // + +export = flattenFromBy; diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/types/test.ts new file mode 100644 index 000000000000..6d3717613c5f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/types/test.ts @@ -0,0 +1,293 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import empty = require( '@stdlib/ndarray/base/empty' ); +import zeros = require( '@stdlib/ndarray/base/zeros' ); +import flattenFromBy = require( './index' ); + +/** +* Evaluates the identity function. +* +* @param x - input value +* @returns input value +*/ +function identity( x: any ): any { + return x; +} + +// TESTS // + +// The function returns an ndarray... +{ + const sh = [ 2, 2, 2 ]; + const ord = 'row-major'; + + flattenFromBy( zeros( 'float64', sh, ord ), 0, identity ); // $ExpectType float64ndarray + flattenFromBy( zeros( 'float64', sh, ord ), 1, {}, identity ); // $ExpectType float64ndarray + flattenFromBy( zeros( 'float64', sh, ord ), 0, identity, {} ); // $ExpectType float64ndarray + flattenFromBy( zeros( 'float64', sh, ord ), 1, {}, identity, {} ); // $ExpectType float64ndarray + flattenFromBy( zeros( 'float32', sh, ord ), 0, identity ); // $ExpectType float32ndarray + flattenFromBy( zeros( 'float32', sh, ord ), 1, {}, identity ); // $ExpectType float32ndarray + flattenFromBy( zeros( 'float32', sh, ord ), 0, identity, {} ); // $ExpectType float32ndarray + flattenFromBy( zeros( 'float32', sh, ord ), 1, {}, identity, {} ); // $ExpectType float32ndarray + flattenFromBy( zeros( 'complex64', sh, ord ), 0, identity ); // $ExpectType complex64ndarray + flattenFromBy( zeros( 'complex64', sh, ord ), 1, {}, identity ); // $ExpectType complex64ndarray + flattenFromBy( zeros( 'complex64', sh, ord ), 0, identity, {} ); // $ExpectType complex64ndarray + flattenFromBy( zeros( 'complex64', sh, ord ), 1, {}, identity, {} ); // $ExpectType complex64ndarray + flattenFromBy( zeros( 'complex128', sh, ord ), 0, identity ); // $ExpectType complex128ndarray + flattenFromBy( zeros( 'complex128', sh, ord ), 1, {}, identity ); // $ExpectType complex128ndarray + flattenFromBy( zeros( 'complex128', sh, ord ), 0, identity, {} ); // $ExpectType complex128ndarray + flattenFromBy( zeros( 'complex128', sh, ord ), 1, {}, identity, {} ); // $ExpectType complex128ndarray + flattenFromBy( zeros( 'int32', sh, ord ), 0, identity ); // $ExpectType int32ndarray + flattenFromBy( zeros( 'int32', sh, ord ), 1, {}, identity ); // $ExpectType int32ndarray + flattenFromBy( zeros( 'int32', sh, ord ), 0, identity, {} ); // $ExpectType int32ndarray + flattenFromBy( zeros( 'int32', sh, ord ), 1, {}, identity, {} ); // $ExpectType int32ndarray + flattenFromBy( zeros( 'int16', sh, ord ), 0, identity ); // $ExpectType int16ndarray + flattenFromBy( zeros( 'int16', sh, ord ), 1, {}, identity ); // $ExpectType int16ndarray + flattenFromBy( zeros( 'int16', sh, ord ), 0, identity, {} ); // $ExpectType int16ndarray + flattenFromBy( zeros( 'int16', sh, ord ), 1, {}, identity, {} ); // $ExpectType int16ndarray + flattenFromBy( zeros( 'int8', sh, ord ), 0, identity ); // $ExpectType int8ndarray + flattenFromBy( zeros( 'int8', sh, ord ), 1, {}, identity ); // $ExpectType int8ndarray + flattenFromBy( zeros( 'int8', sh, ord ), 0, identity, {} ); // $ExpectType int8ndarray + flattenFromBy( zeros( 'int8', sh, ord ), 1, {}, identity, {} ); // $ExpectType int8ndarray + flattenFromBy( zeros( 'uint32', sh, ord ), 0, identity ); // $ExpectType uint32ndarray + flattenFromBy( zeros( 'uint32', sh, ord ), 1, {}, identity ); // $ExpectType uint32ndarray + flattenFromBy( zeros( 'uint32', sh, ord ), 0, identity, {} ); // $ExpectType uint32ndarray + flattenFromBy( zeros( 'uint32', sh, ord ), 1, {}, identity, {} ); // $ExpectType uint32ndarray + flattenFromBy( zeros( 'uint16', sh, ord ), 0, identity ); // $ExpectType uint16ndarray + flattenFromBy( zeros( 'uint16', sh, ord ), 1, {}, identity ); // $ExpectType uint16ndarray + flattenFromBy( zeros( 'uint16', sh, ord ), 0, identity, {} ); // $ExpectType uint16ndarray + flattenFromBy( zeros( 'uint16', sh, ord ), 1, {}, identity, {} ); // $ExpectType uint16ndarray + flattenFromBy( zeros( 'uint8', sh, ord ), 0, identity ); // $ExpectType uint8ndarray + flattenFromBy( zeros( 'uint8', sh, ord ), 1, {}, identity ); // $ExpectType uint8ndarray + flattenFromBy( zeros( 'uint8', sh, ord ), 0, identity, {} ); // $ExpectType uint8ndarray + flattenFromBy( zeros( 'uint8', sh, ord ), 1, {}, identity, {} ); // $ExpectType uint8ndarray + flattenFromBy( zeros( 'uint8c', sh, ord ), 0, identity ); // $ExpectType uint8cndarray + flattenFromBy( zeros( 'uint8c', sh, ord ), 1, {}, identity ); // $ExpectType uint8cndarray + flattenFromBy( zeros( 'uint8c', sh, ord ), 0, identity, {} ); // $ExpectType uint8cndarray + flattenFromBy( zeros( 'uint8c', sh, ord ), 1, {}, identity, {} ); // $ExpectType uint8cndarray + flattenFromBy( empty( 'bool', sh, ord ), 0, identity ); // $ExpectType boolndarray + flattenFromBy( empty( 'bool', sh, ord ), 1, {}, identity ); // $ExpectType boolndarray + flattenFromBy( empty( 'bool', sh, ord ), 0, identity, {} ); // $ExpectType boolndarray + flattenFromBy( empty( 'bool', sh, ord ), 1, {}, identity, {} ); // $ExpectType boolndarray + flattenFromBy( zeros( 'generic', sh, ord ), 0, identity ); // $ExpectType genericndarray + flattenFromBy( zeros( 'generic', sh, ord ), 1, {}, identity ); // $ExpectType genericndarray + flattenFromBy( zeros( 'generic', sh, ord ), 0, identity, {} ); // $ExpectType genericndarray + flattenFromBy( zeros( 'generic', sh, ord ), 1, {}, identity, {} ); // $ExpectType genericndarray + + flattenFromBy( zeros( 'float64', sh, ord ), 0, { 'dtype': 'float32' }, identity ); // $ExpectType float32ndarray + flattenFromBy( zeros( 'float64', sh, ord ), 1, { 'dtype': 'generic' }, identity ); // $ExpectType genericndarray + flattenFromBy( zeros( 'generic', sh, ord ), 0, { 'dtype': 'float64' }, identity ); // $ExpectType float64ndarray + flattenFromBy( zeros( 'generic', sh, ord ), 1, { 'dtype': 'generic' }, identity ); // $ExpectType genericndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + flattenFromBy( '5', 0, identity ); // $ExpectError + flattenFromBy( 5, 0, identity ); // $ExpectError + flattenFromBy( true, 0, identity ); // $ExpectError + flattenFromBy( false, 0, identity ); // $ExpectError + flattenFromBy( null, 0, identity ); // $ExpectError + flattenFromBy( undefined, 0, identity ); // $ExpectError + flattenFromBy( {}, 0, identity ); // $ExpectError + flattenFromBy( [ 1 ], 0, identity ); // $ExpectError + flattenFromBy( ( x: number ): number => x, 0, identity ); // $ExpectError + + flattenFromBy( '5', 0, {}, identity ); // $ExpectError + flattenFromBy( 5, 0, {}, identity ); // $ExpectError + flattenFromBy( true, 0, {}, identity ); // $ExpectError + flattenFromBy( false, 0, {}, identity ); // $ExpectError + flattenFromBy( null, 0, {}, identity ); // $ExpectError + flattenFromBy( undefined, 0, {}, identity ); // $ExpectError + flattenFromBy( {}, 0, {}, identity ); // $ExpectError + flattenFromBy( [ 1 ], 0, {}, identity ); // $ExpectError + flattenFromBy( ( x: number ): number => x, 0, {}, identity ); // $ExpectError + + flattenFromBy( '5', 0, identity, {} ); // $ExpectError + flattenFromBy( 5, 0, identity, {} ); // $ExpectError + flattenFromBy( true, 0, identity, {} ); // $ExpectError + flattenFromBy( false, 0, identity, {} ); // $ExpectError + flattenFromBy( null, 0, identity, {} ); // $ExpectError + flattenFromBy( undefined, 0, identity, {} ); // $ExpectError + flattenFromBy( {}, 0, identity, {} ); // $ExpectError + flattenFromBy( [ 1 ], 0, identity, {} ); // $ExpectError + flattenFromBy( ( x: number ): number => x, 0, identity, {} ); // $ExpectError + + flattenFromBy( '5', 0, {}, identity, {} ); // $ExpectError + flattenFromBy( 5, 0, {}, identity, {} ); // $ExpectError + flattenFromBy( true, 0, {}, identity, {} ); // $ExpectError + flattenFromBy( false, 0, {}, identity, {} ); // $ExpectError + flattenFromBy( null, 0, {}, identity, {} ); // $ExpectError + flattenFromBy( undefined, 0, {}, identity, {} ); // $ExpectError + flattenFromBy( {}, 0, {}, identity, {} ); // $ExpectError + flattenFromBy( [ 1 ], 0, {}, identity, {} ); // $ExpectError + flattenFromBy( ( x: number ): number => x, 0, {}, identity, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), '5', identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), true, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), false, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), null, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), [ 1 ], identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), ( x: number ): number => x, identity ); // $ExpectError + + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), '5', identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), true, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), false, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), null, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), [ 1 ], identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), ( x: number ): number => x, identity ); // $ExpectError + + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), '5', identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), true, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), false, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), null, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), [ 1 ], identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), ( x: number ): number => x, identity ); // $ExpectError + + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), '5', {}, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), true, {}, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), false, {}, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), null, {}, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), [ 1 ], {}, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), ( x: number ): number => x, {}, identity ); // $ExpectError + + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), '5', {}, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), true, {}, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), false, {}, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), null, {}, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), [ 1 ], {}, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), ( x: number ): number => x, {}, identity ); // $ExpectError + + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), '5', {}, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), true, {}, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), false, {}, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), null, {}, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), [ 1 ], {}, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), ( x: number ): number => x, {}, identity ); // $ExpectError +} + +// The compiler throws an error if the function is provided an options argument which is not an object... +{ + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, '5', identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, true, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, false, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, null, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, [ 1 ], identity ); // $ExpectError + + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, '5', identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, true, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, false, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, null, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, [ 1 ], identity ); // $ExpectError + + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, '5', identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, true, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, false, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, null, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, [ 1 ], identity ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `order` option... +{ + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': '5' }, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': true }, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': false }, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': null }, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': [ 1 ] }, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': ( x: number ): number => x }, identity ); // $ExpectError + + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': '5' }, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': true }, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': false }, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': null }, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': [ 1 ] }, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': ( x: number ): number => x }, identity ); // $ExpectError + + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': '5' }, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': true }, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': false }, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': null }, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': [ 1 ] }, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': ( x: number ): number => x }, identity ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dtype` option... +{ + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': '5' }, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': true }, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': false }, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': null }, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': [ 1 ] }, identity ); // $ExpectError + flattenFromBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': ( x: number ): number => x }, identity ); // $ExpectError + + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': '5' }, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': true }, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': false }, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': null }, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': [ 1 ] }, identity ); // $ExpectError + flattenFromBy( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': ( x: number ): number => x }, identity ); // $ExpectError + + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': '5' }, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': true }, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': false }, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': null }, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': [ 1 ] }, identity ); // $ExpectError + flattenFromBy( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': ( x: number ): number => x }, identity ); // $ExpectError +} + +// The compiler throws an error if the function is provided a callback which is not a function... +{ + const x = zeros( 'generic', [ 2, 2 ], 'row-major' ); + + flattenFromBy( x, 0, '5' ); // $ExpectError + flattenFromBy( x, 0, true ); // $ExpectError + flattenFromBy( x, 0, false ); // $ExpectError + flattenFromBy( x, 0, null ); // $ExpectError + flattenFromBy( x, 0, undefined ); // $ExpectError + flattenFromBy( x, 0, {} ); // $ExpectError + flattenFromBy( x, 0, [ 1 ] ); // $ExpectError + + flattenFromBy( x, 0, {}, '5' ); // $ExpectError + flattenFromBy( x, 0, {}, true ); // $ExpectError + flattenFromBy( x, 0, {}, false ); // $ExpectError + flattenFromBy( x, 0, {}, null ); // $ExpectError + flattenFromBy( x, 0, {}, undefined ); // $ExpectError + flattenFromBy( x, 0, {}, {} ); // $ExpectError + flattenFromBy( x, 0, {}, [ 1 ] ); // $ExpectError + + flattenFromBy( x, 0, {}, '5', {} ); // $ExpectError + flattenFromBy( x, 0, {}, true, {} ); // $ExpectError + flattenFromBy( x, 0, {}, false, {} ); // $ExpectError + flattenFromBy( x, 0, {}, null, {} ); // $ExpectError + flattenFromBy( x, 0, {}, undefined, {} ); // $ExpectError + flattenFromBy( x, 0, {}, {}, {} ); // $ExpectError + flattenFromBy( x, 0, {}, [ 1 ], {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( 'generic', [ 2, 2, 2 ], 'row-major' ); + + flattenFromBy(); // $ExpectError + flattenFromBy( x ); // $ExpectError + flattenFromBy( x, 0 ); // $ExpectError + flattenFromBy( x, 0, {}, identity, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from-by/examples/index.js b/lib/node_modules/@stdlib/ndarray/flatten-from-by/examples/index.js new file mode 100644 index 000000000000..bdacdc565531 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from-by/examples/index.js @@ -0,0 +1,41 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var flattenFromBy = require( './../lib' ); + +function scale( value ) { + return value * 2.0; +} + +var xbuf = discreteUniform( 12, -100, 100, { + 'dtype': 'generic' +}); + +var x = array( xbuf, { + 'shape': [ 2, 2, 3 ], + 'dtype': 'generic' +}); +console.log( ndarray2array( x ) ); + +var y = flattenFromBy( x, 1, scale ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from-by/lib/index.js b/lib/node_modules/@stdlib/ndarray/flatten-from-by/lib/index.js new file mode 100644 index 000000000000..1a58a06948cb --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from-by/lib/index.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Flatten an ndarray from a specified dimension while applying a callback function to each element. +* +* @module @stdlib/ndarray/flatten-from-by +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var flattenFromBy = require( '@stdlib/ndarray/flatten-from-by' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +* // returns +* +* var y = flattenFromBy( x, 1, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2.0, 4.0 ], [ 6.0, 8.0 ], [ 10.0, 12.0 ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from-by/lib/main.js b/lib/node_modules/@stdlib/ndarray/flatten-from-by/lib/main.js new file mode 100644 index 000000000000..1601797f1ea6 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from-by/lib/main.js @@ -0,0 +1,315 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isInteger = require( '@stdlib/assert/is-integer' ); +var isMostlySafeCast = require( '@stdlib/ndarray/base/assert/is-mostly-safe-data-type-cast' ); +var isOrder = require( '@stdlib/ndarray/base/assert/is-order' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var getStrides = require( '@stdlib/ndarray/strides' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var getDType = require( '@stdlib/ndarray/base/dtype' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var strides2order = require( '@stdlib/ndarray/base/strides2order' ); +var flattenShapeFrom = require( '@stdlib/ndarray/base/flatten-shape-from' ); +var map = require( '@stdlib/ndarray/base/map' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); +var format = require( '@stdlib/string/format' ); + + +// VARIABLES // + +var ROW_MAJOR = 'row-major'; +var COL_MAJOR = 'column-major'; + + +// MAIN // + +/** +* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* +* @param {ndarray} x - input ndarray +* @param {integer} dim - dimension to start flattening from +* @param {Options} [options] - function options +* @param {string} [options.order='row-major'] - order in which input ndarray elements should be flattened +* @param {*} [options.dtype] - output ndarray data type +* @param {Function} fcn - callback function +* @param {*} [thisArg] - callback execution context +* @throws {TypeError} first argument must be an ndarray having one or more dimensions +* @throws {TypeError} second argument must be an integer +* @throws {TypeError} options argument must be an object +* @throws {TypeError} callback argument must be a function +* @throws {TypeError} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +* // returns +* +* var y = flattenFromBy( x, 1, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2.0, 4.0 ], [ 6.0, 8.0 ], [ 10.0, 12.0 ] ] +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], { +* 'shape': [ 2, 3 ], +* 'order': 'column-major' +* }); +* // returns +* +* var y = flattenFromBy( x, 0, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 2.0, 6.0, 10.0, 4.0, 8.0, 12.0 ] +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], { +* 'shape': [ 2, 3 ], +* 'order': 'row-major' +* }); +* // returns +* +* var opts = { +* 'order': 'column-major' +* }; +* +* var y = flattenFromBy( x, 0, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 2.0, 8.0, 4.0, 10.0, 6.0, 12.0 ] +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], { +* 'shape': [ 2, 3 ], +* 'order': 'column-major' +* }); +* // returns +* +* var opts = { +* 'order': 'row-major' +* }; +* +* var y = flattenFromBy( x, 0, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 2.0, 6.0, 10.0, 4.0, 8.0, 12.0 ] +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], { +* 'shape': [ 2, 3 ], +* 'order': 'row-major' +* }); +* // returns +* +* var opts = { +* 'order': 'same' +* }; +* +* var y = flattenFromBy( x, 0, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* this.count += 1; +* return value * 2.0; +* } +* +* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +* // returns +* +* var ctx = { +* 'count': 0 +* }; +* +* var y = flattenFromBy( x, 1, scale, ctx ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2.0, 4.0 ], [ 6.0, 8.0 ], [ 10.0, 12.0 ] ] +* +* var count = ctx.count; +* // returns 6 +*/ +function flattenFromBy( x, dim, options, fcn, thisArg ) { + var hasOpts; + var nargs; + var view; + var opts; + var ctx; + var xsh; + var cb; + var st; + var y; + var o; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + if ( !isInteger( dim ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', dim ) ); + } + xsh = getShape( x ); + if ( xsh.length < 1 ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray having one or more dimensions. Number of dimensions: %d.', xsh.length ) ); + } + nargs = arguments.length; + hasOpts = false; + + // Define default options: + opts = { + 'order': ROW_MAJOR, // by default, flatten in lexicographic order (i.e., trailing dimensions first; e.g., if `x` is a matrix, flatten row-by-row) + 'dtype': getDType( x ) + }; + + // Case: flattenFromBy( x, dim, fcn ) + if ( nargs <= 3 ) { + cb = options; + } + // Case: flattenFromBy( x, dim, ???, ??? ) + else if ( nargs === 4 ) { + // Case: flattenFromBy( x, dim, fcn, thisArg ) + if ( isFunction( options ) ) { + cb = options; + ctx = fcn; + } + // Case: flattenFromBy( x, dim, options, fcn ) + else { + hasOpts = true; + cb = fcn; + } + } + // Case: flattenFromBy( x, dim, options, fcn, thisArg ) + else { + hasOpts = true; + cb = fcn; + ctx = thisArg; + } + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) ); + } + if ( hasOpts ) { + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + if ( hasOwnProp( options, 'order' ) ) { + if ( options.order === 'any' ) { + // When 'any', we want to flatten according to the physical layout of the data in memory... + o = strides2order( getStrides( x ) ); + if ( o === 1 ) { + // Data is currently arranged in row-major order: + opts.order = ROW_MAJOR; + } else if ( o === 2 ) { + // Data is currently arranged in column-major order: + opts.order = COL_MAJOR; + } else { // o === 0 || o === 3 (i.e., neither row- nor column-major || both row- and column-major + // When the data is either both row- and column-major (e.g., a one-dimensional ndarray) or neither row- nor column-major (e.g., unordered strides), fallback to flattening according to the stated order of the input ndarray: + opts.order = getOrder( x ); + } + } else if ( options.order === 'same' ) { + // When 'same', we want to flatten according to the stated order of the input ndarray: + opts.order = getOrder( x ); + } else if ( isOrder( options.order ) ) { + // When provided a specific order, flatten according to that order regardless of the order of the input ndarray: + opts.order = options.order; + } else { + throw new TypeError( format( 'invalid option. `%s` option must be a recognized order. Option: `%s`.', 'order', options.order ) ); + } + } + if ( hasOwnProp( options, 'dtype' ) ) { + if ( !isMostlySafeCast( opts.dtype, options.dtype ) ) { + throw new TypeError( format( 'invalid option. First argument cannot be safely cast to the specified data type. Input data type: %s. Option: `%s`.', String( opts.dtype ), String( options.dtype ) ) ); + } + opts.dtype = options.dtype; + } + } + // Create an output ndarray having contiguous memory: + y = emptyLike( x, { + 'shape': flattenShapeFrom( xsh, dim ), // note: delegate to `flattenShapeFrom` to handle `dim` normalization + 'order': opts.order, + 'dtype': opts.dtype + }); + + // Create a view on top of the output ndarray having the same shape as the input ndarray: + st = ( xsh.length > 0 ) ? shape2strides( xsh, opts.order ) : [ 0 ]; + view = new ndarray( opts.dtype, getData( y ), xsh, st, 0, opts.order ); + + // Transform and assign elements to the output ndarray: + map( [ x, view ], cb, ctx ); + + return y; +} + + +// EXPORTS // + +module.exports = flattenFromBy; diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from-by/package.json b/lib/node_modules/@stdlib/ndarray/flatten-from-by/package.json new file mode 100644 index 000000000000..bdb4cef2010f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from-by/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/ndarray/flatten-from-by", + "version": "0.0.0", + "description": "Flatten an ndarray from a specified dimension while applying a callback function to each element.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "multidimensional", + "array", + "ndarray", + "tensor", + "matrix", + "flat", + "flatten", + "flatten-from", + "flatten-by", + "flatten-from-by", + "map", + "copy", + "transform", + "callback" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from-by/test/test.js b/lib/node_modules/@stdlib/ndarray/flatten-from-by/test/test.js new file mode 100644 index 000000000000..92d9021450bd --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from-by/test/test.js @@ -0,0 +1,1931 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' ); +var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); +var isSameFloat32Array = require( '@stdlib/assert/is-same-float32array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var identity = require( '@stdlib/number/float64/base/identity' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var flattenFromBy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof flattenFromBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenFromBy( value, 0, identity ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenFromBy( value, 0, {}, identity ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray (thisArg)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenFromBy( value, 0, identity, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray (options, thisArg)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenFromBy( value, 0, {}, identity, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenFromBy( zeros( [ 2, 2, 2 ] ), 0, value, identity ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object (thisArg)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenFromBy( zeros( [ 2, 2, 2 ] ), 0, value, identity, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid `order` option', function test( t ) { + var values; + var opts; + var i; + + values = [ + '5', + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + opts = { + 'order': value + }; + flattenFromBy( zeros( [ 2 ] ), 0, opts, identity ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid `dtype` option', function test( t ) { + var values; + var i; + + values = [ + 'foo', + 'bar', + 1, + NaN, + true, + false, + void 0, + null, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+ values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var opts = { + 'dtype': value + }; + flattenFromBy( zeros( [ 2 ] ), 0, opts, identity ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenFromBy( zeros( [ 2, 2, 2 ] ), 0, {}, value ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function (thisArg)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenFromBy( zeros( [ 2, 2, 2 ] ), 0, {}, value, {} ); + }; + } +}); + +tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, contiguous)', function test( t ) { + var expected; + var xbuf; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flattenFromBy( x, 0, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, 1, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 4 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, 2, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, -1, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, non-contiguous)', function test( t ) { + var expected; + var xbuf; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = [ 8, 4, 2 ]; + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, NaN, 2.0, NaN, 3.0, NaN, 4.0, NaN, 5.0, NaN, 6.0, NaN, 7.0, NaN, 8.0, NaN ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flattenFromBy( x, 0, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, 1, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 4 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, 2, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, -1, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (column-major, contiguous)', function test( t ) { + var expected; + var xbuf; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flattenFromBy( x, 0, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, 1, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 4 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, 2, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, -1, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (column-major, non-contiguous)', function test( t ) { + var expected; + var xbuf; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = [ 2, 4, 8 ]; + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, NaN, 5.0, NaN, 3.0, NaN, 7.0, NaN, 2.0, NaN, 6.0, NaN, 4.0, NaN, 8.0, NaN ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flattenFromBy( x, 0, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, 1, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 4 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, 2, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, -1, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a provided input ndarray in lexicographic order (row-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'row-major' + }; + y = flattenFromBy( x, 0, opts, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, 1, opts, identity ); + expected = [ + [ 1.0, 2.0, 3.0, 4.0 ], + [ 5.0, 6.0, 7.0, 8.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 4 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, 2, opts, identity ); + expected = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, -1, opts, identity ); + expected = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a provided input ndarray in lexicographic order (column-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'row-major' + }; + y = flattenFromBy( x, 0, opts, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, 1, opts, identity ); + expected = [ + [ 1.0, 2.0, 3.0, 4.0 ], + [ 5.0, 6.0, 7.0, 8.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 4 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, 2, opts, identity ); + expected = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, -1, opts, identity ); + expected = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a provided input ndarray in colexicographic order (row-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'column-major' + }; + y = flattenFromBy( x, 0, opts, identity ); + expected = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFromBy( x, 1, opts, identity ); + expected = [ + [ 1.0, 3.0, 2.0, 4.0 ], + [ 5.0, 7.0, 6.0, 8.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 4 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFromBy( x, 2, opts, identity ); + expected = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFromBy( x, -1, opts, identity ); + expected = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a provided input ndarray in colexicographic order (column-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'column-major' + }; + y = flattenFromBy( x, 0, opts, identity ); + expected = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFromBy( x, 1, opts, identity ); + expected = [ + [ 1.0, 3.0, 2.0, 4.0 ], + [ 5.0, 7.0, 6.0, 8.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 4 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFromBy( x, 2, opts, identity ); + expected = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFromBy( x, -1, opts, identity ); + expected = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a provided input ndarray in same order as the input ndarray (row-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'same' + }; + y = flattenFromBy( x, 0, opts, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, 1, opts, identity ); + expected = [ + [ 1.0, 2.0, 3.0, 4.0 ], + [ 5.0, 6.0, 7.0, 8.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 4 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, 2, opts, identity ); + expected = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, -1, opts, identity ); + expected = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a provided input ndarray in same order as the input ndarray (column-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'same' + }; + y = flattenFromBy( x, 0, opts, identity ); + expected = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFromBy( x, 1, opts, identity ); + expected = [ + [ 1.0, 3.0, 2.0, 4.0 ], + [ 5.0, 7.0, 6.0, 8.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 4 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFromBy( x, 2, opts, identity ); + expected = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFromBy( x, -1, opts, identity ); + expected = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a provided input ndarray according to the physical layout of the input ndarray (row-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = [ -1, -2, -4 ]; // reversing and negating the strides simulates a flipped and reversed view + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 8.0, 4.0 ], + * [ 6.0, 2.0 ] + * ], + * [ + * [ 7.0, 3.0 ], + * [ 5.0, 1.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'any' + }; + y = flattenFromBy( x, 0, opts, identity ); + expected = new Float64Array( [ 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFromBy( x, 1, opts, identity ); + expected = [ + [ 8.0, 6.0, 4.0, 2.0 ], + [ 7.0, 5.0, 3.0, 1.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 4 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFromBy( x, 2, opts, identity ); + expected = [ + [ + [ 8.0, 4.0 ], + [ 6.0, 2.0 ] + ], + [ + [ 7.0, 3.0 ], + [ 5.0, 1.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFromBy( x, -1, opts, identity ); + expected = [ + [ + [ 8.0, 4.0 ], + [ 6.0, 2.0 ] + ], + [ + [ 7.0, 3.0 ], + [ 5.0, 1.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a provided input ndarray according to the physical layout of the input ndarray (column-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = [ -4, -2, -1 ]; // reversing and negating the strides simulates a flipped and reversed view + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 8.0, 7.0 ], + * [ 6.0, 5.0 ] + * ], + * [ + * [ 4.0, 3.0 ], + * [ 2.0, 1.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'any' + }; + y = flattenFromBy( x, 0, opts, identity ); + expected = new Float64Array( [ 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, 1, opts, identity ); + expected = [ + [ 8.0, 7.0, 6.0, 5.0 ], + [ 4.0, 3.0, 2.0, 1.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 4 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, 2, opts, identity ); + expected = [ + [ + [ 8.0, 7.0 ], + [ 6.0, 5.0 ] + ], + [ + [ 4.0, 3.0 ], + [ 2.0, 1.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFromBy( x, -1, opts, identity ); + expected = [ + [ + [ 8.0, 7.0 ], + [ 6.0, 5.0 ] + ], + [ + [ 4.0, 3.0 ], + [ 2.0, 1.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the output ndarray data type', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'dtype': 'float32' + }; + y = flattenFromBy( x, 0, opts, identity ); + expected = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), 'float32' ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), ord, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the callback execution context (row-major)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var xbuf; + var ord; + var ctx; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + ctx = { + 'count': 1 + }; + + values = []; + indices = []; + arrays = []; + y = flattenFromBy( x, 0, clbk, ctx ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.strictEqual( ctx.count, 9, 'returns expected value' ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + [ 0, 0, 0 ], + [ 0, 0, 1 ], + [ 0, 1, 0 ], + [ 0, 1, 1 ], + [ 1, 0, 0 ], + [ 1, 0, 1 ], + [ 1, 1, 0 ], + [ 1, 1, 1 ] + ]; + indices.sort( ascending ); // index order is not guaranteed + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + x, + x, + x, + x, + x, + x, + x, + x + ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function clbk( v, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( v ); + indices.push( idx ); + arrays.push( arr ); + return v; + } + + function ascending( a, b ) { + if ( a[ 0 ] > b[ 0 ] ) { + return 1; + } + if ( a[ 0 ] < b[ 0 ] ) { + return -1; + } + if ( a[ 1 ] > b[ 1 ] ) { + return 1; + } + if ( a[ 1 ] < b[ 1 ] ) { + return -1; + } + if ( a[ 2 ] > b[ 2 ] ) { + return 1; + } + if ( a[ 2 ] < b[ 2 ] ) { + return -1; + } + return 0; + } +}); + +tape( 'the function supports specifying the callback execution context (row-major, options)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var xbuf; + var ord; + var ctx; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + ctx = { + 'count': 1 + }; + + values = []; + indices = []; + arrays = []; + y = flattenFromBy( x, 0, {}, clbk, ctx ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.strictEqual( ctx.count, 9, 'returns expected value' ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + [ 0, 0, 0 ], + [ 0, 0, 1 ], + [ 0, 1, 0 ], + [ 0, 1, 1 ], + [ 1, 0, 0 ], + [ 1, 0, 1 ], + [ 1, 1, 0 ], + [ 1, 1, 1 ] + ]; + indices.sort( ascending ); // index order is not guaranteed + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + x, + x, + x, + x, + x, + x, + x, + x + ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function clbk( v, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( v ); + indices.push( idx ); + arrays.push( arr ); + return v; + } + + function ascending( a, b ) { + if ( a[ 0 ] > b[ 0 ] ) { + return 1; + } + if ( a[ 0 ] < b[ 0 ] ) { + return -1; + } + if ( a[ 1 ] > b[ 1 ] ) { + return 1; + } + if ( a[ 1 ] < b[ 1 ] ) { + return -1; + } + if ( a[ 2 ] > b[ 2 ] ) { + return 1; + } + if ( a[ 2 ] < b[ 2 ] ) { + return -1; + } + return 0; + } +}); + +tape( 'the function supports specifying the callback execution context (column-major)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var xbuf; + var ord; + var ctx; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + ctx = { + 'count': 1 + }; + + values = []; + indices = []; + arrays = []; + y = flattenFromBy( x, 0, clbk, ctx ); + expected = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.strictEqual( ctx.count, 9, 'returns expected value' ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + [ 0, 0, 0 ], + [ 0, 0, 1 ], + [ 0, 1, 0 ], + [ 0, 1, 1 ], + [ 1, 0, 0 ], + [ 1, 0, 1 ], + [ 1, 1, 0 ], + [ 1, 1, 1 ] + ]; + indices.sort( ascending ); // index order is not guaranteed + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + x, + x, + x, + x, + x, + x, + x, + x + ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function clbk( v, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( v ); + indices.push( idx ); + arrays.push( arr ); + return v; + } + + function ascending( a, b ) { + if ( a[ 0 ] > b[ 0 ] ) { + return 1; + } + if ( a[ 0 ] < b[ 0 ] ) { + return -1; + } + if ( a[ 1 ] > b[ 1 ] ) { + return 1; + } + if ( a[ 1 ] < b[ 1 ] ) { + return -1; + } + if ( a[ 2 ] > b[ 2 ] ) { + return 1; + } + if ( a[ 2 ] < b[ 2 ] ) { + return -1; + } + return 0; + } +}); + +tape( 'the function supports specifying the callback execution context (column-major, options)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var xbuf; + var ord; + var ctx; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + ctx = { + 'count': 1 + }; + + values = []; + indices = []; + arrays = []; + y = flattenFromBy( x, 0, {}, clbk, ctx ); + expected = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), dt ), true, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.strictEqual( ctx.count, 9, 'returns expected value' ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + [ 0, 0, 0 ], + [ 0, 0, 1 ], + [ 0, 1, 0 ], + [ 0, 1, 1 ], + [ 1, 0, 0 ], + [ 1, 0, 1 ], + [ 1, 1, 0 ], + [ 1, 1, 1 ] + ]; + indices.sort( ascending ); // index order is not guaranteed + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + x, + x, + x, + x, + x, + x, + x, + x + ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function clbk( v, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( v ); + indices.push( idx ); + arrays.push( arr ); + return v; + } + + function ascending( a, b ) { + if ( a[ 0 ] > b[ 0 ] ) { + return 1; + } + if ( a[ 0 ] < b[ 0 ] ) { + return -1; + } + if ( a[ 1 ] > b[ 1 ] ) { + return 1; + } + if ( a[ 1 ] < b[ 1 ] ) { + return -1; + } + if ( a[ 2 ] > b[ 2 ] ) { + return 1; + } + if ( a[ 2 ] < b[ 2 ] ) { + return -1; + } + return 0; + } +}); From 38c087465946c2e3c5468315250efd0fb811e02e Mon Sep 17 00:00:00 2001 From: headlessNode Date: Wed, 17 Dec 2025 19:49:09 +0500 Subject: [PATCH 2/2] docs: apply suggestions from code review --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/flatten-from-by/README.md | 4 ++-- .../ndarray/flatten-from-by/docs/repl.txt | 4 ++-- .../flatten-from-by/docs/types/index.d.ts | 18 +++++++++--------- .../ndarray/flatten-from-by/lib/index.js | 2 +- .../ndarray/flatten-from-by/lib/main.js | 2 +- .../ndarray/flatten-from-by/package.json | 2 +- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from-by/README.md b/lib/node_modules/@stdlib/ndarray/flatten-from-by/README.md index 363780a2e65a..6b0e21884157 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from-by/README.md +++ b/lib/node_modules/@stdlib/ndarray/flatten-from-by/README.md @@ -20,7 +20,7 @@ limitations under the License. # flattenFromBy -> Flatten an [ndarray][@stdlib/ndarray/ctor] from a specified dimension while applying a callback function to each element. +> Flatten an [ndarray][@stdlib/ndarray/ctor] according to a callback function starting from a specified dimension.
@@ -38,7 +38,7 @@ var flattenFromBy = require( '@stdlib/ndarray/flatten-from-by' ); #### flattenFromBy( x, dim\[, options], fcn\[, thisArg] ) -Flattens an [ndarray][@stdlib/ndarray/ctor] from a specified dimension while applying a callback function to each element. +Flattens an [ndarray][@stdlib/ndarray/ctor] according to a callback function starting from a specified dimension. ```javascript var array = require( '@stdlib/ndarray/array' ); diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/repl.txt index 3e6da8dbb9ec..403cb22c4f57 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/repl.txt @@ -1,7 +1,7 @@ {{alias}}( x, dim[, options], fcn[, thisArg] ) - Flattens an ndarray from a specified dimension while applying a callback - function to each element. + Flattens an ndarray according to a callback function starting from a + specified dimension. Parameters ---------- diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/types/index.d.ts index e548a8491af7..44ca3d81b5f2 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/flatten-from-by/docs/types/index.d.ts @@ -99,7 +99,7 @@ type Options = BaseOptions & { }; /** -* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* Flattens an ndarray according to a callback function starting from a specified dimension. * * @param x - input ndarray * @param dim - dimension to start flattening from @@ -133,7 +133,7 @@ type Options = BaseOptions & { declare function flattenFromBy = typedndarray, ThisArg = unknown>( x: T, dim: number, fcn: Callback, thisArg?: ThisParameterType> ): T; /** -* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* Flattens an ndarray according to a callback function starting from a specified dimension. * * @param x - input ndarray * @param dim - dimension to start flattening from @@ -164,7 +164,7 @@ declare function flattenFromBy = typedndarray = typedndarray, ThisArg = unknown>( x: U, dim: number, fcn: Callback, thisArg?: ThisParameterType> ): U; /** -* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* Flattens an ndarray according to a callback function starting from a specified dimension. * * @param x - input ndarray * @param dim - dimension to start flattening from @@ -198,7 +198,7 @@ declare function flattenFromBy = typedndarray, ThisArg = unknown>( x: T, dim: number, fcn: Callback, thisArg?: ThisParameterType> ): T; /** -* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* Flattens an ndarray according to a callback function starting from a specified dimension. * * @param x - input ndarray * @param dim - dimension to start flattening from @@ -231,7 +231,7 @@ declare function flattenFromBy = typedndarray = genericndarray, V = unknown, W extends genericndarray = genericndarray, ThisArg = unknown>( x: U, dim: number, fcn: Callback, thisArg?: ThisParameterType> ): W; /** -* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* Flattens an ndarray according to a callback function starting from a specified dimension. * * @param x - input ndarray * @param dim - dimension to start flattening from @@ -272,7 +272,7 @@ declare function flattenFromBy = generi declare function flattenFromBy = typedndarray, ThisArg = unknown>( x: T, dim: number, options: BaseOptions, fcn: Callback, thisArg?: ThisParameterType> ): T; /** -* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* Flattens an ndarray according to a callback function starting from a specified dimension. * * @param x - input ndarray * @param dim - dimension to start flattening from @@ -310,7 +310,7 @@ declare function flattenFromBy = typedndarray = typedndarray, ThisArg = unknown>( x: U, dim: number, options: BaseOptions, fcn: Callback, thisArg?: ThisParameterType> ): U; /** -* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* Flattens an ndarray according to a callback function starting from a specified dimension. * * @param x - input ndarray * @param dim - dimension to start flattening from @@ -351,7 +351,7 @@ declare function flattenFromBy = typedndarray, ThisArg = unknown>( x: T, dim: number, options: BaseOptions, fcn: Callback, thisArg?: ThisParameterType> ): T; /** -* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* Flattens an ndarray according to a callback function starting from a specified dimension. * * @param x - input ndarray * @param dim - dimension to start flattening from @@ -391,7 +391,7 @@ declare function flattenFromBy = typedndarray = genericndarray, V = unknown, W extends genericndarray = genericndarray, ThisArg = unknown>( x: U, dim: number, options: BaseOptions, fcn: Callback, thisArg?: ThisParameterType> ): W; /** -* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* Flattens an ndarray according to a callback function starting from a specified dimension. * * @param x - input ndarray * @param dim - dimension to start flattening from diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from-by/lib/index.js b/lib/node_modules/@stdlib/ndarray/flatten-from-by/lib/index.js index 1a58a06948cb..39febc133700 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from-by/lib/index.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-from-by/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Flatten an ndarray from a specified dimension while applying a callback function to each element. +* Flatten an ndarray according to a callback function starting from a specified dimension. * * @module @stdlib/ndarray/flatten-from-by * diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from-by/lib/main.js b/lib/node_modules/@stdlib/ndarray/flatten-from-by/lib/main.js index 1601797f1ea6..bfe7354b7d05 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from-by/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-from-by/lib/main.js @@ -50,7 +50,7 @@ var COL_MAJOR = 'column-major'; // MAIN // /** -* Flattens an ndarray from a specified dimension while applying a callback function to each element. +* Flattens an ndarray according to a callback function starting from a specified dimension. * * @param {ndarray} x - input ndarray * @param {integer} dim - dimension to start flattening from diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from-by/package.json b/lib/node_modules/@stdlib/ndarray/flatten-from-by/package.json index bdb4cef2010f..92e16e6f4600 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from-by/package.json +++ b/lib/node_modules/@stdlib/ndarray/flatten-from-by/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/ndarray/flatten-from-by", "version": "0.0.0", - "description": "Flatten an ndarray from a specified dimension while applying a callback function to each element.", + "description": "Flatten an ndarray according to a callback function starting from a specified dimension.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors",