time-to-botec

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

README.md (4545B)


      1 <!--
      2 
      3 @license Apache-2.0
      4 
      5 Copyright (c) 2020 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 # sapx
     22 
     23 > Add a constant to each element in a single-precision floating-point strided array.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var sapx = require( '@stdlib/blas/ext/base/sapx' );
     31 ```
     32 
     33 #### sapx( N, alpha, x, stride )
     34 
     35 Adds a constant `alpha` to each element in a single-precision floating-point strided array `x`.
     36 
     37 ```javascript
     38 var Float32Array = require( '@stdlib/array/float32' );
     39 
     40 var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
     41 
     42 sapx( x.length, 5.0, x, 1 );
     43 // x => <Float32Array>[ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.0 ]
     44 ```
     45 
     46 The function has the following parameters:
     47 
     48 -   **N**: number of indexed elements.
     49 -   **alpha**: scalar constant.
     50 -   **x**: input [`Float32Array`][@stdlib/array/float32].
     51 -   **stride**: index increment.
     52 
     53 The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to add a constant to every other element
     54 
     55 ```javascript
     56 var Float32Array = require( '@stdlib/array/float32' );
     57 var floor = require( '@stdlib/math/base/special/floor' );
     58 
     59 var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
     60 var N = floor( x.length / 2 );
     61 
     62 sapx( N, 5.0, x, 2 );
     63 // x => <Float32Array>[ 3.0, 1.0, 8.0, -5.0, 9.0, 0.0, 4.0, -3.0 ]
     64 ```
     65 
     66 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
     67 
     68 ```javascript
     69 var Float32Array = require( '@stdlib/array/float32' );
     70 var floor = require( '@stdlib/math/base/special/floor' );
     71 
     72 // Initial array...
     73 var x0 = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
     74 
     75 // Create an offset view...
     76 var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
     77 var N = floor( x0.length/2 );
     78 
     79 // Add a constant to every other element...
     80 sapx( N, 5.0, x1, 2 );
     81 // x0 => <Float32Array>[ 1.0, 3.0, 3.0, 1.0, 5.0, -1.0 ]
     82 ```
     83 
     84 #### sapx.ndarray( N, alpha, x, stride, offset )
     85 
     86 Adds a constant `alpha` to each element in a single-precision floating-point strided array `x` using alternative indexing semantics.
     87 
     88 ```javascript
     89 var Float32Array = require( '@stdlib/array/float32' );
     90 
     91 var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
     92 
     93 sapx.ndarray( x.length, 5.0, x, 1, 0 );
     94 // x => <Float32Array>[ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.0 ]
     95 ```
     96 
     97 The function has the following additional parameters:
     98 
     99 -   **offset**: starting index.
    100 
    101 While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to access only the last three elements of `x`
    102 
    103 ```javascript
    104 var Float32Array = require( '@stdlib/array/float32' );
    105 
    106 var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
    107 
    108 sapx.ndarray( 3, 5.0, x, 1, x.length-3 );
    109 // x => <Float32Array>[ 1.0, -2.0, 3.0, 1.0, 10.0, -1.0 ]
    110 ```
    111 
    112 </section>
    113 
    114 <!-- /.usage -->
    115 
    116 <section class="notes">
    117 
    118 ## Notes
    119 
    120 -   If `N <= 0`, both functions return `x` unchanged.
    121 
    122 </section>
    123 
    124 <!-- /.notes -->
    125 
    126 <section class="examples">
    127 
    128 ## Examples
    129 
    130 <!-- eslint no-undef: "error" -->
    131 
    132 ```javascript
    133 var round = require( '@stdlib/math/base/special/round' );
    134 var randu = require( '@stdlib/random/base/randu' );
    135 var Float32Array = require( '@stdlib/array/float32' );
    136 var sapx = require( '@stdlib/blas/ext/base/sapx' );
    137 
    138 var rand;
    139 var sign;
    140 var x;
    141 var i;
    142 
    143 x = new Float32Array( 10 );
    144 for ( i = 0; i < x.length; i++ ) {
    145     rand = round( randu()*100.0 );
    146     sign = randu();
    147     if ( sign < 0.5 ) {
    148         sign = -1.0;
    149     } else {
    150         sign = 1.0;
    151     }
    152     x[ i ] = sign * rand;
    153 }
    154 console.log( x );
    155 
    156 sapx( x.length, 5.0, x, 1 );
    157 console.log( x );
    158 ```
    159 
    160 </section>
    161 
    162 <!-- /.examples -->
    163 
    164 <section class="links">
    165 
    166 [@stdlib/array/float32]: https://www.npmjs.com/package/@stdlib/array-float32
    167 
    168 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    169 
    170 </section>
    171 
    172 <!-- /.links -->