Skip to content

Commit 7293c7b

Browse files
committed
feat: add math/base/special/cphasef
1 parent ccff6bb commit 7293c7b

File tree

33 files changed

+2593
-0
lines changed

33 files changed

+2593
-0
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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+
# cphasef
22+
23+
> Compute the [argument][complex-number-argument] of a single-precision complex floating-point number in radians.
24+
25+
<section class="intro">
26+
27+
The [argument][complex-number-argument] of a complex number, also known as the **phase**, is the angle of the radius extending from the origin to the complex number plotted in the complex plane and the positive real axis.
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<section class="usage">
34+
35+
## Usage
36+
37+
```javascript
38+
var cphasef = require( '@stdlib/math/base/special/cphasef' );
39+
```
40+
41+
#### cphasef( z )
42+
43+
Computes the [argument][complex-number-argument] of a single-precision complex floating-point number.
44+
45+
```javascript
46+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
47+
48+
var phi = cphasef( new Complex64( 5.0, 3.0 ) );
49+
// returns ~0.5404
50+
```
51+
52+
</section>
53+
54+
<!-- /.usage -->
55+
56+
<section class="examples">
57+
58+
## Examples
59+
60+
<!-- eslint no-undef: "error" -->
61+
62+
```javascript
63+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
64+
var uniform = require( '@stdlib/random/base/uniform' );
65+
var cphasef = require( '@stdlib/math/base/special/cphasef' );
66+
67+
var z;
68+
var i;
69+
70+
for ( i = 0; i < 100; i++ ) {
71+
z = new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) );
72+
console.log( 'arg(%s) = %d', z.toString(), cphasef( z ) );
73+
}
74+
```
75+
76+
</section>
77+
78+
<!-- /.examples -->
79+
80+
<!-- C interface documentation. -->
81+
82+
* * *
83+
84+
<section class="c">
85+
86+
## C APIs
87+
88+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
89+
90+
<section class="intro">
91+
92+
</section>
93+
94+
<!-- /.intro -->
95+
96+
<!-- C usage documentation. -->
97+
98+
<section class="usage">
99+
100+
### Usage
101+
102+
```c
103+
#include "stdlib/math/base/special/cphasef.h"
104+
```
105+
106+
#### stdlib_base_cphasef( z )
107+
108+
Computes the [argument][complex-number-argument] of a single-precision complex floating-point number.
109+
110+
```c
111+
#include "stdlib/complex/float32/ctor.h"
112+
#include "stdlib/complex/float32/real.h"
113+
#include "stdlib/complex/float32/imag.h"
114+
115+
stdlib_complex64_t z = stdlib_complex64( 5.0f, 3.0f );
116+
float out = stdlib_base_cphasef( z );
117+
// returns ~0.5404f
118+
```
119+
120+
The function accepts the following arguments:
121+
122+
- **z**: `[in] stdlib_complex64_t` input value.
123+
124+
```c
125+
float stdlib_base_cphasef( const stdlib_complex64_t z );
126+
```
127+
128+
</section>
129+
130+
<!-- /.usage -->
131+
132+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
133+
134+
<section class="notes">
135+
136+
</section>
137+
138+
<!-- /.notes -->
139+
140+
<!-- C API usage examples. -->
141+
142+
<section class="examples">
143+
144+
### Examples
145+
146+
```c
147+
#include "stdlib/math/base/special/cphase.h"
148+
#include "stdlib/complex/float32/ctor.h"
149+
#include "stdlib/complex/float32/reim.h"
150+
#include <stdio.h>
151+
152+
int main( void ) {
153+
const stdlib_complex64_t x[] = {
154+
stdlib_complex64( 3.14f, 1.0f ),
155+
stdlib_complex64( -3.14f, -1.0f ),
156+
stdlib_complex64( 0.0f, 0.0f ),
157+
stdlib_complex64( 0.0f/0.0f, 0.0f/0.0f )
158+
};
159+
160+
stdlib_complex64_t v;
161+
float re;
162+
float im;
163+
float y;
164+
int i;
165+
for ( i = 0; i < 4; i++ ) {
166+
v = x[ i ];
167+
y = stdlib_base_cphasef( v );
168+
stdlib_complex64_reim( v, &re, &im );
169+
printf( "fcphasef(%f + %lfi) = %f\n", re, im, y );
170+
}
171+
}
172+
```
173+
174+
</section>
175+
176+
<!-- /.examples -->
177+
178+
</section>
179+
180+
<!-- /.c -->
181+
182+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
183+
184+
<section class="related">
185+
186+
</section>
187+
188+
<!-- /.related -->
189+
190+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
191+
192+
<section class="links">
193+
194+
[complex-number-argument]: https://en.wikipedia.org/wiki/Argument_%28complex_analysis%29
195+
196+
<!-- <related-links> -->
197+
198+
<!-- </related-links> -->
199+
200+
</section>
201+
202+
<!-- /.links -->
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 uniform = require( '@stdlib/random/base/uniform' );
25+
var Complex64 = require( '@stdlib/complex/float64/ctor' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var pkg = require( './../package.json' ).name;
28+
var cphasef = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var values;
35+
var y;
36+
var i;
37+
38+
values = [
39+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
40+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
41+
];
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
y = cphasef( values[ i%values.length ] );
46+
if ( isnanf( y ) ) {
47+
b.fail( 'should not return NaN' );
48+
}
49+
}
50+
b.toc();
51+
if ( isnanf( y ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/base/uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var cphasef = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( cphasef instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( pkg+'::native', opts, function benchmark( b ) {
43+
var values;
44+
var y;
45+
var i;
46+
47+
values = [
48+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
49+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
50+
];
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
y = cphasef( values[ i%values.length ] );
55+
if ( isnanf( y ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
}
59+
b.toc();
60+
if ( isnanf( y ) ) {
61+
b.fail( 'should not return NaN' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
});

0 commit comments

Comments
 (0)