Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/unflatten-shape/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<!--

@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.

-->

# unflattenShape

> Unflatten a shape over multiple dimensions.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var unflattenShape = require( '@stdlib/ndarray/base/unflatten-shape' );
```

#### unflattenShape( shape, dim, sizes )

Unflattens a shape over multiple dimensions.

```javascript
var sh = unflattenShape( [ 6, 2, 1 ], 0, [ 3, 2 ] );
// returns [ 3, 2, 2, 1 ]
```

The function accepts the following parameters:

- **shape**: array shape.
- **dim**: dimension to be unflattened.
- **sizes**: new shape of the unflattened dimension.

#### unflattenShape.assign( shape, dim, sizes, out )

Unflattens a shape over multiple dimensions and assigns results to a provided output array.

```javascript
var o = [ 0, 0, 0, 0 ];

var out = unflattenShape.assign( [ 6, 2, 1 ], 0, [ 3, 2 ], o );
// returns [ 3, 2, 2, 1 ]

var bool = ( out === o );
// returns true
```

The function accepts the following parameters:

- **shape**: array shape.
- **dim**: dimension to be unflattened.
- **sizes**: new shape of the unflattened dimension.
- **out**: output array.

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var unflattenShape = require( '@stdlib/ndarray/base/unflatten-shape' );

var out = unflattenShape( [ 2, 4, 1 ], 1, [ 2, 2 ] );
// returns [ 2, 2, 2, 1 ]

console.log( 'Unflattened Shape: ', out );
```

</section>

<!-- /.examples -->

<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -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 bench = require( '@stdlib/bench' );
var isArray = require( '@stdlib/assert/is-array' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var unflattenShape = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var shape;
var sizes;
var out;
var i;

shape = [ 5, 9, 3, 4, 2 ];
sizes = [ 1, 1, 3 ];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = unflattenShape( shape, 2, sizes );
if ( out.length !== 7 ) {
b.fail( 'should have expected length' );
}
}
b.toc();
if ( !isArray( out ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s:assign', pkg ), function benchmark( b ) {
var shape;
var sizes;
var out;
var i;

shape = [ 5, 9, 3, 4, 2 ];
sizes = [ 1, 1, 3 ];
out = [ 0, 0, 0, 0, 0, 0, 0 ];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = unflattenShape.assign( shape, 2, sizes, out );
if ( out.length !== 7 ) {
b.fail( 'should have expected length' );
}
}
b.toc();
if ( !isArray( out ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

{{alias}}( shape, dim, sizes )
Unflattens a shape over multiple dimensions.

Parameters
----------
shape: ArrayLike
Array shape.

dim: integer
Dimension to be unflattened.

sizes: ArrayLike
New shape of the unflattened dimension.

Returns
-------
out: Array
Unflattened shape.

Examples
--------
> var sh = [ 6, 2, 1 ];
> var sizes = [ 3, 2 ];
> var out = {{alias}}( sh, 0, sizes )
[ 3, 2, 2, 1 ]


{{alias}}.assign( shape, dim, sizes, out )
Unflattens a shape over multiple dimensions and assigns results to a
provided output array.

Parameters
----------
shape: ArrayLike
Array shape.

dim: integer
Dimension to be unflattened.

sizes: ArrayLike
New shape of the unflattened dimension.

out: Array|TypedArray|Object
Output array.

Returns
-------
out: Array|TypedArray|Object
Output array.

Examples
--------
> var sh = [ 6, 2, 1 ];
> var sizes = [ 3, 2 ];
> var o = [ 0, 0, 0, 0 ];
> var out = {{alias}}.assign( sh, 0, sizes, o )
[ 3, 2, 2, 1 ]
> var bool = ( o === out )
true

See Also
--------
Original file line number Diff line number Diff line change
@@ -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.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

/**
* Interface describing `unflattenShape`
*/
interface Routine {
/**
* Unflattens a shape over multiple dimensions.
*
* @param shape - array shape
* @param dim - dimension to be unflattened
* @param sizes - new shape of the unflattened dimension
* @returns unflattened shape
*
* @example
* var sh = unflattenShape( [ 6, 2, 1 ], 0, [ 3, 2 ] );
* // returns [ 3, 2, 2, 1 ]
*/
( shape: Array<number>, dim: number, sizes: Array<number> ): Array<number>;

/**
* Unflattens a shape over multiple dimensions.
*
* @param shape - array shape
* @param dim - dimension to be unflattened
* @param sizes - new shape of the unflattened dimension
* @param out - output array
* @returns unflattened shape
*
* @example
* var o = [ 0, 0, 0, 0 ];
*
* var out = unflattenShape.assign( [ 6, 2, 1 ], 0, [ 3, 2 ], o );
* // returns [ 3, 2, 2, 1 ]
*
* var bool = ( out === o );
* // returns true
*/
assign( shape: Array<number>, dim: number, sizes: Array<number>, out: Array<number> ): Array<number>;
}

/**
* Unflattens a shape over multiple dimensions.
*
* @param shape - array shape
* @param dim - dimension to be unflattened
* @param sizes - new shape of the unflattened dimension
* @returns unflattened shape
*
* @example
* var sh = unflattenShape( [ 6, 2, 1 ], 0, [ 3, 2 ] );
* // returns [ 3, 2, 2, 1 ]
*
* @example
* var o = [ 0, 0, 0, 0 ];
*
* var out = unflattenShape.assign( [ 6, 2, 1 ], 0, [ 3, 2 ], o );
* // returns [ 3, 2, 2, 1 ]
*
* var bool = ( out === o );
* // returns true
*/
declare var unflattenShape: Routine;


// EXPORTS //

export = unflattenShape;
Loading