|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2023 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 | +# stride |
| 22 | + |
| 23 | +> Return the stride along a specified dimension for a provided [ndarray][@stdlib/ndarray/ctor]. |
| 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 stride = require( '@stdlib/ndarray/stride' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### stride( x, dim ) |
| 44 | + |
| 45 | +Returns the stride along a specified dimension for a provided [ndarray][@stdlib/ndarray/ctor]. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 49 | + |
| 50 | +var x = zeros( [ 3, 2, 3 ] ); |
| 51 | +// returns <ndarray> |
| 52 | + |
| 53 | +var st = stride( x, 0 ); |
| 54 | +// returns 6 |
| 55 | +``` |
| 56 | + |
| 57 | +The function accepts the following arguments: |
| 58 | + |
| 59 | +- **x**: input ndarray. |
| 60 | +- **dim**: dimension index. 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`. |
| 61 | + |
| 62 | +</section> |
| 63 | + |
| 64 | +<!-- /.usage --> |
| 65 | + |
| 66 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 67 | + |
| 68 | +<section class="notes"> |
| 69 | + |
| 70 | +## Notes |
| 71 | + |
| 72 | +- A "stride" is the linear distance (i.e., number of elements) between adjacent elements along a specified dimension. |
| 73 | +- This function is intended as a slight performance optimization over [`@stdlib/ndarray/strides`][@stdlib/ndarray/strides] when **only** a single stride is needed. For retrieving multiple strides, use [`@stdlib/ndarray/strides`][@stdlib/ndarray/strides] directly. |
| 74 | + |
| 75 | +</section> |
| 76 | + |
| 77 | +<!-- /.notes --> |
| 78 | + |
| 79 | +<!-- Package usage examples. --> |
| 80 | + |
| 81 | +<section class="examples"> |
| 82 | + |
| 83 | +## Examples |
| 84 | + |
| 85 | +<!-- eslint no-undef: "error" --> |
| 86 | + |
| 87 | +<!-- eslint-disable new-cap --> |
| 88 | + |
| 89 | +```javascript |
| 90 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 91 | +var slice = require( '@stdlib/ndarray/slice' ); |
| 92 | +var E = require( '@stdlib/slice/multi' ); |
| 93 | +var S = require( '@stdlib/slice/ctor' ); |
| 94 | +var stride = require( '@stdlib/ndarray/stride' ); |
| 95 | + |
| 96 | +// Create an array: |
| 97 | +var x = zeros( [ 10, 10, 10, 10 ] ); |
| 98 | +// returns <ndarray> |
| 99 | + |
| 100 | +// Define some slices: |
| 101 | +var slices = [ |
| 102 | + // :,:,:,: |
| 103 | + E( null, null, null, null ), |
| 104 | + |
| 105 | + // 5:10,4,2:4,::-1 |
| 106 | + E( S( 5, 10 ), 4, S( 2, 4 ), S( null, null, -1 ) ), |
| 107 | + |
| 108 | + // :,:,2,: |
| 109 | + E( null, null, 2, null ), |
| 110 | + |
| 111 | + // 1,2,3,: |
| 112 | + E( 1, 2, 3, null ), |
| 113 | + |
| 114 | + // 1,3,::2,4::2 |
| 115 | + E( 1, 3, S( null, null, 2 ), S( 4, null, 2 ) ) |
| 116 | +]; |
| 117 | + |
| 118 | +// Resolve the stride of the first dimension for each slice... |
| 119 | +var s; |
| 120 | +var i; |
| 121 | +for ( i = 0; i < slices.length; i++ ) { |
| 122 | + s = slice( x, slices[ i ] ); |
| 123 | + console.log( '(%s) => %d', s.shape.join( ',' ), stride( s, 0 ) ); |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +</section> |
| 128 | + |
| 129 | +<!-- /.examples --> |
| 130 | + |
| 131 | +<!-- 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. --> |
| 132 | + |
| 133 | +<section class="references"> |
| 134 | + |
| 135 | +</section> |
| 136 | + |
| 137 | +<!-- /.references --> |
| 138 | + |
| 139 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 140 | + |
| 141 | +<section class="related"> |
| 142 | + |
| 143 | +</section> |
| 144 | + |
| 145 | +<!-- /.related --> |
| 146 | + |
| 147 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 148 | + |
| 149 | +<section class="links"> |
| 150 | + |
| 151 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 152 | + |
| 153 | +[@stdlib/ndarray/strides]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/strides |
| 154 | + |
| 155 | +</section> |
| 156 | + |
| 157 | +<!-- /.links --> |
0 commit comments