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
132 changes: 132 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/reduced-shape/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<!--

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

-->

# reducedShape

> Return a reduced shape of an [ndarray][@stdlib/ndarray/base/ctor].

<!-- 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 reducedShape = require( '@stdlib/ndarray/base/reduced-shape' );
```

#### reducedShape( x, dims )

Returns a reduced shape of an [ndarray][@stdlib/ndarray/base/ctor].

```javascript
var zeros = require( '@stdlib/ndarray/zeros' );

var x = zeros( [ 3, 2, 3 ] );
// returns <ndarray>

var sh = reducedShape( x, [ 0, 1 ] );
// returns [ 3 ]
```

</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" -->

<!-- eslint-disable new-cap -->

```javascript
var zeros = require( '@stdlib/ndarray/zeros' );
var reducedShape = require( '@stdlib/ndarray/base/reduced-shape' );

// Create an array:
var x = zeros( [ 1, 2, 3, 2 ] );
// returns <ndarray>

var sh = reducedShape( x, [ 0, 1 ] );
console.log( sh );
// => [ 3, 2 ]

sh = reducedShape( x, [ 0, 3 ] );
console.log( sh );
// => [ 2, 3 ]

sh = reducedShape( x, [ 1, 3 ] );
console.log( sh );
// => [ 1, 3 ]
```

</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">

[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ctor

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @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 zeros = require( '@stdlib/ndarray/zeros' );
var isCollection = require( '@stdlib/assert/is-collection' );
var pkg = require( './../package.json' ).name;
var reducedShape = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var values;
var dims;
var out;
var i;

values = [
zeros( [ 10, 10, 10, 1 ] ),
zeros( [ 5, 5, 5, 1, 1 ] ),
zeros( [ 3, 4, 5 ] )
];

dims = [
[ 0, 1 ],
[ 0, 2 ],
[ 1 ]
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = reducedShape( values[ i%values.length ], dims[ i%dims.length ] );
if ( typeof out !== 'object' ) {
b.fail( 'should return an array' );
}
}
b.toc();
if ( !isCollection( out ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});
25 changes: 25 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/reduced-shape/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

{{alias}}( x, dims )
Returns a reduced shape of a provided ndarray.

Parameters
----------
x: ndarray
Input ndarray.

dims: Array<integer>
List of dimensions to reduce.

Returns
-------
out: Array<integer>
Reduced shape.

Examples
--------
> var out = {{alias}}( {{alias:@stdlib/ndarray/zeros}}( [ 1, 2, 3, 2 ] ), [ 0, 1 ] )
[ 3, 2 ]

See Also
--------

Original file line number Diff line number Diff line change
@@ -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

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

import { ndarray, Shape } from '@stdlib/types/ndarray';
import { Collection } from '@stdlib/types/array';

/**
* Returns a reduced shape of a provided ndarray.
*
* @param x - input ndarray
* @param dims - list of dimensions to reduce
* @returns reduced shape
*
* @example
* var zeros = require( '@stdlib/ndarray/zeros' );
*
* var x = zeros( [ 1, 2, 3, 2 ] );
* // returns <ndarray>
*
* var sh = reducedShape( x, [ 0, 1 ] );
* // returns [ 3, 2 ]
*/
declare function reducedShape( x: ndarray, dims: Collection<number> ): Shape;


// EXPORTS //

export = reducedShape;
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* @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.
*/

import zeros = require( '@stdlib/ndarray/zeros' );
import reducedShape = require( './index' );


// TESTS //

// The function returns a shape array...
{
reducedShape( zeros( [ 3, 2, 3 ] ), [ 0, 1 ] ); // $ExpectType Shape
}

// The compiler throws an error if the function is provided a first argument which is not an ndarray...
{
reducedShape( '10', [ 0 ] ); // $ExpectError
reducedShape( true, [ 0 ] ); // $ExpectError
reducedShape( false, [ 0 ] ); // $ExpectError
reducedShape( 10, [ 0 ] ); // $ExpectError
reducedShape( null, [ 0 ] ); // $ExpectError
reducedShape( undefined, [ 0 ] ); // $ExpectError
reducedShape( [], [ 0 ] ); // $ExpectError
reducedShape( {}, [ 0 ] ); // $ExpectError
reducedShape( ( x: number ): number => x, [ 0 ] ); // $ExpectError
}

// The compiler throws an error if the function is provided a second argument which is not an array-like object of numbers...
{
reducedShape( zeros( [ 3, 2, 3 ] ), '10' ); // $ExpectError
reducedShape( zeros( [ 3, 2, 3 ] ), true ); // $ExpectError
reducedShape( zeros( [ 3, 2, 3 ] ), false ); // $ExpectError
reducedShape( zeros( [ 3, 2, 3 ] ), 10 ); // $ExpectError
reducedShape( zeros( [ 3, 2, 3 ] ), null ); // $ExpectError
reducedShape( zeros( [ 3, 2, 3 ] ), undefined ); // $ExpectError
reducedShape( zeros( [ 3, 2, 3 ] ), {} ); // $ExpectError
reducedShape( zeros( [ 3, 2, 3 ] ), ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
reducedShape(); // $ExpectError
reducedShape( zeros( [ 3, 2, 3 ] ) ); // $ExpectError
reducedShape( zeros( [ 3, 2, 3 ] ), [ 0 ], {} ); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @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 zeros = require( '@stdlib/ndarray/zeros' );
var reducedShape = require( './../lib' );

// Create an array:
var x = zeros( [ 1, 2, 3, 2 ] );
// returns <ndarray>

var sh = reducedShape( x, [ 0, 1 ] );
console.log( sh );
// => [ 3, 2 ]

sh = reducedShape( x, [ 0, 3 ] );
console.log( sh );
// => [ 2, 3 ]

sh = reducedShape( x, [ 1, 3 ] );
console.log( sh );
// => [ 1, 3 ]
Loading