Skip to content
Merged
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
138 changes: 138 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,138 @@
<!--

@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

> Expand a dimension 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 )

Expands a dimension 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. 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.

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

Expands a dimension 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. 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.
- **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 ]

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

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

</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,67 @@

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

Parameters
----------
shape: ArrayLike<number>
Array shape.

dim: integer
Dimension to be unflattened. If less than zero, the index is resolved
relative to the last dimension, with the last dimension corresponding to
the value `-1`.

sizes: ArrayLike<number>
New shape of the unflattened dimension.

Returns
-------
out: Array<number>
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 )
Expands a dimension over multiple dimensions and assigns results to a
provided output array.

Parameters
----------
shape: ArrayLike<number>
Array shape.

dim: integer
Dimension to be unflattened. If less than zero, the index is resolved
relative to the last dimension, with the last dimension corresponding to
the value `-1`.

sizes: ArrayLike<number>
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,90 @@
/*
* @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"/>

import { ArrayLike } from '@stdlib/types/array';

/**
* Interface describing `unflattenShape`.
*/
interface Routine {
/**
* Expands a dimension 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: ArrayLike<number>, dim: number, sizes: ArrayLike<number> ): Array<number>;

/**
* Expands a dimension 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<T extends ArrayLike<number> = ArrayLike<number>>( shape: ArrayLike<number>, dim: number, sizes: ArrayLike<number>, out: T ): T;
}

/**
* Expands a dimension 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