time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (4664B)


      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 # sub2ind
     22 
     23 > Convert subscripts to a linear index.
     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 sub2ind = require( '@stdlib/ndarray/sub2ind' );
     41 ```
     42 
     43 #### sub2ind( shape, ...subscripts\[, options] )
     44 
     45 Converts subscripts to a linear index.
     46 
     47 ```javascript
     48 var shape = [ 2, 2 ];
     49 
     50 var idx = sub2ind( shape, 1, 0 );
     51 // returns 2
     52 ```
     53 
     54 The function supports the following `options`:
     55 
     56 -   `mode`: specifies how to handle subscripts which exceed array dimensions. The following modes are supported:
     57 
     58     -   `throw`: specifies that the function should throw an error when a subscript exceeds array dimensions.
     59     -   `wrap`: specifies that the function should wrap around subscripts exceeding array dimensions using modulo arithmetic.
     60     -   `clamp`: specifies that the function should set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.
     61 
     62     If provided a `mode` array, each array element corresponds to a dimension. If provided fewer modes than dimensions, the function reuses modes using modulo arithmetic. Default: `[ 'throw' ]`.
     63 
     64 -   `order`: specifies whether an array is `row-major` (C-style) or `column-major` (Fortran-style). Default: `'row-major'`.
     65 
     66 By default, the function assumes a row-major array. To return a linear index for a column-major array, set the `order` option.
     67 
     68 ```javascript
     69 var shape = [ 2, 2 ];
     70 var opts = {};
     71 
     72 opts.order = 'column-major';
     73 var idx = sub2ind( shape, 1, 0, opts );
     74 // returns 1
     75 ```
     76 
     77 By default, the function throws an `error` if provided subscripts which exceed array dimensions. To specify alternative behavior, set the `mode` option.
     78 
     79 ```javascript
     80 var shape = [ 2, 2 ];
     81 var opts = {};
     82 
     83 opts.mode = 'wrap';
     84 var idx = sub2ind( shape, -2, 0, opts );
     85 // returns 0
     86 
     87 opts.mode = 'clamp';
     88 idx = sub2ind( shape, 10, 10, opts );
     89 // returns 3
     90 ```
     91 
     92 To specify separate modes for each dimension, provide a `mode` array.
     93 
     94 ```javascript
     95 var shape = [ 2, 2, 2 ];
     96 var opts = {
     97     'mode': [
     98         'wrap',
     99         'clamp'
    100     ]
    101 };
    102 
    103 var idx = sub2ind( shape, -2, 10, -1, opts );
    104 // returns 3
    105 ```
    106 
    107 </section>
    108 
    109 <!-- /.usage -->
    110 
    111 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    112 
    113 <section class="notes">
    114 
    115 </section>
    116 
    117 <!-- /.notes -->
    118 
    119 <!-- Package usage examples. -->
    120 
    121 <section class="examples">
    122 
    123 ## Examples
    124 
    125 <!-- eslint no-undef: "error" -->
    126 
    127 ```javascript
    128 var numel = require( '@stdlib/ndarray/base/numel' );
    129 var sub2ind = require( '@stdlib/ndarray/sub2ind' );
    130 
    131 var shape = [ 3, 3, 3 ];
    132 var len = numel( shape );
    133 
    134 var arr = [];
    135 var i;
    136 for ( i = 0; i < len; i++ ) {
    137     arr.push( i );
    138 }
    139 
    140 var opts = {
    141     'order': 'column-major'
    142 };
    143 
    144 console.log( '' );
    145 console.log( 'Dimensions: %s.', shape.join( 'x' ) );
    146 console.log( 'View:' );
    147 console.log( '' );
    148 
    149 var slice;
    150 var idx;
    151 var row;
    152 var j;
    153 var k;
    154 for ( k = 0; k < shape[ 2 ]; k++ ) {
    155     slice = 'A[:,:,'+k+'] = ';
    156     console.log( slice );
    157     for ( i = 0; i < shape[ 0 ]; i++ ) {
    158         row = '  ';
    159         for ( j = 0; j < shape[ 1 ]; j++ ) {
    160             idx = sub2ind( shape, i, j, k, opts );
    161             row += arr[ idx ];
    162             if ( j < shape[ 1 ]-1 ) {
    163                 row += ', ';
    164             }
    165         }
    166         console.log( row );
    167     }
    168     console.log( '' );
    169 }
    170 ```
    171 
    172 </section>
    173 
    174 <!-- /.examples -->
    175 
    176 <!-- 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. -->
    177 
    178 <section class="references">
    179 
    180 </section>
    181 
    182 <!-- /.references -->
    183 
    184 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    185 
    186 <section class="links">
    187 
    188 </section>
    189 
    190 <!-- /.links -->