README.md (3101B)
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 # Iteration Order 22 23 > Given a stride array, determine array iteration order. 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 iterationOrder = require( '@stdlib/ndarray/base/iteration-order' ); 41 ``` 42 43 #### iterationOrder( strides ) 44 45 Returns the array iteration order. 46 47 ```javascript 48 var o = iterationOrder( [ 2, 1 ] ); 49 // returns 1 50 51 o = iterationOrder( [ -2, 1 ] ); 52 // returns 0 53 54 o = iterationOrder( [ -2, -1 ] ); 55 // returns -1 56 ``` 57 58 The function returns one of the following values: 59 60 - `0`: unordered (strides are of mixed sign). 61 - `1`: left-to-right iteration order (strides are all nonnegative). 62 - `-1`: right-to-left iteration order (strides are all negative). 63 64 </section> 65 66 <!-- /.usage --> 67 68 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 69 70 <section class="notes"> 71 72 </section> 73 74 <!-- /.notes --> 75 76 <!-- Package usage examples. --> 77 78 <section class="examples"> 79 80 ## Examples 81 82 <!-- eslint no-undef: "error" --> 83 84 ```javascript 85 var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); 86 var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); 87 var randu = require( '@stdlib/random/base/randu' ); 88 var iterationOrder = require( '@stdlib/ndarray/base/iteration-order' ); 89 90 var strides; 91 var shape; 92 var out; 93 var i; 94 var j; 95 96 shape = [ 0, 0, 0 ]; 97 shape[ 0 ] = discreteUniform( 1, 10 ); 98 shape[ 1 ] = discreteUniform( 1, 10 ); 99 shape[ 2 ] = discreteUniform( 1, 10 ); 100 101 strides = shape2strides( shape, 'row-major' ); 102 103 for ( i = 0; i < 100; i++ ) { 104 j = discreteUniform( 0, shape.length-1 ); 105 strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; 106 out = iterationOrder( strides ); 107 console.log( 'strides: %s => %d', strides.join( ',' ), out ); 108 } 109 ``` 110 111 </section> 112 113 <!-- /.examples --> 114 115 <!-- 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. --> 116 117 <section class="references"> 118 119 </section> 120 121 <!-- /.references --> 122 123 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 124 125 <section class="links"> 126 127 </section> 128 129 <!-- /.links -->