README.md (3209B)
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 # ind 22 23 > Return an index given an index mode. 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 ind = require( '@stdlib/ndarray/base/ind' ); 41 ``` 42 43 #### ind( idx, max, mode ) 44 45 Returns an index given an index `mode`. 46 47 <!-- run throws: true --> 48 49 ```javascript 50 var idx = ind( 2, 9, 'throw' ); 51 // returns 2 52 53 idx = ind( -1, 9, 'throw' ); 54 // throws <RangeError> 55 56 idx = ind( 10, 9, 'throw' ); 57 // throws <RangeError> 58 ``` 59 60 The function supports the following `modes`: 61 62 - `throw`: specifies that the function should throw an error when an index is outside the interval `[0,max]`. 63 - `wrap`: specifies that the function should wrap around an index using modulo arithmetic. 64 - `clamp`: specifies that the function should set an index less than `0` to `0` (minimum index) and set an index greater than `max` to `max`. 65 66 ```javascript 67 var idx = ind( 2, 9, 'wrap' ); 68 // returns 2 69 70 idx = ind( 10, 9, 'wrap' ); 71 // returns 0 72 73 idx = ind( -1, 9, 'wrap' ); 74 // returns 9 75 76 idx = ind( 2, 9, 'clamp' ); 77 // returns 2 78 79 idx = ind( 10, 9, 'clamp' ); 80 // returns 9 81 82 idx = ind( -1, 9, 'clamp' ); 83 // returns 0 84 ``` 85 86 </section> 87 88 <!-- /.usage --> 89 90 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 91 92 <section class="notes"> 93 94 </section> 95 96 <!-- /.notes --> 97 98 <!-- Package usage examples. --> 99 100 <section class="examples"> 101 102 ## Examples 103 104 <!-- eslint no-undef: "error" --> 105 106 ```javascript 107 var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); 108 var ind = require( '@stdlib/ndarray/base/ind' ); 109 110 var modes; 111 var mode; 112 var idx; 113 var out; 114 var i; 115 116 modes = [ 'clamp', 'wrap' ]; 117 118 for ( i = 0; i < 100; i++ ) { 119 idx = discreteUniform( -20, 20 ); 120 mode = modes[ discreteUniform( 0, modes.length-1 ) ]; 121 out = ind( idx, 9, mode ); 122 console.log( '%d => %s(%d,%d) => %d', idx, mode, 0, 9, out ); 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 -->