README.md (5162B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # shape2strides 22 23 > Generate a stride array from an array shape. 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 shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); 41 ``` 42 43 #### shape2strides( shape, order ) 44 45 Generates a stride array from an array shape. 46 47 ```javascript 48 var strides = shape2strides( [ 3, 2 ], 'row-major' ); 49 // returns [ 2, 1 ] 50 ``` 51 52 The `order` parameter specifies whether an array is `row-major` (C-style) or `column-major` (Fortran-style). 53 54 ```javascript 55 var strides = shape2strides( [ 3, 2 ], 'column-major' ); 56 // returns [ 1, 3 ] 57 ``` 58 59 #### shape2strides.assign( shape, order, out ) 60 61 Generates a stride array from an array shape and assigns results to a provided output array. 62 63 ```javascript 64 var shape = [ 3, 2 ]; 65 var strides = [ 0, 0 ]; 66 67 var out = shape2strides.assign( shape, 'row-major', strides ); 68 // returns [ 2, 1 ] 69 70 var bool = ( strides === out ); 71 // returns true 72 ``` 73 74 </section> 75 76 <!-- /.usage --> 77 78 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 79 80 <section class="notes"> 81 82 </section> 83 84 <!-- /.notes --> 85 86 <!-- Package usage examples. --> 87 88 <section class="examples"> 89 90 ## Examples 91 92 <!-- eslint no-undef: "error" --> 93 94 ```javascript 95 var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); 96 var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); 97 98 var strides; 99 var shape; 100 var i; 101 102 shape = [ 0, 0, 0 ]; 103 for ( i = 0; i < 100; i++ ) { 104 shape[ 0 ] = discreteUniform( 1, 10 ); 105 shape[ 1 ] = discreteUniform( 1, 10 ); 106 shape[ 2 ] = discreteUniform( 1, 10 ); 107 strides = shape2strides( shape, 'row-major' ); 108 console.log( 'shape: %s. strides: %s.', shape.join( 'x' ), strides.join( ', ' ) ); 109 } 110 ``` 111 112 </section> 113 114 <!-- /.examples --> 115 116 <!-- C interface documentation. --> 117 118 * * * 119 120 <section class="c"> 121 122 ## C APIs 123 124 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 125 126 <section class="intro"> 127 128 </section> 129 130 <!-- /.intro --> 131 132 <!-- C usage documentation. --> 133 134 <section class="usage"> 135 136 ### Usage 137 138 ```c 139 #include "stdlib/ndarray/base/shape2strides.h" 140 ``` 141 142 #### stdlib_ndarray_shape2strides( ndims, \*shape, order, \*out ) 143 144 Generates a stride array from an array shape. 145 146 ```c 147 #include "stdlib/ndarray/orders.h" 148 149 int64_t ndims = 3; 150 int64_t shape[] = { 2, 3, 10 }; 151 int64_t out[ 3 ]; 152 153 stdlib_ndarray_shape2strides( ndims, shape, STDLIB_NDARRAY_ROW_MAJOR, out ); 154 ``` 155 156 The function accepts the following arguments: 157 158 - **ndims**: `[in] int64_t` number of dimensions. 159 - **shape**: `[in] int64_t*` array shape (dimensions). 160 - **order**: `[in] enum STDLIB_NDARRAY_ORDER` specifies whether an array is row-major (C-style) or column-major (Fortran-style). 161 - **out**: `[out] int64_t*` output strides array. 162 163 ```c 164 int8_t stdlib_ndarray_shape2strides( int64_t ndims, int64_t *shape, enum STDLIB_NDARRAY_ORDER order, int64_t *out ); 165 ``` 166 167 </section> 168 169 <!-- /.usage --> 170 171 <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 172 173 <section class="notes"> 174 175 </section> 176 177 <!-- /.notes --> 178 179 <!-- C API usage examples. --> 180 181 <section class="examples"> 182 183 ### Examples 184 185 ```c 186 #include "stdlib/ndarray/base/shape2strides.h" 187 #include "stdlib/ndarray/orders.h" 188 #include <stdio.h> 189 #include <inttypes.h> 190 191 int main() { 192 int64_t shape[] = { 2, 3, 10 }; 193 int64_t ndims = 3; 194 int64_t out[ 3 ]; 195 196 stdlib_ndarray_shape2strides( ndims, shape, STDLIB_NDARRAY_ROW_MAJOR, out ); 197 198 int i; 199 printf( "strides = { " ); 200 for ( i = 0; i < ndims; i++ ) { 201 printf( "%"PRId64"", out[ i ] ); 202 if ( i < ndims-1 ) { 203 printf( ", " ); 204 } 205 } 206 printf( " }\n" ); 207 } 208 ``` 209 210 </section> 211 212 <!-- /.examples --> 213 214 </section> 215 216 <!-- /.c --> 217 218 <!-- 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. --> 219 220 <section class="references"> 221 222 </section> 223 224 <!-- /.references --> 225 226 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 227 228 <section class="links"> 229 230 </section> 231 232 <!-- /.links -->