From 543339e4f92674f75665557b0cd9a46180a9a6f8 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 10 May 2026 03:01:45 +0500 Subject: [PATCH 1/5] feat: add ndarray/to-transposed --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/to-transposed/README.md | 131 +++++++++++++ .../to-transposed/benchmark/benchmark.js | 173 +++++++++++++++++ .../ndarray/to-transposed/docs/repl.txt | 24 +++ .../to-transposed/docs/types/index.d.ts | 45 +++++ .../ndarray/to-transposed/docs/types/test.ts | 54 ++++++ .../ndarray/to-transposed/examples/index.js | 29 +++ .../ndarray/to-transposed/lib/index.js | 44 +++++ .../@stdlib/ndarray/to-transposed/lib/main.js | 71 +++++++ .../ndarray/to-transposed/package.json | 74 ++++++++ .../ndarray/to-transposed/test/test.js | 179 ++++++++++++++++++ 10 files changed, 824 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/to-transposed/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/to-transposed/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/to-transposed/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/to-transposed/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/to-transposed/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/to-transposed/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/to-transposed/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/to-transposed/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/to-transposed/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/to-transposed/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/to-transposed/README.md b/lib/node_modules/@stdlib/ndarray/to-transposed/README.md new file mode 100644 index 000000000000..adde4c371f3f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-transposed/README.md @@ -0,0 +1,131 @@ + + +# toTransposed + +> Return a new [`ndarray`][@stdlib/ndarray/ctor] containing the elements of an input [`ndarray`][@stdlib/ndarray/ctor] but whose last two dimensions are transposed. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var toTransposed = require( '@stdlib/ndarray/to-transposed' ); +``` + +#### toTransposed( x ) + +Returns a new [`ndarray`][@stdlib/ndarray/ctor] containing the elements of an input [`ndarray`][@stdlib/ndarray/ctor] but whose last two dimensions are transposed. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ); +// returns [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] + +var y = toTransposed( x ); +// returns [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ] +``` + +The function accepts the following arguments: + +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. + +
+ + + + + +
+ +## Notes + +- If provided an [`ndarray`][@stdlib/ndarray/ctor] having fewer than two dimensions, the function raises an exception. + +
+ + + + + +
+ +## Examples + + + + + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var toTransposed = require( '@stdlib/ndarray/to-transposed' ); + +var x = array( [ [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], [ [ 7, 8, 9 ], [ 10, 11, 12 ] ] ] ); +console.log( ndarray2array( x ) ); + +var y = toTransposed( x ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/to-transposed/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/to-transposed/benchmark/benchmark.js new file mode 100644 index 000000000000..317901ea4b1a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-transposed/benchmark/benchmark.js @@ -0,0 +1,173 @@ +/** +* @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 Float64Array = require( '@stdlib/array/float64' ); +var ndarrayBase = require( '@stdlib/ndarray/base/ctor' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var toTransposed = require( './../lib' ); + + +// MAIN // + +bench( format( '%s:ctor=base,ndims=2', pkg ), function benchmark( b ) { + var strides; + var buffer; + var offset; + var dtype; + var order; + var shape; + var out; + var i; + var x; + + dtype = 'float64'; + buffer = new Float64Array( 24 ); + shape = [ 4, 6 ]; + strides = [ 6, 1 ]; + offset = 0; + order = 'row-major'; + + x = ndarrayBase( dtype, buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = toTransposed( x ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:ctor=ndarray,ndims=2', pkg ), function benchmark( b ) { + var strides; + var buffer; + var offset; + var dtype; + var order; + var shape; + var out; + var i; + var x; + + dtype = 'float64'; + buffer = new Float64Array( 24 ); + shape = [ 4, 6 ]; + strides = [ 6, 1 ]; + offset = 0; + order = 'row-major'; + + x = ndarray( dtype, buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = toTransposed( x ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:ctor=base,ndims=3', pkg ), function benchmark( b ) { + var strides; + var buffer; + var offset; + var dtype; + var order; + var shape; + var out; + var i; + var x; + + dtype = 'float64'; + buffer = new Float64Array( 24 ); + shape = [ 2, 4, 3 ]; + strides = [ 12, 3, 1 ]; + offset = 0; + order = 'row-major'; + + x = ndarrayBase( dtype, buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = toTransposed( x ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:ctor=ndarray,ndims=3', pkg ), function benchmark( b ) { + var strides; + var buffer; + var offset; + var dtype; + var order; + var shape; + var out; + var i; + var x; + + dtype = 'float64'; + buffer = new Float64Array( 24 ); + shape = [ 2, 4, 3 ]; + strides = [ 12, 3, 1 ]; + offset = 0; + order = 'row-major'; + + x = ndarray( dtype, buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = toTransposed( x ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/to-transposed/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/to-transposed/docs/repl.txt new file mode 100644 index 000000000000..f22f43ef8470 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-transposed/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( x ) + Returns a new ndarray containing the elements of an input ndarray but whose + last two dimensions are transposed. + + Parameters + ---------- + x: ndarray + Input array. + + 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, 4 ], [ 2, 5 ], [ 3, 6 ] ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/to-transposed/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/to-transposed/docs/types/index.d.ts new file mode 100644 index 000000000000..830c1b9edce3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-transposed/docs/types/index.d.ts @@ -0,0 +1,45 @@ +/* +* @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 } from '@stdlib/types/ndarray'; + +/** +* Returns a new ndarray containing the elements of an input ndarray but whose last two dimensions are transposed. +* +* @param x - input array +* @returns output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ); +* // returns [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] +* +* var y = toTransposed( x ); +* // returns [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ] +*/ +declare function toTransposed( x: T ): T; + + +// EXPORTS // + +export = toTransposed; diff --git a/lib/node_modules/@stdlib/ndarray/to-transposed/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/to-transposed/docs/types/test.ts new file mode 100644 index 000000000000..977dee2d20ed --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-transposed/docs/types/test.ts @@ -0,0 +1,54 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import toTransposed = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 3 ], { + 'dtype': 'float64' + }); + + toTransposed( x ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is not provided a first argument which is an ndarray... +{ + toTransposed( true ); // $ExpectError + toTransposed( false ); // $ExpectError + toTransposed( null ); // $ExpectError + toTransposed( undefined ); // $ExpectError + toTransposed( '5' ); // $ExpectError + toTransposed( [ '1', '2' ] ); // $ExpectError + toTransposed( {} ); // $ExpectError + toTransposed( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 3 ] ); + + toTransposed(); // $ExpectError + toTransposed( x, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/to-transposed/examples/index.js b/lib/node_modules/@stdlib/ndarray/to-transposed/examples/index.js new file mode 100644 index 000000000000..2d71af37dd7f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-transposed/examples/index.js @@ -0,0 +1,29 @@ +/** +* @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 array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var toTransposed = require( './../lib' ); + +var x = array( [ [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], [ [ 7, 8, 9 ], [ 10, 11, 12 ] ] ] ); +console.log( ndarray2array( x ) ); + +var y = toTransposed( x ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/to-transposed/lib/index.js b/lib/node_modules/@stdlib/ndarray/to-transposed/lib/index.js new file mode 100644 index 000000000000..56591282a4cc --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-transposed/lib/index.js @@ -0,0 +1,44 @@ +/** +* @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'; + +/** +* Return a new ndarray containing the elements of an input ndarray but whose last two dimensions are transposed. +* +* @module @stdlib/ndarray/to-transposed +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var toTransposed = require( '@stdlib/ndarray/to-transposed' ); +* +* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ); +* // returns [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] +* +* var y = toTransposed( x ); +* // returns [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/to-transposed/lib/main.js b/lib/node_modules/@stdlib/ndarray/to-transposed/lib/main.js new file mode 100644 index 000000000000..e3a0b481e795 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-transposed/lib/main.js @@ -0,0 +1,71 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var ndims = require( '@stdlib/ndarray/ndims' ); +var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' ); +var transpose = require( '@stdlib/ndarray/base/transpose' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); +var assign = require( '@stdlib/ndarray/base/assign' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a new ndarray containing the elements of an input ndarray but whose last two dimensions are transposed. +* +* @param {ndarray} x - input array +* @throws {TypeError} first argument must be an ndarray +* @throws {RangeError} must provide an ndarray having two or more dimensions +* @returns {ndarray} output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ); +* // returns [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] +* +* var y = toTransposed( x ); +* // returns [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ] +*/ +function toTransposed( x ) { + var out; + var bv; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + if ( ndims( x ) < 2 ) { + throw new RangeError( 'invalid argument. Must provide an ndarray having two or more dimensions.' ); + } + bv = ndarraylike2ndarray( x ); + bv = transpose( bv, false ); + out = emptyLike( bv ); + assign( [ bv, out ] ); + return out; +} + + +// EXPORTS // + +module.exports = toTransposed; diff --git a/lib/node_modules/@stdlib/ndarray/to-transposed/package.json b/lib/node_modules/@stdlib/ndarray/to-transposed/package.json new file mode 100644 index 000000000000..e9a915439e50 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-transposed/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/ndarray/to-transposed", + "version": "0.0.0", + "description": "Return a new ndarray containing the elements of an input ndarray but whose last two dimensions are transposed.", + "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", + "data", + "structure", + "ndarray", + "matrix", + "transpose", + "transposed", + "to-transposed", + "stack", + "permute", + "swap", + "swapaxes", + "dimension", + "multidimensional", + "array", + "utilities", + "utility", + "utils", + "util" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/to-transposed/test/test.js b/lib/node_modules/@stdlib/ndarray/to-transposed/test/test.js new file mode 100644 index 000000000000..fd761893c0ea --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-transposed/test/test.js @@ -0,0 +1,179 @@ +/** +* @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 isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' ); +var zeroTo = require( '@stdlib/blas/ext/zero-to' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var toTransposed = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof toTransposed, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + toTransposed( value ); + }; + } +}); + +tape( 'the function throws an error if provided an ndarray having fewer than two dimensions', function test( t ) { + var values; + var opts; + var i; + + opts = { + 'dtype': 'float64' + }; + + values = [ + zeros( [], opts ), + zeros( [ 6 ], opts ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + toTransposed( value ); + }; + } +}); + +tape( 'the function returns a new ndarray containing the elements of an input ndarray but whose last two dimensions are transposed (2D)', function test( t ) { + var expected; + var opts; + var x; + var y; + + opts = { + 'dtype': 'generic', + 'dims': [ 0, 1 ] + }; + x = zeroTo( [ 2, 3 ], opts ); + + expected = [ + [ 0, 3 ], + [ 1, 4 ], + [ 2, 5 ] + ]; + + y = toTransposed( x ); + + t.deepEqual( getShape( y ), [ 3, 2 ], 'returns expected shape' ); + t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected dtype' ); + t.notEqual( getData( y ), getData( x ), 'does not share the same data buffer' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray containing the elements of an input ndarray but whose last two dimensions are transposed (stack of matrices)', function test( t ) { + var expected; + var opts; + var x; + var y; + + opts = { + 'dtype': 'generic', + 'dims': [ 0, 1, 2 ] + }; + x = zeroTo( [ 2, 2, 3 ], opts ); + + expected = [ + [ [ 0, 3 ], [ 1, 4 ], [ 2, 5 ] ], + [ [ 6, 9 ], [ 7, 10 ], [ 8, 11 ] ] + ]; + + y = toTransposed( x ); + + t.deepEqual( getShape( y ), [ 2, 3, 2 ], 'returns expected shape' ); + t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected dtype' ); + t.notEqual( getData( y ), getData( x ), 'does not share the same data buffer' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports column-major order', function test( t ) { + var expected; + var opts; + var x; + var y; + + opts = { + 'dtype': 'generic', + 'order': 'column-major', + 'dims': [ 0, 1 ] + }; + x = zeroTo( [ 2, 3 ], opts ); + + expected = [ + [ 0, 1 ], + [ 2, 3 ], + [ 4, 5 ] + ]; + + y = toTransposed( x ); + + t.deepEqual( getShape( y ), [ 3, 2 ], 'returns expected shape' ); + t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected dtype' ); + t.notEqual( getData( y ), getData( x ), 'does not share the same data buffer' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); From cab60dc35e026f5b8aeb45501b46c11bf1004f60 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 10 May 2026 17:34:08 -0700 Subject: [PATCH 2/5] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- lib/node_modules/@stdlib/ndarray/to-transposed/README.md | 3 ++- .../@stdlib/ndarray/to-transposed/docs/repl.txt | 5 +++++ .../@stdlib/ndarray/to-transposed/docs/types/index.d.ts | 4 ++++ lib/node_modules/@stdlib/ndarray/to-transposed/lib/main.js | 7 ++++++- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/to-transposed/README.md b/lib/node_modules/@stdlib/ndarray/to-transposed/README.md index adde4c371f3f..cfbd27ce49c1 100644 --- a/lib/node_modules/@stdlib/ndarray/to-transposed/README.md +++ b/lib/node_modules/@stdlib/ndarray/to-transposed/README.md @@ -68,7 +68,8 @@ The function accepts the following arguments: ## Notes -- If provided an [`ndarray`][@stdlib/ndarray/ctor] having fewer than two dimensions, the function raises an exception. +- The function operates on a stack of matrices, transposing the last two dimensions of the input ndarray. +- The input ndarray must have at least two dimensions. diff --git a/lib/node_modules/@stdlib/ndarray/to-transposed/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/to-transposed/docs/repl.txt index f22f43ef8470..6646f269c237 100644 --- a/lib/node_modules/@stdlib/ndarray/to-transposed/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/to-transposed/docs/repl.txt @@ -3,6 +3,11 @@ Returns a new ndarray containing the elements of an input ndarray but whose last two dimensions are transposed. + The function operates on a stack of matrices, transposing the last two + dimensions of the input ndarray. + + The input ndarray must have at least two dimensions. + Parameters ---------- x: ndarray diff --git a/lib/node_modules/@stdlib/ndarray/to-transposed/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/to-transposed/docs/types/index.d.ts index 830c1b9edce3..dfe6adf1990e 100644 --- a/lib/node_modules/@stdlib/ndarray/to-transposed/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/to-transposed/docs/types/index.d.ts @@ -25,6 +25,10 @@ import { ndarray } from '@stdlib/types/ndarray'; /** * Returns a new ndarray containing the elements of an input ndarray but whose last two dimensions are transposed. * +* ## Notes +* +* - The function operates on a stack of matrices, transposing the last two dimensions of the input ndarray. +* - The input ndarray must have at least two dimensions. * @param x - input array * @returns output ndarray * diff --git a/lib/node_modules/@stdlib/ndarray/to-transposed/lib/main.js b/lib/node_modules/@stdlib/ndarray/to-transposed/lib/main.js index e3a0b481e795..289d8d6f4306 100644 --- a/lib/node_modules/@stdlib/ndarray/to-transposed/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/to-transposed/lib/main.js @@ -21,7 +21,7 @@ // MODULES // var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); -var ndims = require( '@stdlib/ndarray/ndims' ); +var ndims = require( '@stdlib/ndarray/base/ndims' ); var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' ); var transpose = require( '@stdlib/ndarray/base/transpose' ); var emptyLike = require( '@stdlib/ndarray/empty-like' ); @@ -34,6 +34,11 @@ var format = require( '@stdlib/string/format' ); /** * Returns a new ndarray containing the elements of an input ndarray but whose last two dimensions are transposed. * +* ## Notes +* +* - The function operates on a stack of matrices, transposing the last two dimensions of the input ndarray. +* - The input ndarray must have at least two dimensions. +* * @param {ndarray} x - input array * @throws {TypeError} first argument must be an ndarray * @throws {RangeError} must provide an ndarray having two or more dimensions From 1dad0f49b63e5de340d6aad2ab825e6103452769 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 10 May 2026 17:34:33 -0700 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- .../@stdlib/ndarray/to-transposed/docs/types/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/node_modules/@stdlib/ndarray/to-transposed/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/to-transposed/docs/types/index.d.ts index dfe6adf1990e..17a735ffabfc 100644 --- a/lib/node_modules/@stdlib/ndarray/to-transposed/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/to-transposed/docs/types/index.d.ts @@ -29,6 +29,7 @@ import { ndarray } from '@stdlib/types/ndarray'; * * - The function operates on a stack of matrices, transposing the last two dimensions of the input ndarray. * - The input ndarray must have at least two dimensions. +* * @param x - input array * @returns output ndarray * From 05c8cf0c4d346c70a5dd2958972ac96a263702fe Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 10 May 2026 17:36:52 -0700 Subject: [PATCH 4/5] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- lib/node_modules/@stdlib/ndarray/to-transposed/lib/main.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/to-transposed/lib/main.js b/lib/node_modules/@stdlib/ndarray/to-transposed/lib/main.js index 289d8d6f4306..c74385e3be57 100644 --- a/lib/node_modules/@stdlib/ndarray/to-transposed/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/to-transposed/lib/main.js @@ -40,7 +40,7 @@ var format = require( '@stdlib/string/format' ); * - The input ndarray must have at least two dimensions. * * @param {ndarray} x - input array -* @throws {TypeError} first argument must be an ndarray +* @throws {TypeError} must provide an ndarray * @throws {RangeError} must provide an ndarray having two or more dimensions * @returns {ndarray} output ndarray * @@ -58,10 +58,10 @@ function toTransposed( x ) { var bv; if ( !isndarrayLike( x ) ) { - throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + throw new TypeError( format( 'invalid argument. Must provide an ndarray. Value: `%s`.', x ) ); } if ( ndims( x ) < 2 ) { - throw new RangeError( 'invalid argument. Must provide an ndarray having two or more dimensions.' ); + throw new RangeError( format( 'invalid argument. Must provide an ndarray having two or more dimensions. Number of dimensions: `%u`.', ndims( x ) ) ); } bv = ndarraylike2ndarray( x ); bv = transpose( bv, false ); From 23e81fbe4c5b4fe15c36b0c9281ee2cf96386054 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 10 May 2026 17:39:13 -0700 Subject: [PATCH 5/5] test: update assertion messages Signed-off-by: Athan --- .../@stdlib/ndarray/to-transposed/test/test.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/to-transposed/test/test.js b/lib/node_modules/@stdlib/ndarray/to-transposed/test/test.js index fd761893c0ea..b3976dcb1e01 100644 --- a/lib/node_modules/@stdlib/ndarray/to-transposed/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/to-transposed/test/test.js @@ -114,9 +114,9 @@ tape( 'the function returns a new ndarray containing the elements of an input nd y = toTransposed( x ); - t.deepEqual( getShape( y ), [ 3, 2 ], 'returns expected shape' ); - t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected dtype' ); - t.notEqual( getData( y ), getData( x ), 'does not share the same data buffer' ); + t.deepEqual( getShape( y ), [ 3, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected value' ); + t.notEqual( getData( y ), getData( x ), 'returns expected value' ); t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); t.end(); @@ -141,9 +141,9 @@ tape( 'the function returns a new ndarray containing the elements of an input nd y = toTransposed( x ); - t.deepEqual( getShape( y ), [ 2, 3, 2 ], 'returns expected shape' ); - t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected dtype' ); - t.notEqual( getData( y ), getData( x ), 'does not share the same data buffer' ); + t.deepEqual( getShape( y ), [ 2, 3, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected value' ); + t.notEqual( getData( y ), getData( x ), 'returns expected value' ); t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); t.end(); @@ -170,9 +170,9 @@ tape( 'the function supports column-major order', function test( t ) { y = toTransposed( x ); - t.deepEqual( getShape( y ), [ 3, 2 ], 'returns expected shape' ); - t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected dtype' ); - t.notEqual( getData( y ), getData( x ), 'does not share the same data buffer' ); + t.deepEqual( getShape( y ), [ 3, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected value' ); + t.notEqual( getData( y ), getData( x ), 'returns expected value' ); t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); t.end();