Skip to content

Commit 398ac1c

Browse files
feat: add math/base/special/cphasef
PR-URL: #7158 Ref: #649 Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 0160d55 commit 398ac1c

File tree

33 files changed

+2595
-0
lines changed

33 files changed

+2595
-0
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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 Complex64Array = require( '@stdlib/array/complex64' );
64+
var uniform = require( '@stdlib/random/array/uniform' );
65+
var logEachMap = require( '@stdlib/console/log-each-map' );
66+
var cphasef = require( '@stdlib/math/base/special/cphasef' );
67+
68+
// Create an array of random numbers:
69+
var arr = new Complex64Array( uniform( 200, -100.0, 100.0 ) );
70+
71+
// Compute the inverse of each number in the array:
72+
logEachMap( 'cphasef(%s) = %0.4f', arr, cphasef );
73+
```
74+
75+
</section>
76+
77+
<!-- /.examples -->
78+
79+
<!-- C interface documentation. -->
80+
81+
* * *
82+
83+
<section class="c">
84+
85+
## C APIs
86+
87+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
88+
89+
<section class="intro">
90+
91+
</section>
92+
93+
<!-- /.intro -->
94+
95+
<!-- C usage documentation. -->
96+
97+
<section class="usage">
98+
99+
### Usage
100+
101+
```c
102+
#include "stdlib/math/base/special/cphasef.h"
103+
```
104+
105+
#### stdlib_base_cphasef( z )
106+
107+
Computes the [argument][complex-number-argument] of a single-precision complex floating-point number.
108+
109+
```c
110+
#include "stdlib/complex/float32/ctor.h"
111+
#include "stdlib/complex/float32/real.h"
112+
#include "stdlib/complex/float32/imag.h"
113+
114+
stdlib_complex64_t z = stdlib_complex64( 5.0f, 3.0f );
115+
float out = stdlib_base_cphasef( z );
116+
// returns ~0.5404f
117+
```
118+
119+
The function accepts the following arguments:
120+
121+
- **z**: `[in] stdlib_complex64_t` input value.
122+
123+
```c
124+
float stdlib_base_cphasef( const stdlib_complex64_t z );
125+
```
126+
127+
</section>
128+
129+
<!-- /.usage -->
130+
131+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
132+
133+
<section class="notes">
134+
135+
</section>
136+
137+
<!-- /.notes -->
138+
139+
<!-- C API usage examples. -->
140+
141+
<section class="examples">
142+
143+
### Examples
144+
145+
```c
146+
#include "stdlib/math/base/special/cphasef.h"
147+
#include "stdlib/complex/float32/ctor.h"
148+
#include "stdlib/complex/float32/reim.h"
149+
#include <stdio.h>
150+
151+
int main( void ) {
152+
const stdlib_complex64_t x[] = {
153+
stdlib_complex64( 3.14f, 1.0f ),
154+
stdlib_complex64( -3.14f, -1.0f ),
155+
stdlib_complex64( 0.0f, 0.0f ),
156+
stdlib_complex64( 0.0f/0.0f, 0.0f/0.0f )
157+
};
158+
159+
stdlib_complex64_t v;
160+
float re;
161+
float im;
162+
float y;
163+
int i;
164+
for ( i = 0; i < 4; i++ ) {
165+
v = x[ i ];
166+
y = stdlib_base_cphasef( v );
167+
stdlib_complex64_reim( v, &re, &im );
168+
printf( "cphasef(%f + %fi) = %f\n", re, im, y );
169+
}
170+
}
171+
```
172+
173+
</section>
174+
175+
<!-- /.examples -->
176+
177+
</section>
178+
179+
<!-- /.c -->
180+
181+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
182+
183+
<section class="related">
184+
185+
</section>
186+
187+
<!-- /.related -->
188+
189+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
190+
191+
<section class="links">
192+
193+
[complex-number-argument]: https://en.wikipedia.org/wiki/Argument_%28complex_analysis%29
194+
195+
<!-- <related-links> -->
196+
197+
<!-- </related-links> -->
198+
199+
</section>
200+
201+
<!-- /.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) 2025 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) 2025 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)