README.md (3633B)
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 # strides2order 22 23 > Determine the order of a multidimensional array based on a provided 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 strides2order = require( '@stdlib/ndarray/base/strides2order' ); 41 ``` 42 43 #### strides2order( strides ) 44 45 Determines the order of a multidimensional array based on a provided stride array. 46 47 ```javascript 48 var order = strides2order( [ 2, 1 ] ); 49 // returns 1 50 51 order = strides2order( [ 1, 2 ] ); 52 // returns 2 53 54 order = strides2order( [ 3 ] ); 55 // returns 3 56 57 order = strides2order( [ 1, 3, 2 ] ); 58 // returns 0 59 60 order = strides2order( [] ); 61 // returns 0 62 ``` 63 64 The function returns one of the following values: 65 66 - `1`: based on the stride array, the array is in row-major order. 67 - `2`: based on the stride array, the array is in column-major order. 68 - `3`: based on the stride array, the array is in both row-major and column-major order. 69 - `0`: based on the stride array, the array is in neither row-major nor column-major order. 70 71 </section> 72 73 <!-- /.usage --> 74 75 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 76 77 <section class="notes"> 78 79 </section> 80 81 <!-- /.notes --> 82 83 <!-- Package usage examples. --> 84 85 <section class="examples"> 86 87 ## Examples 88 89 <!-- eslint no-undef: "error" --> 90 91 ```javascript 92 var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); 93 var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); 94 var randu = require( '@stdlib/random/base/randu' ); 95 var strides2order = require( '@stdlib/ndarray/base/strides2order' ); 96 97 var strides; 98 var order; 99 var shape; 100 var i; 101 var j; 102 103 shape = [ 0, 0, 0 ]; 104 105 for ( i = 0; i < 20; i++ ) { 106 // Generate a random array shape: 107 shape[ 0 ] = discreteUniform( 1, 10 ); 108 shape[ 1 ] = discreteUniform( 1, 10 ); 109 shape[ 2 ] = discreteUniform( 1, 10 ); 110 111 // Compute the strides: 112 if ( randu() < 0.5 ) { 113 strides = shape2strides( shape, 'row-major' ); 114 } else { 115 strides = shape2strides( shape, 'column-major' ); 116 } 117 j = discreteUniform( 0, shape.length-1 ); 118 strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; 119 120 // Determine the order: 121 order = strides2order( strides ); 122 console.log( 'Strides: %s. Order: %s.', strides.join( ',' ), order ); 123 } 124 ``` 125 126 </section> 127 128 <!-- /.examples --> 129 130 <!-- 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. --> 131 132 <section class="references"> 133 134 </section> 135 136 <!-- /.references --> 137 138 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 139 140 <section class="links"> 141 142 </section> 143 144 <!-- /.links -->