time-to-botec

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

README.md (11337B)


      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 # Multidimensional Arrays
     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 array = require( '@stdlib/ndarray/array' );
     41 ```
     42 
     43 <a name="main"></a>
     44 
     45 #### array( \[buffer,] \[options] )
     46 
     47 Returns a multidimensional array.
     48 
     49 ```javascript
     50 // Create a 2x2 matrix:
     51 var arr = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
     52 // returns <ndarray>
     53 ```
     54 
     55 To initialize multidimensional array data, provide a `buffer` argument, which may be a [generic array][@stdlib/array/generic], [typed array][@stdlib/array/typed], [Buffer][@stdlib/buffer/ctor], or [ndarray][@stdlib/ndarray/ctor].
     56 
     57 <!-- eslint-disable object-curly-spacing, object-curly-newline -->
     58 
     59 ```javascript
     60 var Float64Array = require( '@stdlib/array/float64' );
     61 var allocUnsafe = require( '@stdlib/buffer/alloc-unsafe' );
     62 
     63 // Create an ndarray from a generic array linear data buffer:
     64 var arr = array( [ 1.0, 2.0, 3.0, 4.0 ], { 'shape': [ 2, 2 ] } );
     65 // returns <ndarray>
     66 
     67 // Create an ndarray from a typed array linear data buffer:
     68 arr = array( new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), { 'shape': [ 2, 2 ] } );
     69 // returns <ndarray>
     70 
     71 // Create an ndarray as a view over a Buffer:
     72 arr = array( allocUnsafe( 4 ), { 'shape': [ 2, 2 ] } );
     73 // returns <ndarray>
     74 
     75 // Create an ndarray from another ndarray:
     76 arr = array( array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ) );
     77 // returns <ndarray>
     78 ```
     79 
     80 The function accepts the following `options`:
     81 
     82 -   `buffer`: data source. If provided along with a `buffer` argument, the argument takes precedence.
     83 
     84 -   `dtype`: underlying storage [data type][@stdlib/ndarray/dtypes]. If not specified and a data source is provided, the data type is inferred from the provided data source. If an input data source is not of the same type, this option specifies the data type to which to cast the input data. For non-[`ndarray`][@stdlib/ndarray/ctor] generic array data sources, the function casts generic array data elements to the default data type. In order to prevent this cast, the `dtype` option **must** be explicitly set to `'generic'`. Any time a cast is required, the `copy` option is set to `true`, as memory must be copied from the data source to an output data buffer. Default: `'float64'`.
     85 
     86 -   `order`: specifies the memory layout of the data source as either row-major (C-style) or column-major (Fortran-style). The option may be one of the following values:
     87 
     88     -   `row-major`: the order of the returned array is row-major.
     89     -   `column-major`: the order of the returned array is column-major.
     90     -   `any`: if a data source is column-major and not row-major, the order of the returned array is column-major; otherwise, the order of the returned array is row-major.
     91     -   `same`: the order of the returned array matches the order of an input data source.
     92 
     93     Note that specifying an order which differs from the order of a provided data source does **not** entail a conversion from one memory layout to another. In short, this option is descriptive, not prescriptive. Default: `'row-major'`.
     94 
     95 -   `shape`: array shape (dimensions). If a shape is not specified, the function attempts to infer a shape based on a provided data source. For example, if provided a nested array, the function resolves nested array dimensions. If provided a multidimensional array data source, the function uses the array's associated shape. For most use cases, such inference suffices. For the remaining use cases, specifying a shape is necessary. For example, provide a shape to create a multidimensional array view over a linear data buffer, ignoring any existing shape meta data associated with a provided data source.
     96 
     97 -   `flatten`: `boolean` indicating whether to automatically flatten generic array data sources. If an array shape is not specified, the shape is inferred from the dimensions of nested arrays prior to flattening. If a use case requires partial flattening, partially flatten **prior** to invoking this function and set the option value to `false` to prevent further flattening during invocation. Default: `true`.
     98 
     99 -   `copy`: `boolean` indicating whether to (shallow) copy source data to a new data buffer. The function does **not** perform a deep copy. To prevent undesired shared changes in state for generic arrays containing objects, perform a deep copy **prior** to invoking this function. Default: `false`.
    100 
    101 -   `ndmin`: specifies the minimum number of dimensions. If an array shape has fewer dimensions than required by `ndmin`, the function **prepends** singleton dimensions to the array shape in order to satisfy the dimensions requirement. Default: `0`.
    102 
    103 -   `casting`: specifies the casting rule used to determine acceptable casts. The option may be one of the following values:
    104 
    105     -   `none`: only allow casting between identical types.
    106     -   `equiv`: allow casting between identical and byte swapped types.
    107     -   `safe`: only allow "safe" casts.
    108     -   `same-kind`: allow "safe" casts and casts within the same kind (e.g., between signed integers or between floats).
    109     -   `unsafe`: allow casting between all types (including between integers and floats).
    110 
    111     Default: `'safe'`.
    112 
    113 -   `mode`: specifies how to handle indices which exceed array dimensions.
    114 
    115     -   `throw`: specifies that an [`ndarray`][@stdlib/ndarray/ctor] instance should throw an error when an index exceeds array dimensions.
    116     -   `wrap`: specifies that an [`ndarray`][@stdlib/ndarray/ctor] instance should wrap around an index exceeding array dimensions using modulo arithmetic.
    117     -   `clamp`: specifies that an [`ndarray`][@stdlib/ndarray/ctor] instance should set an index exceeding array dimensions to either `0` (minimum index) or the maximum index.
    118 
    119     Default: `'throw'`.
    120 
    121 -   `submode`: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions. If provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. Default: `[ options.mode ]`.
    122 
    123 By default, an [`ndarray`][@stdlib/ndarray/ctor] instance **throws** when provided an index which exceeds array dimensions. To support alternative indexing behavior, set the `mode` option, which will affect all public methods for getting and setting array elements.
    124 
    125 ```javascript
    126 var opts = {
    127     'mode': 'clamp'
    128 };
    129 
    130 var arr = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], opts );
    131 // returns <ndarray>
    132 
    133 // Attempt to access an out-of-bounds linear index (clamped):
    134 var v = arr.iget( 10 );
    135 // returns 4.0
    136 ```
    137 
    138 By default, the `mode` option is applied to subscripts which exceed array dimensions. To specify behavior for each dimension, set the `submode` option.
    139 
    140 ```javascript
    141 var opts = {
    142     'submode': [ 'wrap', 'clamp' ]
    143 };
    144 
    145 var arr = array( [ [[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]] ], opts );
    146 // returns <ndarray>
    147 
    148 // Attempt to access out-of-bounds subscripts:
    149 var v = arr.get( -2, 10, -1 ); // linear index: 3
    150 // returns 4.0
    151 ```
    152 
    153 By default, the function automatically flattens [generic array][@stdlib/array/generic] data sources. To prevent flattening, set the `flatten` option to `false`.
    154 
    155 ```javascript
    156 var opts = {
    157     'flatten': false,
    158     'dtype': 'generic'
    159 };
    160 
    161 // Create a generic array which will serve as our ndarray data source:
    162 var buf = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ];
    163 
    164 // Create a 2-element vector:
    165 var arr = array( buf, opts );
    166 // returns <ndarray>
    167 
    168 // Retrieve the first vector element:
    169 var v = arr.get( 0 );
    170 // returns [ 1.0, 2.0 ]
    171 
    172 var bool = ( v === buf[ 0 ] );
    173 // returns true
    174 ```
    175 
    176 </section>
    177 
    178 <!-- /.usage -->
    179 
    180 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    181 
    182 <section class="notes">
    183 
    184 * * *
    185 
    186 ## Notes
    187 
    188 -   The number of elements in a data source `buffer` **must** agree with a specified array `shape` (i.e., the function assumes a single-segment contiguous [`ndarray`][@stdlib/ndarray/ctor]). To create arbitrary multidimensional views over linear data buffers, use a [lower-level constructor][@stdlib/ndarray/ctor].
    189 -   The function supports arbitrary casting between data types. Note, however, that casting from a larger data type to a smaller data type (e.g., `int32` to `int8`) and between signed and unsigned types of the same size should be considered **unsafe**. 
    190 
    191 </section>
    192 
    193 <!-- /.notes -->
    194 
    195 <!-- Package usage examples. -->
    196 
    197 <section class="examples">
    198 
    199 * * *
    200 
    201 ## Examples
    202 
    203 <!-- eslint no-undef: "error" -->
    204 
    205 ```javascript
    206 var array = require( '@stdlib/ndarray/array' );
    207 
    208 // Create a 4-dimensional array containing single-precision floating-point numbers:
    209 var arr = array({
    210     'dtype': 'float32',
    211     'shape': [ 3, 3, 3, 3 ]
    212 });
    213 
    214 // Retrieve an array value:
    215 var v = arr.get( 1, 2, 1, 2 );
    216 // returns 0.0
    217 
    218 // Set an array value:
    219 arr.set( 1, 2, 1, 2, 10.0 );
    220 
    221 // Retrieve the array value:
    222 v = arr.get( 1, 2, 1, 2 );
    223 // returns 10.0
    224 
    225 // Serialize the array as a string:
    226 var str = arr.toString();
    227 // 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' )"
    228 
    229 // Serialize the array as JSON:
    230 str = JSON.stringify( arr.toJSON() );
    231 // 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]}'
    232 ```
    233 
    234 </section>
    235 
    236 <!-- /.examples -->
    237 
    238 <!-- 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. -->
    239 
    240 <section class="references">
    241 
    242 </section>
    243 
    244 <!-- /.references -->
    245 
    246 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    247 
    248 <section class="links">
    249 
    250 [@stdlib/ndarray/dtypes]: https://www.npmjs.com/package/@stdlib/ndarray/tree/main/dtypes
    251 
    252 [@stdlib/ndarray/ctor]: https://www.npmjs.com/package/@stdlib/ndarray/tree/main/ctor
    253 
    254 [@stdlib/array/generic]: https://www.npmjs.com/package/@stdlib/array-generic
    255 
    256 [@stdlib/array/typed]: https://www.npmjs.com/package/@stdlib/array-typed
    257 
    258 [@stdlib/buffer/ctor]: https://www.npmjs.com/package/@stdlib/buffer-ctor
    259 
    260 </section>
    261 
    262 <!-- /.links -->