README.md (3160B)
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 # strides2offset 22 23 > Determine the index offset which specifies the location of the first indexed value in a multidimensional array based on a stride array. 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 strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); 41 ``` 42 43 #### strides2offset( shape, strides ) 44 45 Returns the index offset which specifies the location of the first indexed value in a multidimensional array. 46 47 ```javascript 48 var offset = strides2offset( [ 3, 2 ], [ 2, 1 ] ); 49 // returns 0 50 51 offset = strides2offset( [ 3, 2 ], [ -2, 1 ] ); 52 // returns 4 53 ``` 54 55 </section> 56 57 <!-- /.usage --> 58 59 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 60 61 <section class="notes"> 62 63 </section> 64 65 <!-- /.notes --> 66 67 <!-- Package usage examples. --> 68 69 <section class="examples"> 70 71 ## Examples 72 73 <!-- eslint no-undef: "error" --> 74 75 ```javascript 76 var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); 77 var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); 78 var numel = require( '@stdlib/ndarray/base/numel' ); 79 var randu = require( '@stdlib/random/base/randu' ); 80 var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); 81 82 var strides; 83 var offset; 84 var shape; 85 var len; 86 var arr; 87 var i; 88 var j; 89 90 shape = [ 0, 0, 0 ]; 91 shape[ 0 ] = discreteUniform( 1, 10 ); 92 shape[ 1 ] = discreteUniform( 1, 10 ); 93 shape[ 2 ] = discreteUniform( 1, 10 ); 94 95 strides = shape2strides( shape, 'row-major' ); 96 len = numel( shape ); 97 98 arr = []; 99 for ( i = 0; i < len; i++ ) { 100 arr.push( i ); 101 } 102 for ( i = 0; i < 100; i++ ) { 103 j = discreteUniform( 0, shape.length-1 ); 104 strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; 105 offset = strides2offset( shape, strides ); 106 console.log( 'arr[0][0][0] = %d', arr[ offset ] ); 107 } 108 ``` 109 110 </section> 111 112 <!-- /.examples --> 113 114 <!-- 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. --> 115 116 <section class="references"> 117 118 </section> 119 120 <!-- /.references --> 121 122 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 123 124 <section class="links"> 125 126 </section> 127 128 <!-- /.links -->