Skip to content
Draft
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
140 changes: 140 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/permute-dimensions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<!--

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

-->

# permute-dimensions

> Permute the dimensions of an input ndarray according to specified dimension indices.

<!-- 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 permuteDimensions = require( '@stdlib/ndarray/base/permute-dimensions' );
```

#### permuteDimensions( x, dims, writable )

Permutes the dimensions of an input ndarray.

```javascript
var getData = require( '@stdlib/ndarray/data-buffer' );
var array = require( '@stdlib/ndarray/array' );

var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] );
// returns <ndarray>[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]

var y = permuteDimensions( x, [ 1, 0 ], false );
// returns <ndarray>[ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]

var bool = ( getData( x ) === getData( y ) );
// returns true
```

The function accepts the following arguments:

- **x**: input ndarray.
- **dims**: dimension indices defining the new dimension order. Must contain exactly as many unique dimension indices as the number of dimensions in the input ndarray. If a dimension index is provided as an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`.
- **writable**: boolean indicating whether a returned ndarray should be writable.

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

## Notes

- The returned ndarray is a **view** of the input ndarray. Accordingly, writing to the original ndarray will **mutate** the returned ndarray and vice versa.
- Each dimension index must be unique and fall within the interval `[-ndims, ndims-1]`, where `ndims` is the number of dimensions in the input ndarray.
- Operating on views with permuted dimensions can result in performance degradation due to poor cache locality, especially for reductions and operations performed over one or more dimensions on larger arrays.
- The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint-disable stdlib/no-redeclare -->

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

```javascript
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var uniform = require( '@stdlib/random/uniform' );
var permuteDimensions = require( '@stdlib/ndarray/base/permute-dimensions' );

// Create a random array:
var x = uniform( [ 2, 2, 3 ], 0.0, 100.0 );
console.log( ndarray2array( x ) );

// Permute the dimensions:
var y = permuteDimensions( x, [ 2, 0, 1 ], false );
console.log( ndarray2array( y ) );
```

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

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

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

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/**
* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var zeros = require( '@stdlib/ndarray/base/zeros' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var permuteDimensions = require( './../lib' );


// MAIN //

bench( format( '%s::base:dtype=float64', pkg ), function benchmark( b ) {
var x;
var y;
var i;

x = zeros( 'float64', [ 2, 2 ], 'row-major' );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = permuteDimensions( x, [ 1, 0 ], false );
if ( y.length !== 4 ) {
b.fail( 'should have expected length' );
}
}
b.toc();
if ( !isndarrayLike( y ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s::base:dtype=float32', pkg ), function benchmark( b ) {
var x;
var y;
var i;

x = zeros( 'float32', [ 2, 2 ], 'row-major' );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = permuteDimensions( x, [ 1, 0 ], false );
if ( y.length !== 4 ) {
b.fail( 'should have expected length' );
}
}
b.toc();
if ( !isndarrayLike( y ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s::base:dtype=complex128', pkg ), function benchmark( b ) {
var x;
var y;
var i;

x = zeros( 'complex128', [ 2, 2 ], 'row-major' );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = permuteDimensions( x, [ 1, 0 ], false );
if ( y.length !== 4 ) {
b.fail( 'should have expected length' );
}
}
b.toc();
if ( !isndarrayLike( y ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s::base:dtype=int32', pkg ), function benchmark( b ) {
var x;
var y;
var i;

x = zeros( 'int32', [ 2, 2 ], 'row-major' );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = permuteDimensions( x, [ 1, 0 ], false );
if ( y.length !== 4 ) {
b.fail( 'should have expected length' );
}
}
b.toc();
if ( !isndarrayLike( y ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s::base:dtype=generic', pkg ), function benchmark( b ) {
var x;
var y;
var i;

x = zeros( 'generic', [ 2, 2 ], 'row-major' );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = permuteDimensions( x, [ 1, 0 ], false );
if ( y.length !== 4 ) {
b.fail( 'should have expected length' );
}
}
b.toc();
if ( !isndarrayLike( y ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* @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 pow = require( '@stdlib/math/base/special/pow' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var zeros = require( '@stdlib/ndarray/base/zeros' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var permuteDimensions = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = zeros( 'complex128', [ 10, len/10 ], 'row-major' );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var arr;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr = permuteDimensions( x, [ 1, 0 ], false );
if ( arr.length !== len ) {
b.fail( 'unexpected length' );
}
}
b.toc();
if ( !isndarrayLike( arr ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( format( '%s::base:dtype=complex128,size=%d', pkg, len ), f );
}
}

main();
Loading