diff --git a/lib/node_modules/@stdlib/ndarray/flatten/README.md b/lib/node_modules/@stdlib/ndarray/flatten/README.md new file mode 100644 index 000000000000..156801cd8799 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten/README.md @@ -0,0 +1,175 @@ + + +# flatten + +> Return a flattened copy of an input [ndarray][@stdlib/ndarray/ctor]. + +
+ +
+ + + +
+ +## Usage + +```javascript +var flatten = require( '@stdlib/ndarray/flatten' ); +``` + +#### flatten( x\[, options] ) + +Returns a flattened copy of an input [ndarray][@stdlib/ndarray/ctor]. + +```javascript +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 = flatten( x ); +// returns + +var arr = ndarray2array( y ); +// 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]. +- **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'`. + +- **depth**: maximum number of input [ndarray][@stdlib/ndarray/ctor] dimensions to flatten. + +By default, the function flattens all dimensions of the input [ndarray][@stdlib/ndarray/ctor]. To flatten to a desired depth, specify the `depth` option. + +```javascript +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 = flatten( x, { + 'depth': 1 +}); +// returns + +var arr = ndarray2array( y ); +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +``` + +By default, the input [ndarray][@stdlib/ndarray/ctor] is flattened in lexicographic order. To flatten elements in a different order, specify the `order` option. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +// returns + +var y = flatten( x, { + 'order': 'column-major' +}); +// returns + +var arr = ndarray2array( y ); +// returns [ 1.0, 3.0, 5.0, 2.0, 4.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 flatten = require( '@stdlib/ndarray/flatten' ); + +var xbuf = discreteUniform( 12, -100, 100, { + 'dtype': 'generic' +}); + +var x = array( xbuf, { + 'shape': [ 2, 2, 3 ], + 'dtype': 'generic' +}); +console.log( ndarray2array( x ) ); + +var y = flatten( x ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/flatten/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/flatten/benchmark/benchmark.js new file mode 100644 index 000000000000..6c9f713addc7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten/benchmark/benchmark.js @@ -0,0 +1,310 @@ +/** +* @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 flatten = 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 = { + 'depth': 1, + 'order': 'row-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flatten( values[ j ], 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 = { + 'depth': 1, + 'order': 'column-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flatten( values[ j ], 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 = { + 'depth': 2, + 'order': 'row-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flatten( values[ j ], 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 = { + 'depth': 2, + 'order': 'column-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flatten( values[ j ], 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 = { + 'depth': 3, + 'order': 'row-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flatten( values[ j ], 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 = { + 'depth': 3, + 'order': 'column-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flatten( values[ j ], 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 = { + 'depth': 4, + 'order': 'row-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flatten( values[ j ], 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 = { + 'depth': 4, + 'order': 'column-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flatten( values[ j ], 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/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/flatten/docs/repl.txt new file mode 100644 index 000000000000..4e2e4208667e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten/docs/repl.txt @@ -0,0 +1,45 @@ + +{{alias}}( x[, options] ) + Returns a flattened copy of an input ndarray. + + 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. + + options: Object (optional) + Function options. + + options.depth: integer (optional) + Maximum number of dimensions to flatten. By default, the function + flattens all input ndarray dimensions. + + 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'. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); + > var y = {{alias}}( x ); + > 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/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/flatten/docs/types/index.d.ts new file mode 100644 index 000000000000..c7d0484a7b1d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten/docs/types/index.d.ts @@ -0,0 +1,86 @@ +/* +* @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, Order } from '@stdlib/types/ndarray'; + +/** +* Interface defining function options. +*/ +interface Options { + /** + * Maximum number of dimensions to flatten. + * + * ## Notes + * + * - By default, the function flattens all input ndarray dimensions. + */ + depth?: number; + + /** + * 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'; +} + +/** +* Returns a flattened copy of an input ndarray. +* +* ## 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 options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened +* @returns 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 ] ] ] );; +* // return +* +* var y = flatten( x ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] +*/ +declare function flatten( x: T, options?: Options ): T; + + +// EXPORTS // + +export = flatten; diff --git a/lib/node_modules/@stdlib/ndarray/flatten/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/flatten/docs/types/test.ts new file mode 100644 index 000000000000..bc9d1cf920bc --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten/docs/types/test.ts @@ -0,0 +1,137 @@ +/* +* @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 flatten = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ) ); // $ExpectType float64ndarray + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ) ); // $ExpectType complex128ndarray + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ) ); // $ExpectType genericndarray + + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), {} ); // $ExpectType float64ndarray + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), {} ); // $ExpectType complex128ndarray + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), {} ); // $ExpectType genericndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray-like object... +{ + flatten( '5' ); // $ExpectError + flatten( 5 ); // $ExpectError + flatten( true ); // $ExpectError + flatten( false ); // $ExpectError + flatten( null ); // $ExpectError + flatten( undefined ); // $ExpectError + flatten( {} ); // $ExpectError + flatten( [ 1 ] ); // $ExpectError + flatten( ( x: number ): number => x ); // $ExpectError + + flatten( '5', {} ); // $ExpectError + flatten( 5, {} ); // $ExpectError + flatten( true, {} ); // $ExpectError + flatten( false, {} ); // $ExpectError + flatten( null, {} ); // $ExpectError + flatten( undefined, {} ); // $ExpectError + flatten( {}, {} ); // $ExpectError + flatten( [ 1 ], {} ); // $ExpectError + flatten( ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an object... +{ + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), '5' ); // $ExpectError + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), true ); // $ExpectError + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), false ); // $ExpectError + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), null ); // $ExpectError + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), [ 1 ] ); // $ExpectError + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), ( x: number ): number => x ); // $ExpectError + + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), '5' ); // $ExpectError + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), true ); // $ExpectError + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), false ); // $ExpectError + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), null ); // $ExpectError + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), [ 1 ] ); // $ExpectError + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), ( x: number ): number => x ); // $ExpectError + + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), '5' ); // $ExpectError + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), true ); // $ExpectError + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), false ); // $ExpectError + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), null ); // $ExpectError + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), [ 1 ] ); // $ExpectError + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument with invalid `depth` option... +{ + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'depth': '5' } ); // $ExpectError + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'depth': true } ); // $ExpectError + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'depth': false } ); // $ExpectError + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'depth': null } ); // $ExpectError + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'depth': [ 1 ] } ); // $ExpectError + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'depth': ( x: number ): number => x } ); // $ExpectError + + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'depth': '5' } ); // $ExpectError + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'depth': true } ); // $ExpectError + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'depth': false } ); // $ExpectError + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'depth': null } ); // $ExpectError + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'depth': [ 1 ] } ); // $ExpectError + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'depth': ( x: number ): number => x } ); // $ExpectError + + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'depth': '5' } ); // $ExpectError + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'depth': true } ); // $ExpectError + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'depth': false } ); // $ExpectError + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'depth': null } ); // $ExpectError + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'depth': [ 1 ] } ); // $ExpectError + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'depth': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument with invalid `order` option... +{ + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'order': '5' } ); // $ExpectError + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'order': true } ); // $ExpectError + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'order': false } ); // $ExpectError + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'order': null } ); // $ExpectError + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'order': [ 1 ] } ); // $ExpectError + flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'order': ( x: number ): number => x } ); // $ExpectError + + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'order': '5' } ); // $ExpectError + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'order': true } ); // $ExpectError + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'order': false } ); // $ExpectError + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'order': null } ); // $ExpectError + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'order': [ 1 ] } ); // $ExpectError + flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'order': ( x: number ): number => x } ); // $ExpectError + + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'order': '5' } ); // $ExpectError + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'order': true } ); // $ExpectError + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'order': false } ); // $ExpectError + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'order': null } ); // $ExpectError + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'order': [ 1 ] } ); // $ExpectError + flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'order': ( 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' ); + + flatten(); // $ExpectError + flatten( x, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/flatten/examples/index.js b/lib/node_modules/@stdlib/ndarray/flatten/examples/index.js new file mode 100644 index 000000000000..f3c5b9214327 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten/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 flatten = 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 = flatten( x ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/flatten/lib/index.js b/lib/node_modules/@stdlib/ndarray/flatten/lib/index.js new file mode 100644 index 000000000000..85584b794fd1 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten/lib/index.js @@ -0,0 +1,50 @@ +/** +* @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 flattened copy of an input ndarray. +* +* @module @stdlib/ndarray/flatten +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var flatten = require( '@stdlib/ndarray/flatten' ); +* +* // Create an input ndarray: +* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +* // return +* +* // Flatten the input ndarray: +* var y = flatten( x ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/flatten/lib/main.js b/lib/node_modules/@stdlib/ndarray/flatten/lib/main.js new file mode 100644 index 000000000000..22ecfba4112d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten/lib/main.js @@ -0,0 +1,358 @@ +/** +* @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 isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-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 flattenShape = require( '@stdlib/ndarray/base/flatten-shape' ); +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 flattened copy of an input ndarray. +* +* @param {ndarray} x - input ndarray +* @param {Options} [options] - function options +* @param {NonNegativeInteger} [options.depth] - maximum number of dimensions to flatten +* @param {string} [options.order='row-major'] - order in which input ndarray elements should be flattened +* @throws {TypeError} first argument must be an ndarray-like object +* @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 ] ] ] ); +* // return +* +* var y = flatten( x ); +* // 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' +* }); +* // return +* +* var y = flatten( x ); +* // 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' +* }); +* // return +* +* var y = flatten( x, { +* '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' +* }); +* // return +* +* var y = flatten( x, { +* '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' +* }); +* // return +* +* var y = flatten( x, { +* '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' +* }); +* // return +* +* var y = flatten( x, { +* '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' ); +* // return +* +* var y = flatten( x ); +* // 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' ); +* // return +* +* var y = flatten( x ); +* // 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' ); +* // return +* +* var y = flatten( x, { +* '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' ); +* // return +* +* var y = flatten( x, { +* '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' ); +* // return +* +* var y = flatten( x ); +* // 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' ); +* // return +* +* var y = flatten( x, { +* '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' ); +* // return +* +* var y = flatten( x, { +* 'order': 'any' +* }); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 ] +*/ +function flatten( x, 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 ) ); + } + nargs = arguments.length; + xsh = getShape( x ); + + // Define default options: + opts = { + 'depth': xsh.length, // by default, flatten to a one-dimensional ndarray + '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) + }; + + // 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, 'depth' ) ) { + if ( !isNonNegativeInteger( options.depth ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', options.depth ) ); + } + opts.depth = options.depth; + } + 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 ) ); + } + } + } + // Create an output ndarray having contiguous memory: + y = emptyLike( x, { + 'shape': flattenShape( xsh, opts.depth ), + 'order': opts.order + }); + + // 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( getDType( y ), getData( y ), xsh, st, 0, opts.order ); + + // Copy elements to the output ndarray: + assign( [ x, view ] ); + + return y; +} + + +// EXPORTS // + +module.exports = flatten; diff --git a/lib/node_modules/@stdlib/ndarray/flatten/package.json b/lib/node_modules/@stdlib/ndarray/flatten/package.json new file mode 100644 index 000000000000..317642b2cdde --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/ndarray/flatten", + "version": "0.0.0", + "description": "Return a flattened copy of an input ndarray.", + "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", + "reshape", + "copy", + "transform" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/flatten/test/test.js b/lib/node_modules/@stdlib/ndarray/flatten/test/test.js new file mode 100644 index 000000000000..02a079145248 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten/test/test.js @@ -0,0 +1,1360 @@ +/** +* @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 zeros = require( '@stdlib/ndarray/zeros' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +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 flatten = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof flatten, '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() { + flatten( value ); + }; + } +}); + +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() { + flatten( 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() { + flatten( zeros( [ 2, 2, 2 ] ), value ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid `depth` option', function test( t ) { + var values; + var opts; + 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() { + opts = { + 'depth': value + }; + flatten( zeros( [ 2 ] ), opts ); + }; + } +}); + +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 + }; + flatten( zeros( [ 2 ] ), opts ); + }; + } +}); + +tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, contiguous)', function test( t ) { + var expected; + var xbuf; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flatten( x ); + 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( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, non-contiguous)', function test( t ) { + var expected; + var xbuf; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = [ 8, 4, 2 ]; + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, NaN, 2.0, NaN, 3.0, NaN, 4.0, NaN, 5.0, NaN, 6.0, NaN, 7.0, NaN, 8.0, NaN ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flatten( x ); + 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( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (column-major, contiguous)', function test( t ) { + var expected; + var xbuf; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flatten( x ); + 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( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (column-major, non-contiguous)', function test( t ) { + var expected; + var xbuf; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = [ 2, 4, 8 ]; + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, NaN, 5.0, NaN, 3.0, NaN, 7.0, NaN, 2.0, NaN, 6.0, NaN, 4.0, NaN, 8.0, NaN ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flatten( x ); + 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 specifying the maximum number of dimensions to flatten (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 = flatten( x, { + 'depth': 0 + }); + 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' ); + + y = flatten( x, { + 'depth': 1 + }); + 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 ), [ 4, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flatten( x, { + 'depth': 2 + }); + 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 ), [ 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 specifying the maximum number of dimensions to flatten (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 = flatten( x, { + 'depth': 0 + }); + 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' ); + + y = flatten( x, { + 'depth': 1 + }); + 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 ), [ 4, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + y = flatten( x, { + 'depth': 2 + }); + 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 ), [ 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 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 = flatten( x, { + '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 = flatten( x, { + 'depth': 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 ), [ 4, 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 = flatten( x, { + '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 = flatten( x, { + 'depth': 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 ), [ 4, 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 = flatten( x, { + '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 = flatten( x, { + 'depth': 1, + 'order': 'column-major' + }); + expected = [ + [ 1.0, 2.0 ], + [ 5.0, 6.0 ], + [ 3.0, 4.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 ), [ 4, 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 = flatten( x, { + '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 = flatten( x, { + 'depth': 1, + 'order': 'column-major' + }); + expected = [ + [ 1.0, 2.0 ], + [ 5.0, 6.0 ], + [ 3.0, 4.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 ), [ 4, 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 = flatten( x, { + '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 = flatten( x, { + 'depth': 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 ), [ 4, 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 = flatten( x, { + '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 = flatten( x, { + 'depth': 1, + 'order': 'same' + }); + expected = [ + [ 1.0, 2.0 ], + [ 5.0, 6.0 ], + [ 3.0, 4.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 ), [ 4, 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 = flatten( x, { + '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 = flatten( x, { + 'depth': 1, + 'order': 'any' + }); + expected = [ + [ 8.0, 4.0 ], + [ 7.0, 3.0 ], + [ 6.0, 2.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 ), [ 4, 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 = flatten( x, { + '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 = flatten( x, { + 'depth': 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 ), [ 4, 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 = flatten( x ); + 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 = flatten( x ); + 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 = flatten( x, { + '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 = flatten( x, { + '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 = flatten( x, { + '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 = flatten( x, { + '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 = flatten( x ); + 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 = flatten( x ); + 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 = flatten( x, { + '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 = flatten( x, { + '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 = flatten( x, { + '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 = flatten( x, { + '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(); +});