time-to-botec

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

README.md (16968B)


      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 # ndarray
     22 
     23 > Create a multidimensional 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 ndarray = require( '@stdlib/ndarray/base/ctor' );
     41 ```
     42 
     43 <a name="main"></a>
     44 
     45 #### ndarray( dtype, buffer, shape, strides, offset, order )
     46 
     47 Returns an `ndarray` instance.
     48 
     49 ```javascript
     50 // Specify the array configuration:
     51 var buffer = [ 1, 2, 3, 4 ];
     52 var shape = [ 2, 2 ];
     53 var order = 'row-major';
     54 var strides = [ 2, 1 ];
     55 var offset = 0;
     56 
     57 // Create a new ndarray:
     58 var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
     59 // returns <ndarray>
     60 ```
     61 
     62 The constructor has the following parameters:
     63 
     64 -   **dtype**: underlying [data type][@stdlib/ndarray/dtypes].
     65 -   **buffer**: data buffer.
     66 -   **shape**: array shape (dimensions).
     67 -   **strides**: array strides which are index offsets specifying how to access along corresponding dimensions.
     68 -   **offset**: index offset specifying the location of the first indexed element in the data buffer.
     69 -   **order**: array order, which is either `row-major` (C-style) or `column-major` (Fortran-style).
     70 
     71 To create a zero-dimensional array, provide an empty `shape` and a single `strides` element equal to `0`. The `order` can be either `row-major` or `column-major` and has no effect on data storage or access.
     72 
     73 ```javascript
     74 var buffer = [ 1 ];
     75 var shape = [];
     76 var order = 'row-major';
     77 var strides = [ 0 ];
     78 var offset = 0;
     79 
     80 // Create a new zero-dimensional array:
     81 var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
     82 // returns <ndarray>
     83 ```
     84 
     85 * * *
     86 
     87 ### Properties
     88 
     89 <a name="static-prop-name"></a>
     90 
     91 #### ndarray.name
     92 
     93 String value of the ndarray constructor name.
     94 
     95 ```javascript
     96 var str = ndarray.name;
     97 // returns 'ndarray'
     98 ```
     99 
    100 <a name="prop-byte-length"></a>
    101 
    102 #### ndarray.prototype.byteLength
    103 
    104 Size (in bytes) of the array (if known).
    105 
    106 ```javascript
    107 var Float64Array = require( '@stdlib/array/float64' );
    108 
    109 // Specify the array configuration:
    110 var buffer = new Float64Array( [ 1, 2, 3, 4 ] );
    111 var shape = [ 2, 2 ];
    112 var order = 'row-major';
    113 var strides = [ 2, 1 ];
    114 var offset = 0;
    115 
    116 // Create a new ndarray:
    117 var arr = ndarray( 'float64', buffer, shape, strides, offset, order );
    118 
    119 // Get the byte length:
    120 var nbytes = arr.byteLength;
    121 // returns 32
    122 ```
    123 
    124 If unable to determine the size of the array, the property value is `null`.
    125 
    126 ```javascript
    127 // Specify the array configuration:
    128 var buffer = [ 1, 2, 3, 4 ];
    129 var shape = [ 2, 2 ];
    130 var order = 'row-major';
    131 var strides = [ 2, 1 ];
    132 var offset = 0;
    133 
    134 // Create a new ndarray:
    135 var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
    136 
    137 // Get the byte length:
    138 var nbytes = arr.byteLength;
    139 // returns null
    140 ```
    141 
    142 <a name="prop-bytes-per-element"></a>
    143 
    144 #### ndarray.prototype.BYTES_PER_ELEMENT
    145 
    146 Size (in bytes) of each array element (if known).
    147 
    148 ```javascript
    149 var Float32Array = require( '@stdlib/array/float32' );
    150 
    151 // Specify the array configuration:
    152 var buffer = new Float32Array( [ 1, 2, 3, 4 ] );
    153 var shape = [ 2, 2 ];
    154 var order = 'row-major';
    155 var strides = [ 2, 1 ];
    156 var offset = 0;
    157 
    158 // Create a new ndarray:
    159 var arr = ndarray( 'float32', buffer, shape, strides, offset, order );
    160 
    161 // Get the number of bytes per element:
    162 var nbytes = arr.BYTES_PER_ELEMENT;
    163 // returns 4
    164 ```
    165 
    166 If size of each array element is unknown, the property value is `null`.
    167 
    168 ```javascript
    169 // Specify the array configuration:
    170 var buffer = [ 1, 2, 3, 4 ];
    171 var shape = [ 2, 2 ];
    172 var order = 'row-major';
    173 var strides = [ 2, 1 ];
    174 var offset = 0;
    175 
    176 // Create a new ndarray:
    177 var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
    178 
    179 // Get the number of bytes per element:
    180 var nbytes = arr.BYTES_PER_ELEMENT;
    181 // returns null
    182 ```
    183 
    184 <a name="prop-data"></a>
    185 
    186 #### ndarray.prototype.data
    187 
    188 A reference to the underlying data buffer.
    189 
    190 ```javascript
    191 var Int8Array = require( '@stdlib/array/int8' );
    192 
    193 // Specify the array configuration:
    194 var buffer = new Int8Array( [ 1, 2, 3, 4 ] );
    195 var shape = [ 2, 2 ];
    196 var order = 'row-major';
    197 var strides = [ 2, 1 ];
    198 var offset = 0;
    199 
    200 // Create a new ndarray:
    201 var arr = ndarray( 'int8', buffer, shape, strides, offset, order );
    202 
    203 // Get the buffer reference:
    204 var d = arr.data;
    205 // returns <Int8Array>[ 1, 2, 3, 4 ]
    206 
    207 var bool = ( d === buffer );
    208 // returns true
    209 ```
    210 
    211 <a name="prop-dtype"></a>
    212 
    213 #### ndarray.prototype.dtype
    214 
    215 Underlying [data type][@stdlib/ndarray/dtypes].
    216 
    217 ```javascript
    218 var Uint8Array = require( '@stdlib/array/uint8' );
    219 
    220 // Specify the array configuration:
    221 var buffer = new Uint8Array( [ 1, 2, 3, 4 ] );
    222 var shape = [ 2, 2 ];
    223 var order = 'row-major';
    224 var strides = [ -2, 1 ];
    225 var offset = 2;
    226 
    227 // Create a new ndarray:
    228 var arr = ndarray( 'uint8', buffer, shape, strides, offset, order );
    229 
    230 // Get the underlying data type:
    231 var dtype = arr.dtype;
    232 // returns 'uint8'
    233 ```
    234 
    235 <a name="prop-flags"></a>
    236 
    237 #### ndarray.prototype.flags
    238 
    239 Information regarding the memory layout of the array. The returned `object` has the following properties:
    240 
    241 -   **ROW_MAJOR_CONTIGUOUS**: `boolean` indicating if an array is row-major contiguous.
    242 -   **COLUMN_MAJOR_CONTIGUOUS**: `boolean` indicating if an array is column-major contiguous.
    243 
    244 An array is contiguous if (1) an array is compatible with being stored in a single memory segment and (2) each array element is adjacent to the next array element. Note that an array can be both row-major contiguous and column-major contiguous at the same time (e.g., if an array is a 1-dimensional ndarray with `strides = [1]`).
    245 
    246 ```javascript
    247 var Int32Array = require( '@stdlib/array/int32' );
    248 
    249 // Specify the array configuration:
    250 var buffer = new Int32Array( [ 1, 2, 3, 4 ] );
    251 var shape = [ 2, 2 ];
    252 var order = 'column-major';
    253 var strides = [ 1, 2 ];
    254 var offset = 0;
    255 
    256 // Create a new ndarray:
    257 var arr = ndarray( 'int32', buffer, shape, strides, offset, order );
    258 
    259 // Get the array flags:
    260 var flg = arr.flags;
    261 // returns {...}
    262 ```
    263 
    264 <a name="prop-length"></a>
    265 
    266 #### ndarray.prototype.length
    267 
    268 Number of array elements.
    269 
    270 ```javascript
    271 var Uint16Array = require( '@stdlib/array/uint16' );
    272 
    273 // Specify the array configuration:
    274 var buffer = new Uint16Array( [ 1, 2, 3, 4 ] );
    275 var shape = [ 2, 2 ];
    276 var order = 'column-major';
    277 var strides = [ -1, -2 ];
    278 var offset = 3;
    279 
    280 // Create a new ndarray:
    281 var arr = ndarray( 'uint16', buffer, shape, strides, offset, order );
    282 
    283 // Get the array length:
    284 var len = arr.length;
    285 // returns 4
    286 ```
    287 
    288 <a name="prop-ndims"></a>
    289 
    290 #### ndarray.prototype.ndims
    291 
    292 Number of dimensions.
    293 
    294 ```javascript
    295 var Uint8ClampedArray = require( '@stdlib/array/uint8c' );
    296 
    297 // Specify the array configuration:
    298 var buffer = new Uint8ClampedArray( [ 1, 2, 3, 4 ] );
    299 var shape = [ 2, 2 ];
    300 var order = 'row-major';
    301 var strides = [ -2, -1 ];
    302 var offset = 3;
    303 
    304 // Create a new ndarray:
    305 var arr = ndarray( 'uint8c', buffer, shape, strides, offset, order );
    306 
    307 // Get the number of dimensions:
    308 var ndims = arr.ndims;
    309 // returns 2
    310 ```
    311 
    312 <a name="prop-offset"></a>
    313 
    314 #### ndarray.prototype.offset
    315 
    316 Index offset which specifies the `buffer` index at which to start iterating over array elements.
    317 
    318 ```javascript
    319 var Int16Array = require( '@stdlib/array/int16' );
    320 
    321 // Specify the array configuration:
    322 var buffer = new Int16Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] );
    323 var shape = [ 2, 2 ];
    324 var order = 'row-major';
    325 var strides = [ -2, -1 ];
    326 var offset = 10;
    327 
    328 // Create a new ndarray:
    329 var arr = ndarray( 'int16', buffer, shape, strides, offset, order );
    330 
    331 // Get the index offset:
    332 var o = arr.offset;
    333 // returns 10
    334 ```
    335 
    336 <a name="prop-order"></a>
    337 
    338 #### ndarray.prototype.order
    339 
    340 Array order. The array order is either row-major (C-style) or column-major (Fortran-style).
    341 
    342 ```javascript
    343 var Uint32Array = require( '@stdlib/array/uint32' );
    344 
    345 // Specify the array configuration:
    346 var buffer = new Uint32Array( [ 1, 2, 3, 4 ] );
    347 var shape = [ 2, 2 ];
    348 var order = 'row-major';
    349 var strides = [ 2, 1 ];
    350 var offset = 0;
    351 
    352 // Create a new ndarray:
    353 var arr = ndarray( 'uint32', buffer, shape, strides, offset, order );
    354 
    355 // Get the array order:
    356 var ord = arr.order;
    357 // returns 'row-major'
    358 ```
    359 
    360 <a name="prop-shape"></a>
    361 
    362 #### ndarray.prototype.shape
    363 
    364 Returns a copy of the array shape.
    365 
    366 ```javascript
    367 // Specify the array configuration:
    368 var buffer = [ 1, 2, 3, 4, 5, 6 ];
    369 var shape = [ 2, 2 ];
    370 var order = 'row-major';
    371 var strides = [ 2, 1 ];
    372 var offset = 2;
    373 
    374 // Create a new ndarray:
    375 var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
    376 
    377 // Get the array shape:
    378 var dims = arr.shape;
    379 // returns [ 2, 2 ]
    380 ```
    381 
    382 <a name="prop-strides"></a>
    383 
    384 #### ndarray.prototype.strides
    385 
    386 Returns a copy of the array strides which specify how to access data along corresponding array dimensions.
    387 
    388 ```javascript
    389 // Specify the array configuration:
    390 var buffer = [ 1, 2, 3, 4 ];
    391 var shape = [ 2, 2 ];
    392 var order = 'column-major';
    393 var strides = [ -1, 2 ];
    394 var offset = 1;
    395 
    396 // Create a new ndarray:
    397 var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
    398 
    399 // Get the array strides:
    400 var s = arr.strides;
    401 // returns [ -1, 2 ]
    402 ```
    403 
    404 * * *
    405 
    406 ### Methods
    407 
    408 <a name="method-get"></a>
    409 
    410 #### ndarray.prototype.get( i, j, k, ... )
    411 
    412 Returns an array element specified according to provided subscripts. The number of provided subscripts should **equal** the number of dimensions.
    413 
    414 ```javascript
    415 // Specify the array configuration:
    416 var buffer = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
    417 var shape = [ 2, 2 ];
    418 var order = 'row-major';
    419 var strides = [ 2, 1 ];
    420 var offset = 2;
    421 
    422 // Create a new ndarray:
    423 var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
    424 
    425 // Get the element located at (1,1):
    426 var v = arr.get( 1, 1 );
    427 // returns 6
    428 ```
    429 
    430 <a name="method-iget"></a>
    431 
    432 #### ndarray.prototype.iget( idx )
    433 
    434 Returns an array element located at a specified linear index.
    435 
    436 ```javascript
    437 // Specify the array configuration:
    438 var buffer = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
    439 var shape = [ 2, 2 ];
    440 var order = 'row-major';
    441 var strides = [ 2, 1 ];
    442 var offset = 2;
    443 
    444 // Create a new ndarray:
    445 var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
    446 
    447 // Get the element located at index 3:
    448 var v = arr.iget( 3 );
    449 // returns 6
    450 ```
    451 
    452 For zero-dimensional arrays, the input argument is ignored and, for clarity, should **not** be provided.
    453 
    454 <a name="method-set"></a>
    455 
    456 #### ndarray.prototype.set( i, j, k, ..., v )
    457 
    458 Sets an array element specified according to provided subscripts. The number of provided subscripts should **equal** the number of dimensions.
    459 
    460 ```javascript
    461 // Specify the array configuration:
    462 var buffer = [ 1, 2, 3, 4 ];
    463 var shape = [ 2, 2 ];
    464 var order = 'row-major';
    465 var strides = [ 2, 1 ];
    466 var offset = 0;
    467 
    468 // Create a new ndarray:
    469 var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
    470 
    471 // Set the element located at (1,1):
    472 arr.set( 1, 1, 40 );
    473 var v = arr.get( 1, 1 );
    474 // returns 40
    475 
    476 // Get the underlying buffer:
    477 var d = arr.data;
    478 // returns [ 1, 2, 3, 40 ]
    479 ```
    480 
    481 The method returns the `ndarray` instance.
    482 
    483 <a name="method-iset"></a>
    484 
    485 #### ndarray.prototype.iset( idx, v )
    486 
    487 Sets an array element located at a specified linear index.
    488 
    489 ```javascript
    490 // Specify the array configuration:
    491 var buffer = [ 1, 2, 3, 4 ];
    492 var shape = [ 2, 2 ];
    493 var order = 'row-major';
    494 var strides = [ 2, 1 ];
    495 var offset = 0;
    496 
    497 // Create a new ndarray:
    498 var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
    499 
    500 // Set the element located at index 3:
    501 arr.iset( 3, 40 );
    502 var v = arr.iget( 3 );
    503 // returns 40
    504 
    505 // Get the underlying buffer:
    506 var d = arr.data;
    507 // returns [ 1, 2, 3, 40 ]
    508 ```
    509 
    510 For zero-dimensional arrays, the first, and **only**, argument should be the value `v` to set. The method returns the `ndarray` instance.
    511 
    512 <a name="method-to-string"></a>
    513 
    514 #### ndarray.prototype.toString()
    515 
    516 Serializes an `ndarray` as a `string`.
    517 
    518 ```javascript
    519 // Specify the array configuration:
    520 var buffer = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
    521 var shape = [ 3, 2 ];
    522 var order = 'row-major';
    523 var strides = [ 2, 1 ];
    524 var offset = 2;
    525 
    526 // Create a new ndarray:
    527 var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
    528 
    529 // Serialize to a string:
    530 var str = arr.toString();
    531 // returns "ndarray( 'generic', [ 3, 4, 5, 6, 7, 8 ], [ 3, 2 ], [ 2, 1 ], 0, 'row-major' )"
    532 ```
    533 
    534 The method does **not** serialize data outside of the buffer region defined by the array configuration.
    535 
    536 <a name="method-to-json"></a>
    537 
    538 #### ndarray.prototype.toJSON()
    539 
    540 Serializes an `ndarray` as a [JSON][json] `object`. `JSON.stringify()` implicitly calls this method when stringifying an `ndarray` instance.
    541 
    542 ```javascript
    543 // Specify the array configuration:
    544 var buffer = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
    545 var shape = [ 3, 2 ];
    546 var order = 'row-major';
    547 var strides = [ 2, 1 ];
    548 var offset = 2;
    549 
    550 // Create a new ndarray:
    551 var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
    552 
    553 // Serialize to JSON:
    554 var o = arr.toJSON();
    555 // returns { 'type': 'ndarray', 'dtype': 'generic', 'flags': {...}, 'offset': 0, 'order': 'row-major', 'shape': [ 3, 2 ], 'strides': [ 2, 1 ], 'data': [ 3, 4, 5, 6, 7, 8 ] }
    556 ```
    557 
    558 The method does **not** serialize data outside of the buffer region defined by the array configuration.
    559 
    560 </section>
    561 
    562 <!-- /.usage -->
    563 
    564 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    565 
    566 * * *
    567 
    568 <section class="notes">
    569 
    570 ## Notes
    571 
    572 -   A data buffer must be an array-like object (i.e., have a `length` property). For data buffers which are not indexed collections (i.e., collections which cannot support direct index access, such as `buffer[ index ]`; e.g., [`Complex64Array`][@stdlib/array/complex64], [`Complex128Array`][@stdlib/array/complex128], etc), a data buffer should provide `#.get( idx )` and `#.set( v[, idx] )` methods. Note that, for `set` methods, the value to set should be the first argument, followed by the linear index, similar to the native typed array `set` method.
    573 
    574 </section>
    575 
    576 <!-- /.notes -->
    577 
    578 <!-- Package usage examples. -->
    579 
    580 <section class="examples">
    581 
    582 * * *
    583 
    584 ## Examples
    585 
    586 <!-- eslint no-undef: "error" -->
    587 
    588 ```javascript
    589 var Float32Array = require( '@stdlib/array/float32' );
    590 var ndarray = require( '@stdlib/ndarray/base/ctor' );
    591 
    592 // Create a data buffer:
    593 var buffer = new Float32Array( (3*3*3*3) + 100 );
    594 
    595 // Specify the array shape:
    596 var shape = [ 3, 3, 3, 3 ];
    597 
    598 // Specify the array strides:
    599 var strides = [ 27, 9, 3, 1 ];
    600 
    601 // Specify the index offset:
    602 var offset = 4;
    603 
    604 // Specify the order:
    605 var order = 'row-major'; // C-style
    606 
    607 // Create a new ndarray:
    608 var arr = ndarray( 'float32', buffer, shape, strides, offset, order );
    609 
    610 // Retrieve an array value:
    611 var v = arr.get( 1, 2, 1, 2 );
    612 // returns 0.0
    613 
    614 // Set an array value:
    615 arr.set( 1, 2, 1, 2, 10.0 );
    616 
    617 // Retrieve the array value:
    618 v = arr.get( 1, 2, 1, 2 );
    619 // returns 10.0
    620 
    621 // Serialize the array as a string:
    622 var str = arr.toString();
    623 // returns "ndarray( 'float32', new Float32Array( [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ), [ 3, 3, 3, 3 ], [ 27, 9, 3, 1 ], 0, 'row-major' )"
    624 
    625 // Serialize the array as JSON:
    626 str = JSON.stringify( arr.toJSON() );
    627 // returns '{"type":"ndarray","dtype":"float32","flags":{},"order":"row-major","shape":[3,3,3,3],"strides":[27,9,3,1],"data":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}'
    628 ```
    629 
    630 </section>
    631 
    632 <!-- /.examples -->
    633 
    634 <!-- 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. -->
    635 
    636 <section class="references">
    637 
    638 </section>
    639 
    640 <!-- /.references -->
    641 
    642 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    643 
    644 <section class="links">
    645 
    646 [json]: http://www.json.org/
    647 
    648 [@stdlib/ndarray/dtypes]: https://www.npmjs.com/package/@stdlib/ndarray/tree/main/dtypes
    649 
    650 [@stdlib/array/complex64]: https://www.npmjs.com/package/@stdlib/array-complex64
    651 
    652 [@stdlib/array/complex128]: https://www.npmjs.com/package/@stdlib/array-complex128
    653 
    654 </section>
    655 
    656 <!-- /.links -->