README.md (8531B)
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 # dmap 22 23 > Apply a unary function accepting and returning double-precision floating-point numbers to each element in a double-precision floating-point strided input array and assign each result to an element in a double-precision floating-point strided output 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 dmap = require( '@stdlib/strided/base/dmap' ); 37 ``` 38 39 #### dmap( N, x, strideX, y, strideY, fcn ) 40 41 Applies a unary function accepting and returning double-precision floating-point numbers to each element in a double-precision floating-point strided input array and assigns each result to an element in a double-precision floating-point strided output array. 42 43 ```javascript 44 var Float64Array = require( '@stdlib/array/float64' ); 45 var abs = require( '@stdlib/math/base/special/abs' ); 46 47 var x = new Float64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] ); 48 49 // Compute the absolute values in-place: 50 dmap( x.length, x, 1, x, 1, abs ); 51 // x => <Float64Array>[ 2.0, 1.0, 3.0, 5.0, 4.0, 0.0, 1.0, 3.0 ] 52 ``` 53 54 The function accepts the following arguments: 55 56 - **N**: number of indexed elements. 57 - **x**: input [`Float64Array`][@stdlib/array/float64]. 58 - **strideX**: index increment for `x`. 59 - **y**: output [`Float64Array`][@stdlib/array/float64]. 60 - **strideY**: index increment for `y`. 61 - **fcn**: function to apply. 62 63 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 to index the first `N` elements of `y` in reverse order, 64 65 ```javascript 66 var Float64Array = require( '@stdlib/array/float64' ); 67 var floor = require( '@stdlib/math/base/special/floor' ); 68 var abs = require( '@stdlib/math/base/special/abs' ); 69 70 var x = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ] ); 71 var y = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); 72 73 var N = floor( x.length / 2 ); 74 75 dmap( N, x, 2, y, -1, abs ); 76 // y => <Float64Array>[ 5.0, 3.0, 1.0, 0.0, 0.0, 0.0 ] 77 ``` 78 79 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][@stdlib/array/float64] views. 80 81 ```javascript 82 var Float64Array = require( '@stdlib/array/float64' ); 83 var floor = require( '@stdlib/math/base/special/floor' ); 84 var abs = require( '@stdlib/math/base/special/abs' ); 85 86 // Initial arrays... 87 var x0 = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ] ); 88 var y0 = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); 89 90 // Create offset views... 91 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 92 var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element 93 94 var N = floor( x0.length / 2 ); 95 96 dmap( N, x1, -2, y1, 1, abs ); 97 // y0 => <Float64Array>[ 0.0, 0.0, 0.0, 6.0, 4.0, 2.0 ] 98 ``` 99 100 #### dmap.ndarray( N, x, strideX, offsetX, y, strideY, offsetY, fcn ) 101 102 Applies a unary function accepting and returning double-precision floating-point numbers to each element in a double-precision floating-point strided input array and assigns each result to an element in a double-precision floating-point strided output array using alternative indexing semantics. 103 104 ```javascript 105 var Float64Array = require( '@stdlib/array/float64' ); 106 var abs = require( '@stdlib/math/base/special/abs' ); 107 108 var x = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0 ] ); 109 var y = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ); 110 111 dmap.ndarray( x.length, x, 1, 0, y, 1, 0, abs ); 112 // y => <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ] 113 ``` 114 115 The function accepts the following additional arguments: 116 117 - **offsetX**: starting index for `x`. 118 - **offsetY**: starting index for `y`. 119 120 While [`typed array`][@stdlib/array/float64] 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`, 121 122 ```javascript 123 var Float64Array = require( '@stdlib/array/float64' ); 124 var floor = require( '@stdlib/math/base/special/floor' ); 125 var abs = require( '@stdlib/math/base/special/abs' ); 126 127 var x = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ] ); 128 var y = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); 129 130 var N = floor( x.length / 2 ); 131 132 dmap.ndarray( N, x, 2, 1, y, -1, y.length-1, abs ); 133 // y => <Float64Array>[ 0.0, 0.0, 0.0, 6.0, 4.0, 2.0 ] 134 ``` 135 136 </section> 137 138 <!-- /.usage --> 139 140 <section class="notes"> 141 142 </section> 143 144 <!-- /.notes --> 145 146 <section class="examples"> 147 148 ## Examples 149 150 <!-- eslint no-undef: "error" --> 151 152 ```javascript 153 var round = require( '@stdlib/math/base/special/round' ); 154 var randu = require( '@stdlib/random/base/randu' ); 155 var Float64Array = require( '@stdlib/array/float64' ); 156 var dmap = require( '@stdlib/strided/base/dmap' ); 157 158 function scale( x ) { 159 return x * 10.0; 160 } 161 162 var x = new Float64Array( 10 ); 163 var y = new Float64Array( 10 ); 164 165 var i; 166 for ( i = 0; i < x.length; i++ ) { 167 x[ i ] = round( (randu()*200.0) - 100.0 ); 168 } 169 console.log( x ); 170 console.log( y ); 171 172 dmap.ndarray( x.length, x, 1, 0, y, -1, y.length-1, scale ); 173 console.log( y ); 174 ``` 175 176 </section> 177 178 <!-- /.examples --> 179 180 <!-- C interface documentation. --> 181 182 * * * 183 184 <section class="c"> 185 186 ## C APIs 187 188 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 189 190 <section class="intro"> 191 192 </section> 193 194 <!-- /.intro --> 195 196 <!-- C usage documentation. --> 197 198 <section class="usage"> 199 200 ### Usage 201 202 ```c 203 #include "stdlib/strided/base/dmap.h" 204 ``` 205 206 #### stdlib_strided_dmap( N, \*X, strideX, \*Y, strideY, fcn ) 207 208 Applies a unary function accepting and returning double-precision floating-point numbers to each element in a double-precision floating-point strided input array and assigns each result to an element in a double-precision floating-point strided output array. 209 210 ```c 211 #include <stdint.h> 212 213 static double scale( const double x ) { 214 return x * 10.0; 215 } 216 217 double X[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }; 218 double Y[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; 219 220 int64_t N = 6; 221 222 stdlib_strided_dmap( N, X, 1, Y, 1, scale ); 223 ``` 224 225 The function accepts the following arguments: 226 227 - **N**: `[in] int64_t` number of indexed elements. 228 - **X**: `[in] double*` input array. 229 - **strideX** `[in] int64_t` index increment for `X`. 230 - **Y**: `[out] double*` output array. 231 - **strideY**: `[in] int64_t` index increment for `Y`. 232 - **fcn**: `[in] double (*fcn)( double )` unary function to apply. 233 234 ```c 235 void stdlib_strided_dmap( const int64_t N, const double *X, const int64_t strideX, double *Y, const int64_t strideY, double (*fcn)( double ) ); 236 ``` 237 238 </section> 239 240 <!-- /.usage --> 241 242 <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 243 244 <section class="notes"> 245 246 </section> 247 248 <!-- /.notes --> 249 250 <!-- C API usage examples. --> 251 252 <section class="examples"> 253 254 ### Examples 255 256 ```c 257 #include "stdlib/strided/base/dmap.h" 258 #include <stdint.h> 259 #include <stdio.h> 260 #include <inttypes.h> 261 262 // Define a callback: 263 static double scale( const double x ) { 264 return x * 10.0; 265 } 266 267 int main() { 268 // Create an input strided array: 269 double X[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }; 270 271 // Create an output strided array: 272 double Y[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; 273 274 // Specify the number of elements: 275 int64_t N = 6; 276 277 // Define the strides: 278 int64_t strideX = 1; 279 int64_t strideY = -1; 280 281 // Apply the callback: 282 stdlib_strided_dmap( N, X, strideX, Y, strideY, scale ); 283 284 // Print the results: 285 for ( int64_t i = 0; i < N; i++ ) { 286 printf( "Y[ %"PRId64" ] = %lf\n", i, Y[ i ] ); 287 } 288 } 289 ``` 290 291 </section> 292 293 <!-- /.examples --> 294 295 </section> 296 297 <!-- /.c --> 298 299 <section class="links"> 300 301 [@stdlib/array/float64]: https://www.npmjs.com/package/@stdlib/array-float64 302 303 </section> 304 305 <!-- /.links -->