Skip to content

Commit 8001cae

Browse files
committed
Add strided interface to apply a unary function to a double-precision complex floating-point strided array
1 parent 7fad933 commit 8001cae

File tree

21 files changed

+3094
-0
lines changed

21 files changed

+3094
-0
lines changed
Lines changed: 364 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,364 @@
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+
# zmap
22+
23+
> Apply a unary function to a double-precision floating-point strided input array and assign results to 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 zmap = require( '@stdlib/strided/base/zmap' );
37+
```
38+
39+
#### zmap( N, x, strideX, y, strideY, fcn )
40+
41+
Applies a unary function to a double-precision complex floating-point strided input array and assigns results to a double-precision complex floating-point strided output array.
42+
43+
```javascript
44+
var Complex128Array = require( '@stdlib/array/complex128' );
45+
var real = require( '@stdlib/complex/real' )
46+
var imag = require( '@stdlib/complex/imag' );
47+
var cceil = require( '@stdlib/math/base/special/cceil' );
48+
49+
var x = new Complex128Array( [ -2.3, 1.5, 3.1, -5.2, 4.8, 0.0, -1.6, 3.4 ] );
50+
var y = new Complex128Array( x.length );
51+
52+
zmap( x.length, x, 1, y, 1, cceil );
53+
54+
var v = y.get( 0 );
55+
// returns <Complex128>
56+
57+
var re = real( v );
58+
// returns -2.0
59+
60+
var im = imag( v );
61+
// returns 2.0
62+
```
63+
64+
The function accepts the following arguments:
65+
66+
- **N**: number of indexed elements.
67+
- **x**: input [`Complex128Array`][@stdlib/array/complex128].
68+
- **strideX**: index increment for `x`.
69+
- **y**: output [`Complex128Array`][@stdlib/array/complex128].
70+
- **strideY**: index increment for `y`.
71+
- **fcn**: function to apply.
72+
73+
The `N` and stride parameters determine which elements in the strided arrays 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,
74+
75+
```javascript
76+
var Complex128Array = require( '@stdlib/array/complex128' );
77+
var real = require( '@stdlib/complex/real' )
78+
var imag = require( '@stdlib/complex/imag' );
79+
var cceil = require( '@stdlib/math/base/special/cceil' );
80+
81+
var x = new Complex128Array( [ -2.3, 1.5, 3.1, -5.2, 4.8, 0.0, -1.6, 3.4 ] );
82+
var y = new Complex128Array( x.length );
83+
84+
zmap( 2, x, 2, y, -1, cceil );
85+
86+
var v = y.get( 0 );
87+
// returns <Complex128>
88+
89+
var re = real( v );
90+
// returns 5.0
91+
92+
var im = imag( v );
93+
// returns 0.0
94+
```
95+
96+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][@stdlib/array/complex128] views.
97+
98+
```javascript
99+
var Complex128Array = require( '@stdlib/array/complex128' );
100+
var real = require( '@stdlib/complex/real' )
101+
var imag = require( '@stdlib/complex/imag' );
102+
var cceil = require( '@stdlib/math/base/special/cceil' );
103+
104+
// Initial arrays...
105+
var x0 = new Complex128Array( [ -2.3, 1.5, 3.1, -5.2, 4.8, 0.0, -1.6, 3.4 ] );
106+
var y0 = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
107+
108+
// Create offset views...
109+
var x1 = new Complex128Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
110+
var y1 = new Complex128Array( y0.buffer, y0.BYTES_PER_ELEMENT*2 ); // start at 3rd element
111+
112+
zmap( 2, x1, -2, y1, 1, cceil );
113+
114+
var v = y.get( 0 );
115+
// returns <Complex128>
116+
117+
var re = real( v );
118+
// returns -1.0
119+
120+
var im = imag( v );
121+
// returns 4.0
122+
```
123+
124+
#### zmap.ndarray( N, x, strideX, offsetX, y, strideY, offsetY, fcn )
125+
126+
Applies a unary function to a double-precision complex floating-point strided input array and assigns results to a double-precision complex floating-point strided output array using alternative indexing semantics.
127+
128+
```javascript
129+
var Complex128Array = require( '@stdlib/array/complex128' );
130+
var real = require( '@stdlib/complex/real' )
131+
var imag = require( '@stdlib/complex/imag' );
132+
var cceil = require( '@stdlib/math/base/special/cceil' );
133+
134+
var x = new Complex128Array( [ -2.3, 1.5, 3.1, -5.2, 4.8, 0.0, -1.6, 3.4 ] );
135+
var y = new Complex128Array( x.length );
136+
137+
zmap.ndarray( x.length, x, 1, 0, y, 1, 0, cceil );
138+
139+
var v = y.get( 0 );
140+
// returns <Complex128>
141+
142+
var re = real( v );
143+
// returns -2.0
144+
145+
var im = imag( v );
146+
// returns 2.0
147+
```
148+
149+
The function accepts the following additional arguments:
150+
151+
- **offsetX**: starting index for `x`.
152+
- **offsetY**: starting index for `y`.
153+
154+
While [`typed array`][@stdlib/array/complex128] 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,
155+
156+
```javascript
157+
var Complex128Array = require( '@stdlib/array/complex128' );
158+
var real = require( '@stdlib/complex/real' )
159+
var imag = require( '@stdlib/complex/imag' );
160+
var cceil = require( '@stdlib/math/base/special/cceil' );
161+
162+
var x = new Complex128Array( [ -2.3, 1.5, 3.1, -5.2, 4.8, 0.0, -1.6, 3.4 ] );
163+
var y = new Complex128Array( x.length );
164+
165+
zmap.ndarray( 2, x, 2, 1, y, -1, y.length-1, cceil );
166+
167+
var v = y.get( y.length-1 );
168+
// returns <Complex128>
169+
170+
var re = real( v );
171+
// returns 4.0
172+
173+
var im = imag( v );
174+
// returns -5.0
175+
```
176+
177+
</section>
178+
179+
<!-- /.usage -->
180+
181+
<section class="notes">
182+
183+
</section>
184+
185+
<!-- /.notes -->
186+
187+
<section class="examples">
188+
189+
## Examples
190+
191+
<!-- eslint no-undef: "error" -->
192+
193+
```javascript
194+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
195+
var Complex128Array = require( '@stdlib/array/complex128' );
196+
var filledarrayBy = require( '@stdlib/array/filled-by' );
197+
var real = require( '@stdlib/complex/real' );
198+
var imag = require( '@stdlib/complex/imag' );
199+
var Complex128 = require( '@stdlib/complex/float64' );
200+
var zmap = require( '@stdlib/strided/base/zmap' );
201+
202+
function scale( x ) {
203+
var re = real( x );
204+
var im = imag( x );
205+
return new Complex128( re*10.0, im*10.0 );
206+
}
207+
208+
var xbuf = filledarrayBy( 10*2, 'float64', discreteUniform( -100.0, 100.0 ) );
209+
var x = new Complex128Array( xbuf.buffer );
210+
console.log( x );
211+
212+
var y = new Complex128Array( x.length );
213+
console.log( y );
214+
215+
zmap.ndarray( x.length, x, 1, 0, y, -1, y.length-1, scale );
216+
console.log( y );
217+
```
218+
219+
</section>
220+
221+
<!-- /.examples -->
222+
223+
<!-- C interface documentation. -->
224+
225+
* * *
226+
227+
<section class="c">
228+
229+
## C APIs
230+
231+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
232+
233+
<section class="intro">
234+
235+
</section>
236+
237+
<!-- /.intro -->
238+
239+
<!-- C usage documentation. -->
240+
241+
<section class="usage">
242+
243+
### Usage
244+
245+
```c
246+
#include "stdlib/strided/base/zmap.h"
247+
```
248+
249+
#### stdlib_strided_zmap( N, \*X, strideX, \*Y, strideY, fcn )
250+
251+
Applies a unary function to a double-precision complex floating-point strided input array and assigns results to a double-precision complex floating-point strided output array.
252+
253+
```c
254+
#include <stdint.h>
255+
#include <complex.h>
256+
257+
static double complex scale( const double complex x ) {
258+
double re = creal( x );
259+
double im = cimag( x );
260+
return ( re+10.0 ) + ( im+10.0 )*I;
261+
}
262+
263+
double complex X[] = { 1.0+1.0*I, 2.0+2.0*I, 3.0+3.0*I, 4.0+4.0*I, 5.0+5.0*I, 6.0+6.0*I };
264+
double complex Y[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
265+
266+
int64_t N = 6;
267+
268+
stdlib_strided_zmap( N, X, 1, Y, 1, scale );
269+
```
270+
271+
The function accepts the following arguments:
272+
273+
- **N**: `[in] int64_t` number of indexed elements.
274+
- **X**: `[in] double complex*` input array.
275+
- **strideX** `[in] int64_t` index increment for `X`.
276+
- **Y**: `[out] double complex*` output array.
277+
- **strideY**: `[in] int64_t` index increment for `Y`.
278+
- **fcn**: `[in] double complex (*fcn)( double complex )` unary function to apply.
279+
280+
```c
281+
void stdlib_strided_zmap( const int64_t N, const double complex *X, const int64_t strideX, double complex *Y, const int64_t strideY, double complex (*fcn)( double complex ) );
282+
```
283+
284+
</section>
285+
286+
<!-- /.usage -->
287+
288+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
289+
290+
<section class="notes">
291+
292+
</section>
293+
294+
<!-- /.notes -->
295+
296+
<!-- C API usage examples. -->
297+
298+
<section class="examples">
299+
300+
### Examples
301+
302+
```c
303+
#include "stdlib/strided/base/zmap.h"
304+
#include <stdint.h>
305+
#include <stdio.h>
306+
#include <inttypes.h>
307+
#include <complex.h>
308+
309+
// Define a callback:
310+
static double complex scale( const double complex x ) {
311+
double re = creal( x );
312+
double im = cimag( x );
313+
return ( re+10.0 ) + ( im+10.0 )*I;
314+
}
315+
316+
int main() {
317+
// Create an input strided array:
318+
double complex X[] = { 1.0+1.0*I, 2.0+2.0*I, 3.0+3.0*I, 4.0+4.0*I, 5.0+5.0*I, 6.0+6.0*I };
319+
320+
// Create an output strided array:
321+
double complex Y[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
322+
323+
// Specify the number of elements:
324+
int64_t N = 6;
325+
326+
// Define the strides:
327+
int64_t strideX = 1;
328+
int64_t strideY = -1;
329+
330+
// Apply the callback:
331+
stdlib_strided_zmap( N, X, strideX, Y, strideY, scale );
332+
333+
// Print the results:
334+
for ( int64_t i = 0; i < N; i++ ) {
335+
printf( "Y[ %"PRId64" ] = %lf + %lfi\n", i, creal( Y[i] ), cimag( Y[i] ) );
336+
}
337+
}
338+
```
339+
340+
</section>
341+
342+
<!-- /.examples -->
343+
344+
</section>
345+
346+
<!-- /.c -->
347+
348+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
349+
350+
<section class="related">
351+
352+
</section>
353+
354+
<!-- /.related -->
355+
356+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
357+
358+
<section class="links">
359+
360+
[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128
361+
362+
</section>
363+
364+
<!-- /.links -->

0 commit comments

Comments
 (0)