Skip to content

Commit 1ab13f0

Browse files
feat: add math/base/special/cpolarf
PR-URL: #7342 Ref: #649 Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 11565b7 commit 1ab13f0

File tree

27 files changed

+2216
-0
lines changed

27 files changed

+2216
-0
lines changed
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
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+
# cpolarf
22+
23+
> Compute the [absolute value][@stdlib/math/base/special/cabsf] and [phase][@stdlib/math/base/special/cphasef] of a single-precision complex floating-point number.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var cpolarf = require( '@stdlib/math/base/special/cpolarf' );
37+
```
38+
39+
#### cpolarf( z )
40+
41+
Computes the [absolute value][@stdlib/math/base/special/cabsf] and [phase][@stdlib/math/base/special/cphasef] of a single-precision complex floating-point number.
42+
43+
```javascript
44+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
45+
46+
var o = cpolarf( new Complex64( 5.0, 3.0 ) );
47+
// returns [ ~5.83, ~0.5404 ]
48+
```
49+
50+
#### cpolarf.assign( z, out, stride, offset )
51+
52+
Computes the [absolute value][@stdlib/math/base/special/cabsf] and [phase][@stdlib/math/base/special/cphasef] of a single-precision complex floating-point number and assigns results to a provided output array.
53+
54+
```javascript
55+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
56+
var Float32Array = require( '@stdlib/array/float32' );
57+
58+
var out = new Float32Array( 2 );
59+
60+
var v = cpolarf.assign( new Complex64( 5.0, 3.0 ), out, 1, 0 );
61+
// returns <Float32Array>[ ~5.83, ~0.5404 ]
62+
63+
var bool = ( v === out );
64+
// returns true
65+
```
66+
67+
</section>
68+
69+
<!-- /.usage -->
70+
71+
<section class="examples">
72+
73+
## Examples
74+
75+
<!-- eslint no-undef: "error" -->
76+
77+
```javascript
78+
var Complex64Array = require( '@stdlib/array/complex64' );
79+
var uniform = require( '@stdlib/random/array/uniform' );
80+
var logEachMap = require( '@stdlib/console/log-each-map' );
81+
var cpolarf = require( '@stdlib/math/base/special/cpolarf' );
82+
83+
// Create an array of random numbers:
84+
var arr = new Complex64Array( uniform( 200, -100.0, 100.0 ) );
85+
86+
// Compute the polar form of each number in the array:
87+
logEachMap( 'cpolarf(%s) = [%s]', arr, cpolarf );
88+
```
89+
90+
</section>
91+
92+
<!-- /.examples -->
93+
94+
<!-- C interface documentation. -->
95+
96+
* * *
97+
98+
<section class="c">
99+
100+
## C APIs
101+
102+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
103+
104+
<section class="intro">
105+
106+
</section>
107+
108+
<!-- /.intro -->
109+
110+
<!-- C usage documentation. -->
111+
112+
<section class="usage">
113+
114+
### Usage
115+
116+
```c
117+
#include "stdlib/math/base/special/cpolarf.h"
118+
```
119+
120+
#### stdlib_base_cpolarf( z, cabsf, cphasef )
121+
122+
Computes the [absolute value][@stdlib/math/base/special/cabsf] and [phase][@stdlib/math/base/special/cphasef] of a single-precision complex floating-point number.
123+
124+
```c
125+
#include "stdlib/complex/float32/ctor.h"
126+
#include "stdlib/complex/float32/real.h"
127+
#include "stdlib/complex/float32/imag.h"
128+
129+
stdlib_complex64_t z = stdlib_complex64( 5.0f, 3.0f );
130+
float cabsf;
131+
float cphasef;
132+
stdlib_base_cpolarf( z, &cabsf, &cphasef );
133+
```
134+
135+
The function accepts the following arguments:
136+
137+
- **z**: `[in] stdlib_complex64_t` input value.
138+
- **cabsf**: `[out] float*` destination for the absolute value.
139+
- **cphasef**: `[out] float*` destination for the phase value in radians.
140+
141+
```c
142+
void stdlib_base_cpolarf( const stdlib_complex64_t z, float *cabsf, float *cphasef );
143+
```
144+
145+
</section>
146+
147+
<!-- /.usage -->
148+
149+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
150+
151+
<section class="notes">
152+
153+
</section>
154+
155+
<!-- /.notes -->
156+
157+
<!-- C API usage examples. -->
158+
159+
<section class="examples">
160+
161+
### Examples
162+
163+
```c
164+
#include "stdlib/math/base/special/cpolarf.h"
165+
#include "stdlib/complex/float32/ctor.h"
166+
#include "stdlib/complex/float32/reim.h"
167+
#include <stdio.h>
168+
169+
int main( void ) {
170+
const stdlib_complex64_t x[] = {
171+
stdlib_complex64( 3.14f, 1.0f ),
172+
stdlib_complex64( -3.14f, -1.0f ),
173+
stdlib_complex64( 0.0f, 0.0f ),
174+
stdlib_complex64( 0.0f/0.0f, 0.0f/0.0f )
175+
};
176+
177+
float cphasef;
178+
float cabsf;
179+
float re;
180+
float im;
181+
int i;
182+
for ( i = 0; i < 4; i++ ) {
183+
stdlib_base_cpolarf( x[i], &cabsf, &cphasef );
184+
stdlib_complex64_reim( x[i], &re, &im );
185+
printf( "cpolarf(%f + %fi) => cabsf: %f, cphasef: %f\n", re, im, cabsf, cphasef );
186+
}
187+
}
188+
```
189+
190+
</section>
191+
192+
<!-- /.examples -->
193+
194+
</section>
195+
196+
<!-- /.c -->
197+
198+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
199+
200+
<section class="related">
201+
202+
</section>
203+
204+
<!-- /.related -->
205+
206+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
207+
208+
<section class="links">
209+
210+
[@stdlib/math/base/special/cabsf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cabsf
211+
212+
[@stdlib/math/base/special/cphasef]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cphasef
213+
214+
<!-- <related-links> -->
215+
216+
<!-- </related-links> -->
217+
218+
</section>
219+
220+
<!-- /.links -->
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 isArray = require( '@stdlib/assert/is-array' );
26+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
27+
var pkg = require( './../package.json' ).name;
28+
var cpolarf = 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 = cpolarf( values[ i%values.length ] );
46+
if ( y.length === 0 ) {
47+
b.fail( 'should not be empty' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isArray( y ) ) {
52+
b.fail( 'should return an array' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
57+
58+
bench( pkg+':assign', function benchmark( b ) {
59+
var values;
60+
var y;
61+
var i;
62+
63+
values = [
64+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
65+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
66+
];
67+
68+
y = [ 0.0, 0.0 ];
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
cpolarf.assign( values[ i%values.length ], y, 1, 0 );
73+
if ( y[ 0 ] !== y[ 0 ] || y[ 1 ] !== y[ 1 ] ) {
74+
b.fail( 'should not be empty' );
75+
}
76+
}
77+
b.toc();
78+
if ( !isArray( y ) ) {
79+
b.fail( 'should return an array' );
80+
}
81+
b.pass( 'benchmark finished' );
82+
b.end();
83+
});
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 isFloat32Array = require( '@stdlib/assert/is-float32array' );
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 cpolarf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( cpolarf 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 = cpolarf( values[ i%values.length ] );
55+
if ( y.length === 0 ) {
56+
b.fail( 'should not be empty' );
57+
}
58+
}
59+
b.toc();
60+
if ( !isFloat32Array( y ) ) {
61+
b.fail( 'should return a Float32Array' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
});

0 commit comments

Comments
 (0)