diff --git a/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/README.md b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/README.md new file mode 100644 index 000000000000..3f821e9da33b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/README.md @@ -0,0 +1,140 @@ + + +# permute-dimensions + +> Permute the dimensions of an input ndarray according to specified dimension indices. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var permuteDimensions = require( '@stdlib/ndarray/base/permute-dimensions' ); +``` + +#### permuteDimensions( x, dims, writable ) + +Permutes the dimensions of an input ndarray according to specified dimension indices. + +```javascript +var getData = require( '@stdlib/ndarray/data-buffer' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ); +// returns [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] + +var y = permuteDimensions( x, [ 1, 0 ], false ); +// returns [ [ 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. + +
+ + + + + +
+ +## Notes + +- The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances. +- 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 returned ndarray is a **view** of the input ndarray. Accordingly, writing to the original ndarray will **mutate** the returned ndarray and vice versa. + +
+ + + + + +
+ +## Examples + + + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +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 ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.js new file mode 100644 index 000000000000..e6cccb872d8e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.js @@ -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(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.size.complex128.js b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.size.complex128.js new file mode 100644 index 000000000000..933de4003db5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.size.complex128.js @@ -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(); diff --git a/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.size.float32.js b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.size.float32.js new file mode 100644 index 000000000000..c798f67ecf8b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.size.float32.js @@ -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( 'float32', [ 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=float32,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.size.float64.js b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.size.float64.js new file mode 100644 index 000000000000..e620cea3fa69 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.size.float64.js @@ -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( 'float64', [ 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=float64,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.size.generic.js b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.size.generic.js new file mode 100644 index 000000000000..335bc284789b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.size.generic.js @@ -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( 'generic', [ 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=generic,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.size.int32.js b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.size.int32.js new file mode 100644 index 000000000000..497d3b5dd536 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/benchmark/benchmark.size.int32.js @@ -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( 'int32', [ 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=int32,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/docs/repl.txt new file mode 100644 index 000000000000..20292c81b57c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/docs/repl.txt @@ -0,0 +1,42 @@ + +{{alias}}( x, dims, writable ) + Permutes the dimensions of an input ndarray according to specified dimension + indices. + + The returned ndarray is a *view* of the input ndarray. Accordingly, writing + to the original ndarray will mutate the returned ndarray and vice versa. + + The `writable` parameter only applies to ndarray constructors supporting + read-only instances. + + 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. + + Parameters + ---------- + x: ndarray + Input array. + + dims: ArrayLike + Dimension indices defining the new dimension order. 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. + + writable: boolean + Boolean indicating whether the returned ndarray should be writable. + + 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, [ 1, 0 ], false ) + [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/docs/types/index.d.ts new file mode 100644 index 000000000000..4758489e1394 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/docs/types/index.d.ts @@ -0,0 +1,76 @@ +/* +* @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, typedndarray } from '@stdlib/types/ndarray'; + +/** +* Permutes the dimensions of an ndarray according to specified dimension indices. +* +* @param x - input array +* @param dims - dimension indices defining the new dimension order +* @param writable - boolean indicating whether the returned ndarray should be writable +* @returns ndarray view +* +* @example +* var getData = require( '@stdlib/ndarray/data-buffer' ); +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ); +* // returns [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] +* +* var y = permuteDimensions( x, [ 1, 0 ], false ); +* // returns [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ] +* +* var bool = ( getData( x ) === getData( y ) ); +* // returns true +*/ +declare function permuteDimensions = typedndarray>( x: T, dims: Array, writable: boolean ): T; + +/** +* Permutes the dimensions of an ndarray according to specified dimension indices. +* +* @param x - input array +* @param dims - dimension indices defining the new dimension order +* @param writable - boolean indicating whether the returned ndarray should be writable +* @returns ndarray view +* +* @example +* var getData = require( '@stdlib/ndarray/data-buffer' ); +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], { +* 'dtype': 'generic' +* }); +* // returns [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] +* +* var y = permuteDimensions( x, [ 1, 0 ], false ); +* // returns [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ] +* +* var bool = ( getData( x ) === getData( y ) ); +* // returns true +*/ +declare function permuteDimensions( x: ndarray, dims: Array, writable: boolean ): ndarray; + + +// EXPORTS // + +export = permuteDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/docs/types/test.ts new file mode 100644 index 000000000000..b8029d86eb6b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/docs/types/test.ts @@ -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. +*/ + +import zeros = require( '@stdlib/ndarray/base/zeros' ); +import permuteDimensions = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const sh = [ 2, 2 ]; + const ord = 'row-major'; + + permuteDimensions( zeros( 'float64', sh, ord ), [ 0, 1 ], false ); // $ExpectType float64ndarray + permuteDimensions( zeros( 'float32', sh, ord ), [ 0, 1 ], false ); // $ExpectType float32ndarray + permuteDimensions( zeros( 'complex128', sh, ord ), [ 0, 1 ], false ); // $ExpectType complex128ndarray + permuteDimensions( zeros( 'complex64', sh, ord ), [ 0, 1 ], false ); // $ExpectType complex64ndarray + permuteDimensions( zeros( 'int32', sh, ord ), [ 0, 1 ], false ); // $ExpectType int32ndarray + permuteDimensions( zeros( 'int16', sh, ord ), [ 0, 1 ], false ); // $ExpectType int16ndarray + permuteDimensions( zeros( 'int8', sh, ord ), [ 0, 1 ], false ); // $ExpectType int8ndarray + permuteDimensions( zeros( 'uint32', sh, ord ), [ 0, 1 ], false ); // $ExpectType uint32ndarray + permuteDimensions( zeros( 'uint16', sh, ord ), [ 0, 1 ], false ); // $ExpectType uint16ndarray + permuteDimensions( zeros( 'uint8', sh, ord ), [ 0, 1 ], false ); // $ExpectType uint8ndarray + permuteDimensions( zeros( 'uint8c', sh, ord ), [ 0, 1 ], false ); // $ExpectType uint8cndarray + permuteDimensions( zeros( 'generic', sh, ord ), [ 0, 1 ], false ); // $ExpectType genericndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + permuteDimensions( '10', [ 0, 1 ], false ); // $ExpectError + permuteDimensions( 10, [ 0, 1 ], false ); // $ExpectError + permuteDimensions( false, [ 0, 1 ], false ); // $ExpectError + permuteDimensions( true, [ 0, 1 ], false ); // $ExpectError + permuteDimensions( null, [ 0, 1 ], false ); // $ExpectError + permuteDimensions( void 0, [ 0, 1 ], false ); // $ExpectError + permuteDimensions( [], [ 0, 1 ], false ); // $ExpectError + permuteDimensions( {}, [ 0, 1 ], false ); // $ExpectError + permuteDimensions( ( x: number ): number => x, [ 0, 1 ], false ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a collection of numbers... +{ + const x = zeros( 'float64', [ 2, 2 ], 'row-major' ); + + permuteDimensions( x, '10', false ); // $ExpectError + permuteDimensions( x, 10, false ); // $ExpectError + permuteDimensions( x, false, false ); // $ExpectError + permuteDimensions( x, true, false ); // $ExpectError + permuteDimensions( x, null, false ); // $ExpectError + permuteDimensions( x, void 0, false ); // $ExpectError + permuteDimensions( x, {}, false ); // $ExpectError + permuteDimensions( x, ( x: number ): number => x, false ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a boolean... +{ + const x = zeros( 'float64', [ 2, 2 ], 'row-major' ); + + permuteDimensions( x, [ 0, 1 ], '10' ); // $ExpectError + permuteDimensions( x, [ 0, 1 ], 10 ); // $ExpectError + permuteDimensions( x, [ 0, 1 ], null ); // $ExpectError + permuteDimensions( x, [ 0, 1 ], void 0 ); // $ExpectError + permuteDimensions( x, [ 0, 1 ], [] ); // $ExpectError + permuteDimensions( x, [ 0, 1 ], {} ); // $ExpectError + permuteDimensions( x, [ 0, 1 ], ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + permuteDimensions(); // $ExpectError + permuteDimensions( zeros( 'float64', [ 2, 2 ], 'row-major' ) ); // $ExpectError + permuteDimensions( zeros( 'float64', [ 2, 2 ], 'row-major' ), [ 0, 1 ] ); // $ExpectError + permuteDimensions( zeros( 'float64', [ 2, 2 ], 'row-major' ), [ 0, 1 ], false, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/examples/index.js new file mode 100644 index 000000000000..564450ffe9b6 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/examples/index.js @@ -0,0 +1,31 @@ +/** +* @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 ndarray2array = require( '@stdlib/ndarray/to-array' ); +var uniform = require( '@stdlib/random/uniform' ); +var permuteDimensions = require( './../lib' ); + +// 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 ) ); diff --git a/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/lib/index.js new file mode 100644 index 000000000000..a9b98a4f4881 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/lib/index.js @@ -0,0 +1,48 @@ +/** +* @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'; + +/** +* Permute the dimensions of an input ndarray according to specified dimension indices. +* +* @module @stdlib/ndarray/base/permute-dimensions +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var getData = require( '@stdlib/ndarray/data-buffer' ); +* var permuteDimensions = require( '@stdlib/ndarray/base/permute-dimensions' ); +* +* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ); +* // returns [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] +* +* var y = permuteDimensions( x, [ 1, 0 ], false ); +* // returns [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ] +* +* var bool = ( getData( x ) === getData( y ) ); +* // returns true +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/lib/main.js new file mode 100644 index 000000000000..5b2782280036 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/lib/main.js @@ -0,0 +1,107 @@ +/** +* @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 toUniqueNormalizedIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var getStrides = require( '@stdlib/ndarray/base/strides' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var take = require( '@stdlib/array/base/take-indexed' ); +var getDType = require( '@stdlib/ndarray/base/dtype' ); +var getShape = require( '@stdlib/ndarray/base/shape' ); +var getOrder = require( '@stdlib/ndarray/base/order' ); +var format = require( '@stdlib/string/format' ); +var join = require( '@stdlib/array/base/join' ); + + +// MAIN // + +/** +* Permutes the dimensions of an input ndarray according to specified dimension indices. +* +* ## Notes +* +* - 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. +* +* @param {ndarray} x - input array +* @param {IntegerArray} dims - dimension indices defining the new dimension order +* @param {boolean} writable - boolean indicating whether the returned ndarray should be writable +* @throws {RangeError} input ndarray must have one or more dimensions +* @throws {RangeError} must provide dimension indices equal to the number of dimensions in the input ndarray +* @throws {RangeError} must provide valid dimension indices +* @throws {Error} must provide unique dimension indices +* @returns {ndarray} ndarray view +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var getData = require( '@stdlib/ndarray/data-buffer' ); +* var permuteDimensions = require( '@stdlib/ndarray/base/permute-dimensions' ); +* +* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ); +* // returns [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] +* +* var y = permuteDimensions( x, [ 1, 0 ], false ); +* // returns [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ] +* +* var bool = ( getData( x ) === getData( y ) ); +* // returns true +*/ +function permuteDimensions( x, dims, writable ) { + var st1; + var sh1; + var sh; + var st; + var d; + var N; + + sh = getShape( x, true ); + N = sh.length; + + if ( N === 0 ) { + throw new RangeError( format( 'invalid argument. First argument must be an ndarray having one or more dimensions. Number of dimensions: %d.', N ) ); + } + if ( dims.length !== N ) { + throw new RangeError( format( 'invalid argument. Must provide dimension indices equal to the number of dimensions in the input ndarray. Number of dimensions: %d. Number of dimension indices: %d.', N, dims.length ) ); + } + st = getStrides( x, true ); + + // Resolve and normalize dimension indices... + d = toUniqueNormalizedIndices( dims, N-1 ); + if ( d === null ) { + throw new RangeError( format( 'invalid argument. Specified dimension index is out-of-bounds. Must be on the interval: [-%u, %u]. Value: `[%s]`.', N, N-1, join( dims, ', ' ) ) ); + } + if ( d.length !== dims.length ) { + throw new Error( format( 'invalid argument. Must provide unique dimension indices. Value: `[%s]`.', join( dims, ', ' ) ) ); + } + + // Permute shape and strides... + sh1 = take( sh, d ); + st1 = take( st, d ); + + return new x.constructor( getDType( x ), getData( x ), sh1, st1, getOffset( x ), getOrder( x ), { // eslint-disable-line max-len + 'readonly': !writable + }); +} + + +// EXPORTS // + +module.exports = permuteDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/package.json b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/package.json new file mode 100644 index 000000000000..de5572c791f6 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/ndarray/base/permute-dimensions", + "version": "0.0.0", + "description": "Permute the dimensions of an ndarray.", + "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", + "data", + "structure", + "ndarray", + "permute", + "dimensions", + "axis", + "axes" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/test/test.js b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/test/test.js new file mode 100644 index 000000000000..0cd360c80b74 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/permute-dimensions/test/test.js @@ -0,0 +1,329 @@ +/** +* @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 Float64Array = require( '@stdlib/array/float64' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +var base = require( '@stdlib/ndarray/base/ctor' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getStrides = require( '@stdlib/ndarray/strides' ); +var permuteDimensions = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof permuteDimensions, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided an ndarray with zero dimensions', function test( t ) { + var x; + + x = new ndarray( 'float64', [], [], [ 0 ], 0, 'row-major' ); + t.throws( badValue( x ), RangeError, 'throws an error when provided zero-dimensional array' ); + t.end(); + + function badValue( value ) { + return function badValue() { + permuteDimensions( value, [], false ); + }; + } +}); + +tape( 'the function throws an error if dims length does not match number of dimensions', function test( t ) { + var values; + var x; + var i; + + x = new base( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ), [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + values = [ + [], + [ 0 ], + [ 0, 1, 2 ] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided dims with length '+values[ i ].length ); + } + t.end(); + + function badValue( dims ) { + return function badValue() { + permuteDimensions( x, dims, false ); + }; + } +}); + +tape( 'the function throws an error if provided out-of-bounds dimension indices', function test( t ) { + var values; + var x; + var i; + + x = new base( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ), [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + values = [ + [ 2, 0 ], + [ 0, 2 ], + [ -3, 0 ], + [ 0, -3 ] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided out-of-bounds dimension index' ); + } + t.end(); + + function badValue( dims ) { + return function badValue() { + permuteDimensions( x, dims, false ); + }; + } +}); + +tape( 'the function throws an error if provided duplicate dimension indices', function test( t ) { + var values; + var x; + var i; + + x = new base( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ), [ 2, 2, 3 ], [ 6, 3, 1 ], 0, 'row-major' ); + + values = [ + [ 0, 0, 1 ], + [ 0, 1, 0 ], + [ 1, 1, 2 ], + [ 2, 2, 0 ] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided duplicate dimension indices' ); + } + t.end(); + + function badValue( dims ) { + return function badValue() { + permuteDimensions( x, dims, false ); + }; + } +}); + +tape( 'the function permutes the dimensions of a 2D matrix (dtype=float64, base)', function test( t ) { + var arr; + var buf; + var x; + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + x = new base( 'float64', buf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + arr = permuteDimensions( x, [ 1, 0 ], false ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), String( getDType( x ) ), 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 3, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( arr ), [ 1, 3 ], 'returns expected value' ); + t.strictEqual( getData( arr ), getData( x ), 'returns expected value' ); + t.strictEqual( getOrder( arr ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( arr ), [ [ 1.0, 4.0 ], [ 2.0, 5.0 ], [ 3.0, 6.0 ] ], 'returns expected value' ); + + t.end(); +}); + +tape( 'the function permutes the dimensions of a 2D matrix (dtype=float64, base, column-major)', function test( t ) { + var arr; + var buf; + var x; + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + x = new base( 'float64', buf, [ 2, 3 ], [ 1, 2 ], 0, 'column-major' ); + arr = permuteDimensions( x, [ 1, 0 ], false ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), String( getDType( x ) ), 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 3, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( arr ), [ 2, 1 ], 'returns expected value' ); + t.strictEqual( getData( arr ), getData( x ), 'returns expected value' ); + t.strictEqual( getOrder( arr ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( arr ), [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ], 'returns expected value' ); + + t.end(); +}); + +tape( 'the function permutes the dimensions of a 2D matrix (dtype=float64, non-base)', function test( t ) { + var arr; + var buf; + var x; + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + x = array( buf, { + 'shape': [ 2, 3 ], + 'dtype': 'float64' + }); + arr = permuteDimensions( x, [ 1, 0 ], false ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), String( getDType( x ) ), 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 3, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( arr ), [ 1, 3 ], 'returns expected value' ); + t.strictEqual( getData( arr ), getData( x ), 'returns expected value' ); + t.strictEqual( getOrder( arr ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( arr ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( arr ), [ [ 1.0, 4.0 ], [ 2.0, 5.0 ], [ 3.0, 6.0 ] ], 'returns expected value' ); + + arr = permuteDimensions( x, [ 1, 0 ], true ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), String( getDType( x ) ), 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 3, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( arr ), [ 1, 3 ], 'returns expected value' ); + t.strictEqual( getData( arr ), getData( x ), 'returns expected value' ); + t.strictEqual( getOrder( arr ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( arr ), false, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function permutes the dimensions of a 3D matrix (dtype=float64, base, row-major)', function test( t ) { + var arr; + var buf; + var x; + + buf = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 7.0, + 8.0, + 9.0, + 10.0, + 11.0, + 12.0 + ]); + x = new base( 'float64', buf, [ 2, 2, 3 ], [ 6, 3, 1 ], 0, 'row-major' ); + + arr = permuteDimensions( x, [ 0, 2, 1 ], false ); + t.deepEqual( getShape( arr ), [ 2, 3, 2 ], 'returns expected value' ); + + arr = permuteDimensions( x, [ 1, 0, 2 ], false ); + t.deepEqual( getShape( arr ), [ 2, 2, 3 ], 'returns expected value' ); + + arr = permuteDimensions( x, [ 2, 1, 0 ], false ); + t.deepEqual( getShape( arr ), [ 3, 2, 2 ], 'returns expected value' ); + + t.end(); +}); + +tape( 'the function permutes the dimensions of a 3D matrix (dtype=float64, base, column-major)', function test( t ) { + var arr; + var buf; + var x; + + buf = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 7.0, + 8.0, + 9.0, + 10.0, + 11.0, + 12.0 + ]); + x = new base( 'float64', buf, [ 2, 2, 3 ], [ 1, 2, 4 ], 0, 'column-major' ); + + arr = permuteDimensions( x, [ 0, 2, 1 ], false ); + t.deepEqual( getShape( arr ), [ 2, 3, 2 ], 'returns expected value' ); + + arr = permuteDimensions( x, [ 1, 0, 2 ], false ); + t.deepEqual( getShape( arr ), [ 2, 2, 3 ], 'returns expected value' ); + + arr = permuteDimensions( x, [ 2, 1, 0 ], false ); + t.deepEqual( getShape( arr ), [ 3, 2, 2 ], 'returns expected value' ); + + t.end(); +}); + +tape( 'the function permutes the dimensions of a 3D matrix (dtype=float64, non-base)', function test( t ) { + var arr; + var buf; + var x; + + buf = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 7.0, + 8.0, + 9.0, + 10.0, + 11.0, + 12.0 + ]); + x = array( buf, { + 'shape': [ 2, 2, 3 ], + 'dtype': 'float64' + }); + + arr = permuteDimensions( x, [ 0, 2, 1 ], false ); + t.deepEqual( getShape( arr ), [ 2, 3, 2 ], 'returns expected value' ); + t.strictEqual( isReadOnly( arr ), true, 'returns expected value' ); + + arr = permuteDimensions( x, [ 1, 0, 2 ], true ); + t.deepEqual( getShape( arr ), [ 2, 2, 3 ], 'returns expected value' ); + t.strictEqual( isReadOnly( arr ), false, 'returns expected value' ); + + arr = permuteDimensions( x, [ 2, 1, 0 ], false ); + t.deepEqual( getShape( arr ), [ 3, 2, 2 ], 'returns expected value' ); + t.strictEqual( isReadOnly( arr ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing negative dimension indices', function test( t ) { + var arr; + var buf; + var x; + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + x = new base( 'float64', buf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + arr = permuteDimensions( x, [ -1, -2 ], false ); + t.deepEqual( getShape( arr ), [ 3, 2 ], 'returns expected value' ); + + t.end(); +});