|
| 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 containing the elements of an input ndarray 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 and another before the `/section` close. --> |
| 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/base/to-transposed' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### toTransposed( x ) |
| 44 | + |
| 45 | +Returns a new ndarray containing the elements of an input ndarray 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 out = 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. |
| 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 with 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-disable stdlib/no-redeclare --> |
| 84 | + |
| 85 | +<!-- eslint no-undef: "error" --> |
| 86 | + |
| 87 | +```javascript |
| 88 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 89 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 90 | +var rpad = require( '@stdlib/string/right-pad' ); |
| 91 | +var toTransposed = require( '@stdlib/ndarray/base/to-transposed' ); |
| 92 | + |
| 93 | +function print( arr, name ) { |
| 94 | + var str; |
| 95 | + var sh; |
| 96 | + var p; |
| 97 | + var i; |
| 98 | + var j; |
| 99 | + var k; |
| 100 | + |
| 101 | + sh = arr.shape; |
| 102 | + for ( i = 0; i < sh[0]; i++ ) { |
| 103 | + str = name+'['+i+',:,:] = [ '; |
| 104 | + p = str.length + 1; |
| 105 | + for ( j = 0; j < sh[1]; j++ ) { |
| 106 | + if ( j > 0 ) { |
| 107 | + str += rpad( '\n', p, ' ' ); |
| 108 | + } |
| 109 | + for ( k = 0; k < sh[2]; k++ ) { |
| 110 | + str += arr.get( i, j, k ); |
| 111 | + if ( k < sh[2]-1 ) { |
| 112 | + str += ', '; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + console.log( str + ' ]\n' ); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// Create a data buffer: |
| 121 | +var buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 122 | + |
| 123 | +// Create a stack of matrices: |
| 124 | +var x = new ndarray( 'float64', buf, [ 2, 2, 3 ], [ 0, 3, 1 ], 0, 'row-major' ); |
| 125 | + |
| 126 | +// Transpose the stack of matrices: |
| 127 | +var y = toTransposed( x ); |
| 128 | + |
| 129 | +// Print the stacks: |
| 130 | +console.log( '' ); |
| 131 | +print( x, 'X' ); |
| 132 | +console.log( '' ); |
| 133 | +print( y, 'Y' ); |
| 134 | +``` |
| 135 | + |
| 136 | +</section> |
| 137 | + |
| 138 | +<!-- /.examples --> |
| 139 | + |
| 140 | +<!-- 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. --> |
| 141 | + |
| 142 | +<section class="references"> |
| 143 | + |
| 144 | +</section> |
| 145 | + |
| 146 | +<!-- /.references --> |
| 147 | + |
| 148 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 149 | + |
| 150 | +<section class="related"> |
| 151 | + |
| 152 | +</section> |
| 153 | + |
| 154 | +<!-- /.related --> |
| 155 | + |
| 156 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 157 | + |
| 158 | +<section class="links"> |
| 159 | + |
| 160 | +<!-- <related-links> --> |
| 161 | + |
| 162 | +<!-- </related-links> --> |
| 163 | + |
| 164 | +</section> |
| 165 | + |
| 166 | +<!-- /.links --> |
0 commit comments