Skip to content

Commit 93baa0f

Browse files
committed
Add strided utility to apply a binary function to single-precision floating-point strided arrays
1 parent e83ea71 commit 93baa0f

File tree

21 files changed

+3067
-0
lines changed

21 files changed

+3067
-0
lines changed
Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2021 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+
<!-- lint disable maximum-heading-length -->
22+
23+
# smap2
24+
25+
> Apply a binary function accepting and returning single-precision floating-point numbers to corresponding elements in two single-precision floating-point strided input arrays and assign each result to an element in a single-precision floating-point strided output array.
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<section class="usage">
34+
35+
## Usage
36+
37+
```javascript
38+
var smap2 = require( '@stdlib/strided/base/smap2' );
39+
```
40+
41+
#### smap2( N, x, strideX, y, strideY, z, strideZ, fcn )
42+
43+
Applies a binary function accepting and returning single-precision floating-point numbers to corresponding elements in two single-precision floating-point strided input arrays and assigns each result to an element in a single-precision floating-point strided output array.
44+
45+
```javascript
46+
var Float32Array = require( '@stdlib/array/float32' );
47+
var addf = require( '@stdlib/math/base/ops/addf' );
48+
49+
var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
50+
var y = new Float32Array( [ 2.0, 1.0, 3.0, -2.0, 4.0, 1.0, -1.0, 3.0 ] );
51+
var z = new Float32Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
52+
53+
smap2( x.length, x, 1, y, 1, z, 1, addf );
54+
// z => <Float32Array>[ 0.0, 2.0, 6.0, -7.0, 8.0, 1.0, -2.0, 0.0 ]
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **N**: number of indexed elements.
60+
- **x**: input [`Float32Array`][@stdlib/array/float32].
61+
- **strideX**: index increment for `x`.
62+
- **y**: input [`Float32Array`][@stdlib/array/float32].
63+
- **strideY**: index increment for `y`.
64+
- **z**: output [`Float32Array`][@stdlib/array/float32].
65+
- **strideZ**: index increment for `z`.
66+
- **fcn**: function to apply.
67+
68+
The `N` and `stride` parameters determine which strided array elements 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,
69+
70+
```javascript
71+
var Float32Array = require( '@stdlib/array/float32' );
72+
var addf = require( '@stdlib/math/base/ops/addf' );
73+
74+
var x = new Float32Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ] );
75+
var y = new Float32Array( [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] );
76+
var z = new Float32Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
77+
78+
smap2( 3, x, 2, y, -1, z, 1, addf );
79+
// z => <Float32Array>[ 2.0, 0.0, -3.0, 0.0, 0.0, 0.0 ]
80+
```
81+
82+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][@stdlib/array/float32] views.
83+
84+
```javascript
85+
var Float32Array = require( '@stdlib/array/float32' );
86+
var addf = require( '@stdlib/math/base/ops/addf' );
87+
88+
// Initial arrays...
89+
var x0 = new Float32Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ] );
90+
var y0 = new Float32Array( [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] );
91+
var z0 = new Float32Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
92+
93+
// Create offset views...
94+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
95+
var y1 = new Float32Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element
96+
var z1 = new Float32Array( z0.buffer, z0.BYTES_PER_ELEMENT*2 ); // start at 3rd element
97+
98+
smap2( 3, x1, -2, y1, 1, z1, 1, addf );
99+
// z0 => <Float32Array>[ 0.0, 0.0, -4.0, -1.0, 0.0, 0.0 ]
100+
```
101+
102+
#### smap2.ndarray( N, x, strideX, offsetX, y, strideY, offsetY, z, strideZ, offsetZ, fcn )
103+
104+
Applies a binary function accepting and returning single-precision floating-point numbers to corresponding elements in two single-precision floating-point strided input arrays and assigns each result to an element in a single-precision floating-point strided output array using alternative indexing semantics.
105+
106+
```javascript
107+
var Float32Array = require( '@stdlib/array/float32' );
108+
var addf = require( '@stdlib/math/base/ops/addf' );
109+
110+
var x = new Float32Array( [ -1.0, -2.0, -3.0, -4.0, -5.0 ] );
111+
var y = new Float32Array( [ 1.0, 1.0, 2.0, 2.0, 3.0 ] );
112+
var z = new Float32Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] );
113+
114+
smap2.ndarray( x.length, x, 1, 0, y, 1, 0, z, 1, 0, addf );
115+
// y => <Float32Array>[ 0.0, -1.0, -1.0, -2.0, -2.0 ]
116+
```
117+
118+
The function accepts the following addfitional arguments:
119+
120+
- **offsetX**: starting index for `x`.
121+
- **offsetY**: starting index for `y`.
122+
- **offsetZ**: starting index for `z`.
123+
124+
While [`typed array`][@stdlib/array/float32] views mandate a view offset based on the underlying `buffer`, the offset 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` in reverse order,
125+
126+
```javascript
127+
var Float32Array = require( '@stdlib/array/float32' );
128+
var addf = require( '@stdlib/math/base/ops/addf' );
129+
130+
var x = new Float32Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ] );
131+
var y = new Float32Array( [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] );
132+
var z = new Float32Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
133+
134+
smap2.ndarray( 3, x, 2, 1, y, -1, y.length-1, z, 1, 3, addf );
135+
// z => <Float32Array>[ 0.0, 0.0, 0.0, 1.0, -1.0, -1.0 ]
136+
```
137+
138+
</section>
139+
140+
<!-- /.usage -->
141+
142+
<section class="notes">
143+
144+
</section>
145+
146+
<!-- /.notes -->
147+
148+
<section class="examples">
149+
150+
## Examples
151+
152+
<!-- eslint no-undef: "error" -->
153+
154+
```javascript
155+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
156+
var filledarrayBy = require( '@stdlib/array/filled-by' );
157+
var Float32Array = require( '@stdlib/array/float32' );
158+
var addf = require( '@stdlib/math/base/ops/addf' );
159+
var smap2 = require( '@stdlib/strided/base/smap2' );
160+
161+
var x = filledarrayBy( 10, 'float32', discreteUniform( -100, 100 ) );
162+
console.log( x );
163+
164+
var y = filledarrayBy( x.length, 'float32', discreteUniform( -100, 100 ) );
165+
console.log( y );
166+
167+
var z = new Float32Array( x.length );
168+
console.log( z );
169+
170+
smap2.ndarray( x.length, x, 1, 0, y, -1, y.length-1, z, 1, 0, addf );
171+
console.log( z );
172+
```
173+
174+
</section>
175+
176+
<!-- /.examples -->
177+
178+
<!-- C interface documentation. -->
179+
180+
* * *
181+
182+
<section class="c">
183+
184+
## C APIs
185+
186+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
187+
188+
<section class="intro">
189+
190+
</section>
191+
192+
<!-- /.intro -->
193+
194+
<!-- C usage documentation. -->
195+
196+
<section class="usage">
197+
198+
### Usage
199+
200+
```c
201+
#include "stdlib/strided/base/smap2.h"
202+
```
203+
204+
#### stdlib_strided_smap2( N, \*X, strideX, \*Y, strideY, \*Z, strideZ, fcn )
205+
206+
Applies a binary function accepting and returning single-precision floating-point numbers to corresponding elements in two single-precision floating-point strided input arrays and assigns each result to an element in a single-precision floating-point strided output array.
207+
208+
```c
209+
#include <stdint.h>
210+
211+
static float addf( const float x, const float y ) {
212+
return x + y;
213+
}
214+
215+
float X[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f };
216+
float Y[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f };
217+
float Z[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
218+
219+
int64_t N = 6;
220+
221+
stdlib_strided_smap2( N, X, 1, Y, 1, Z, 1, addf );
222+
```
223+
224+
The function accepts the following arguments:
225+
226+
- **N**: `[in] int64_t` number of indexed elements.
227+
- **X**: `[in] float*` input array.
228+
- **strideX** `[in] int64_t` index increment for `X`.
229+
- **Y**: `[in] float*` input array.
230+
- **strideY**: `[in] int64_t` index increment for `Y`.
231+
- **Z**: `[out] float*` output array.
232+
- **strideZ**: `[in] int64_t` index increment for `Z`.
233+
- **fcn**: `[in] float (*fcn)( float, float )` unary function to apply.
234+
235+
```c
236+
void stdlib_strided_smap2( const int64_t N, const float *X, const int64_t strideX, const float *Y, const int64_t strideY, float *Z, const int64_t strideZ, float (*fcn)( float, float ) );
237+
```
238+
239+
</section>
240+
241+
<!-- /.usage -->
242+
243+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
244+
245+
<section class="notes">
246+
247+
</section>
248+
249+
<!-- /.notes -->
250+
251+
<!-- C API usage examples. -->
252+
253+
<section class="examples">
254+
255+
### Examples
256+
257+
```c
258+
#include "stdlib/strided/base/smap2.h"
259+
#include <stdint.h>
260+
#include <stdio.h>
261+
#include <inttypes.h>
262+
263+
// Define a callback:
264+
static float addf( const float x, const float y ) {
265+
return x + y;
266+
}
267+
268+
int main() {
269+
// Create input strided arrays:
270+
float X[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f };
271+
float Y[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f };
272+
273+
// Create an output strided array:
274+
float Z[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
275+
276+
// Specify the number of elements:
277+
int64_t N = 6;
278+
279+
// Define the strides:
280+
int64_t strideX = 1;
281+
int64_t strideY = -1;
282+
int64_t strideZ = 1;
283+
284+
// Apply the callback:
285+
stdlib_strided_smap2( N, X, strideX, Y, strideY, Z, strideZ, addf );
286+
287+
// Print the results:
288+
for ( int64_t i = 0; i < N; i++ ) {
289+
printf( "Z[ %"PRId64" ] = %f\n", i, Z[ i ] );
290+
}
291+
}
292+
```
293+
294+
</section>
295+
296+
<!-- /.examples -->
297+
298+
</section>
299+
300+
<!-- /.c -->
301+
302+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
303+
304+
<section class="related">
305+
306+
</section>
307+
308+
<!-- /.related -->
309+
310+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
311+
312+
<section class="links">
313+
314+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
315+
316+
</section>
317+
318+
<!-- /.links -->

0 commit comments

Comments
 (0)