Skip to content

Commit 3e956ce

Browse files
committed
feat: add complex/float64/imag
Ref: #2260
1 parent 1ab834c commit 3e956ce

File tree

21 files changed

+1638
-0
lines changed

21 files changed

+1638
-0
lines changed
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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+
# imag
22+
23+
> Return the imaginary component of a double-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 imag = require( '@stdlib/complex/float64/imag' );
41+
```
42+
43+
#### imag( z )
44+
45+
Returns the **imaginary** component of a double-precision complex floating-point number.
46+
47+
```javascript
48+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
49+
50+
var z = new Complex128( 5.0, 3.0 );
51+
var im = imag( z );
52+
// returns 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 Complex128 = require( '@stdlib/complex/float64/ctor' );
79+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
80+
var filledarrayBy = require( '@stdlib/array/filled-by' );
81+
var imag = require( '@stdlib/complex/float64/imag' );
82+
83+
function random() {
84+
return new Complex128( discreteUniform( -10, 10 ), discreteUniform( -10, 10 ) );
85+
}
86+
87+
// Generate an array of random complex numbers:
88+
var x = filledarrayBy( 100, 'complex128', random );
89+
// returns <Complex128Array>
90+
91+
// Retrieve the imaginary component of each complex number...
92+
var z;
93+
var i;
94+
for ( i = 0; i < x.length; i++ ) {
95+
z = x.get( i );
96+
console.log( 'imag(%s) = %d', z.toString(), imag( z ) );
97+
}
98+
```
99+
100+
</section>
101+
102+
<!-- /.examples -->
103+
104+
<!-- C interface documentation. -->
105+
106+
* * *
107+
108+
<section class="c">
109+
110+
## C APIs
111+
112+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
113+
114+
<section class="intro">
115+
116+
</section>
117+
118+
<!-- /.intro -->
119+
120+
<!-- C usage documentation. -->
121+
122+
<section class="usage">
123+
124+
### Usage
125+
126+
```c
127+
#include "stdlib/complex/float64/imag.h"
128+
```
129+
130+
#### stdlib_complex128_imag( z )
131+
132+
Returns the imaginary component of a double-precision complex floating-point number.
133+
134+
```c
135+
#include "stdlib/complex/float64/ctor.h"
136+
137+
stdlib_complex128_t z = stdlib_complex128( 5.0, 2.0 );
138+
139+
// ...
140+
141+
double im = stdlib_complex128_imag( z );
142+
// returns 2.0
143+
```
144+
145+
The function accepts the following arguments:
146+
147+
- **z**: `[in] stdlib_complex128_t` double-precision complex floating-point number.
148+
149+
```c
150+
double stdlib_complex128_imag( const stdlib_complex128_t z );
151+
```
152+
153+
</section>
154+
155+
<!-- /.usage -->
156+
157+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
158+
159+
<section class="notes">
160+
161+
</section>
162+
163+
<!-- /.notes -->
164+
165+
<!-- C API usage examples. -->
166+
167+
<section class="examples">
168+
169+
### Examples
170+
171+
```c
172+
#include "stdlib/complex/float64/imag.h"
173+
#include "stdlib/complex/float64/ctor.h"
174+
#include <stdio.h>
175+
176+
int main( void ) {
177+
const stdlib_complex128_t x[] = {
178+
stdlib_complex128( 5.0, 2.0 ),
179+
stdlib_complex128( -2.0, 1.0 ),
180+
stdlib_complex128( 0.0, -0.0 ),
181+
stdlib_complex128( 0.0/0.0, 0.0/0.0 )
182+
};
183+
184+
int i;
185+
for ( i = 0; i < 4; i++ ) {
186+
printf( "imag(v) = %lf\n", stdlib_complex128_imag( x[ i ] ) );
187+
}
188+
}
189+
```
190+
191+
</section>
192+
193+
<!-- /.examples -->
194+
195+
</section>
196+
197+
<!-- /.c -->
198+
199+
<!-- 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. -->
200+
201+
<section class="references">
202+
203+
</section>
204+
205+
<!-- /.references -->
206+
207+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
208+
209+
<section class="related">
210+
211+
* * *
212+
213+
## See Also
214+
215+
- <span class="package-name">[`@stdlib/complex/real`][@stdlib/complex/real]</span><span class="delimiter">: </span><span class="description">return the real component of a double-precision complex floating-point number.</span>
216+
- <span class="package-name">[`@stdlib/complex/float64/reim`][@stdlib/complex/float64/reim]</span><span class="delimiter">: </span><span class="description">return the real and imaginary components of a double-precision complex floating-point number.</span>
217+
218+
</section>
219+
220+
<!-- /.related -->
221+
222+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
223+
224+
<section class="links">
225+
226+
<!-- <related-links> -->
227+
228+
[@stdlib/complex/real]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/real
229+
230+
[@stdlib/complex/float64/reim]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/float64/reim
231+
232+
<!-- </related-links> -->
233+
234+
</section>
235+
236+
<!-- /.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) 2018 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 Complex128 = require( '@stdlib/complex/float64/ctor' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pkg = require( './../package.json' ).name;
28+
var imag = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var values;
35+
var im;
36+
var i;
37+
38+
values = [
39+
new Complex128( randu(), randu() ),
40+
new Complex128( randu(), randu() ),
41+
new Complex128( randu(), randu() )
42+
];
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
im = imag( values[ i%values.length ] );
47+
if ( isnan( im ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
}
51+
b.toc();
52+
if ( isnan( im ) ) {
53+
b.fail( 'should not return NaN' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});

0 commit comments

Comments
 (0)