From d452d68178e7d36277eb4fe7d6d0f09af749c616 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Thu, 5 Mar 2026 01:29:06 +0500 Subject: [PATCH] feat: add ndarray/base/unflatten --- 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/base/unflatten/README.md | 126 ++ .../base/unflatten/benchmark/benchmark.js | 205 ++++ .../ndarray/base/unflatten/docs/repl.txt | 35 + .../base/unflatten/docs/types/index.d.ts | 48 + .../ndarray/base/unflatten/docs/types/test.ts | 99 ++ .../ndarray/base/unflatten/examples/index.js | 29 + .../ndarray/base/unflatten/lib/index.js | 44 + .../ndarray/base/unflatten/lib/main.js | 110 ++ .../ndarray/base/unflatten/package.json | 67 ++ .../ndarray/base/unflatten/test/test.js | 1025 +++++++++++++++++ 10 files changed, 1788 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/base/unflatten/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/base/unflatten/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/unflatten/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/base/unflatten/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/unflatten/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/unflatten/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/unflatten/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/unflatten/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/unflatten/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/unflatten/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten/README.md b/lib/node_modules/@stdlib/ndarray/base/unflatten/README.md new file mode 100644 index 000000000000..f4704605d411 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten/README.md @@ -0,0 +1,126 @@ + + +# unflatten + +> Return a view of an input ndarray in which a specified dimension is expanded over multiple dimensions. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var unflatten = require( '@stdlib/ndarray/base/unflatten' ); +``` + +#### unflatten( x, dim, sizes, writable ) + +Returns a view of an input ndarray in which a specified dimension is expanded over multiple dimensions. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +// returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] + +var out = unflatten( x, 0, [ 2, 3 ], false ); +// returns [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] +``` + +The function accepts the following arguments: + +- **x**: input ndarray. +- **dim**: dimension to be unflattened. 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`. +- **sizes**: new shape of the unflattened dimension. +- **writable**: boolean indicating whether a returned ndarray should be writable. + +
+ + + + + +
+ +## Notes + +- The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances. + +
+ + + + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var unflatten = require( '@stdlib/ndarray/base/unflatten' ); + +var x = uniform( [ 12 ], -100, 100 ); +console.log( ndarray2array( x ) ); + +var out = unflatten( x, 0, [ 3, 4 ], false ); +console.log( ndarray2array( out ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/unflatten/benchmark/benchmark.js new file mode 100644 index 000000000000..2d838082bbec --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten/benchmark/benchmark.js @@ -0,0 +1,205 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 Float64Array = require( '@stdlib/array/float64' ); +var ndarrayBase = require( '@stdlib/ndarray/base/ctor' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var unflatten = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::base_ndarray, 2d, row-major', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var sizes; + var out; + var i; + + dtype = 'float64'; + buffer = new Float64Array( 24 ); + shape = [ 2, 12 ]; + strides = [ 12, 1 ]; + offset = 0; + order = 'row-major'; + sizes = [ 3, 4 ]; + + values = [ + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = unflatten( values[ i%values.length ], 1, sizes, false ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::ndarray, 2d, row-major', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var sizes; + var out; + var i; + + dtype = 'float64'; + buffer = new Float64Array( 24 ); + shape = [ 2, 12 ]; + strides = [ 12, 1 ]; + offset = 0; + order = 'row-major'; + sizes = [ 3, 4 ]; + + values = [ + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = unflatten( values[ i%values.length ], 1, sizes, false ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::base_ndarray, 2d, column-major', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var sizes; + var out; + var i; + + dtype = 'float64'; + buffer = new Float64Array( 24 ); + shape = [ 2, 12 ]; + strides = [ 1, 2 ]; + offset = 0; + order = 'column-major'; + sizes = [ 3, 4 ]; + + values = [ + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = unflatten( values[ i%values.length ], 1, sizes, false ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::ndarray, 2d, column-major', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var sizes; + var out; + var i; + + dtype = 'float64'; + buffer = new Float64Array( 24 ); + shape = [ 2, 12 ]; + strides = [ 1, 2 ]; + offset = 0; + order = 'column-major'; + sizes = [ 3, 4 ]; + + values = [ + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = unflatten( values[ i%values.length ], 1, sizes, false ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/unflatten/docs/repl.txt new file mode 100644 index 000000000000..b49c197f90b7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten/docs/repl.txt @@ -0,0 +1,35 @@ + +{{alias}}( x, dim, sizes, writable ) + Returns a view of an input ndarray in which a specified dimension is + expanded over multiple dimensions. + + Parameters + ---------- + x: ndarray + Input array. + + dim: integer + Dimension to be unflattened. 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`. + + sizes: ArrayLikeObject + New shape of the unflattened dimension. + + writable: boolean + Boolean indicating whether the returned ndarray should be writable. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ 1, 2, 3, 4, 5, 6 ] ) + [ 1, 2, 3, 4, 5, 6 ] + > var out = {{alias}}( x, 0, [ 2, 3 ], false ) + [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/unflatten/docs/types/index.d.ts new file mode 100644 index 000000000000..a07e6b9a8026 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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 } from '@stdlib/types/ndarray'; + +/** +* Returns a view of an input ndarray in which a specified dimension is expanded over multiple dimensions. +* +* @param x - input array +* @param dim - dimension to be unflattened +* @param sizes - new shape of the unflattened dimension +* @param writable - boolean indicating whether the returned ndarray should be writable +* @returns output array +* +* @example +* var array = require( `@stdlib/ndarray/array` ); +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] +* +* var out = unflatten( x, 0, [ 2, 3 ], false ); +* // returns [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] +*/ +declare function unflatten( x: U, dim: number, sizes: ArrayLike, writable: boolean ): U; + + +// EXPORTS // + +export = unflatten; diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/unflatten/docs/types/test.ts new file mode 100644 index 000000000000..fada438bd7fa --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten/docs/types/test.ts @@ -0,0 +1,99 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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 space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import unflatten = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 6 ], { + 'dtype': 'float64' + }); + + unflatten( x, 1, [ 2, 3 ], false ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is not provided a first argument which is an ndarray... +{ + unflatten( '5', 1, [ 2, 3 ], false ); // $ExpectError + unflatten( 5, 1, [ 2, 3 ], false ); // $ExpectError + unflatten( true, 1, [ 2, 3 ], false ); // $ExpectError + unflatten( false, 1, [ 2, 3 ], false ); // $ExpectError + unflatten( void 0, 1, [ 2, 3 ], false ); // $ExpectError + unflatten( null, 1, [ 2, 3 ], false ); // $ExpectError + unflatten( {}, 1, [ 2, 3 ], false ); // $ExpectError + unflatten( [ '5' ], 1, [ 2, 3 ], false ); // $ExpectError + unflatten( ( x: number ): number => x, 1, [ 2, 3 ], false ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a second argument which is a number... +{ + const x = zeros( [ 2, 6 ] ); + + unflatten( x, '5', [ 2, 3 ], false ); // $ExpectError + unflatten( x, true, [ 2, 3 ], false ); // $ExpectError + unflatten( x, false, [ 2, 3 ], false ); // $ExpectError + unflatten( x, void 0, [ 2, 3 ], false ); // $ExpectError + unflatten( x, null, [ 2, 3 ], false ); // $ExpectError + unflatten( x, {}, [ 2, 3 ], false ); // $ExpectError + unflatten( x, [ '5' ], [ 2, 3 ], false ); // $ExpectError + unflatten( x, ( x: number ): number => x, [ 2, 3 ], false ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a third argument which is an array-like object of numbers... +{ + const x = zeros( [ 2, 6 ] ); + + unflatten( x, 1, '5', false ); // $ExpectError + unflatten( x, 1, 5, false ); // $ExpectError + unflatten( x, 1, true, false ); // $ExpectError + unflatten( x, 1, false, false ); // $ExpectError + unflatten( x, 1, void 0, false ); // $ExpectError + unflatten( x, 1, null, false ); // $ExpectError + unflatten( x, 1, {}, false ); // $ExpectError + unflatten( x, 1, ( x: number ): number => x, false ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a fourth argument which is a boolean... +{ + const x = zeros( [ 2, 6 ] ); + + unflatten( x, 1, [ 2, 3 ], '5' ); // $ExpectError + unflatten( x, 1, [ 2, 3 ], 5 ); // $ExpectError + unflatten( x, 1, [ 2, 3 ], void 0 ); // $ExpectError + unflatten( x, 1, [ 2, 3 ], null ); // $ExpectError + unflatten( x, 1, [ 2, 3 ], {} ); // $ExpectError + unflatten( x, 1, [ 2, 3 ], [ '5' ] ); // $ExpectError + unflatten( x, 1, [ 2, 3 ], ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 6 ] ); + + unflatten(); // $ExpectError + unflatten( x ); // $ExpectError + unflatten( x, 1 ); // $ExpectError + unflatten( x, 1, [ 2, 3 ] ); // $ExpectError + unflatten( x, 1, [ 2, 3 ], false, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/unflatten/examples/index.js new file mode 100644 index 000000000000..3fdab1380a34 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten/examples/index.js @@ -0,0 +1,29 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 uniform = require( '@stdlib/random/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var unflatten = require( './../lib' ); + +var x = uniform( [ 12 ], -100, 100 ); +console.log( ndarray2array( x ) ); + +var out = unflatten( x, 0, [ 3, 4 ], false ); +console.log( ndarray2array( out ) ); diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/unflatten/lib/index.js new file mode 100644 index 000000000000..329dde65a460 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten/lib/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 view of an input ndarray in which a specified dimension is expanded over multiple dimensions. +* +* @module @stdlib/ndarray/base/unflatten +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var unflatten = require( '@stdlib/ndarray/base/unflatten' ); +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] +* +* var out = unflatten( x, 0, [ 2, 3 ], false ); +* // 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/base/unflatten/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/unflatten/lib/main.js new file mode 100644 index 000000000000..2f045a76966b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten/lib/main.js @@ -0,0 +1,110 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major-string' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var getDType = require( '@stdlib/ndarray/base/dtype' ); +var getShape = require( '@stdlib/ndarray/base/shape' ); +var getStrides = require( '@stdlib/ndarray/base/strides' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getOrder = require( '@stdlib/ndarray/base/order' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var unflattenShape = require( '@stdlib/ndarray/base/unflatten-shape' ); + + +// MAIN // + +/** +* Returns a view of an input ndarray in which a specified dimension is expanded over multiple dimensions. +* +* @param {ndarray} x - input array +* @param {integer} dim - dimension index +* @param {NonNegativeIntegerArray} sizes - new shape of the unflattened dimension +* @param {boolean} writable - boolean indicating whether the returned ndarray should be writable +* @throws {RangeError} must provide a valid dimension index +* @throws {RangeError} product of the sizes must be equal to the size of the dimension to be unflattened +* @returns {ndarray} output array +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] +* +* var out = unflatten( x, 0, [ 2, 3 ], false ); +* // returns [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] +*/ +function unflatten( x, dim, sizes, writable ) { + var strides; + var shape; + var isrm; + var ord; + var sh; + var st; + var S2; + var d; + var i; + + sh = getShape( x, false ); + st = getStrides( x, false ); + ord = getOrder( x ); + isrm = isRowMajor( ord ); + + // Normalize the dimension to be unflattened: + d = normalizeIndex( dim, sh.length - 1 ); + + // Compute the output shape: + shape = unflattenShape( sh, d, sizes ); + + // Resolve the output strides: + strides = []; + S2 = sizes.length; + for ( i = 0; i < d; i++ ) { + strides.push( st[ i ] ); + } + for ( i = 0; i < S2; i++ ) { + strides.push( 0 ); + } + for ( i = d + 1; i < sh.length; i++ ) { + strides.push( st[ i ] ); + } + // Compute strides for the unflattened dimensions... + if ( isrm ) { + strides[ d + S2 - 1 ] = st[ d ]; + for ( i = S2 - 2; i >= 0; i-- ) { + strides[ d + i ] = strides[ d + i + 1 ] * sizes[ i + 1 ]; + } + } else { + strides[ d ] = st[ d ]; + for ( i = 1; i < S2; i++ ) { + strides[ d + i ] = strides[ d + i - 1 ] * sizes[ i - 1 ]; + } + } + return new x.constructor( getDType( x ), getData( x ), shape, strides, getOffset( x ), ord, { // eslint-disable-line max-len + 'readonly': !writable + }); +} + + +// EXPORTS // + +module.exports = unflatten; diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten/package.json b/lib/node_modules/@stdlib/ndarray/base/unflatten/package.json new file mode 100644 index 000000000000..e0cd229dce96 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/ndarray/base/unflatten", + "version": "0.0.0", + "description": "Return a view of an input ndarray in which a specified dimension is expanded over multiple dimensions.", + "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", + "stdtypes", + "types", + "base", + "ndarray", + "unflatten", + "reshape", + "multidimensional", + "array", + "utilities", + "utility", + "utils", + "util" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten/test/test.js b/lib/node_modules/@stdlib/ndarray/base/unflatten/test/test.js new file mode 100644 index 000000000000..31641eeb1132 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten/test/test.js @@ -0,0 +1,1025 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); +var zeroTo = require( '@stdlib/array/zero-to' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getStrides = require( '@stdlib/ndarray/strides' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +var unflatten = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof unflatten, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a view of an input ndarray in which a specified dimension is expanded over multiple dimensions (base; row-major)', function test( t ) { + var expected; + var buf; + var x; + var y; + + buf = zeroTo( 64 ); + x = ndarray( 'generic', buf, [ 4, 4, 4 ], [ 16, 4, 1 ], 0, 'row-major' ); + + // First dimension: + expected = [ + [ + [ + [ 0, 1, 2, 3 ], + [ 4, 5, 6, 7 ], + [ 8, 9, 10, 11 ], + [ 12, 13, 14, 15 ] + ], + [ + [ 16, 17, 18, 19 ], + [ 20, 21, 22, 23 ], + [ 24, 25, 26, 27 ], + [ 28, 29, 30, 31 ] + ] + ], + [ + [ + [ 32, 33, 34, 35 ], + [ 36, 37, 38, 39 ], + [ 40, 41, 42, 43 ], + [ 44, 45, 46, 47 ] + ], + [ + [ 48, 49, 50, 51 ], + [ 52, 53, 54, 55 ], + [ 56, 57, 58, 59 ], + [ 60, 61, 62, 63 ] + ] + ] + ]; + + y = unflatten( x, 0, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 4, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 32, 16, 4, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, 0, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 4, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 32, 16, 4, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + // Middle dimension: + expected = [ + [ + [ + [ 0, 1, 2, 3 ], + [ 4, 5, 6, 7 ] + ], + [ + [ 8, 9, 10, 11 ], + [ 12, 13, 14, 15 ] + ] + ], + [ + [ + [ 16, 17, 18, 19 ], + [ 20, 21, 22, 23 ] + ], + [ + [ 24, 25, 26, 27 ], + [ 28, 29, 30, 31 ] + ] + ], + [ + [ + [ 32, 33, 34, 35 ], + [ 36, 37, 38, 39 ] + ], + [ + [ 40, 41, 42, 43 ], + [ 44, 45, 46, 47 ] + ] + ], + [ + [ + [ 48, 49, 50, 51 ], + [ 52, 53, 54, 55 ] + ], + [ + [ 56, 57, 58, 59 ], + [ 60, 61, 62, 63 ] + ] + ] + ]; + + y = unflatten( x, 1, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2, 2, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 16, 8, 4, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, 1, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2, 2, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 16, 8, 4, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + // Last dimension: + expected = [ + [ + [ [ 0, 1 ], [ 2, 3 ] ], + [ [ 4, 5 ], [ 6, 7 ] ], + [ [ 8, 9 ], [ 10, 11 ] ], + [ [ 12, 13 ], [ 14, 15 ] ] + ], + [ + [ [ 16, 17 ], [ 18, 19 ] ], + [ [ 20, 21 ], [ 22, 23 ] ], + [ [ 24, 25 ], [ 26, 27 ] ], + [ [ 28, 29 ], [ 30, 31 ] ] + ], + [ + [ [ 32, 33 ], [ 34, 35 ] ], + [ [ 36, 37 ], [ 38, 39 ] ], + [ [ 40, 41 ], [ 42, 43 ] ], + [ [ 44, 45 ], [ 46, 47 ] ] + ], + [ + [ [ 48, 49 ], [ 50, 51 ] ], + [ [ 52, 53 ], [ 54, 55 ] ], + [ [ 56, 57 ], [ 58, 59 ] ], + [ [ 60, 61 ], [ 62, 63 ] ] + ] + ]; + + y = unflatten( x, 2, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 4, 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 16, 4, 2, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, 2, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 4, 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 16, 4, 2, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a view of an input ndarray in which a specified dimension is expanded over multiple dimensions (base; row-major; negative dimension indices)', function test( t ) { + var expected; + var buf; + var x; + var y; + + buf = zeroTo( 64 ); + x = ndarray( 'generic', buf, [ 4, 4, 4 ], [ 16, 4, 1 ], 0, 'row-major' ); + + // First dimension: + expected = [ + [ + [ + [ 0, 1, 2, 3 ], + [ 4, 5, 6, 7 ], + [ 8, 9, 10, 11 ], + [ 12, 13, 14, 15 ] + ], + [ + [ 16, 17, 18, 19 ], + [ 20, 21, 22, 23 ], + [ 24, 25, 26, 27 ], + [ 28, 29, 30, 31 ] + ] + ], + [ + [ + [ 32, 33, 34, 35 ], + [ 36, 37, 38, 39 ], + [ 40, 41, 42, 43 ], + [ 44, 45, 46, 47 ] + ], + [ + [ 48, 49, 50, 51 ], + [ 52, 53, 54, 55 ], + [ 56, 57, 58, 59 ], + [ 60, 61, 62, 63 ] + ] + ] + ]; + + y = unflatten( x, -3, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 4, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 32, 16, 4, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, -3, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 4, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 32, 16, 4, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + // Middle dimension: + expected = [ + [ + [ + [ 0, 1, 2, 3 ], + [ 4, 5, 6, 7 ] + ], + [ + [ 8, 9, 10, 11 ], + [ 12, 13, 14, 15 ] + ] + ], + [ + [ + [ 16, 17, 18, 19 ], + [ 20, 21, 22, 23 ] + ], + [ + [ 24, 25, 26, 27 ], + [ 28, 29, 30, 31 ] + ] + ], + [ + [ + [ 32, 33, 34, 35 ], + [ 36, 37, 38, 39 ] + ], + [ + [ 40, 41, 42, 43 ], + [ 44, 45, 46, 47 ] + ] + ], + [ + [ + [ 48, 49, 50, 51 ], + [ 52, 53, 54, 55 ] + ], + [ + [ 56, 57, 58, 59 ], + [ 60, 61, 62, 63 ] + ] + ] + ]; + + y = unflatten( x, -2, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2, 2, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 16, 8, 4, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, -2, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2, 2, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 16, 8, 4, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + // Last dimension: + expected = [ + [ + [ [ 0, 1 ], [ 2, 3 ] ], + [ [ 4, 5 ], [ 6, 7 ] ], + [ [ 8, 9 ], [ 10, 11 ] ], + [ [ 12, 13 ], [ 14, 15 ] ] + ], + [ + [ [ 16, 17 ], [ 18, 19 ] ], + [ [ 20, 21 ], [ 22, 23 ] ], + [ [ 24, 25 ], [ 26, 27 ] ], + [ [ 28, 29 ], [ 30, 31 ] ] + ], + [ + [ [ 32, 33 ], [ 34, 35 ] ], + [ [ 36, 37 ], [ 38, 39 ] ], + [ [ 40, 41 ], [ 42, 43 ] ], + [ [ 44, 45 ], [ 46, 47 ] ] + ], + [ + [ [ 48, 49 ], [ 50, 51 ] ], + [ [ 52, 53 ], [ 54, 55 ] ], + [ [ 56, 57 ], [ 58, 59 ] ], + [ [ 60, 61 ], [ 62, 63 ] ] + ] + ]; + + y = unflatten( x, -1, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 4, 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 16, 4, 2, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, -1, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 4, 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 16, 4, 2, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a view of an input ndarray in which a specified dimension is expanded over multiple dimensions (base; column-major)', function test( t ) { + var expected; + var buf; + var x; + var y; + + buf = zeroTo( 64 ); + x = ndarray( 'generic', buf, [ 4, 4, 4 ], [ 1, 4, 16 ], 0, 'column-major' ); + + // First dimension: + expected = [ + [ + [ + [ 0, 16, 32, 48 ], + [ 4, 20, 36, 52 ], + [ 8, 24, 40, 56 ], + [ 12, 28, 44, 60 ] + ], + [ + [ 2, 18, 34, 50 ], + [ 6, 22, 38, 54 ], + [ 10, 26, 42, 58 ], + [ 14, 30, 46, 62 ] + ] + ], + [ + [ + [ 1, 17, 33, 49 ], + [ 5, 21, 37, 53 ], + [ 9, 25, 41, 57 ], + [ 13, 29, 45, 61 ] + ], + [ + [ 3, 19, 35, 51 ], + [ 7, 23, 39, 55 ], + [ 11, 27, 43, 59 ], + [ 15, 31, 47, 63 ] + ] + ] + ]; + + y = unflatten( x, 0, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 4, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1, 2, 4, 16 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, 0, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 4, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1, 2, 4, 16 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + // Middle dimension: + expected = [ + [ + [ + [ 0, 16, 32, 48 ], + [ 8, 24, 40, 56 ] + ], + [ + [ 4, 20, 36, 52 ], + [ 12, 28, 44, 60 ] + ] + ], + [ + [ + [ 1, 17, 33, 49 ], + [ 9, 25, 41, 57 ] + ], + [ + [ 5, 21, 37, 53 ], + [ 13, 29, 45, 61 ] + ] + ], + [ + [ + [ 2, 18, 34, 50 ], + [ 10, 26, 42, 58 ] + ], + [ + [ 6, 22, 38, 54 ], + [ 14, 30, 46, 62 ] + ] + ], + [ + [ + [ 3, 19, 35, 51 ], + [ 11, 27, 43, 59 ] + ], + [ + [ 7, 23, 39, 55 ], + [ 15, 31, 47, 63 ] + ] + ] + ]; + + y = unflatten( x, 1, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2, 2, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1, 4, 8, 16 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, 1, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2, 2, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1, 4, 8, 16 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + // Last dimension: + expected = [ + [ + [ [ 0, 32 ], [ 16, 48 ] ], + [ [ 4, 36 ], [ 20, 52 ] ], + [ [ 8, 40 ], [ 24, 56 ] ], + [ [ 12, 44 ], [ 28, 60 ] ] + ], + [ + [ [ 1, 33 ], [ 17, 49 ] ], + [ [ 5, 37 ], [ 21, 53 ] ], + [ [ 9, 41 ], [ 25, 57 ] ], + [ [ 13, 45 ], [ 29, 61 ] ] + ], + [ + [ [ 2, 34 ], [ 18, 50 ] ], + [ [ 6, 38 ], [ 22, 54 ] ], + [ [ 10, 42 ], [ 26, 58 ] ], + [ [ 14, 46 ], [ 30, 62 ] ] + ], + [ + [ [ 3, 35 ], [ 19, 51 ] ], + [ [ 7, 39 ], [ 23, 55 ] ], + [ [ 11, 43 ], [ 27, 59 ] ], + [ [ 15, 47 ], [ 31, 63 ] ] + ] + ]; + + y = unflatten( x, 2, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 4, 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1, 4, 16, 32 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, 2, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 4, 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1, 4, 16, 32 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a view of an input ndarray in which a specified dimension is expanded over multiple dimensions (base; column-major; negative dimension indices)', function test( t ) { + var expected; + var buf; + var x; + var y; + + buf = zeroTo( 64 ); + x = ndarray( 'generic', buf, [ 4, 4, 4 ], [ 1, 4, 16 ], 0, 'column-major' ); + + // First dimension: + expected = [ + [ + [ + [ 0, 16, 32, 48 ], + [ 4, 20, 36, 52 ], + [ 8, 24, 40, 56 ], + [ 12, 28, 44, 60 ] + ], + [ + [ 2, 18, 34, 50 ], + [ 6, 22, 38, 54 ], + [ 10, 26, 42, 58 ], + [ 14, 30, 46, 62 ] + ] + ], + [ + [ + [ 1, 17, 33, 49 ], + [ 5, 21, 37, 53 ], + [ 9, 25, 41, 57 ], + [ 13, 29, 45, 61 ] + ], + [ + [ 3, 19, 35, 51 ], + [ 7, 23, 39, 55 ], + [ 11, 27, 43, 59 ], + [ 15, 31, 47, 63 ] + ] + ] + ]; + + y = unflatten( x, -3, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 4, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1, 2, 4, 16 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, -3, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 4, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1, 2, 4, 16 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + // Middle dimension: + expected = [ + [ + [ + [ 0, 16, 32, 48 ], + [ 8, 24, 40, 56 ] + ], + [ + [ 4, 20, 36, 52 ], + [ 12, 28, 44, 60 ] + ] + ], + [ + [ + [ 1, 17, 33, 49 ], + [ 9, 25, 41, 57 ] + ], + [ + [ 5, 21, 37, 53 ], + [ 13, 29, 45, 61 ] + ] + ], + [ + [ + [ 2, 18, 34, 50 ], + [ 10, 26, 42, 58 ] + ], + [ + [ 6, 22, 38, 54 ], + [ 14, 30, 46, 62 ] + ] + ], + [ + [ + [ 3, 19, 35, 51 ], + [ 11, 27, 43, 59 ] + ], + [ + [ 7, 23, 39, 55 ], + [ 15, 31, 47, 63 ] + ] + ] + ]; + + y = unflatten( x, -2, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2, 2, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1, 4, 8, 16 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, -2, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2, 2, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1, 4, 8, 16 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + // Last dimension: + expected = [ + [ + [ [ 0, 32 ], [ 16, 48 ] ], + [ [ 4, 36 ], [ 20, 52 ] ], + [ [ 8, 40 ], [ 24, 56 ] ], + [ [ 12, 44 ], [ 28, 60 ] ] + ], + [ + [ [ 1, 33 ], [ 17, 49 ] ], + [ [ 5, 37 ], [ 21, 53 ] ], + [ [ 9, 41 ], [ 25, 57 ] ], + [ [ 13, 45 ], [ 29, 61 ] ] + ], + [ + [ [ 2, 34 ], [ 18, 50 ] ], + [ [ 6, 38 ], [ 22, 54 ] ], + [ [ 10, 42 ], [ 26, 58 ] ], + [ [ 14, 46 ], [ 30, 62 ] ] + ], + [ + [ [ 3, 35 ], [ 19, 51 ] ], + [ [ 7, 39 ], [ 23, 55 ] ], + [ [ 11, 43 ], [ 27, 59 ] ], + [ [ 15, 47 ], [ 31, 63 ] ] + ] + ]; + + y = unflatten( x, -1, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 4, 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1, 4, 16, 32 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, -1, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 4, 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1, 4, 16, 32 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a view of an input ndarray in which a specified dimension is expanded over multiple dimensions (row-major)', function test( t ) { + var expected; + var x; + var y; + + x = array( zeroTo( 64 ), { + 'shape': [ 4, 4, 4 ] + }); + + // First dimension: + expected = [ + [ + [ + [ 0, 1, 2, 3 ], + [ 4, 5, 6, 7 ], + [ 8, 9, 10, 11 ], + [ 12, 13, 14, 15 ] + ], + [ + [ 16, 17, 18, 19 ], + [ 20, 21, 22, 23 ], + [ 24, 25, 26, 27 ], + [ 28, 29, 30, 31 ] + ] + ], + [ + [ + [ 32, 33, 34, 35 ], + [ 36, 37, 38, 39 ], + [ 40, 41, 42, 43 ], + [ 44, 45, 46, 47 ] + ], + [ + [ 48, 49, 50, 51 ], + [ 52, 53, 54, 55 ], + [ 56, 57, 58, 59 ], + [ 60, 61, 62, 63 ] + ] + ] + ]; + + y = unflatten( x, 0, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 4, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 32, 16, 4, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, 0, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 4, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 32, 16, 4, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + // Middle dimension: + expected = [ + [ + [ + [ 0, 1, 2, 3 ], + [ 4, 5, 6, 7 ] + ], + [ + [ 8, 9, 10, 11 ], + [ 12, 13, 14, 15 ] + ] + ], + [ + [ + [ 16, 17, 18, 19 ], + [ 20, 21, 22, 23 ] + ], + [ + [ 24, 25, 26, 27 ], + [ 28, 29, 30, 31 ] + ] + ], + [ + [ + [ 32, 33, 34, 35 ], + [ 36, 37, 38, 39 ] + ], + [ + [ 40, 41, 42, 43 ], + [ 44, 45, 46, 47 ] + ] + ], + [ + [ + [ 48, 49, 50, 51 ], + [ 52, 53, 54, 55 ] + ], + [ + [ 56, 57, 58, 59 ], + [ 60, 61, 62, 63 ] + ] + ] + ]; + + y = unflatten( x, 1, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2, 2, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 16, 8, 4, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, 1, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2, 2, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 16, 8, 4, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + // Last dimension: + expected = [ + [ + [ [ 0, 1 ], [ 2, 3 ] ], + [ [ 4, 5 ], [ 6, 7 ] ], + [ [ 8, 9 ], [ 10, 11 ] ], + [ [ 12, 13 ], [ 14, 15 ] ] + ], + [ + [ [ 16, 17 ], [ 18, 19 ] ], + [ [ 20, 21 ], [ 22, 23 ] ], + [ [ 24, 25 ], [ 26, 27 ] ], + [ [ 28, 29 ], [ 30, 31 ] ] + ], + [ + [ [ 32, 33 ], [ 34, 35 ] ], + [ [ 36, 37 ], [ 38, 39 ] ], + [ [ 40, 41 ], [ 42, 43 ] ], + [ [ 44, 45 ], [ 46, 47 ] ] + ], + [ + [ [ 48, 49 ], [ 50, 51 ] ], + [ [ 52, 53 ], [ 54, 55 ] ], + [ [ 56, 57 ], [ 58, 59 ] ], + [ [ 60, 61 ], [ 62, 63 ] ] + ] + ]; + + y = unflatten( x, 2, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 4, 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 16, 4, 2, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, 2, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 4, 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 16, 4, 2, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a view of an input ndarray in which a specified dimension is expanded over multiple dimensions (row-major; negative dimension indices)', function test( t ) { + var expected; + var x; + var y; + + x = array( zeroTo( 64 ), { + 'shape': [ 4, 4, 4 ] + }); + + // First dimension: + expected = [ + [ + [ + [ 0, 1, 2, 3 ], + [ 4, 5, 6, 7 ], + [ 8, 9, 10, 11 ], + [ 12, 13, 14, 15 ] + ], + [ + [ 16, 17, 18, 19 ], + [ 20, 21, 22, 23 ], + [ 24, 25, 26, 27 ], + [ 28, 29, 30, 31 ] + ] + ], + [ + [ + [ 32, 33, 34, 35 ], + [ 36, 37, 38, 39 ], + [ 40, 41, 42, 43 ], + [ 44, 45, 46, 47 ] + ], + [ + [ 48, 49, 50, 51 ], + [ 52, 53, 54, 55 ], + [ 56, 57, 58, 59 ], + [ 60, 61, 62, 63 ] + ] + ] + ]; + + y = unflatten( x, -3, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 4, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 32, 16, 4, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, -3, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 4, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 32, 16, 4, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + // Middle dimension: + expected = [ + [ + [ + [ 0, 1, 2, 3 ], + [ 4, 5, 6, 7 ] + ], + [ + [ 8, 9, 10, 11 ], + [ 12, 13, 14, 15 ] + ] + ], + [ + [ + [ 16, 17, 18, 19 ], + [ 20, 21, 22, 23 ] + ], + [ + [ 24, 25, 26, 27 ], + [ 28, 29, 30, 31 ] + ] + ], + [ + [ + [ 32, 33, 34, 35 ], + [ 36, 37, 38, 39 ] + ], + [ + [ 40, 41, 42, 43 ], + [ 44, 45, 46, 47 ] + ] + ], + [ + [ + [ 48, 49, 50, 51 ], + [ 52, 53, 54, 55 ] + ], + [ + [ 56, 57, 58, 59 ], + [ 60, 61, 62, 63 ] + ] + ] + ]; + + y = unflatten( x, -2, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2, 2, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 16, 8, 4, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, -2, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2, 2, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 16, 8, 4, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + // Last dimension: + expected = [ + [ + [ [ 0, 1 ], [ 2, 3 ] ], + [ [ 4, 5 ], [ 6, 7 ] ], + [ [ 8, 9 ], [ 10, 11 ] ], + [ [ 12, 13 ], [ 14, 15 ] ] + ], + [ + [ [ 16, 17 ], [ 18, 19 ] ], + [ [ 20, 21 ], [ 22, 23 ] ], + [ [ 24, 25 ], [ 26, 27 ] ], + [ [ 28, 29 ], [ 30, 31 ] ] + ], + [ + [ [ 32, 33 ], [ 34, 35 ] ], + [ [ 36, 37 ], [ 38, 39 ] ], + [ [ 40, 41 ], [ 42, 43 ] ], + [ [ 44, 45 ], [ 46, 47 ] ] + ], + [ + [ [ 48, 49 ], [ 50, 51 ] ], + [ [ 52, 53 ], [ 54, 55 ] ], + [ [ 56, 57 ], [ 58, 59 ] ], + [ [ 60, 61 ], [ 62, 63 ] ] + ] + ]; + + y = unflatten( x, -1, [ 2, 2 ], false ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 4, 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 16, 4, 2, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = unflatten( x, -1, [ 2, 2 ], true ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 4, 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 16, 4, 2, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +});