time-to-botec

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

README.md (5506B)


      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 # abs2
     22 
     23 > Compute the squared [absolute value][absolute-value] for each element in a strided array.
     24 
     25 <section class="intro">
     26 
     27 </section>
     28 
     29 <!-- /.intro -->
     30 
     31 <section class="usage">
     32 
     33 ## Usage
     34 
     35 ```javascript
     36 var abs2 = require( '@stdlib/math/strided/special/abs2' );
     37 ```
     38 
     39 #### abs2( N, x, strideX, y, strideY )
     40 
     41 Computes the squared [absolute value][absolute-value] for each element in `x` and assigns the results to elements in `y`.
     42 
     43 ```javascript
     44 var Float64Array = require( '@stdlib/array/float64' );
     45 
     46 var x = new Float64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
     47 
     48 // Compute the squared absolute values in-place:
     49 abs2( x.length, x, 1, x, 1 );
     50 // x => <Float64Array>[ 4.0, 1.0, 9.0, 25.0, 16.0, 0.0, 1.0, 9.0 ]
     51 ```
     52 
     53 The function accepts the following arguments:
     54 
     55 -   **N**: number of indexed elements.
     56 -   **x**: input array-like object.
     57 -   **strideX**: index increment for `x`.
     58 -   **y**: output array-like object.
     59 -   **strideY**: index increment for `y`.
     60 
     61 The `N` and `stride` parameters determine which elements in `x` and `y` are accessed at runtime. For example, to index every other value in `x` and the first `N` elements of `y` in reverse order,
     62 
     63 ```javascript
     64 var Float64Array = require( '@stdlib/array/float64' );
     65 var floor = require( '@stdlib/math/base/special/floor' );
     66 
     67 var x = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ] );
     68 var y = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
     69 
     70 var N = floor( x.length / 2 );
     71 
     72 abs2( N, x, 2, y, -1 );
     73 // y => <Float64Array>[ 25.0, 9.0, 1.0, 0.0, 0.0, 0.0 ]
     74 ```
     75 
     76 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
     77 
     78 ```javascript
     79 var Float64Array = require( '@stdlib/array/float64' );
     80 var floor = require( '@stdlib/math/base/special/floor' );
     81 
     82 // Initial arrays...
     83 var x0 = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ] );
     84 var y0 = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
     85 
     86 // Create offset views...
     87 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
     88 var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element
     89 
     90 var N = floor( x0.length / 2 );
     91 
     92 abs2( N, x1, -2, y1, 1 );
     93 // y0 => <Float64Array>[ 0.0, 0.0, 0.0, 36.0, 16.0, 4.0 ]
     94 ```
     95 
     96 #### abs2.ndarray( N, x, strideX, offsetX, y, strideY, offsetY )
     97 
     98 Computes the squared [absolute value][absolute-value] for each element in `x` and assigns the result to an element in `y` using alternative indexing semantics.
     99 
    100 ```javascript
    101 var Float64Array = require( '@stdlib/array/float64' );
    102 
    103 var x = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0 ] );
    104 var y = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] );
    105 
    106 abs2.ndarray( x.length, x, 1, 0, y, 1, 0 );
    107 // y => <Float64Array>[ 1.0, 4.0, 9.0, 16.0, 25.0 ]
    108 ```
    109 
    110 The function accepts the following additional arguments:
    111 
    112 -   **offsetX**: starting index for `x`.
    113 -   **offsetY**: starting index for `y`.
    114 
    115 While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offsetX` and `offsetY` parameters support indexing semantics based on starting indices. For example, to index every other value in `x` starting from the second value and to index the last `N` elements in `y`,
    116 
    117 ```javascript
    118 var Float64Array = require( '@stdlib/array/float64' );
    119 var floor = require( '@stdlib/math/base/special/floor' );
    120 
    121 var x = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ] );
    122 var y = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
    123 
    124 var N = floor( x.length / 2 );
    125 
    126 abs2.ndarray( N, x, 2, 1, y, -1, y.length-1 );
    127 // y => <Float64Array>[ 0.0, 0.0, 0.0, 36.0, 16.0, 4.0 ]
    128 ```
    129 
    130 </section>
    131 
    132 <!-- /.usage -->
    133 
    134 <section class="notes">
    135 
    136 ## Notes
    137 
    138 -   For output strided arrays having an integer data type, be careful of overflow, as overflow can lead to unexpected results.
    139 
    140 </section>
    141 
    142 <!-- /.notes -->
    143 
    144 <section class="examples">
    145 
    146 ## Examples
    147 
    148 <!-- eslint no-undef: "error" -->
    149 
    150 ```javascript
    151 var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
    152 var filledarray = require( '@stdlib/array/filled' );
    153 var dtypes = require( '@stdlib/array/dtypes' );
    154 var gfillBy = require( '@stdlib/blas/ext/base/gfill-by' );
    155 var abs2 = require( '@stdlib/math/strided/special/abs2' );
    156 
    157 var dt;
    158 var x;
    159 var y;
    160 var i;
    161 
    162 dt = dtypes();
    163 for ( i = 0; i < dt.length; i++ ) {
    164     x = filledarray( 0.0, 10, dt[ i ] );
    165     gfillBy( x.length, x, 1, discreteUniform( -10, 10 ) );
    166     console.log( x );
    167 
    168     y = filledarray( 0.0, x.length, dt[ i ] );
    169     console.log( y );
    170 
    171     abs2.ndarray( x.length, x, 1, 0, y, -1, y.length-1 );
    172     console.log( y );
    173     console.log( '' );
    174 }
    175 ```
    176 
    177 </section>
    178 
    179 <!-- /.examples -->
    180 
    181 <section class="links">
    182 
    183 [absolute-value]: https://en.wikipedia.org/wiki/Absolute_value
    184 
    185 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    186 
    187 </section>
    188 
    189 <!-- /.links -->