diff --git a/lib/node_modules/@stdlib/ndarray/to-unflattened/README.md b/lib/node_modules/@stdlib/ndarray/to-unflattened/README.md new file mode 100644 index 000000000000..4d080467934d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-unflattened/README.md @@ -0,0 +1,127 @@ + + +# toUnflattened + +> Return a new [`ndarray`][@stdlib/ndarray/ctor] in which a specified dimension of an input [`ndarray`][@stdlib/ndarray/ctor] is expanded over multiple dimensions. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var toUnflattened = require( '@stdlib/ndarray/to-unflattened' ); +``` + +#### toUnflattened( x, dim, sizes ) + +Returns a new [`ndarray`][@stdlib/ndarray/ctor] in which a specified dimension of an input [`ndarray`][@stdlib/ndarray/ctor] 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 y = toUnflattened( 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/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var toUnflattened = require( '@stdlib/ndarray/to-unflattened' ); + +var x = uniform( [ 12 ], -100, 100 ); +console.log( ndarray2array( x ) ); + +var y = toUnflattened( x, 0, [ 3, 4 ] ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/to-unflattened/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/to-unflattened/benchmark/benchmark.js new file mode 100644 index 000000000000..682c091cd564 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-unflattened/benchmark/benchmark.js @@ -0,0 +1,181 @@ +/** +* @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 toUnflattened = require( './../lib' ); + + +// MAIN // + +bench( format( '%s:ctor=base,ndims=1', pkg ), function benchmark( b ) { + var strides; + var buffer; + var offset; + var dtype; + var order; + var shape; + var sizes; + var out; + var i; + var x; + + dtype = 'float64'; + buffer = new Float64Array( 24 ); + shape = [ 24 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + sizes = [ 4, 6 ]; + + x = ndarrayBase( dtype, buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = toUnflattened( x, 0, sizes ); + 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:ctor=ndarray,ndims=1', pkg ), function benchmark( b ) { + var strides; + var buffer; + var offset; + var dtype; + var order; + var shape; + var sizes; + var out; + var i; + var x; + + dtype = 'float64'; + buffer = new Float64Array( 24 ); + shape = [ 24 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + sizes = [ 4, 6 ]; + + x = ndarray( dtype, buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = toUnflattened( x, 0, sizes ); + 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:ctor=base,ndims=2', pkg ), function benchmark( b ) { + var strides; + var buffer; + var offset; + var dtype; + var order; + var shape; + var sizes; + var out; + var i; + var x; + + dtype = 'float64'; + buffer = new Float64Array( 24 ); + shape = [ 24, 1 ]; + strides = [ 1, 24 ]; + offset = 0; + order = 'row-major'; + sizes = [ 4, 6 ]; + + x = ndarrayBase( dtype, buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = toUnflattened( x, 0, sizes ); + 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:ctor=ndarray,ndims=2', pkg ), function benchmark( b ) { + var strides; + var buffer; + var offset; + var dtype; + var order; + var shape; + var sizes; + var out; + var i; + var x; + + dtype = 'float64'; + buffer = new Float64Array( 24 ); + shape = [ 24, 1 ]; + strides = [ 1, 24 ]; + offset = 0; + order = 'row-major'; + sizes = [ 4, 6 ]; + + x = ndarray( dtype, buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = toUnflattened( x, 0, sizes ); + 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/to-unflattened/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/to-unflattened/docs/repl.txt new file mode 100644 index 000000000000..f8cfa815132c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-unflattened/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( x, dim, sizes ) + Returns a new ndarray in which a specified dimension of an input ndarray + 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/to-unflattened/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/to-unflattened/docs/types/index.d.ts new file mode 100644 index 000000000000..b0851e1284c4 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-unflattened/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'; +import { Collection } from '@stdlib/types/array'; + +/** +* Returns a new ndarray in which a specified dimension of an input ndarray 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 ndarray +* +* @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 = toUnflattened( x, 0, [ 2, 3 ] ); +* // returns [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] +*/ +declare function toUnflattened( x: U, dim: number, sizes: Collection ): U; + + +// EXPORTS // + +export = toUnflattened; diff --git a/lib/node_modules/@stdlib/ndarray/to-unflattened/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/to-unflattened/docs/types/test.ts new file mode 100644 index 000000000000..73286d7dcd86 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-unflattened/docs/types/test.ts @@ -0,0 +1,82 @@ +/* +* @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 toUnflattened = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 12 ], { + 'dtype': 'float64' + }); + + toUnflattened( x, 0, [ 3, 4 ] ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is not provided a first argument which is an ndarray... +{ + toUnflattened( true, 0, [ 3, 4 ] ); // $ExpectError + toUnflattened( false, 0, [ 3, 4 ] ); // $ExpectError + toUnflattened( null, 0, [ 3, 4 ] ); // $ExpectError + toUnflattened( undefined, 0, [ 3, 4 ] ); // $ExpectError + toUnflattened( '5', 0, [ 3, 4 ] ); // $ExpectError + toUnflattened( [ '1', '2' ], 0, [ 3, 4 ] ); // $ExpectError + toUnflattened( {}, 0, [ 3, 4 ] ); // $ExpectError + toUnflattened( ( x: number ): number => x, 0, [ 3, 4 ] ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a second argument which is a number... +{ + const x = zeros( [ 12 ] ); + + toUnflattened( x, true, [ 3, 4 ] ); // $ExpectError + toUnflattened( x, false, [ 3, 4 ] ); // $ExpectError + toUnflattened( x, null, [ 3, 4 ] ); // $ExpectError + toUnflattened( x, undefined, [ 3, 4 ] ); // $ExpectError + toUnflattened( x, '5', [ 3, 4 ] ); // $ExpectError + toUnflattened( x, [ '1', '2' ], [ 3, 4 ] ); // $ExpectError + toUnflattened( x, ( x: number ): number => x, [ 3, 4 ] ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a third argument which is a collection of numbers... +{ + const x = zeros( [ 12 ] ); + + toUnflattened( x, 0, '10' ); // $ExpectError + toUnflattened( x, 0, 10 ); // $ExpectError + toUnflattened( x, 0, true ); // $ExpectError + toUnflattened( x, 0, false ); // $ExpectError + toUnflattened( x, 0, null ); // $ExpectError + toUnflattened( x, 0, [ '1', '2' ] ); // $ExpectError + toUnflattened( 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( [ 12 ] ); + + toUnflattened(); // $ExpectError + toUnflattened( x ); // $ExpectError + toUnflattened( x, 0 ); // $ExpectError + toUnflattened( x, 0, [ 3, 4 ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/to-unflattened/examples/index.js b/lib/node_modules/@stdlib/ndarray/to-unflattened/examples/index.js new file mode 100644 index 000000000000..2b4eddc7e81a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-unflattened/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 toUnflattened = require( './../lib' ); + +var x = uniform( [ 12 ], -100, 100 ); +console.log( ndarray2array( x ) ); + +var y = toUnflattened( x, 0, [ 3, 4 ] ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/to-unflattened/lib/index.js b/lib/node_modules/@stdlib/ndarray/to-unflattened/lib/index.js new file mode 100644 index 000000000000..304b670fa43e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-unflattened/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 new ndarray in which a specified dimension of an input ndarray is expanded over multiple dimensions. +* +* @module @stdlib/ndarray/to-unflattened +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var toUnflattened = require( '@stdlib/ndarray/to-unflattened' ); +* +* 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 = toUnflattened( 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/to-unflattened/lib/main.js b/lib/node_modules/@stdlib/ndarray/to-unflattened/lib/main.js new file mode 100644 index 000000000000..9a5d403a94d9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-unflattened/lib/main.js @@ -0,0 +1,89 @@ +/** +* @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/shape' ); +var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' ); +var unflatten = require( '@stdlib/ndarray/base/unflatten' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); +var assign = require( '@stdlib/ndarray/base/assign' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a new ndarray in which a specified dimension of an input ndarray 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} output ndarray +* +* @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 = toUnflattened( x, 0, [ 2, 3 ] ); +* // returns [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] +*/ +function toUnflattened( x, dim, sizes ) { + var out; + var bv; + 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 ); + 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 ) ); + } + bv = ndarraylike2ndarray( x ); + bv = unflatten( bv, d, sizes, false ); + out = emptyLike( bv ); + assign( [ bv, out ] ); + return out; +} + + +// EXPORTS // + +module.exports = toUnflattened; diff --git a/lib/node_modules/@stdlib/ndarray/to-unflattened/package.json b/lib/node_modules/@stdlib/ndarray/to-unflattened/package.json new file mode 100644 index 000000000000..2656dc06d93d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-unflattened/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/ndarray/to-unflattened", + "version": "0.0.0", + "description": "Return a new ndarray in which a specified dimension of an input ndarray 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", + "ndarray", + "to-unflattened", + "unflattened", + "unflatten", + "reshape", + "expand", + "dimension", + "multidimensional", + "array", + "utilities", + "utility", + "utils", + "util" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/to-unflattened/test/test.js b/lib/node_modules/@stdlib/ndarray/to-unflattened/test/test.js new file mode 100644 index 000000000000..f765b71afd11 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-unflattened/test/test.js @@ -0,0 +1,325 @@ +/** +* @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 isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' ); +var zeroTo = require( '@stdlib/blas/ext/zero-to' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var toUnflattened = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof toUnflattened, '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() { + toUnflattened( 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 opts; + var x; + var i; + + opts = { + 'dtype': 'float64' + }; + x = zeroTo( [ 6 ], opts ); + + 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() { + toUnflattened( 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 opts; + var x; + var i; + + opts = { + 'dtype': 'float64' + }; + x = zeroTo( [ 6 ], opts ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ '1', '2' ], + [ 1.5, 2.5 ], + [ -1, 6 ], + {}, + 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() { + toUnflattened( x, 0, value ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid dimension index', function test( t ) { + var values; + var opts; + var i; + var x; + + opts = { + 'dtype': 'float64' + }; + x = zeroTo( [ 6 ], opts ); + + values = [ + 5, + -3 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( dim ) { + return function test() { + toUnflattened( x, dim, [ 2, 3 ] ); + }; + } +}); + +tape( 'the function throws an error if the product of `sizes` does not match the dimension size', function test( t ) { + var values; + var opts; + var i; + var x; + + opts = { + 'dtype': 'float64' + }; + x = zeroTo( [ 6 ], opts ); + + values = [ + [ 2, 4 ] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when product of sizes does not match dimension size' ); + } + t.end(); + + function badValue( sizes ) { + return function test() { + toUnflattened( x, 0, sizes ); + }; + } +}); + +tape( 'the function returns a new ndarray with the unflattened dimension (1D input)', function test( t ) { + var expected; + var opts; + var x; + var y; + + opts = { + 'dtype': 'generic' + }; + x = zeroTo( [ 6 ], opts ); + + expected = [ + [ 0, 1, 2 ], + [ 3, 4, 5 ] + ]; + + y = toUnflattened( x, 0, [ 2, 3 ] ); + t.deepEqual( getShape( y ), [ 2, 3 ], 'returns expected shape' ); + t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected dtype' ); + t.notEqual( getData( y ), getData( x ), 'does not share the same data buffer' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray with the unflattened dimension (1D input, unflattened to 3D)', function test( t ) { + var expected; + var opts; + var x; + var y; + + opts = { + 'dtype': 'generic' + }; + x = zeroTo( [ 12 ], opts ); + + expected = [ + [ [ 0, 1, 2 ], [ 3, 4, 5 ] ], + [ [ 6, 7, 8 ], [ 9, 10, 11 ] ] + ]; + + y = toUnflattened( x, 0, [ 2, 2, 3 ] ); + t.deepEqual( getShape( y ), [ 2, 2, 3 ], 'returns expected shape' ); + t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected dtype' ); + t.notEqual( getData( y ), getData( x ), 'does not share the same data buffer' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative dimension indices', function test( t ) { + var expected; + var opts; + var x; + var y; + + opts = { + 'dtype': 'generic' + }; + x = zeroTo( [ 6 ], opts ); + + expected = [ + [ 0, 1, 2 ], + [ 3, 4, 5 ] + ]; + + y = toUnflattened( x, -1, [ 2, 3 ] ); + t.deepEqual( getShape( y ), [ 2, 3 ], 'returns expected shape' ); + t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected dtype' ); + t.notEqual( getData( y ), getData( x ), 'does not share the same data buffer' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports column-major order', function test( t ) { + var expected; + var opts; + var x; + var y; + + opts = { + 'dtype': 'generic', + 'order': 'column-major' + }; + x = zeroTo( [ 6 ], opts ); + + expected = [ + [ 0, 2, 4 ], + [ 1, 3, 5 ] + ]; + + y = toUnflattened( x, 0, [ 2, 3 ] ); + t.deepEqual( getShape( y ), [ 2, 3 ], 'returns expected shape' ); + t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected dtype' ); + t.notEqual( getData( y ), getData( x ), 'does not share the same data buffer' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function unflattens a middle dimension in a multidimensional input', function test( t ) { + var expected; + var opts; + var x; + var y; + + opts = { + 'dtype': 'generic', + 'dims': [ 0, 1 ] + }; + x = zeroTo( [ 3, 4 ], opts ); + + expected = [ + [ [ 0, 1 ], [ 2, 3 ] ], + [ [ 4, 5 ], [ 6, 7 ] ], + [ [ 8, 9 ], [ 10, 11 ] ] + ]; + + y = toUnflattened( x, 1, [ 2, 2 ] ); + t.deepEqual( getShape( y ), [ 3, 2, 2 ], 'returns expected shape' ); + t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected dtype' ); + t.notEqual( getData( y ), getData( x ), 'does not share the same data buffer' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +});