From 5da54027ca853a5028af317b3a50fccbfa029e19 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 28 Sep 2025 19:43:00 +0500 Subject: [PATCH 1/4] feat: add ndarray/flatten-from --- 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/README.md | 203 +++ .../flatten-from/benchmark/benchmark.js | 302 ++++ .../ndarray/flatten-from/docs/repl.txt | 51 + .../flatten-from/docs/types/index.d.ts | 132 ++ .../ndarray/flatten-from/docs/types/test.ts | 187 ++ .../ndarray/flatten-from/examples/index.js | 37 + .../@stdlib/ndarray/flatten-from/lib/index.js | 57 + .../@stdlib/ndarray/flatten-from/lib/main.js | 362 ++++ .../@stdlib/ndarray/flatten-from/package.json | 66 + .../@stdlib/ndarray/flatten-from/test/test.js | 1518 +++++++++++++++++ 10 files changed, 2915 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-from/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/README.md b/lib/node_modules/@stdlib/ndarray/flatten-from/README.md new file mode 100644 index 000000000000..1f80b6360406 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/README.md @@ -0,0 +1,203 @@ + + +# flattenFrom + +> Return a copy of an input [ndarray][@stdlib/ndarray/ctor], where all dimensions of the input [ndarray][@stdlib/ndarray/ctor] are flattened starting from a specific dimension. + +
+ +
+ + + +
+ +## Usage + +```javascript +var flattenFrom = require( '@stdlib/ndarray/flatten-from' ); +``` + +#### flattenFrom( x, dim\[, options] ) + +Returns a copy of an input [ndarray][@stdlib/ndarray/ctor], where all dimensions of the input [ndarray][@stdlib/ndarray/ctor] are flattened starting from a specific dimension. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var shape = require( '@stdlib/ndarray/shape' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +// returns + +var shx = shape( x ); +// returns [ 3, 1, 2 ] + +var y = flattenFrom( x, 1 ); +// returns + +var shy = shape( y ); +// returns [ 3, 2 ] + +var arr = ndarray2array( y ); +// returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +``` + +The function accepts the following arguments: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **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_). + +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 shape = require( '@stdlib/ndarray/shape' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +// returns + +var shx = shape( x ); +// returns [ 3, 1, 2 ] + +var y = flattenFrom( x, 0, { + 'order': 'column-major' +}); +// returns + +var shy = shape( y ); +// returns [ 6 ] + +var arr = ndarray2array( y ); +// returns [ 1.0, 3.0, 5.0, 2.0, 4.0, 6.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 shape = require( '@stdlib/ndarray/shape' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +// returns + +var shx = shape( x ); +// returns [ 3, 1, 2 ] + +var y = flattenFrom( x, 0, { + 'dtype': 'float32' +}); +// returns + +var shy = shape( y ); +// returns [ 6 ] + +var dt = dtype( y ); +// returns 'float32' + +var arr = ndarray2array( y ); +// returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] +``` + +
+ + + +
+ +## 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. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var flattenFrom = require( '@stdlib/ndarray/flatten-from' ); + +var xbuf = discreteUniform( 12, -100, 100, { + 'dtype': 'generic' +}); + +var x = array( xbuf, { + 'shape': [ 2, 2, 3 ], + 'dtype': 'generic' +}); +console.log( ndarray2array( x ) ); + +var y = flattenFrom( x, 1 ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/flatten-from/benchmark/benchmark.js new file mode 100644 index 000000000000..b86770566ddc --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/benchmark/benchmark.js @@ -0,0 +1,302 @@ +/** +* @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 flattenFrom = require( './../lib' ); + + +// 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( 'complex128', [ 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 = flattenFrom( values[ j ], 0, opts ); + 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( 'complex128', [ 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 = flattenFrom( values[ j ], 0, opts ); + 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( 'complex128', [ 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 = flattenFrom( values[ j ], 0, opts ); + 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( 'complex128', [ 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 = flattenFrom( values[ j ], 0, opts ); + 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( 'complex128', [ 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 = flattenFrom( values[ j ], 0, opts ); + 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( 'complex128', [ 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 = flattenFrom( values[ j ], 0, opts ); + 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( 'complex128', [ 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 = flattenFrom( values[ j ], 0, opts ); + 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( 'complex128', [ 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 = flattenFrom( values[ j ], 0, opts ); + 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/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/flatten-from/docs/repl.txt new file mode 100644 index 000000000000..fd55a15c6063 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/docs/repl.txt @@ -0,0 +1,51 @@ + +{{alias}}( x, dim[, options] ) + Returns a copy of an input ndarray, where all dimensions of the input + ndarray are flattened starting from a specific dimension. + + The function always returns a copy of input ndarray data, even when an input + ndarray already has the desired number of dimensions. + + Parameters + ---------- + x: ndarray + Input ndarray. + + 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. 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 physical layout of the input ndarray data in + memory, regardless of the stated order of the input ndarray. + + Default: 'row-major'. + + options.dtype: string (optional) + Output ndarray data type. By default, the function returns an ndarray + having the same data type as the provided input ndarray. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); + > var y = {{alias}}( x, 0 ); + > var arr = {{alias:@stdlib/ndarray/to-array}}( y ) + [ 1.0, 2.0, 3.0, 4.0 ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/index.d.ts new file mode 100644 index 000000000000..ac7b5e99fb09 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/index.d.ts @@ -0,0 +1,132 @@ +/* +* @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 { ndarray, typedndarray, Order, DataTypeMap } from '@stdlib/types/ndarray'; + +/** +* 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; +}; + +/** +* Returns a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specific dimension. +* +* ## Notes +* +* - The function **always** returns a copy of input ndarray data, even when an input ndarray already has the desired number of dimensions. +* - By default, the function returns an ndarray having the same data type as a provided input ndarray. +* +* @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 +* @returns output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var shape = require( '@stdlib/ndarray/shape' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +* // returns +* +* var shx = shape( x ); +* // returns [ 3, 1, 2 ] +* +* var y = flattenFrom( x, 1 ); +* // returns +* +* var shy = shape( y ); +* // returns [ 3, 2 ] +* +* var arr = ndarray2array( y ); +* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +*/ +declare function flattenFrom( x: T, dim: number, options?: BaseOptions ): T; + +/** +* Returns a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specific dimension. +* +* ## Notes +* +* - The function **always** returns a copy of input ndarray data, even when an input ndarray already has the desired number of dimensions. +* +* @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 +* @returns output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var shape = require( '@stdlib/ndarray/shape' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +* // returns +* +* var shx = shape( x ); +* // returns [ 3, 1, 2 ] +* +* var y = flattenFrom( x, 1 ); +* // returns +* +* var shy = shape( y ); +* // returns [ 3, 2 ] +* +* var arr = ndarray2array( y ); +* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +*/ +declare function flattenFrom = 'generic'>( x: typedndarray, dim: number, options: Options ): DataTypeMap[U]; + + +// EXPORTS // + +export = flattenFrom; diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/test.ts new file mode 100644 index 000000000000..8e95897392de --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/test.ts @@ -0,0 +1,187 @@ +/* +* @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 zeros = require( '@stdlib/ndarray/base/zeros' ); +import flattenFrom = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0 ); // $ExpectType float64ndarray + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0 ); // $ExpectType complex128ndarray + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0 ); // $ExpectType genericndarray + + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, {} ); // $ExpectType float64ndarray + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, {} ); // $ExpectType complex128ndarray + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, {} ); // $ExpectType genericndarray + + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': 'float32' } ); // $ExpectType float32ndarray + flattenFrom( zeros( 'int32', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': 'float64' } ); // $ExpectType float64ndarray + flattenFrom( zeros( 'int32', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': 'generic' } ); // $ExpectType genericndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray-like object... +{ + flattenFrom( '5', 0 ); // $ExpectError + flattenFrom( 5, 0 ); // $ExpectError + flattenFrom( true, 0 ); // $ExpectError + flattenFrom( false, 0 ); // $ExpectError + flattenFrom( null, 0 ); // $ExpectError + flattenFrom( undefined, 0 ); // $ExpectError + flattenFrom( {}, 0 ); // $ExpectError + flattenFrom( [ 1 ], 0 ); // $ExpectError + flattenFrom( ( x: number ): number => x, 0 ); // $ExpectError + + flattenFrom( '5', 0, {} ); // $ExpectError + flattenFrom( 5, 0, {} ); // $ExpectError + flattenFrom( true, 0, {} ); // $ExpectError + flattenFrom( false, 0, {} ); // $ExpectError + flattenFrom( null, 0, {} ); // $ExpectError + flattenFrom( undefined, 0, {} ); // $ExpectError + flattenFrom( {}, 0, {} ); // $ExpectError + flattenFrom( [ 1 ], 0, {} ); // $ExpectError + flattenFrom( ( x: number ): number => x, 0, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an integer... +{ + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), '5' ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), true ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), false ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), null ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), [ 1 ] ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), ( x: number ): number => x ); // $ExpectError + + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), '5' ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), true ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), false ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), null ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), [ 1 ] ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), ( x: number ): number => x ); // $ExpectError + + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), '5' ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), true ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), false ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), null ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), [ 1 ] ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), ( x: number ): number => x ); // $ExpectError + + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), '5', {} ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), true, {} ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), false, {} ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), null, {} ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), [ 1 ], {} ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), ( x: number ): number => x, {} ); // $ExpectError + + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), '5', {} ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), true, {} ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), false, {} ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), null, {} ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), [ 1 ], {} ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), ( x: number ): number => x, {} ); // $ExpectError + + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), '5', {} ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), true, {} ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), false, {} ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), null, {} ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), [ 1 ], {} ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), ( x: number ): number => x, {} ); // $ExpectError +} + + +// The compiler throws an error if the function is provided an options argument which is not an object... +{ + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, '5' ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, true ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, false ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, null ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, [ 1 ] ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, ( x: number ): number => x ); // $ExpectError + + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, '5' ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, true ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, false ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, null ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, [ 1 ] ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, ( x: number ): number => x ); // $ExpectError + + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, '5' ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, true ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, false ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, null ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, [ 1 ] ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument with invalid `order` option... +{ + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': '5' } ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': true } ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': false } ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': null } ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': [ 1 ] } ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': ( x: number ): number => x } ); // $ExpectError + + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': '5' } ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': true } ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': false } ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': null } ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': [ 1 ] } ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': ( x: number ): number => x } ); // $ExpectError + + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': '5' } ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': true } ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': false } ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': null } ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': [ 1 ] } ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument with invalid `dtype` option... +{ + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': '5' } ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': true } ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': false } ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': null } ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': [ 1 ] } ); // $ExpectError + flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': ( x: number ): number => x } ); // $ExpectError + + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': '5' } ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': true } ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': false } ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': null } ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': [ 1 ] } ); // $ExpectError + flattenFrom( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': ( x: number ): number => x } ); // $ExpectError + + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': '5' } ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': true } ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': false } ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': null } ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': [ 1 ] } ); // $ExpectError + flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( 'float64', [ 2, 2, 2 ], 'row-major' ); + + flattenFrom( x ); // $ExpectError + flattenFrom( x, 0, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/examples/index.js b/lib/node_modules/@stdlib/ndarray/flatten-from/examples/index.js new file mode 100644 index 000000000000..5bec663f17fe --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/examples/index.js @@ -0,0 +1,37 @@ +/** +* @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 flattenFrom = require( './../lib' ); + +var xbuf = discreteUniform( 12, -100, 100, { + 'dtype': 'generic' +}); + +var x = array( xbuf, { + 'shape': [ 2, 2, 3 ], + 'dtype': 'generic' +}); +console.log( ndarray2array( x ) ); + +var y = flattenFrom( x, 1 ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/lib/index.js b/lib/node_modules/@stdlib/ndarray/flatten-from/lib/index.js new file mode 100644 index 000000000000..674954209bbe --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/lib/index.js @@ -0,0 +1,57 @@ +/** +* @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'; + +/** +* Return a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specific dimension. +* +* @module @stdlib/ndarray/flatten-from +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var shape = require( '@stdlib/ndarray/shape' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var flattenFrom = require( '@stdlib/ndarray/flatten-from' ); +* +* // Create an input ndarray: +* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +* // returns +* +* var shx = shape( x ); +* // returns [ 3, 1, 2 ] +* +* // Flatten the input ndarray: +* var y = flattenFrom( x, 1 ); +* // returns +* +* var shy = shape( y ); +* // returns [ 3, 2 ] +* +* var arr = ndarray2array( y ); +* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/lib/main.js b/lib/node_modules/@stdlib/ndarray/flatten-from/lib/main.js new file mode 100644 index 000000000000..19415b3b4fff --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/lib/main.js @@ -0,0 +1,362 @@ +/** +* @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 hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isInteger = require( '@stdlib/assert/is-integer' ); +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 assign = require( '@stdlib/ndarray/base/assign' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var format = require( '@stdlib/string/format' ); + + +// VARIABLES // + +var ROW_MAJOR = 'row-major'; +var COL_MAJOR = 'column-major'; + + +// MAIN // + +/** +* Returns a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specific dimension. +* +* @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 +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} second argument must be an integer +* @throws {TypeError} options argument must be an object +* @throws {TypeError} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +* // returns +* +* var y = flattenFrom( x, 1 ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], { +* 'shape': [ 2, 3 ], +* 'order': 'column-major' +* }); +* // returns +* +* var y = flattenFrom( x, 0 ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 1.0, 3.0, 5.0, 2.0, 4.0, 6.0 ] +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], { +* 'shape': [ 2, 3 ], +* 'order': 'row-major' +* }); +* // returns +* +* var y = flattenFrom( x, 0, { +* 'order': 'column-major' +* }); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 1.0, 4.0, 2.0, 5.0, 3.0, 6.0 ] +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], { +* 'shape': [ 2, 3 ], +* 'order': 'column-major' +* }); +* // returns +* +* var y = flattenFrom( x, 0, { +* 'order': 'row-major' +* }); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 1.0, 3.0, 5.0, 2.0, 4.0, 6.0 ] +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], { +* 'shape': [ 2, 3 ], +* 'order': 'row-major' +* }); +* // returns +* +* var y = flattenFrom( x, 0, { +* 'order': 'same' +* }); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], { +* 'shape': [ 2, 3 ], +* 'order': 'column-major' +* }); +* // returns +* +* var y = flattenFrom( x, 0, { +* 'order': 'same' +* }); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var xbuf = [ 1.0, null, 2.0, null, 3.0, null, 4.0, null, 5.0, null, 6.0, null ]; +* +* var x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ -6, -2 ], 10, 'row-major' ); +* // returns +* +* var y = flattenFrom( x, 0 ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 ] +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var xbuf = [ 1.0, null, 2.0, null, 3.0, null, 4.0, null, 5.0, null, 6.0, null ]; +* +* // Create an ndarray whose stated order is column-major, but which has been transposed: +* var x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ -6, -2 ], 10, 'column-major' ); +* // returns +* +* var y = flattenFrom( x, 0 ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 ] +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var xbuf = [ 1.0, null, 2.0, null, 3.0, null, 4.0, null, 5.0, null, 6.0, null ]; +* +* // Create an ndarray whose stated order is column-major, but which has been transposed: +* var x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ -6, -2 ], 10, 'column-major' ); +* // returns +* +* var y = flattenFrom( x, 0, { +* 'order': 'same' +* }); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 6.0, 3.0, 5.0, 2.0, 4.0, 1.0 ] +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var xbuf = [ 1.0, null, 2.0, null, 3.0, null, 4.0, null, 5.0, null, 6.0, null ]; +* +* // Create an ndarray whose stated order is column-major, but which has been transposed: +* var x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ -6, -2 ], 10, 'column-major' ); +* // returns +* +* var y = flattenFrom( x, 0, { +* 'order': 'any' +* }); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 ] +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var xbuf = [ 1.0, null, 2.0, null, 3.0, null, 4.0, null, 5.0, null, 6.0, null ]; +* +* // Create an ndarray whose stated order is row-major, but which has been transposed: +* var x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ -2, -4 ], 10, 'row-major' ); +* // returns +* +* var y = flattenFrom( x, 0 ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 6.0, 4.0, 2.0, 5.0, 3.0, 1.0 ] +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var xbuf = [ 1.0, null, 2.0, null, 3.0, null, 4.0, null, 5.0, null, 6.0, null ]; +* +* // Create an ndarray whose stated order is row-major, but which has been transposed: +* var x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ -2, -4 ], 10, 'row-major' ); +* // returns +* +* var y = flattenFrom( x, 0, { +* 'order': 'same' +* }); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 6.0, 4.0, 2.0, 5.0, 3.0, 1.0 ] +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var xbuf = [ 1.0, null, 2.0, null, 3.0, null, 4.0, null, 5.0, null, 6.0, null ]; +* +* // Create an ndarray whose stated order is row-major, but which has been transposed: +* var x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ -2, -4 ], 10, 'row-major' ); +* // returns +* +* var y = flattenFrom( x, 0, { +* 'order': 'any' +* }); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 ] +*/ +function flattenFrom( x, dim, options ) { + var nargs; + var view; + var opts; + var xsh; + var st; + var o; + var y; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + if ( !isInteger( dim ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', dim ) ); + } + nargs = arguments.length; + xsh = getShape( x ); + + // 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 ) + }; + + // Resolve function options... + if ( nargs > 2 ) { + 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' ) ) { + // Delegate `dtype` validation to `emptyLike` during output array creation: + opts.dtype = options.dtype; + } + } + // Create an output ndarray having contiguous memory: + y = emptyLike( x, { + 'shape': flattenShapeFrom( xsh, dim ), + 'order': opts.order, + 'dtype': opts.dtype + }); + + // Create a view on top of output ndarray having the same shape as the input ndarray: + st = ( xsh.length > 0 ) ? shape2strides( xsh, opts.order ) : [ 0 ]; + view = ndarray( opts.dtype, getData( y ), xsh, st, 0, opts.order ); + + // Copy elements to the output ndarray: + assign( [ x, view ] ); + + return y; +} + + +// EXPORTS // + +module.exports = flattenFrom; diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/package.json b/lib/node_modules/@stdlib/ndarray/flatten-from/package.json new file mode 100644 index 000000000000..7a2dc47d4805 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/ndarray/flatten-from", + "version": "0.0.0", + "description": "Return a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specific dimension.", + "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", + "reshape", + "copy", + "transform" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/test/test.js b/lib/node_modules/@stdlib/ndarray/flatten-from/test/test.js new file mode 100644 index 000000000000..2cdc03d39235 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/test/test.js @@ -0,0 +1,1518 @@ +/** +* @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 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 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 scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var flattenFrom = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof flattenFrom, '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() { + flattenFrom( value, 0 ); + }; + } +}); + +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() { + flattenFrom( value, 0, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an integer', function test( t ) { + var values; + 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() { + flattenFrom( zeros( [ 2, 2 ] ), value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an integer (options)', function test( t ) { + var values; + 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() { + flattenFrom( zeros( [ 2, 2 ] ), value, {} ); + }; + } +}); + +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, + 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() { + flattenFrom( zeros( [ 2, 2, 2 ] ), 0, value ); + }; + } +}); + +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 + }; + flattenFrom( zeros( [ 2 ] ), 0, opts ); + }; + } +}); + +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 + }; + flattenFrom( zeros( [ 2 ] ), 0, opts ); + }; + } +}); + +tape( 'by default, the function flattens all dimensions of a provided input ndarray starting from a specific dimension 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 = flattenFrom( x, 0 ); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFrom( x, 1 ); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFrom( x, 2 ); + 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( getDType( y ), dt, '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 starting from a specific dimension 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 = flattenFrom( x, 0 ); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFrom( x, 1 ); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFrom( x, 2 ); + 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( getDType( y ), dt, '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 starting from a specific dimension 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 = flattenFrom( x, 0 ); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFrom( x, 1 ); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFrom( x, 2 ); + 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( getDType( y ), dt, '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 starting from a specific dimension 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 = flattenFrom( x, 0 ); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFrom( x, 1 ); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFrom( x, 2 ); + 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( getDType( y ), dt, '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 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 = flattenFrom( x, 0, { + 'order': 'row-major' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFrom( x, 1, { + 'order': 'row-major' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFrom( x, 2, { + 'order': 'row-major' + }); + 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( getDType( y ), dt, '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 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 = flattenFrom( x, 0, { + 'order': 'row-major' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFrom( x, 1, { + 'order': 'row-major' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFrom( x, 2, { + 'order': 'row-major' + }); + 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( getDType( y ), dt, '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 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 = flattenFrom( x, 0, { + 'order': 'column-major' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFrom( x, 1, { + 'order': 'column-major' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFrom( x, 2, { + 'order': 'column-major' + }); + 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( getDType( y ), dt, '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 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 = flattenFrom( x, 0, { + 'order': 'column-major' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFrom( x, 1, { + 'order': 'column-major' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFrom( x, 2, { + 'order': 'column-major' + }); + 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( getDType( y ), dt, '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 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 = flattenFrom( x, 0, { + 'order': 'same' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFrom( x, 1, { + 'order': 'same' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFrom( x, 2, { + 'order': 'same' + }); + 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( getDType( y ), dt, '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 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 = flattenFrom( x, 0, { + 'order': 'same' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFrom( x, 1, { + 'order': 'same' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFrom( x, 2, { + 'order': 'same' + }); + 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( getDType( y ), dt, '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 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 ); + + y = flattenFrom( x, 0, { + 'order': 'any' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFrom( x, 1, { + 'order': 'any' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + y = flattenFrom( x, 2, { + 'order': 'any' + }); + 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( getDType( y ), dt, '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 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 ); + + y = flattenFrom( x, 0, { + 'order': 'any' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFrom( x, 1, { + 'order': 'any' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flattenFrom( x, 2, { + 'order': 'any' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a zero-dimensional input ndarray', function test( t ) { + var expected; + var xbuf; + var dt; + var x; + var y; + + dt = 'float64'; + x = scalar2ndarray( 3.0, { + 'dtype': dt, + 'order': 'row-major' + }); + + y = flattenFrom( x, 0 ); + expected = new Float64Array( [ 3.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 ), [], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + dt = 'float64'; + x = scalar2ndarray( 3.0, { + 'dtype': dt, + 'order': 'column-major' + }); + + y = flattenFrom( x, 1 ); + expected = new Float64Array( [ 3.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 ), [], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a zero-dimensional input ndarray (order=same)', function test( t ) { + var expected; + var xbuf; + var dt; + var x; + var y; + + dt = 'float64'; + x = scalar2ndarray( 3.0, { + 'dtype': dt, + 'order': 'row-major' + }); + + y = flattenFrom( x, 0, { + 'order': 'same' + }); + expected = new Float64Array( [ 3.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 ), [], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + dt = 'float64'; + x = scalar2ndarray( 3.0, { + 'dtype': dt, + 'order': 'column-major' + }); + + y = flattenFrom( x, 1, { + 'order': 'same' + }); + expected = new Float64Array( [ 3.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 ), [], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a zero-dimensional input ndarray (order=any)', function test( t ) { + var expected; + var xbuf; + var dt; + var x; + var y; + + dt = 'float64'; + x = scalar2ndarray( 3.0, { + 'dtype': dt, + 'order': 'row-major' + }); + + y = flattenFrom( x, 0, { + 'order': 'any' + }); + expected = new Float64Array( [ 3.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 ), [], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + dt = 'float64'; + x = scalar2ndarray( 3.0, { + 'dtype': dt, + 'order': 'column-major' + }); + + y = flattenFrom( x, 1, { + 'order': 'any' + }); + expected = new Float64Array( [ 3.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 ), [], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a one-dimensional input ndarray', 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 = [ 8 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + 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 = flattenFrom( x, 0 ); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + dt = 'float64'; + ord = 'column-major'; + sh = [ 8 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + 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 = flattenFrom( x, 1 ); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a one-dimensional input ndarray (order=same)', 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 = [ 8 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + 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 = flattenFrom( x, 0, { + 'order': 'same' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + dt = 'float64'; + ord = 'column-major'; + sh = [ 8 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + 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 = flattenFrom( x, 1, { + 'order': 'same' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a one-dimensional input ndarray (order=any)', 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 = [ 8 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + 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 = flattenFrom( x, 0, { + 'order': 'any' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + dt = 'float64'; + ord = 'column-major'; + sh = [ 8 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + 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 = flattenFrom( x, 1, { + 'order': 'any' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the output ndarray data type', 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 = flattenFrom( x, 0, { + 'dtype': 'float32' + }); + 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( getDType( y ), 'float32', 'returns expected value' ); + t.strictEqual( getOrder( y ), ord, 'returns expected value' ); + + t.end(); +}); From 558f8d1b9b97d7009a332680df75e497c0227b0d Mon Sep 17 00:00:00 2001 From: headlessNode Date: Mon, 29 Sep 2025 20:19:07 +0500 Subject: [PATCH 2/4] refactor: 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: passed - 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/README.md | 25 +- .../ndarray/flatten-from/docs/repl.txt | 2 +- .../flatten-from/docs/types/index.d.ts | 4 +- .../@stdlib/ndarray/flatten-from/lib/index.js | 9 +- .../@stdlib/ndarray/flatten-from/lib/main.js | 2 +- .../@stdlib/ndarray/flatten-from/package.json | 2 +- .../@stdlib/ndarray/flatten-from/test/test.js | 284 +++++++++++++++++- 7 files changed, 288 insertions(+), 40 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/README.md b/lib/node_modules/@stdlib/ndarray/flatten-from/README.md index 1f80b6360406..7dd6a9354fc9 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from/README.md +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/README.md @@ -20,7 +20,7 @@ limitations under the License. # flattenFrom -> Return a copy of an input [ndarray][@stdlib/ndarray/ctor], where all dimensions of the input [ndarray][@stdlib/ndarray/ctor] are flattened starting from a specific dimension. +> Return a copy of an input [ndarray][@stdlib/ndarray/ctor], where all dimensions of the input [ndarray][@stdlib/ndarray/ctor] are flattened starting from a specified dimension.
@@ -38,25 +38,18 @@ var flattenFrom = require( '@stdlib/ndarray/flatten-from' ); #### flattenFrom( x, dim\[, options] ) -Returns a copy of an input [ndarray][@stdlib/ndarray/ctor], where all dimensions of the input [ndarray][@stdlib/ndarray/ctor] are flattened starting from a specific dimension. +Returns a copy of an input [ndarray][@stdlib/ndarray/ctor], where all dimensions of the input [ndarray][@stdlib/ndarray/ctor] are flattened starting from a specified dimension. ```javascript var array = require( '@stdlib/ndarray/array' ); -var shape = require( '@stdlib/ndarray/shape' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); // returns -var shx = shape( x ); -// returns [ 3, 1, 2 ] - var y = flattenFrom( x, 1 ); // returns -var shy = shape( y ); -// returns [ 3, 2 ] - var arr = ndarray2array( y ); // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] ``` @@ -84,23 +77,16 @@ By default, the input [ndarray][@stdlib/ndarray/ctor] is flattened in lexicograp ```javascript var array = require( '@stdlib/ndarray/array' ); -var shape = require( '@stdlib/ndarray/shape' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); // returns -var shx = shape( x ); -// returns [ 3, 1, 2 ] - var y = flattenFrom( x, 0, { 'order': 'column-major' }); // returns -var shy = shape( y ); -// returns [ 6 ] - var arr = ndarray2array( y ); // returns [ 1.0, 3.0, 5.0, 2.0, 4.0, 6.0 ] ``` @@ -110,23 +96,16 @@ By default, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred f ```javascript var array = require( '@stdlib/ndarray/array' ); var dtype = require( '@stdlib/ndarray/dtype' ); -var shape = require( '@stdlib/ndarray/shape' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); // returns -var shx = shape( x ); -// returns [ 3, 1, 2 ] - var y = flattenFrom( x, 0, { 'dtype': 'float32' }); // returns -var shy = shape( y ); -// returns [ 6 ] - var dt = dtype( y ); // returns 'float32' diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/flatten-from/docs/repl.txt index fd55a15c6063..72be6e9cec51 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/docs/repl.txt @@ -1,7 +1,7 @@ {{alias}}( x, dim[, options] ) Returns a copy of an input ndarray, where all dimensions of the input - ndarray are flattened starting from a specific dimension. + ndarray are flattened starting from a specified dimension. The function always returns a copy of input ndarray data, even when an input ndarray already has the desired number of dimensions. diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/index.d.ts index ac7b5e99fb09..0fb2135968c4 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/index.d.ts @@ -54,7 +54,7 @@ type Options = BaseOptions & { }; /** -* Returns a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specific dimension. +* Returns a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specified dimension. * * ## Notes * @@ -91,7 +91,7 @@ type Options = BaseOptions & { declare function flattenFrom( x: T, dim: number, options?: BaseOptions ): T; /** -* Returns a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specific dimension. +* Returns a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specified dimension. * * ## Notes * diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/lib/index.js b/lib/node_modules/@stdlib/ndarray/flatten-from/lib/index.js index 674954209bbe..9c3e12ff5121 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from/lib/index.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/lib/index.js @@ -19,13 +19,12 @@ 'use strict'; /** -* Return a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specific dimension. +* Return a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specified dimension. * * @module @stdlib/ndarray/flatten-from * * @example * var array = require( '@stdlib/ndarray/array' ); -* var shape = require( '@stdlib/ndarray/shape' ); * var ndarray2array = require( '@stdlib/ndarray/to-array' ); * var flattenFrom = require( '@stdlib/ndarray/flatten-from' ); * @@ -33,16 +32,10 @@ * var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); * // returns * -* var shx = shape( x ); -* // returns [ 3, 1, 2 ] -* * // Flatten the input ndarray: * var y = flattenFrom( x, 1 ); * // returns * -* var shy = shape( y ); -* // returns [ 3, 2 ] -* * var arr = ndarray2array( y ); * // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] */ diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/lib/main.js b/lib/node_modules/@stdlib/ndarray/flatten-from/lib/main.js index 19415b3b4fff..d7e55865aae8 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/lib/main.js @@ -48,7 +48,7 @@ var COL_MAJOR = 'column-major'; // MAIN // /** -* Returns a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specific dimension. +* Returns a copy of an input ndarray, where all dimensions of the input ndarray are flattened 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/package.json b/lib/node_modules/@stdlib/ndarray/flatten-from/package.json index 7a2dc47d4805..3531908dfc2a 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from/package.json +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/ndarray/flatten-from", "version": "0.0.0", - "description": "Return a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specific dimension.", + "description": "Return a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specified dimension.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/test/test.js b/lib/node_modules/@stdlib/ndarray/flatten-from/test/test.js index 2cdc03d39235..11f8379018ba 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/test/test.js @@ -248,7 +248,7 @@ tape( 'the function throws an error if provided an invalid `dtype` option', func } }); -tape( 'by default, the function flattens all dimensions of a provided input ndarray starting from a specific dimension in lexicographic order (row-major, contiguous)', function test( t ) { +tape( 'by default, the function flattens all dimensions of a provided input ndarray starting from a specified dimension in lexicographic order (row-major, contiguous)', function test( t ) { var expected; var xbuf; var ord; @@ -310,10 +310,20 @@ tape( 'by default, the function flattens all dimensions of a provided input ndar t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + y = flattenFrom( x, -1 ); + 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( getDType( y ), dt, '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 starting from a specific dimension in lexicographic order (row-major, non-contiguous)', function test( t ) { +tape( 'by default, the function flattens all dimensions of a provided input ndarray starting from a specified dimension in lexicographic order (row-major, non-contiguous)', function test( t ) { var expected; var xbuf; var ord; @@ -375,10 +385,20 @@ tape( 'by default, the function flattens all dimensions of a provided input ndar t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + y = flattenFrom( x, -1 ); + 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( getDType( y ), dt, '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 starting from a specific dimension in lexicographic order (column-major, contiguous)', function test( t ) { +tape( 'by default, the function flattens all dimensions of a provided input ndarray starting from a specified dimension in lexicographic order (column-major, contiguous)', function test( t ) { var expected; var xbuf; var ord; @@ -440,10 +460,20 @@ tape( 'by default, the function flattens all dimensions of a provided input ndar t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + y = flattenFrom( x, -1 ); + 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( getDType( y ), dt, '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 starting from a specific dimension in lexicographic order (column-major, non-contiguous)', function test( t ) { +tape( 'by default, the function flattens all dimensions of a provided input ndarray starting from a specified dimension in lexicographic order (column-major, non-contiguous)', function test( t ) { var expected; var xbuf; var ord; @@ -505,6 +535,16 @@ tape( 'by default, the function flattens all dimensions of a provided input ndar t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + y = flattenFrom( x, -1 ); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.end(); }); @@ -588,6 +628,27 @@ tape( 'the function supports flattening a provided input ndarray in lexicographi t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + y = flattenFrom( x, -1, { + 'order': 'row-major' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.end(); }); @@ -671,6 +732,27 @@ tape( 'the function supports flattening a provided input ndarray in lexicographi t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + y = flattenFrom( x, -1, { + 'order': 'row-major' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.end(); }); @@ -754,6 +836,27 @@ tape( 'the function supports flattening a provided input ndarray in colexicograp t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + y = flattenFrom( x, -1, { + 'order': 'column-major' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + t.end(); }); @@ -837,6 +940,27 @@ tape( 'the function supports flattening a provided input ndarray in colexicograp t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + y = flattenFrom( x, -1, { + 'order': 'column-major' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + t.end(); }); @@ -920,6 +1044,27 @@ tape( 'the function supports flattening a provided input ndarray in same order a t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + y = flattenFrom( x, -1, { + 'order': 'same' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.end(); }); @@ -1003,6 +1148,27 @@ tape( 'the function supports flattening a provided input ndarray in same order a t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + y = flattenFrom( x, -1, { + 'order': 'same' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + t.end(); }); @@ -1086,6 +1252,27 @@ tape( 'the function supports flattening a provided input ndarray according to th t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + y = flattenFrom( x, -1, { + 'order': 'any' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + t.end(); }); @@ -1169,6 +1356,27 @@ tape( 'the function supports flattening a provided input ndarray according to th t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + y = flattenFrom( x, -1, { + 'order': 'any' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.end(); }); @@ -1211,6 +1419,16 @@ tape( 'the function supports flattening a zero-dimensional input ndarray', funct t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + y = flattenFrom( x, -1 ); + expected = new Float64Array( [ 3.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 ), [], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.end(); }); @@ -1257,6 +1475,18 @@ tape( 'the function supports flattening a zero-dimensional input ndarray (order= t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + y = flattenFrom( x, -1, { + 'order': 'same' + }); + expected = new Float64Array( [ 3.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 ), [], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + t.end(); }); @@ -1303,6 +1533,18 @@ tape( 'the function supports flattening a zero-dimensional input ndarray (order= t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + y = flattenFrom( x, -1, { + 'order': 'any' + }); + expected = new Float64Array( [ 3.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 ), [], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + t.end(); }); @@ -1355,6 +1597,16 @@ tape( 'the function supports flattening a one-dimensional input ndarray', functi t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + y = flattenFrom( x, -1 ); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.end(); }); @@ -1411,6 +1663,18 @@ tape( 'the function supports flattening a one-dimensional input ndarray (order=s t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + y = flattenFrom( x, -1, { + 'order': 'same' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + t.end(); }); @@ -1467,6 +1731,18 @@ tape( 'the function supports flattening a one-dimensional input ndarray (order=a t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + y = flattenFrom( x, -1, { + 'order': 'any' + }); + 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( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + t.end(); }); From fd1aff4fdb04cb275a1d050f2b93adc7d9c81da0 Mon Sep 17 00:00:00 2001 From: Athan Date: Fri, 3 Oct 2025 10:39:16 +0100 Subject: [PATCH 3/4] chore: clean-up --- 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: passed - 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/README.md | 8 +- .../ndarray/flatten-from/docs/repl.txt | 6 +- .../flatten-from/docs/types/index.d.ts | 8 +- .../ndarray/flatten-from/docs/types/test.ts | 4 +- .../@stdlib/ndarray/flatten-from/lib/index.js | 4 +- .../@stdlib/ndarray/flatten-from/lib/main.js | 22 +- .../@stdlib/ndarray/flatten-from/package.json | 2 +- .../@stdlib/ndarray/flatten-from/test/test.js | 283 +++--------------- 8 files changed, 71 insertions(+), 266 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/README.md b/lib/node_modules/@stdlib/ndarray/flatten-from/README.md index 7dd6a9354fc9..6e28f0881530 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from/README.md +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/README.md @@ -20,7 +20,7 @@ limitations under the License. # flattenFrom -> Return a copy of an input [ndarray][@stdlib/ndarray/ctor], where all dimensions of the input [ndarray][@stdlib/ndarray/ctor] are flattened starting from a specified dimension. +> Return a copy of an input [ndarray][@stdlib/ndarray/ctor] where all dimensions of the input [ndarray][@stdlib/ndarray/ctor] are flattened starting from a specified dimension.
@@ -38,7 +38,7 @@ var flattenFrom = require( '@stdlib/ndarray/flatten-from' ); #### flattenFrom( x, dim\[, options] ) -Returns a copy of an input [ndarray][@stdlib/ndarray/ctor], where all dimensions of the input [ndarray][@stdlib/ndarray/ctor] are flattened starting from a specified dimension. +Returns a copy of an input [ndarray][@stdlib/ndarray/ctor] where all dimensions of the input [ndarray][@stdlib/ndarray/ctor] are flattened starting from a specified dimension. ```javascript var array = require( '@stdlib/ndarray/array' ); @@ -51,12 +51,12 @@ var y = flattenFrom( x, 1 ); // returns var arr = ndarray2array( y ); -// returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] ``` The function accepts the following arguments: -- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **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_). diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/flatten-from/docs/repl.txt index 72be6e9cec51..bfe2f6ea9597 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/docs/repl.txt @@ -1,7 +1,7 @@ {{alias}}( x, dim[, options] ) - Returns a copy of an input ndarray, where all dimensions of the input - ndarray are flattened starting from a specified dimension. + Returns a copy of an input ndarray where all dimensions of the input ndarray + are flattened starting from a specified dimension. The function always returns a copy of input ndarray data, even when an input ndarray already has the desired number of dimensions. @@ -9,7 +9,7 @@ Parameters ---------- x: ndarray - Input ndarray. + Input ndarray. Must have one or more dimensions. dim: integer Dimension to start flattening from. If provided an integer less than diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/index.d.ts index 0fb2135968c4..e917c415ba16 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/index.d.ts @@ -54,7 +54,7 @@ type Options = BaseOptions & { }; /** -* Returns a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specified dimension. +* Returns a copy of an input ndarray where all dimensions of the input ndarray are flattened starting from a specified dimension. * * ## Notes * @@ -86,12 +86,12 @@ type Options = BaseOptions & { * // returns [ 3, 2 ] * * var arr = ndarray2array( y ); -* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] */ declare function flattenFrom( x: T, dim: number, options?: BaseOptions ): T; /** -* Returns a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specified dimension. +* Returns a copy of an input ndarray where all dimensions of the input ndarray are flattened starting from a specified dimension. * * ## Notes * @@ -122,7 +122,7 @@ declare function flattenFrom( x: T, dim: number, options?: Ba * // returns [ 3, 2 ] * * var arr = ndarray2array( y ); -* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] */ declare function flattenFrom = 'generic'>( x: typedndarray, dim: number, options: Options ): DataTypeMap[U]; diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/test.ts index 8e95897392de..98275aba15cf 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/docs/types/test.ts @@ -130,7 +130,7 @@ import flattenFrom = require( './index' ); flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, ( x: number ): number => x ); // $ExpectError } -// The compiler throws an error if the function is provided a second argument with invalid `order` option... +// The compiler throws an error if the function is provided a second argument with an invalid `order` option... { flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': '5' } ); // $ExpectError flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': true } ); // $ExpectError @@ -154,7 +154,7 @@ import flattenFrom = require( './index' ); flattenFrom( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), 0, { 'order': ( x: number ): number => x } ); // $ExpectError } -// The compiler throws an error if the function is provided a second argument with invalid `dtype` option... +// The compiler throws an error if the function is provided a second argument with an invalid `dtype` option... { flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': '5' } ); // $ExpectError flattenFrom( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), 0, { 'dtype': true } ); // $ExpectError diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/lib/index.js b/lib/node_modules/@stdlib/ndarray/flatten-from/lib/index.js index 9c3e12ff5121..b2ad4f7665b2 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from/lib/index.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Return a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specified dimension. +* Return a copy of an input ndarray where all dimensions of the input ndarray are flattened starting from a specified dimension. * * @module @stdlib/ndarray/flatten-from * @@ -37,7 +37,7 @@ * // returns * * var arr = ndarray2array( y ); -* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] */ // MODULES // diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/lib/main.js b/lib/node_modules/@stdlib/ndarray/flatten-from/lib/main.js index d7e55865aae8..1891baa27fec 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/lib/main.js @@ -48,14 +48,14 @@ var COL_MAJOR = 'column-major'; // MAIN // /** -* Returns a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specified dimension. +* Returns a copy of an input ndarray where all dimensions of the input ndarray are flattened starting from a specified dimension. * * @param {ndarray} x - input ndarray -* @param {integer} dim - dimension to start flattening from. +* @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 -* @throws {TypeError} first argument must be an ndarray-like object +* @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} must provide valid options @@ -72,7 +72,7 @@ var COL_MAJOR = 'column-major'; * // returns * * var arr = ndarray2array( y ); -* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] * * @example * var array = require( '@stdlib/ndarray/array' ); @@ -282,7 +282,6 @@ var COL_MAJOR = 'column-major'; * // returns [ 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 ] */ function flattenFrom( x, dim, options ) { - var nargs; var view; var opts; var xsh; @@ -291,14 +290,15 @@ function flattenFrom( x, dim, options ) { var y; if ( !isndarrayLike( x ) ) { - throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', 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 ) ); } - nargs = arguments.length; 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 ) ); + } // 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) @@ -306,7 +306,7 @@ function flattenFrom( x, dim, options ) { }; // Resolve function options... - if ( nargs > 2 ) { + if ( arguments.length > 2 ) { if ( !isPlainObject( options ) ) { throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); } @@ -341,12 +341,12 @@ function flattenFrom( x, dim, options ) { } // Create an output ndarray having contiguous memory: y = emptyLike( x, { - 'shape': flattenShapeFrom( xsh, dim ), + 'shape': flattenShapeFrom( xsh, dim ), // note: delegate to `flattenShapeFrom` to handle `dim` normalization 'order': opts.order, 'dtype': opts.dtype }); - // Create a view on top of output ndarray having the same shape as the input ndarray: + // 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 = ndarray( opts.dtype, getData( y ), xsh, st, 0, opts.order ); diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/package.json b/lib/node_modules/@stdlib/ndarray/flatten-from/package.json index 3531908dfc2a..30a33c2a05b3 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from/package.json +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/ndarray/flatten-from", "version": "0.0.0", - "description": "Return a copy of an input ndarray, where all dimensions of the input ndarray are flattened starting from a specified dimension.", + "description": "Return a copy of an input ndarray where all dimensions of the input ndarray are flattened starting from a specified dimension.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/test/test.js b/lib/node_modules/@stdlib/ndarray/flatten-from/test/test.js index 11f8379018ba..835601dee284 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/test/test.js @@ -76,6 +76,26 @@ tape( 'the function throws an error if provided a first argument which is not an } }); +tape( 'the function throws an error if provided a first argument which is not an ndarray having one or more dimensions', function test( t ) { + var values; + var i; + + values = [ + scalar2ndarray( 3.0 ), + scalar2ndarray( true ) + ]; + 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() { + flattenFrom( value, 0 ); + }; + } +}); + tape( 'the function throws an error if provided a first argument which is not an ndarray (options)', function test( t ) { var values; var i; @@ -170,6 +190,7 @@ tape( 'the function throws an error if provided an options argument which is not false, null, void 0, + [], function noop() {} ]; for ( i = 0; i < values.length; i++ ) { @@ -628,24 +649,18 @@ tape( 'the function supports flattening a provided input ndarray in lexicographi t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - y = flattenFrom( x, -1, { + y = flattenFrom( x, -2, { 'order': 'row-major' }); expected = [ - [ - [ 1.0, 2.0 ], - [ 3.0, 4.0 ] - ], - [ - [ 5.0, 6.0 ], - [ 7.0, 8.0 ] - ] + [ 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.deepEqual( getShape( y ), [ 2, 4 ], 'returns expected value' ); t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); @@ -732,24 +747,18 @@ tape( 'the function supports flattening a provided input ndarray in lexicographi t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - y = flattenFrom( x, -1, { + y = flattenFrom( x, -2, { 'order': 'row-major' }); expected = [ - [ - [ 1.0, 2.0 ], - [ 3.0, 4.0 ] - ], - [ - [ 5.0, 6.0 ], - [ 7.0, 8.0 ] - ] + [ 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.deepEqual( getShape( y ), [ 2, 4 ], 'returns expected value' ); t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); @@ -836,24 +845,15 @@ tape( 'the function supports flattening a provided input ndarray in colexicograp t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); - y = flattenFrom( x, -1, { + y = flattenFrom( x, -3, { 'order': 'column-major' }); - expected = [ - [ - [ 1.0, 2.0 ], - [ 3.0, 4.0 ] - ], - [ - [ 5.0, 6.0 ], - [ 7.0, 8.0 ] - ] - ]; + 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.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); @@ -940,24 +940,15 @@ tape( 'the function supports flattening a provided input ndarray in colexicograp t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); - y = flattenFrom( x, -1, { + y = flattenFrom( x, -3, { 'order': 'column-major' }); - expected = [ - [ - [ 1.0, 2.0 ], - [ 3.0, 4.0 ] - ], - [ - [ 5.0, 6.0 ], - [ 7.0, 8.0 ] - ] - ]; + 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.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); @@ -1252,24 +1243,15 @@ tape( 'the function supports flattening a provided input ndarray according to th t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); - y = flattenFrom( x, -1, { + y = flattenFrom( x, -3, { 'order': 'any' }); - expected = [ - [ - [ 8.0, 4.0 ], - [ 6.0, 2.0 ] - ], - [ - [ 7.0, 3.0 ], - [ 5.0, 1.0 ] - ] - ]; + 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.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); @@ -1356,195 +1338,18 @@ tape( 'the function supports flattening a provided input ndarray according to th t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - y = flattenFrom( x, -1, { + y = flattenFrom( x, -3, { 'order': 'any' }); - 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( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports flattening a zero-dimensional input ndarray', function test( t ) { - var expected; - var xbuf; - var dt; - var x; - var y; - - dt = 'float64'; - x = scalar2ndarray( 3.0, { - 'dtype': dt, - 'order': 'row-major' - }); - - y = flattenFrom( x, 0 ); - expected = new Float64Array( [ 3.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 ), [], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - dt = 'float64'; - x = scalar2ndarray( 3.0, { - 'dtype': dt, - 'order': 'column-major' - }); - - y = flattenFrom( x, 1 ); - expected = new Float64Array( [ 3.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 ), [], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - y = flattenFrom( x, -1 ); - expected = new Float64Array( [ 3.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 ), [], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports flattening a zero-dimensional input ndarray (order=same)', function test( t ) { - var expected; - var xbuf; - var dt; - var x; - var y; - - dt = 'float64'; - x = scalar2ndarray( 3.0, { - 'dtype': dt, - 'order': 'row-major' - }); - - y = flattenFrom( x, 0, { - 'order': 'same' - }); - expected = new Float64Array( [ 3.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 ), [], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - dt = 'float64'; - x = scalar2ndarray( 3.0, { - 'dtype': dt, - 'order': 'column-major' - }); - - y = flattenFrom( x, 1, { - 'order': 'same' - }); - expected = new Float64Array( [ 3.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 ), [], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); - - y = flattenFrom( x, -1, { - 'order': 'same' - }); - expected = new Float64Array( [ 3.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 ), [], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports flattening a zero-dimensional input ndarray (order=any)', function test( t ) { - var expected; - var xbuf; - var dt; - var x; - var y; - - dt = 'float64'; - x = scalar2ndarray( 3.0, { - 'dtype': dt, - 'order': 'row-major' - }); - - y = flattenFrom( x, 0, { - 'order': 'any' - }); - expected = new Float64Array( [ 3.0 ] ); + 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 ), [], 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - dt = 'float64'; - x = scalar2ndarray( 3.0, { - 'dtype': dt, - 'order': 'column-major' - }); - - y = flattenFrom( x, 1, { - 'order': 'any' - }); - expected = new Float64Array( [ 3.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 ), [], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); - - y = flattenFrom( x, -1, { - 'order': 'any' - }); - expected = new Float64Array( [ 3.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 ), [], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); - t.end(); }); From a0e71b2a88d7bdc5250c8f86223873b0862b9935 Mon Sep 17 00:00:00 2001 From: Athan Date: Fri, 3 Oct 2025 11:12:38 +0100 Subject: [PATCH 4/4] refactor: use base constructor and remove unreachable branch --- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - 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: na - task: lint_license_headers status: passed --- --- lib/node_modules/@stdlib/ndarray/flatten-from/lib/main.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-from/lib/main.js b/lib/node_modules/@stdlib/ndarray/flatten-from/lib/main.js index 1891baa27fec..dc47ec8736e0 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-from/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-from/lib/main.js @@ -34,8 +34,8 @@ var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); var strides2order = require( '@stdlib/ndarray/base/strides2order' ); var flattenShapeFrom = require( '@stdlib/ndarray/base/flatten-shape-from' ); var assign = require( '@stdlib/ndarray/base/assign' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); var emptyLike = require( '@stdlib/ndarray/empty-like' ); -var ndarray = require( '@stdlib/ndarray/ctor' ); var format = require( '@stdlib/string/format' ); @@ -285,7 +285,6 @@ function flattenFrom( x, dim, options ) { var view; var opts; var xsh; - var st; var o; var y; @@ -347,8 +346,7 @@ function flattenFrom( x, dim, options ) { }); // 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 = ndarray( opts.dtype, getData( y ), xsh, st, 0, opts.order ); + view = new ndarray( opts.dtype, getData( y ), xsh, shape2strides( xsh, opts.order ), 0, opts.order ); // eslint-disable-line max-len // Copy elements to the output ndarray: assign( [ x, view ] );