diff --git a/lib/node_modules/@stdlib/ndarray/unflatten/README.md b/lib/node_modules/@stdlib/ndarray/unflatten/README.md new file mode 100644 index 000000000000..a67c001bb48e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unflatten/README.md @@ -0,0 +1,129 @@ + + +# unflatten + +> Return a read-only view of an input [ndarray][@stdlib/ndarray/ctor] in which a specified dimension is expanded over multiple dimensions. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var unflatten = require( '@stdlib/ndarray/unflatten' ); +``` + +#### unflatten( x, dim, sizes ) + +Returns a read-only view of an input [ndarray][@stdlib/ndarray/ctor] in which a specified dimension is expanded over multiple dimensions. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +// Create a 1-dimensional ndarray: +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 ] + +// Unflatten the first dimension: +var y = unflatten( x, 0, [ 2, 3 ] ); +// 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]. +- **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. + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var unflatten = require( '@stdlib/ndarray/unflatten' ); + +var x = uniform( [ 12 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = unflatten( x, 0, [ 3, 4 ] ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/unflatten/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/unflatten/benchmark/benchmark.js new file mode 100644 index 000000000000..fd8ec6c55173 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unflatten/benchmark/benchmark.js @@ -0,0 +1,127 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var empty = require( '@stdlib/ndarray/empty' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var unflatten = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::1d', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 6 ], { 'dtype': 'float64' } ), + empty( [ 6 ], { 'dtype': 'float32' } ), + empty( [ 6 ], { 'dtype': 'int32' } ), + empty( [ 6 ], { 'dtype': 'complex128' } ), + empty( [ 6 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = unflatten( values[ i%values.length ], 0, [ 2, 3 ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::2d', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2, 6 ], { 'dtype': 'float64' } ), + empty( [ 2, 6 ], { 'dtype': 'float32' } ), + empty( [ 2, 6 ], { 'dtype': 'int32' } ), + empty( [ 2, 6 ], { 'dtype': 'complex128' } ), + empty( [ 2, 6 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = unflatten( values[ i%values.length ], 1, [ 2, 3 ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::3d', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2, 2, 6 ], { 'dtype': 'float64' } ), + empty( [ 2, 2, 6 ], { 'dtype': 'float32' } ), + empty( [ 2, 2, 6 ], { 'dtype': 'int32' } ), + empty( [ 2, 2, 6 ], { 'dtype': 'complex128' } ), + empty( [ 2, 2, 6 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = unflatten( values[ i%values.length ], 2, [ 2, 3 ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/unflatten/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/unflatten/docs/repl.txt new file mode 100644 index 000000000000..8cc08e0a68b6 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unflatten/docs/repl.txt @@ -0,0 +1,33 @@ + +{{alias}}( x, dim, sizes ) + Returns a read-only 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. + + 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 y = {{alias}}( x, 0, [ 2, 3 ] ) + [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/unflatten/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/unflatten/docs/types/index.d.ts new file mode 100644 index 000000000000..7fa631bde0b4 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/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 { typedndarray } from '@stdlib/types/ndarray'; +import { Collection } from '@stdlib/types/array'; + +/** +* Returns a read-only 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 +* @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 y = unflatten( x, 0, [ 2, 3 ] ); +* // returns [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] +*/ +declare function unflatten = typedndarray>( x: U, dim: number, sizes: Collection ): U; + + +// EXPORTS // + +export = unflatten; diff --git a/lib/node_modules/@stdlib/ndarray/unflatten/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/unflatten/docs/types/test.ts new file mode 100644 index 000000000000..ea29afbbbf1d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unflatten/docs/types/test.ts @@ -0,0 +1,88 @@ +/* +* @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( [ 6 ], { + 'dtype': 'float64' + }); + + unflatten( x, 0, [ 2, 3 ] ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is not provided a first argument which is an ndarray... +{ + unflatten( '5', 0, [ 2, 3 ] ); // $ExpectError + unflatten( 5, 0, [ 2, 3 ] ); // $ExpectError + unflatten( true, 0, [ 2, 3 ] ); // $ExpectError + unflatten( false, 0, [ 2, 3 ] ); // $ExpectError + unflatten( null, 0, [ 2, 3 ] ); // $ExpectError + unflatten( {}, 0, [ 2, 3 ] ); // $ExpectError + unflatten( [ '5' ], 0, [ 2, 3 ] ); // $ExpectError + unflatten( ( x: number ): number => x, 0, [ 2, 3 ] ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a second argument which is a number... +{ + const x = zeros( [ 6 ], { + 'dtype': 'float64' + }); + + unflatten( x, '5', [ 2, 3 ] ); // $ExpectError + unflatten( x, true, [ 2, 3 ] ); // $ExpectError + unflatten( x, false, [ 2, 3 ] ); // $ExpectError + unflatten( x, null, [ 2, 3 ] ); // $ExpectError + unflatten( x, {}, [ 2, 3 ] ); // $ExpectError + unflatten( x, [ '5' ], [ 2, 3 ] ); // $ExpectError + unflatten( x, ( x: number ): number => x, [ 2, 3 ] ); // $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( [ 6 ], { + 'dtype': 'float64' + }); + + unflatten( x, 0, '5' ); // $ExpectError + unflatten( x, 0, 5 ); // $ExpectError + unflatten( x, 0, true ); // $ExpectError + unflatten( x, 0, false ); // $ExpectError + unflatten( x, 0, null ); // $ExpectError + unflatten( x, 0, {} ); // $ExpectError + unflatten( x, 0, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 6 ], { + 'dtype': 'float64' + }); + + unflatten(); // $ExpectError + unflatten( x ); // $ExpectError + unflatten( x, 0 ); // $ExpectError + unflatten( x, 0, [ 2, 3 ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/unflatten/examples/index.js b/lib/node_modules/@stdlib/ndarray/unflatten/examples/index.js new file mode 100644 index 000000000000..d80833d19a4c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/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/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var unflatten = require( './../lib' ); + +var x = uniform( [ 12 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = unflatten( x, 0, [ 3, 4 ] ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/unflatten/lib/index.js b/lib/node_modules/@stdlib/ndarray/unflatten/lib/index.js new file mode 100644 index 000000000000..f0a1b5718123 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/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 read-only view of an input ndarray in which a specified dimension is expanded over multiple dimensions. +* +* @module @stdlib/ndarray/unflatten +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var unflatten = require( '@stdlib/ndarray/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 y = unflatten( x, 0, [ 2, 3 ] ); +* // 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/unflatten/lib/main.js b/lib/node_modules/@stdlib/ndarray/unflatten/lib/main.js new file mode 100644 index 000000000000..0a0b4153b5a5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unflatten/lib/main.js @@ -0,0 +1,79 @@ +/** +* @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 isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ).primitives; +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var getShape = require( '@stdlib/ndarray/base/shape' ); +var base = require( '@stdlib/ndarray/base/unflatten' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a read-only view of an input ndarray in which a specified dimension is expanded over multiple dimensions. +* +* @param {ndarray} x - input array +* @param {integer} dim - dimension to be unflattened +* @param {NonNegativeIntegerArray} sizes - new shape of the unflattened dimension +* @throws {TypeError} first argument must be an ndarray +* @throws {TypeError} second argument must be an integer +* @throws {TypeError} third argument must be an array of nonnegative integers +* @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} ndarray view +* +* @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 y = unflatten( x, 0, [ 2, 3 ] ); +* // returns [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] +*/ +function unflatten( x, dim, sizes ) { + var sh; + var d; + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + if ( !isInteger( dim ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', dim ) ); + } + if ( !isNonNegativeIntegerArray( sizes ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be an array of nonnegative integers. Value: `%s`.', sizes ) ); + } + sh = getShape( x, false ); + d = normalizeIndex( dim, sh.length-1 ); + if ( d === -1 ) { + throw new RangeError( format( 'invalid argument. Dimension index exceeds the number of dimensions. Number of dimensions: %d. Value: `%d`.', sh.length, dim ) ); + } + return base( x, d, sizes, false ); +} + + +// EXPORTS // + +module.exports = unflatten; diff --git a/lib/node_modules/@stdlib/ndarray/unflatten/package.json b/lib/node_modules/@stdlib/ndarray/unflatten/package.json new file mode 100644 index 000000000000..1feaafa727bc --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unflatten/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/ndarray/unflatten", + "version": "0.0.0", + "description": "Return a read-only 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", + "data", + "structure", + "vector", + "ndarray", + "matrix", + "unflatten", + "expand", + "reshape", + "dimensions", + "multidimensional" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/unflatten/test/test.js b/lib/node_modules/@stdlib/ndarray/unflatten/test/test.js new file mode 100644 index 000000000000..c9f9926c8517 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unflatten/test/test.js @@ -0,0 +1,261 @@ +/** +* @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 isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +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 throws an error if provided a first argument which is not an ndarray', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + 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() { + unflatten( value, 0, [ 2, 3 ] ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an integer', function test( t ) { + var values; + var x; + var i; + + x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + values = [ + '5', + 3.14, + 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() { + unflatten( x, value, [ 2, 3 ] ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not an array of nonnegative integers', function test( t ) { + var values; + var x; + var i; + + x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + values = [ + '5', + 5, + 3.14, + NaN, + true, + false, + null, + void 0, + [ '5' ], + [ 3.14 ], + [ -1 ], + {}, + 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() { + unflatten( x, 0, value ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid dimension index', function test( t ) { + var values; + var x; + var i; + + x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + values = [ + -10, + -2, + 1, + 10 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + unflatten( x, value, [ 2, 3 ] ); + }; + } +}); + +tape( 'the function throws an error if the product of the sizes is not equal to the size of the dimension to be unflattened', function test( t ) { + var values; + var x; + var i; + + x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + values = [ + [ 2, 2 ], + [ 3, 3 ], + [ 1, 2 ], + [ 7, 1 ] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + unflatten( x, 0, value ); + }; + } +}); + +tape( 'the function returns a read-only view of an input ndarray in which a specified dimension is expanded over multiple dimensions', function test( t ) { + var expected; + var x; + var y; + + x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + y = unflatten( x, 0, [ 2, 3 ] ); + + expected = [ + [ 1.0, 2.0, 3.0 ], + [ 4.0, 5.0, 6.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 3 ], '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' ); + + t.end(); +}); + +tape( 'the function supports negative dimension indices', function test( t ) { + var expected; + var x; + var y; + + x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + y = unflatten( x, -1, [ 3, 2 ] ); + + expected = [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ], + [ 5.0, 6.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 3, 2 ], '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' ); + + t.end(); +}); + +tape( 'the function expands an interior dimension of a multi-dimensional ndarray', function test( t ) { + var expected; + var x; + var y; + + x = array([ + [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], + [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] + ]); + y = unflatten( x, 1, [ 2, 3 ] ); + + expected = [ + [ + [ 1.0, 2.0, 3.0 ], + [ 4.0, 5.0, 6.0 ] + ], + [ + [ 7.0, 8.0, 9.0 ], + [ 10.0, 11.0, 12.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 3 ], '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' ); + + t.end(); +});