Skip to content

Commit 3ff2dfa

Browse files
committed
feat: add C implementation for math/base/special/cinvf
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent fd578c4 commit 3ff2dfa

39 files changed

+3287
-0
lines changed
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
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+
# 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 Complex64 = require( '@stdlib/complex/float32/ctor' );
80+
var uniform = require( '@stdlib/random/base/uniform' );
81+
var cinvf = require( '@stdlib/math/base/special/cinvf' );
82+
83+
var z1;
84+
var z2;
85+
var i;
86+
87+
for ( i = 0; i < 100; i++ ) {
88+
z1 = new Complex64( uniform( -50.0, 50.0 ), uniform( -50.0, 50.0 ) );
89+
z2 = cinvf( z1 );
90+
91+
console.log( '1.0 / (%s) = %s', z1.toString(), z2.toString() );
92+
}
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
<!-- C interface documentation. -->
100+
101+
* * *
102+
103+
<section class="c">
104+
105+
## C APIs
106+
107+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
108+
109+
<section class="intro">
110+
111+
</section>
112+
113+
<!-- /.intro -->
114+
115+
<!-- C usage documentation. -->
116+
117+
<section class="usage">
118+
119+
### Usage
120+
121+
```c
122+
#include "stdlib/math/base/special/cinvf.h"
123+
```
124+
125+
#### stdlib_base_cinvf( z )
126+
127+
Computes the inverse of a single-precision complex floating-point number.
128+
129+
```c
130+
#include "stdlib/complex/float32/ctor.h"
131+
#include "stdlib/complex/float32/real.h"
132+
#include "stdlib/complex/float32/imag.h"
133+
134+
stdlib_complex64_t z = stdlib_complex64( 2.0f, 4.0f );
135+
136+
stdlib_complex64_t out = stdlib_base_cinvf( z );
137+
138+
float re = stdlib_complex64_real( out );
139+
// returns 0.1f
140+
141+
float im = stdlib_complex64_imag( out );
142+
// returns -0.2f
143+
```
144+
145+
The function accepts the following arguments:
146+
147+
- **z**: `[in] stdlib_complex64_t` input value.
148+
149+
```c
150+
stdlib_complex64_t stdlib_base_cinvf( const stdlib_complex64_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/math/base/special/cinvf.h"
173+
#include "stdlib/complex/float32/ctor.h"
174+
#include "stdlib/complex/float32/reim.h"
175+
#include <stdio.h>
176+
177+
int main( void ) {
178+
const stdlib_complex64_t x[] = {
179+
stdlib_complex64( 3.14f, 1.5f ),
180+
stdlib_complex64( -3.14f, -1.5f ),
181+
stdlib_complex64( 0.0f, 0.0f ),
182+
stdlib_complex64( 0.0f/0.0f, 0.0f/0.0f )
183+
};
184+
185+
stdlib_complex64_t v;
186+
stdlib_complex64_t y;
187+
float re1;
188+
float im1;
189+
float re2;
190+
float im2;
191+
int i;
192+
for ( i = 0; i < 4; i++ ) {
193+
v = x[ i ];
194+
y = stdlib_base_cinvf( v );
195+
stdlib_complex64_reim( v, &re1, &im1 );
196+
stdlib_complex64_reim( y, &re2, &im2 );
197+
printf( "cinvf(%f + %fi) = %f + %fi\n", re1, im1, re2, im2 );
198+
}
199+
}
200+
```
201+
202+
</section>
203+
204+
<!-- /.examples -->
205+
206+
</section>
207+
208+
<!-- /.c -->
209+
210+
* * *
211+
212+
<section class="references">
213+
214+
## References
215+
216+
- 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].
217+
- 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].
218+
- 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].
219+
- 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].
220+
221+
</section>
222+
223+
<!-- /.references -->
224+
225+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
226+
227+
<section class="related">
228+
229+
</section>
230+
231+
<!-- /.related -->
232+
233+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
234+
235+
<section class="links">
236+
237+
[@smith:1962a]: https://doi.org/10.1145/368637.368661
238+
239+
[@stewart:1985a]: https://doi.org/10.1145/214408.214414
240+
241+
[@priest:2004a]: https://doi.org/10.1145/1039813.1039814
242+
243+
[@baudin:2012a]: https://arxiv.org/abs/1210.4539
244+
245+
<!-- <related-links> -->
246+
247+
<!-- </related-links> -->
248+
249+
</section>
250+
251+
<!-- /.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) 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 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) 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 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)