Skip to content

Commit 5325850

Browse files
committed
feat: add complex/float32/reim
Ref: #2260
1 parent 146dc69 commit 5325850

File tree

19 files changed

+1411
-0
lines changed

19 files changed

+1411
-0
lines changed
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
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+
# reim
22+
23+
> Return the real and imaginary components of a single-precision complex floating-point number.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var reim = require( '@stdlib/complex/float32/reim' );
41+
```
42+
43+
#### reim( z )
44+
45+
Returns the **real** and **imaginary** components of a single-precision complex floating-point number.
46+
47+
```javascript
48+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
49+
50+
var z = new Complex64( 5.0, 3.0 );
51+
var out = reim( z );
52+
// returns <Float32Array>[ 5.0, 3.0 ]
53+
```
54+
55+
</section>
56+
57+
<!-- /.usage -->
58+
59+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
60+
61+
<section class="notes">
62+
63+
</section>
64+
65+
<!-- /.notes -->
66+
67+
<!-- Package usage examples. -->
68+
69+
<section class="examples">
70+
71+
## Examples
72+
73+
<!-- eslint-disable max-len -->
74+
75+
<!-- eslint no-undef: "error" -->
76+
77+
```javascript
78+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
79+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
80+
var filledarrayBy = require( '@stdlib/array/filled-by' );
81+
var reim = require( '@stdlib/complex/float32/reim' );
82+
83+
function random() {
84+
return new Complex64( discreteUniform( -10, 10 ), discreteUniform( -10, 10 ) );
85+
}
86+
87+
// Generate an array of random complex numbers:
88+
var x = filledarrayBy( 100, 'complex64', random );
89+
// returns <Complex64Array>
90+
91+
// Return the real and imaginary components of each complex number...
92+
var out;
93+
var z;
94+
var i;
95+
for ( i = 0; i < x.length; i++ ) {
96+
z = x.get( i );
97+
out = reim( z );
98+
console.log( '%s => %d, %d', z.toString(), out[ 0 ], out[ 1 ] );
99+
}
100+
```
101+
102+
</section>
103+
104+
<!-- /.examples -->
105+
106+
<!-- C interface documentation. -->
107+
108+
* * *
109+
110+
<section class="c">
111+
112+
## C APIs
113+
114+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
115+
116+
<section class="intro">
117+
118+
</section>
119+
120+
<!-- /.intro -->
121+
122+
<!-- C usage documentation. -->
123+
124+
<section class="usage">
125+
126+
### Usage
127+
128+
```c
129+
#include "stdlib/complex/float32/reim.h"
130+
```
131+
132+
#### stdlib_complex64_reim( z, \*re, \*im )
133+
134+
Returns the real and imaginary components of a single-precision complex floating-point number.
135+
136+
```c
137+
#include "stdlib/complex/float32/ctor.h"
138+
139+
stdlib_complex64_t z = stdlib_complex64( 5.0f, 2.0f );
140+
141+
// ...
142+
143+
float re;
144+
float im;
145+
146+
stdlib_complex64_reim( z, &re, &im );
147+
```
148+
149+
The function accepts the following arguments:
150+
151+
- **z**: `[in] stdlib_complex64_t` single-precision complex floating-point number.
152+
- **re**: `[out] float*` destination for real component.
153+
- **im**: `[out] float*` destination for imaginary component.
154+
155+
```c
156+
void stdlib_complex64_reim( const stdlib_complex64_t z, float *re, float *im );
157+
```
158+
159+
</section>
160+
161+
<!-- /.usage -->
162+
163+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
164+
165+
<section class="notes">
166+
167+
</section>
168+
169+
<!-- /.notes -->
170+
171+
<!-- C API usage examples. -->
172+
173+
<section class="examples">
174+
175+
### Examples
176+
177+
```c
178+
#include "stdlib/complex/float32/reim.h"
179+
#include "stdlib/complex/float32/ctor.h"
180+
#include <stdio.h>
181+
182+
int main( void ) {
183+
const stdlib_complex64_t x[] = {
184+
stdlib_complex64( 5.0f, 2.0f ),
185+
stdlib_complex64( -2.0f, 1.0f ),
186+
stdlib_complex64( 0.0f, -0.0f ),
187+
stdlib_complex64( 0.0f/0.0f, 0.0f/0.0f )
188+
};
189+
190+
float re;
191+
float im;
192+
int i;
193+
for ( i = 0; i < 4; i++ ) {
194+
stdlib_complex64_reim( x[ i ], &re, &im );
195+
printf( "reim(v) = %f, %f\n", re, im );
196+
}
197+
}
198+
```
199+
200+
</section>
201+
202+
<!-- /.examples -->
203+
204+
</section>
205+
206+
<!-- /.c -->
207+
208+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
209+
210+
<section class="references">
211+
212+
</section>
213+
214+
<!-- /.references -->
215+
216+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
217+
218+
<section class="related">
219+
220+
* * *
221+
222+
## See Also
223+
224+
- <span class="package-name">[`@stdlib/complex/imagf`][@stdlib/complex/imagf]</span><span class="delimiter">: </span><span class="description">return the imaginary component of a single-precision complex floating-point number.</span>
225+
- <span class="package-name">[`@stdlib/complex/realf`][@stdlib/complex/realf]</span><span class="delimiter">: </span><span class="description">return the real component of a single-precision complex floating-point number.</span>
226+
- <span class="package-name">[`@stdlib/complex/reim`][@stdlib/complex/reim]</span><span class="delimiter">: </span><span class="description">return the real and imaginary components of a double-precision complex floating-point number.</span>
227+
228+
</section>
229+
230+
<!-- /.related -->
231+
232+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
233+
234+
<section class="links">
235+
236+
<!-- <related-links> -->
237+
238+
[@stdlib/complex/imagf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/imagf
239+
240+
[@stdlib/complex/realf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/realf
241+
242+
[@stdlib/complex/reim]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/reim
243+
244+
<!-- </related-links> -->
245+
246+
</section>
247+
248+
<!-- /.links -->
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var isArrayLike = require( '@stdlib/assert/is-array-like' );
27+
var pkg = require( './../package.json' ).name;
28+
var reim = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var values;
35+
var arr;
36+
var i;
37+
38+
values = [
39+
new Complex64( randu(), randu() ),
40+
new Complex64( randu(), randu() ),
41+
new Complex64( randu(), randu() )
42+
];
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
arr = reim( values[ i%values.length ] );
47+
if ( arr.length === 0 ) {
48+
b.fail( 'should not be empty' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isArrayLike( arr ) ) {
53+
b.fail( 'should return an array' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});

0 commit comments

Comments
 (0)