diff --git a/lib/node_modules/@stdlib/ndarray/base/to-unflattened/README.md b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/README.md new file mode 100644 index 000000000000..5a3578c52ed9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/README.md @@ -0,0 +1,121 @@ + + +# toUnflattened + +> Return a new ndarray in which a specified dimension of an input ndarray is expanded over multiple dimensions. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var toUnflattened = require( '@stdlib/ndarray/base/to-unflattened' ); +``` + +#### toUnflattened( x, dim, sizes ) + +Returns a new ndarray in which a specified dimension of an input ndarray 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. +- **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/base/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/base/to-unflattened/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/benchmark/benchmark.js new file mode 100644 index 000000000000..1e1538360597 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/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 toUnflattened = require( './../lib' ); + + +// MAIN // + +bench( format( '%s:ctor=base,ndims=2,order=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 = toUnflattened( values[ i%values.length ], 1, 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,order=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 = toUnflattened( values[ i%values.length ], 1, 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,order=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 = toUnflattened( values[ i%values.length ], 1, 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,order=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 = toUnflattened( values[ i%values.length ], 1, 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/base/to-unflattened/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/docs/repl.txt new file mode 100644 index 000000000000..81b40dfe3028 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/docs/repl.txt @@ -0,0 +1,33 @@ + +{{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/base/to-unflattened/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/docs/types/index.d.ts new file mode 100644 index 000000000000..0a896eb12281 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/docs/types/index.d.ts @@ -0,0 +1,47 @@ +/* +* @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 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 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 = 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: ArrayLike ): U; + + +// EXPORTS // + +export = toUnflattened; diff --git a/lib/node_modules/@stdlib/ndarray/base/to-unflattened/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/docs/types/test.ts new file mode 100644 index 000000000000..1ea05462edce --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/docs/types/test.ts @@ -0,0 +1,84 @@ +/* +* @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( [ 2, 6 ], { + 'dtype': 'float64' + }); + + toUnflattened( x, 1, [ 2, 3 ] ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is not provided a first argument which is an ndarray... +{ + toUnflattened( true, 1, [ 2, 3 ] ); // $ExpectError + toUnflattened( false, 1, [ 2, 3 ] ); // $ExpectError + toUnflattened( null, 1, [ 2, 3 ] ); // $ExpectError + toUnflattened( undefined, 1, [ 2, 3 ] ); // $ExpectError + toUnflattened( '5', 1, [ 2, 3 ] ); // $ExpectError + toUnflattened( [ '1', '2' ], 1, [ 2, 3 ] ); // $ExpectError + toUnflattened( {}, 1, [ 2, 3 ] ); // $ExpectError + toUnflattened( ( x: number ): number => x, 1, [ 2, 3 ] ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a second argument which is a number... +{ + const x = zeros( [ 2, 6 ] ); + + toUnflattened( x, true, [ 2, 3 ] ); // $ExpectError + toUnflattened( x, false, [ 2, 3 ] ); // $ExpectError + toUnflattened( x, null, [ 2, 3 ] ); // $ExpectError + toUnflattened( x, undefined, [ 2, 3 ] ); // $ExpectError + toUnflattened( x, '5', [ 2, 3 ] ); // $ExpectError + toUnflattened( x, [ '1', '2' ], [ 2, 3 ] ); // $ExpectError + toUnflattened( x, {}, [ 2, 3 ] ); // $ExpectError + toUnflattened( 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( [ 2, 6 ] ); + + toUnflattened( x, 1, true ); // $ExpectError + toUnflattened( x, 1, false ); // $ExpectError + toUnflattened( x, 1, null ); // $ExpectError + toUnflattened( x, 1, undefined ); // $ExpectError + toUnflattened( x, 1, '5' ); // $ExpectError + toUnflattened( x, 1, [ '1', '2' ] ); // $ExpectError + toUnflattened( x, 1, {} ); // $ExpectError + toUnflattened( x, 1, ( 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 ] ); + + toUnflattened(); // $ExpectError + toUnflattened( x ); // $ExpectError + toUnflattened( x, 1 ); // $ExpectError + toUnflattened( x, 1, [ 2, 3 ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/to-unflattened/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/examples/index.js new file mode 100644 index 000000000000..2b4eddc7e81a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/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/base/to-unflattened/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/lib/index.js new file mode 100644 index 000000000000..986e55045eac --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/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/base/to-unflattened +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var toUnflattened = require( '@stdlib/ndarray/base/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/base/to-unflattened/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/lib/main.js new file mode 100644 index 000000000000..22faad24ecbb --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/lib/main.js @@ -0,0 +1,68 @@ +/** +* @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 emptyLike = require( '@stdlib/ndarray/base/empty-like' ); +var unflatten = require( '@stdlib/ndarray/base/unflatten' ); +var assign = require( '@stdlib/ndarray/base/assign' ); + + +// 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 index +* @param {NonNegativeIntegerArray} sizes - new shape of the unflattened dimension +* @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 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 xv; + + // Create an unflattened view of the input ndarray: + xv = unflatten( x, dim, sizes, false ); + + // Create an output ndarray with the same data type as the input ndarray having the unflattened shape: + out = emptyLike( xv ); + + // Assign the elements of the unflattened input ndarray view to the output ndarray: + assign( [ xv, out ] ); + + return out; +} + + +// EXPORTS // + +module.exports = toUnflattened; diff --git a/lib/node_modules/@stdlib/ndarray/base/to-unflattened/package.json b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/package.json new file mode 100644 index 000000000000..57af073af3f5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/ndarray/base/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", + "base", + "ndarray", + "to-unflattened", + "unflatten", + "unflattened", + "reshape", + "multidimensional", + "array", + "utilities", + "utility", + "utils", + "util" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/base/to-unflattened/test/test.js b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/test/test.js new file mode 100644 index 000000000000..ff9726345ee7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-unflattened/test/test.js @@ -0,0 +1,397 @@ +/** +* @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 getDType = require( '@stdlib/ndarray/dtype' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +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 returns a new ndarray in which a specified dimension of an input ndarray 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 = toUnflattened( x, 0, [ 2, 2 ] ); + t.deepEqual( getShape( y ), [ 2, 2, 4, 4 ], 'returns expected value' ); + t.strictEqual( String( getDType( y ) ), String( getDType( x ) ), 'returns expected value' ); + t.notEqual( getData( y ), getData( x ), '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 = toUnflattened( x, 1, [ 2, 2 ] ); + t.deepEqual( getShape( y ), [ 4, 2, 2, 4 ], 'returns expected value' ); + t.strictEqual( String( getDType( y ) ), String( getDType( x ) ), 'returns expected value' ); + t.notEqual( getData( y ), getData( x ), '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 = toUnflattened( x, 2, [ 2, 2 ] ); + t.deepEqual( getShape( y ), [ 4, 4, 2, 2 ], 'returns expected value' ); + t.strictEqual( String( getDType( y ) ), String( getDType( x ) ), 'returns expected value' ); + t.notEqual( getData( y ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray in which a specified dimension of an input ndarray 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 = toUnflattened( x, -3, [ 2, 2 ] ); + t.deepEqual( getShape( y ), [ 2, 2, 4, 4 ], 'returns expected value' ); + t.strictEqual( String( getDType( y ) ), String( getDType( x ) ), 'returns expected value' ); + t.notEqual( getData( y ), getData( x ), '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 = toUnflattened( x, -1, [ 2, 2 ] ); + t.deepEqual( getShape( y ), [ 4, 4, 2, 2 ], 'returns expected value' ); + t.strictEqual( String( getDType( y ) ), String( getDType( x ) ), 'returns expected value' ); + t.notEqual( getData( y ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray in which a specified dimension of an input ndarray 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 = toUnflattened( x, 0, [ 2, 2 ] ); + t.deepEqual( getShape( y ), [ 2, 2, 4, 4 ], 'returns expected value' ); + t.strictEqual( String( getDType( y ) ), String( getDType( x ) ), 'returns expected value' ); + t.notEqual( getData( y ), getData( x ), '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 = toUnflattened( x, 2, [ 2, 2 ] ); + t.deepEqual( getShape( y ), [ 4, 4, 2, 2 ], 'returns expected value' ); + t.strictEqual( String( getDType( y ) ), String( getDType( x ) ), 'returns expected value' ); + t.notEqual( getData( y ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a writable ndarray when the input is a read-only ndarray', function test( t ) { + var y; + var x; + + x = array( zeroTo( 12 ), { + 'shape': [ 12 ], + 'dtype': 'float64', + 'readonly': true + }); + + y = toUnflattened( x, 0, [ 3, 4 ] ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.notEqual( getData( y ), getData( x ), 'returns expected value' ); + t.deepEqual( getShape( y ), [ 3, 4 ], 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray with contiguous strides (row-major)', function test( t ) { + var y; + var x; + + x = array( zeroTo( 12 ), { + 'shape': [ 12 ], + 'dtype': 'float64', + 'order': 'row-major' + }); + + y = toUnflattened( x, 0, [ 3, 4 ] ); + t.deepEqual( getShape( y ), [ 3, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 4, 1 ], 'returns expected value' ); + t.deepEqual( ndarray2array( y ), [ [ 0, 1, 2, 3 ], [ 4, 5, 6, 7 ], [ 8, 9, 10, 11 ] ], 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray with contiguous strides (column-major)', function test( t ) { + var y; + var x; + + x = array( zeroTo( 12 ), { + 'shape': [ 12 ], + 'dtype': 'float64', + 'order': 'column-major' + }); + + y = toUnflattened( x, 0, [ 3, 4 ] ); + t.deepEqual( getShape( y ), [ 3, 4 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1, 3 ], 'returns expected value' ); + + t.end(); +});