Skip to content

Commit 543339e

Browse files
committed
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 ---
1 parent 068acb1 commit 543339e

10 files changed

Lines changed: 824 additions & 0 deletions

File tree

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# toTransposed
22+
23+
> Return a new [`ndarray`][@stdlib/ndarray/ctor] containing the elements of an input [`ndarray`][@stdlib/ndarray/ctor] but whose last two dimensions are transposed.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro section element. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var toTransposed = require( '@stdlib/ndarray/to-transposed' );
41+
```
42+
43+
#### toTransposed( x )
44+
45+
Returns a new [`ndarray`][@stdlib/ndarray/ctor] containing the elements of an input [`ndarray`][@stdlib/ndarray/ctor] but whose last two dimensions are transposed.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
50+
var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] );
51+
// returns <ndarray>[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
52+
53+
var y = toTransposed( x );
54+
// returns <ndarray>[ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: input [`ndarray`][@stdlib/ndarray/ctor].
60+
61+
</section>
62+
63+
<!-- /.usage -->
64+
65+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
66+
67+
<section class="notes">
68+
69+
## Notes
70+
71+
- If provided an [`ndarray`][@stdlib/ndarray/ctor] having fewer than two dimensions, the function raises an exception.
72+
73+
</section>
74+
75+
<!-- /.notes -->
76+
77+
<!-- Package usage examples. -->
78+
79+
<section class="examples">
80+
81+
## Examples
82+
83+
<!-- eslint no-undef: "error" -->
84+
85+
<!-- eslint-disable max-len -->
86+
87+
```javascript
88+
var array = require( '@stdlib/ndarray/array' );
89+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
90+
var toTransposed = require( '@stdlib/ndarray/to-transposed' );
91+
92+
var x = array( [ [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], [ [ 7, 8, 9 ], [ 10, 11, 12 ] ] ] );
93+
console.log( ndarray2array( x ) );
94+
95+
var y = toTransposed( x );
96+
console.log( ndarray2array( y ) );
97+
```
98+
99+
</section>
100+
101+
<!-- /.examples -->
102+
103+
<!-- 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. -->
104+
105+
<section class="references">
106+
107+
</section>
108+
109+
<!-- /.references -->
110+
111+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
112+
113+
<section class="related">
114+
115+
</section>
116+
117+
<!-- /.related -->
118+
119+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
120+
121+
<section class="links">
122+
123+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
124+
125+
<!-- <related-links> -->
126+
127+
<!-- </related-links> -->
128+
129+
</section>
130+
131+
<!-- /.links -->
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var Float64Array = require( '@stdlib/array/float64' );
25+
var ndarrayBase = require( '@stdlib/ndarray/base/ctor' );
26+
var ndarray = require( '@stdlib/ndarray/ctor' );
27+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var toTransposed = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( format( '%s:ctor=base,ndims=2', pkg ), function benchmark( b ) {
36+
var strides;
37+
var buffer;
38+
var offset;
39+
var dtype;
40+
var order;
41+
var shape;
42+
var out;
43+
var i;
44+
var x;
45+
46+
dtype = 'float64';
47+
buffer = new Float64Array( 24 );
48+
shape = [ 4, 6 ];
49+
strides = [ 6, 1 ];
50+
offset = 0;
51+
order = 'row-major';
52+
53+
x = ndarrayBase( dtype, buffer, shape, strides, offset, order );
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
out = toTransposed( x );
58+
if ( typeof out !== 'object' ) {
59+
b.fail( 'should return an object' );
60+
}
61+
}
62+
b.toc();
63+
if ( !isndarrayLike( out ) ) {
64+
b.fail( 'should return an ndarray' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
});
69+
70+
bench( format( '%s:ctor=ndarray,ndims=2', pkg ), function benchmark( b ) {
71+
var strides;
72+
var buffer;
73+
var offset;
74+
var dtype;
75+
var order;
76+
var shape;
77+
var out;
78+
var i;
79+
var x;
80+
81+
dtype = 'float64';
82+
buffer = new Float64Array( 24 );
83+
shape = [ 4, 6 ];
84+
strides = [ 6, 1 ];
85+
offset = 0;
86+
order = 'row-major';
87+
88+
x = ndarray( dtype, buffer, shape, strides, offset, order );
89+
90+
b.tic();
91+
for ( i = 0; i < b.iterations; i++ ) {
92+
out = toTransposed( x );
93+
if ( typeof out !== 'object' ) {
94+
b.fail( 'should return an object' );
95+
}
96+
}
97+
b.toc();
98+
if ( !isndarrayLike( out ) ) {
99+
b.fail( 'should return an ndarray' );
100+
}
101+
b.pass( 'benchmark finished' );
102+
b.end();
103+
});
104+
105+
bench( format( '%s:ctor=base,ndims=3', pkg ), function benchmark( b ) {
106+
var strides;
107+
var buffer;
108+
var offset;
109+
var dtype;
110+
var order;
111+
var shape;
112+
var out;
113+
var i;
114+
var x;
115+
116+
dtype = 'float64';
117+
buffer = new Float64Array( 24 );
118+
shape = [ 2, 4, 3 ];
119+
strides = [ 12, 3, 1 ];
120+
offset = 0;
121+
order = 'row-major';
122+
123+
x = ndarrayBase( dtype, buffer, shape, strides, offset, order );
124+
125+
b.tic();
126+
for ( i = 0; i < b.iterations; i++ ) {
127+
out = toTransposed( x );
128+
if ( typeof out !== 'object' ) {
129+
b.fail( 'should return an object' );
130+
}
131+
}
132+
b.toc();
133+
if ( !isndarrayLike( out ) ) {
134+
b.fail( 'should return an ndarray' );
135+
}
136+
b.pass( 'benchmark finished' );
137+
b.end();
138+
});
139+
140+
bench( format( '%s:ctor=ndarray,ndims=3', pkg ), function benchmark( b ) {
141+
var strides;
142+
var buffer;
143+
var offset;
144+
var dtype;
145+
var order;
146+
var shape;
147+
var out;
148+
var i;
149+
var x;
150+
151+
dtype = 'float64';
152+
buffer = new Float64Array( 24 );
153+
shape = [ 2, 4, 3 ];
154+
strides = [ 12, 3, 1 ];
155+
offset = 0;
156+
order = 'row-major';
157+
158+
x = ndarray( dtype, buffer, shape, strides, offset, order );
159+
160+
b.tic();
161+
for ( i = 0; i < b.iterations; i++ ) {
162+
out = toTransposed( x );
163+
if ( typeof out !== 'object' ) {
164+
b.fail( 'should return an object' );
165+
}
166+
}
167+
b.toc();
168+
if ( !isndarrayLike( out ) ) {
169+
b.fail( 'should return an ndarray' );
170+
}
171+
b.pass( 'benchmark finished' );
172+
b.end();
173+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
{{alias}}( x )
3+
Returns a new ndarray containing the elements of an input ndarray but whose
4+
last two dimensions are transposed.
5+
6+
Parameters
7+
----------
8+
x: ndarray
9+
Input array.
10+
11+
Returns
12+
-------
13+
out: ndarray
14+
Output array.
15+
16+
Examples
17+
--------
18+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] )
19+
<ndarray>[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
20+
> var y = {{alias}}( x )
21+
<ndarray>[ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
22+
23+
See Also
24+
--------
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Returns a new ndarray containing the elements of an input ndarray but whose last two dimensions are transposed.
27+
*
28+
* @param x - input array
29+
* @returns output ndarray
30+
*
31+
* @example
32+
* var array = require( '@stdlib/ndarray/array' );
33+
*
34+
* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] );
35+
* // returns <ndarray>[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
36+
*
37+
* var y = toTransposed( x );
38+
* // returns <ndarray>[ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
39+
*/
40+
declare function toTransposed<T extends ndarray = ndarray>( x: T ): T;
41+
42+
43+
// EXPORTS //
44+
45+
export = toTransposed;

0 commit comments

Comments
 (0)