|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# toReversedDimension |
| 22 | + |
| 23 | +> Return a new ndarray where the order of elements of an input ndarray along a specified dimension is reversed. |
| 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 toReversedDimension = require( '@stdlib/ndarray/base/to-reversed-dimension' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### toReversedDimension( x, dim ) |
| 44 | + |
| 45 | +Returns a new ndarray where the order of elements of an input ndarray along a specified dimension is reversed. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 49 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 50 | + |
| 51 | +var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; |
| 52 | +var shape = [ 3, 2 ]; |
| 53 | +var strides = [ 2, 1 ]; |
| 54 | +var offset = 0; |
| 55 | + |
| 56 | +var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); |
| 57 | +// returns <ndarray> |
| 58 | + |
| 59 | +var sh = x.shape; |
| 60 | +// returns [ 3, 2 ] |
| 61 | + |
| 62 | +var arr = ndarray2array( x ); |
| 63 | +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] |
| 64 | + |
| 65 | +var y = toReversedDimension( x, 0 ); |
| 66 | +// returns <ndarray> |
| 67 | + |
| 68 | +sh = y.shape; |
| 69 | +// returns [ 3, 2 ] |
| 70 | + |
| 71 | +arr = ndarray2array( y ); |
| 72 | +// returns [ [ 5.0, 6.0 ], [ 3.0, 4.0 ], [ 1.0, 2.0 ] ] |
| 73 | +``` |
| 74 | + |
| 75 | +The function accepts the following arguments: |
| 76 | + |
| 77 | +- **x**: input ndarray. |
| 78 | +- **dim**: index of dimension along which to reverse elements. If provided an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`. |
| 79 | + |
| 80 | +</section> |
| 81 | + |
| 82 | +<!-- /.usage --> |
| 83 | + |
| 84 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 85 | + |
| 86 | +<section class="notes"> |
| 87 | + |
| 88 | +</section> |
| 89 | + |
| 90 | +<!-- /.notes --> |
| 91 | + |
| 92 | +<!-- Package usage examples. --> |
| 93 | + |
| 94 | +<section class="examples"> |
| 95 | + |
| 96 | +## Examples |
| 97 | + |
| 98 | +<!-- eslint no-undef: "error" --> |
| 99 | + |
| 100 | +```javascript |
| 101 | +var array = require( '@stdlib/ndarray/array' ); |
| 102 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 103 | +var zeroTo = require( '@stdlib/array/base/zero-to' ); |
| 104 | +var toReversedDimension = require( '@stdlib/ndarray/base/to-reversed-dimension' ); |
| 105 | + |
| 106 | +// Create a linear ndarray buffer: |
| 107 | +var buf = zeroTo( 16 ); |
| 108 | + |
| 109 | +// Create a three-dimensional ndarray: |
| 110 | +var x = array( buf, { |
| 111 | + 'shape': [ 2, 4, 2 ] |
| 112 | +}); |
| 113 | + |
| 114 | +// Reverse the order of first axis: |
| 115 | +var y0 = toReversedDimension( x, 0 ); |
| 116 | +// returns <ndarray> |
| 117 | + |
| 118 | +var a0 = ndarray2array( y0 ); |
| 119 | +// returns [ [ [ 8, 9 ], [ 10, 11 ], [ 12, 13 ], [ 14, 15 ] ], [ [ 0, 1 ], [ 2, 3 ], [ 4, 5 ], [ 6, 7 ] ] ] |
| 120 | + |
| 121 | +// Reverse the order of second axis: |
| 122 | +var y1 = toReversedDimension( x, 1 ); |
| 123 | +// returns <ndarray> |
| 124 | + |
| 125 | +var a1 = ndarray2array( y1 ); |
| 126 | +// returns [ [ [ 6, 7 ], [ 4, 5 ], [ 2, 3 ], [ 0, 1 ] ], [ [ 14, 15 ], [ 12, 13 ], [ 10, 11 ], [ 8, 9 ] ] ] |
| 127 | + |
| 128 | +// Reverse the order of third axis: |
| 129 | +var y2 = toReversedDimension( x, 2 ); |
| 130 | +// returns <ndarray> |
| 131 | + |
| 132 | +var a2 = ndarray2array( y2 ); |
| 133 | +// returns [ [ [ 1, 0 ], [ 3, 2 ], [ 5, 4 ], [ 7, 6 ] ], [ [ 9, 8 ], [ 11, 10 ], [ 13, 12 ], [ 15, 14 ] ] ] |
| 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 | +</section> |
| 161 | + |
| 162 | +<!-- /.links --> |
0 commit comments