README.md (6350B)
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 # Min and Max View Buffer Indices 22 23 > Compute the minimum and maximum linear indices in an underlying data buffer which are accessible to an array view. 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 minmaxViewBufferIndex = require( '@stdlib/ndarray/base/minmax-view-buffer-index' ); 41 ``` 42 43 #### minmaxViewBufferIndex( shape, strides, offset ) 44 45 Computes the minimum and maximum linear indices in an underlying data buffer which are accessible to an array view. 46 47 ```javascript 48 // Array shape: 49 var shape = [ 2, 2 ]; 50 51 // Stride array: 52 var strides = [ 2, 1 ]; 53 54 // Index offset which specifies the location of the first indexed value: 55 var offset = 0; 56 57 var idx = minmaxViewBufferIndex( shape, strides, offset ); 58 // returns [ 0, 3 ] 59 ``` 60 61 #### minmaxViewBufferIndex.assign( shape, strides, offset, out ) 62 63 Computes the minimum and maximum linear indices in an underlying data buffer which are accessible to an array view and assigns results to a provided output array. 64 65 ```javascript 66 var shape = [ 2, 2 ]; 67 var strides = [ -1, -2 ]; 68 var offset = 3; 69 70 var out = [ 0, 0 ]; 71 var idx = minmaxViewBufferIndex.assign( shape, strides, offset, out ); 72 // returns [ 0, 3 ] 73 74 var bool = ( idx === out ); 75 // returns true 76 ``` 77 78 </section> 79 80 <!-- /.usage --> 81 82 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 83 84 <section class="notes"> 85 86 </section> 87 88 <!-- /.notes --> 89 90 <!-- Package usage examples. --> 91 92 <section class="examples"> 93 94 ## Examples 95 96 <!-- eslint no-undef: "error" --> 97 98 ```javascript 99 var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); 100 var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); 101 var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); 102 var randu = require( '@stdlib/random/base/randu' ); 103 var minmaxViewBufferIndex = require( '@stdlib/ndarray/base/minmax-view-buffer-index' ); 104 105 var strides; 106 var offset; 107 var shape; 108 var idx; 109 var i; 110 var j; 111 112 shape = [ 0, 0, 0 ]; 113 114 for ( i = 0; i < 100; i++ ) { 115 // Generate a random array shape: 116 shape[ 0 ] = discreteUniform( 1, 10 ); 117 shape[ 1 ] = discreteUniform( 1, 10 ); 118 shape[ 2 ] = discreteUniform( 1, 10 ); 119 120 // Generate strides: 121 if ( randu() < 0.5 ) { 122 strides = shape2strides( shape, 'row-major' ); 123 } else { 124 strides = shape2strides( shape, 'column-major' ); 125 } 126 j = discreteUniform( 0, shape.length-1 ); 127 strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; 128 129 // Compute the index offset: 130 offset = strides2offset( shape, strides ) + 25; // include a view offset 131 132 // Compute the minimum and maximum linear indices: 133 idx = minmaxViewBufferIndex( shape, strides, offset ); 134 console.log( 'Shape: %s. Strides: %s. Offset: %d. Min idx: %d. Max idx: %d.', shape.join( 'x' ), strides.join( ',' ), offset, idx[ 0 ], idx[ 1 ] ); 135 } 136 ``` 137 138 </section> 139 140 <!-- /.examples --> 141 142 <!-- C interface documentation. --> 143 144 * * * 145 146 <section class="c"> 147 148 ## C APIs 149 150 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 151 152 <section class="intro"> 153 154 </section> 155 156 <!-- /.intro --> 157 158 <!-- C usage documentation. --> 159 160 <section class="usage"> 161 162 ### Usage 163 164 ```c 165 #include "stdlib/ndarray/base/minmax_view_buffer_index.h" 166 ``` 167 168 #### stdlib_ndarray_minmax_view_buffer_index( ndims, \*shape, \*strides, offset, \*out ) 169 170 Computes the minimum and maximum linear indices (in bytes) in an underlying data buffer accessible to an array view. 171 172 ```c 173 int64_t ndims = 2; 174 int64_t shape[] = { 10, 10 }; 175 int64_t strides[] = { 10, 1 }; 176 int64_t offset = 0; 177 int64_t out[ 2 ]; 178 179 stdlib_ndarray_minmax_view_buffer_index( ndims, shape, strides, offset, out ); 180 181 int64_t min = out[ 0 ]; 182 // returns 0 183 184 int64_t max = out[ 1 ]; 185 // returns 99 186 ``` 187 188 The function accepts the following arguments: 189 190 - **ndims**: `[in] int64_t` number of dimensions. 191 - **shape**: `[in] int64_t*` array shape (dimensions). 192 - **strides**: `[in] int64_t*` array strides (in bytes). 193 - **offset**: `[in] int64_t` index offset. 194 - **out**: `[out] int64_t*` two-element output array. 195 196 ```c 197 int8_t stdlib_ndarray_minmax_view_buffer_index( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *out ); 198 ``` 199 200 </section> 201 202 <!-- /.usage --> 203 204 <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 205 206 <section class="notes"> 207 208 </section> 209 210 <!-- /.notes --> 211 212 <!-- C API usage examples. --> 213 214 <section class="examples"> 215 216 ### Examples 217 218 ```c 219 #include "stdlib/ndarray/base/minmax_view_buffer_index.h" 220 #include <stdio.h> 221 #include <inttypes.h> 222 223 int main() { 224 int64_t ndims = 2; 225 int64_t shape[] = { 10, 10 }; 226 int64_t strides[] = { 10, 1 }; 227 int64_t offset = 0; 228 int64_t out[ 2 ]; 229 230 stdlib_ndarray_minmax_view_buffer_index( ndims, shape, strides, offset, out ); 231 232 printf( "min: %"PRId64"\n", out[ 0 ] ); 233 printf( "max: %"PRId64"\n", out[ 1 ] ); 234 } 235 ``` 236 237 </section> 238 239 <!-- /.examples --> 240 241 </section> 242 243 <!-- /.c --> 244 245 <!-- 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. --> 246 247 <section class="references"> 248 249 </section> 250 251 <!-- /.references --> 252 253 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 254 255 <section class="links"> 256 257 </section> 258 259 <!-- /.links -->