Skip to content

Commit e99f5d4

Browse files
anandkaranubckgrytestdlib-bot
authored
feat: add math/base/special/cinvf
PR-URL: #7007 Ref: #649 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent f3d9c6a commit e99f5d4

39 files changed

+3280
-0
lines changed
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
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+
# cinvf
22+
23+
> Compute the inverse of a single-precision complex floating-point number.
24+
25+
<section class="intro">
26+
27+
The inverse (or reciprocal) of a non-zero complex number `z = a + bi` is defined as
28+
29+
<!-- <equation class="equation" label="eq:complex_inverse" align="center" raw="{\frac {1}{z}}=\frac{\bar{z}}{z{\bar{z}}} = \frac{a}{a^{2}+b^{2}} - \frac{b}{a^2+b^2}i." alt="Complex Inverse" > -->
30+
31+
```math
32+
{\frac {1}{z}}=\frac{\bar{z}}{z{\bar{z}}} = \frac{a}{a^{2}+b^{2}} - \frac{b}{a^2+b^2}i.
33+
```
34+
35+
<!-- </equation> -->
36+
37+
</section>
38+
39+
<!-- /.intro -->
40+
41+
<section class="usage">
42+
43+
## Usage
44+
45+
```javascript
46+
var cinvf = require( '@stdlib/math/base/special/cinvf' );
47+
```
48+
49+
#### cinvf( z )
50+
51+
Computes the inverse of a single-precision complex floating-point number.
52+
53+
```javascript
54+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
55+
var realf = require( '@stdlib/complex/float32/real' );
56+
var imagf = require( '@stdlib/complex/float32/imag' );
57+
58+
var v = cinvf( new Complex64( 2.0, 4.0 ) );
59+
// returns <Complex64>
60+
61+
var re = realf( v );
62+
// returns ~0.1
63+
64+
var im = imagf( v );
65+
// returns ~-0.2
66+
```
67+
68+
</section>
69+
70+
<!-- /.usage -->
71+
72+
<section class="examples">
73+
74+
## Examples
75+
76+
<!-- eslint no-undef: "error" -->
77+
78+
```javascript
79+
var Complex64Array = require( '@stdlib/array/complex64' );
80+
var uniform = require( '@stdlib/random/array/uniform' );
81+
var logEachMap = require( '@stdlib/console/log-each-map' );
82+
var cinvf = require( '@stdlib/math/base/special/cinvf' );
83+
84+
// Create an array of random numbers:
85+
var arr = new Complex64Array( uniform( 200, -100.0, 100.0 ) );
86+
87+
// Compute the inverse of each number in the array:
88+
logEachMap( '1.0 / (%s) = %s', arr, cinvf );
89+
```
90+
91+
</section>
92+
93+
<!-- /.examples -->
94+
95+
<!-- C interface documentation. -->
96+
97+
* * *
98+
99+
<section class="c">
100+
101+
## C APIs
102+
103+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
104+
105+
<section class="intro">
106+
107+
</section>
108+
109+
<!-- /.intro -->
110+
111+
<!-- C usage documentation. -->
112+
113+
<section class="usage">
114+
115+
### Usage
116+
117+
```c
118+
#include "stdlib/math/base/special/cinvf.h"
119+
```
120+
121+
#### stdlib_base_cinvf( z )
122+
123+
Computes the inverse of a single-precision complex floating-point number.
124+
125+
```c
126+
#include "stdlib/complex/float32/ctor.h"
127+
#include "stdlib/complex/float32/real.h"
128+
#include "stdlib/complex/float32/imag.h"
129+
130+
stdlib_complex64_t z = stdlib_complex64( 2.0f, 4.0f );
131+
132+
stdlib_complex64_t out = stdlib_base_cinvf( z );
133+
134+
float re = stdlib_complex64_real( out );
135+
// returns 0.1f
136+
137+
float im = stdlib_complex64_imag( out );
138+
// returns -0.2f
139+
```
140+
141+
The function accepts the following arguments:
142+
143+
- **z**: `[in] stdlib_complex64_t` input value.
144+
145+
```c
146+
stdlib_complex64_t stdlib_base_cinvf( const stdlib_complex64_t z );
147+
```
148+
149+
</section>
150+
151+
<!-- /.usage -->
152+
153+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
154+
155+
<section class="notes">
156+
157+
</section>
158+
159+
<!-- /.notes -->
160+
161+
<!-- C API usage examples. -->
162+
163+
<section class="examples">
164+
165+
### Examples
166+
167+
```c
168+
#include "stdlib/math/base/special/cinvf.h"
169+
#include "stdlib/complex/float32/ctor.h"
170+
#include "stdlib/complex/float32/reim.h"
171+
#include <stdio.h>
172+
173+
int main( void ) {
174+
const stdlib_complex64_t x[] = {
175+
stdlib_complex64( 3.14f, 1.5f ),
176+
stdlib_complex64( -3.14f, -1.5f ),
177+
stdlib_complex64( 0.0f, 0.0f ),
178+
stdlib_complex64( 0.0f/0.0f, 0.0f/0.0f )
179+
};
180+
181+
stdlib_complex64_t v;
182+
stdlib_complex64_t y;
183+
float re1;
184+
float im1;
185+
float re2;
186+
float im2;
187+
int i;
188+
for ( i = 0; i < 4; i++ ) {
189+
v = x[ i ];
190+
y = stdlib_base_cinvf( v );
191+
stdlib_complex64_reim( v, &re1, &im1 );
192+
stdlib_complex64_reim( y, &re2, &im2 );
193+
printf( "cinvf(%f + %fi) = %f + %fi\n", re1, im1, re2, im2 );
194+
}
195+
}
196+
```
197+
198+
</section>
199+
200+
<!-- /.examples -->
201+
202+
</section>
203+
204+
<!-- /.c -->
205+
206+
* * *
207+
208+
<section class="references">
209+
210+
## References
211+
212+
- Smith, Robert L. 1962. "Algorithm 116: Complex Division." _Commun. ACM_ 5 (8). New York, NY, USA: ACM: 435. doi:[10.1145/368637.368661][@smith:1962a].
213+
- Stewart, G. W. 1985. "A Note on Complex Division." _ACM Trans. Math. Softw._ 11 (3). New York, NY, USA: ACM: 238–41. doi:[10.1145/214408.214414][@stewart:1985a].
214+
- Priest, Douglas M. 2004. "Efficient Scaling for Complex Division." _ACM Trans. Math. Softw._ 30 (4). New York, NY, USA: ACM: 389–401. doi:[10.1145/1039813.1039814][@priest:2004a].
215+
- Baudin, Michael, and Robert L. Smith. 2012. "A Robust Complex Division in Scilab." _arXiv_ abs/1210.4539 \[cs.MS] (October): 1–25. [&lt;https://arxiv.org/abs/1210.4539>][@baudin:2012a].
216+
217+
</section>
218+
219+
<!-- /.references -->
220+
221+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
222+
223+
<section class="related">
224+
225+
</section>
226+
227+
<!-- /.related -->
228+
229+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
230+
231+
<section class="links">
232+
233+
[@smith:1962a]: https://doi.org/10.1145/368637.368661
234+
235+
[@stewart:1985a]: https://doi.org/10.1145/214408.214414
236+
237+
[@priest:2004a]: https://doi.org/10.1145/1039813.1039814
238+
239+
[@baudin:2012a]: https://arxiv.org/abs/1210.4539
240+
241+
<!-- <related-links> -->
242+
243+
<!-- </related-links> -->
244+
245+
</section>
246+
247+
<!-- /.links -->
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
27+
var realf = require( '@stdlib/complex/float32/real' );
28+
var imagf = require( '@stdlib/complex/float32/imag' );
29+
var pkg = require( './../package.json' ).name;
30+
var cinvf = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg, function benchmark( b ) {
36+
var values;
37+
var y;
38+
var i;
39+
40+
values = [
41+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
42+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
43+
];
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
y = cinvf( values[ i%values.length ] );
48+
if ( isnanf( realf( y ) ) ) {
49+
b.fail( 'should not return NaN' );
50+
}
51+
}
52+
b.toc();
53+
if ( isnanf( imagf( y ) ) ) {
54+
b.fail( 'should not return not NaN' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 realf = require( '@stdlib/complex/float32/real' );
28+
var imagf = require( '@stdlib/complex/float32/imag' );
29+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
30+
var tryRequire = require( '@stdlib/utils/try-require' );
31+
var pkg = require( './../package.json' ).name;
32+
33+
34+
// VARIABLES //
35+
36+
var cinvf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
37+
var opts = {
38+
'skip': ( cinvf instanceof Error )
39+
};
40+
41+
42+
// MAIN //
43+
44+
bench( pkg+'::native', opts, function benchmark( b ) {
45+
var values;
46+
var y;
47+
var i;
48+
49+
values = [
50+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
51+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
52+
];
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
y = cinvf( values[ i%values.length ] );
57+
if ( isnanf( realf( y ) ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnanf( imagf( y ) ) ) {
63+
b.fail( 'should not return not NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});

0 commit comments

Comments
 (0)