Skip to content

Commit afa2318

Browse files
gururaj1512kgryte
authored andcommitted
feat: add complex/float64/base/div
Ref: stdlib-js#2261 --- 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: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - 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 9708f63 commit afa2318

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+4907
-0
lines changed
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
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+
# cdiv
22+
23+
> Divide two double-precision complex floating-point numbers.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var cdiv = require( '@stdlib/complex/float64/base/div' );
37+
```
38+
39+
#### cdiv( z1, z2 )
40+
41+
Divides two double-precision complex floating-point numbers.
42+
43+
```javascript
44+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
45+
var real = require( '@stdlib/complex/float64/real' );
46+
var imag = require( '@stdlib/complex/float64/imag' );
47+
48+
var z1 = new Complex128( -13.0, -1.0 );
49+
var z2 = new Complex128( -2.0, 1.0 );
50+
51+
var v = cdiv( z1, z2 );
52+
// returns <Complex128>
53+
54+
var re = real( v );
55+
// returns 5.0
56+
57+
var im = imag( v );
58+
// returns 3.0
59+
```
60+
61+
</section>
62+
63+
<!-- /.usage -->
64+
65+
<section class="examples">
66+
67+
## Examples
68+
69+
<!-- eslint no-undef: "error" -->
70+
71+
```javascript
72+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
73+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
74+
var real = require( '@stdlib/complex/float64/real' );
75+
var imag = require( '@stdlib/complex/float64/imag' );
76+
var cdiv = require( '@stdlib/complex/float64/base/div' );
77+
78+
function randomComplex() {
79+
var re = discreteUniform( -50, 50 );
80+
var im = discreteUniform( -50, 50 );
81+
return new Complex128( re, im );
82+
}
83+
84+
var z1;
85+
var z2;
86+
var z3;
87+
var i;
88+
for ( i = 0; i < 100; i++ ) {
89+
z1 = randomComplex();
90+
z2 = randomComplex();
91+
z3 = cdiv( z1, z2 );
92+
console.log( '(%s) / (%s) = %s', z1.toString(), z2.toString(), z3.toString() );
93+
}
94+
```
95+
96+
</section>
97+
98+
<!-- /.examples -->
99+
100+
<!-- C interface documentation. -->
101+
102+
* * *
103+
104+
<section class="c">
105+
106+
## C APIs
107+
108+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
109+
110+
<section class="intro">
111+
112+
</section>
113+
114+
<!-- /.intro -->
115+
116+
<!-- C usage documentation. -->
117+
118+
<section class="usage">
119+
120+
### Usage
121+
122+
```c
123+
#include "stdlib/complex/float64/base/div.h"
124+
```
125+
126+
#### stdlib_base_complex128_div( z1, z2 )
127+
128+
Divides two double-precision complex floating-point numbers.
129+
130+
```c
131+
#include "stdlib/complex/float64/ctor.h"
132+
#include "stdlib/complex/float64/real.h"
133+
#include "stdlib/complex/float64/imag.h"
134+
135+
stdlib_complex128_t z1 = stdlib_complex128( -13.0, -1.0 );
136+
stdlib_complex128_t z2 = stdlib_complex128( -2.0, 1.0 );
137+
138+
stdlib_complex128_t out = stdlib_base_complex128_div( z1, z2 );
139+
140+
double re = stdlib_complex128_real( out );
141+
// returns 5.0
142+
143+
double im = stdlib_complex128_imag( out );
144+
// returns 3.0
145+
```
146+
147+
The function accepts the following arguments:
148+
149+
- **z1**: `[in] stdlib_complex128_t` input value.
150+
- **z2**: `[in] stdlib_complex128_t` input value.
151+
152+
```c
153+
stdlib_complex128_t stdlib_base_complex128_div( const stdlib_complex128_t z1, const stdlib_complex128_t z2 );
154+
```
155+
156+
</section>
157+
158+
<!-- /.usage -->
159+
160+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
161+
162+
<section class="notes">
163+
164+
</section>
165+
166+
<!-- /.notes -->
167+
168+
<!-- C API usage examples. -->
169+
170+
<section class="examples">
171+
172+
### Examples
173+
174+
```c
175+
#include "stdlib/complex/float64/base/div.h"
176+
#include "stdlib/complex/float64/ctor.h"
177+
#include "stdlib/complex/float64/reim.h"
178+
#include <stdio.h>
179+
180+
int main( void ) {
181+
const stdlib_complex128_t x[] = {
182+
stdlib_complex128( 3.14, 1.5 ),
183+
stdlib_complex128( -3.14, 1.5 ),
184+
stdlib_complex128( 0.0, -0.0 ),
185+
stdlib_complex128( 0.0/0.0, 0.0/0.0 )
186+
};
187+
188+
stdlib_complex128_t v;
189+
stdlib_complex128_t y;
190+
double re;
191+
double im;
192+
int i;
193+
for ( i = 0; i < 4; i++ ) {
194+
v = x[ i ];
195+
stdlib_complex128_reim( v, &re, &im );
196+
printf( "z = %lf + %lfi\n", re, im );
197+
198+
y = stdlib_base_complex128_div( v, v );
199+
stdlib_complex128_reim( y, &re, &im );
200+
printf( "cdiv(z, z) = %lf + %lfi\n", re, im );
201+
}
202+
}
203+
```
204+
205+
</section>
206+
207+
<!-- /.examples -->
208+
209+
</section>
210+
211+
<!-- /.c -->
212+
213+
* * *
214+
215+
<section class="references">
216+
217+
## References
218+
219+
- 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].
220+
- 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].
221+
- 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].
222+
- 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].
223+
224+
</section>
225+
226+
<!-- /.references -->
227+
228+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
229+
230+
<section class="related">
231+
232+
* * *
233+
234+
## See Also
235+
236+
- <span class="package-name">[`@stdlib/complex/float64/base/add`][@stdlib/complex/float64/base/add]</span><span class="delimiter">: </span><span class="description">add two double-precision complex floating-point numbers.</span>
237+
- <span class="package-name">[`@stdlib/complex/float64/base/mul`][@stdlib/complex/float64/base/mul]</span><span class="delimiter">: </span><span class="description">multiply two double-precision complex floating-point numbers.</span>
238+
- <span class="package-name">[`@stdlib/math/base/ops/csub`][@stdlib/math/base/ops/csub]</span><span class="delimiter">: </span><span class="description">subtract two double-precision complex floating-point numbers.</span>
239+
240+
</section>
241+
242+
<!-- /.related -->
243+
244+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
245+
246+
<section class="links">
247+
248+
[@smith:1962a]: https://doi.org/10.1145/368637.368661
249+
250+
[@stewart:1985a]: https://doi.org/10.1145/214408.214414
251+
252+
[@priest:2004a]: https://doi.org/10.1145/1039813.1039814
253+
254+
[@baudin:2012a]: https://arxiv.org/abs/1210.4539
255+
256+
<!-- <related-links> -->
257+
258+
[@stdlib/complex/float64/base/add]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/float64/base/add
259+
260+
[@stdlib/complex/float64/base/mul]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/float64/base/mul
261+
262+
[@stdlib/math/base/ops/csub]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/ops/csub
263+
264+
<!-- </related-links> -->
265+
266+
</section>
267+
268+
<!-- /.links -->
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 Complex128 = require( '@stdlib/complex/float64/ctor' );
25+
var isComplex128 = require( '@stdlib/assert/is-complex128' );
26+
var uniform = require( '@stdlib/random/base/uniform' );
27+
var real = require( '@stdlib/complex/float64/real' );
28+
var imag = require( '@stdlib/complex/float64/imag' );
29+
var abs = require( '@stdlib/math/base/special/abs' );
30+
var pkg = require( './../package.json' ).name;
31+
var cdiv = require( './../lib' );
32+
33+
34+
// MAIN //
35+
36+
bench( pkg, function benchmark( b ) {
37+
var values;
38+
var y;
39+
var i;
40+
var z;
41+
42+
values = [
43+
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
44+
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
45+
];
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
z = values[ i % values.length ];
50+
y = cdiv( z, z );
51+
if ( typeof y !== 'object' ) {
52+
b.fail( 'should return an object' );
53+
}
54+
}
55+
b.toc();
56+
if ( !isComplex128( y ) ) {
57+
b.fail( 'should return a Complex128' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});
62+
63+
bench( pkg+'::smiths_algorithm', function benchmark( b ) {
64+
var values;
65+
var y;
66+
var i;
67+
var z;
68+
69+
values = [
70+
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
71+
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
72+
];
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
z = values[ i % values.length ];
77+
y = cdiv( z, z );
78+
if ( typeof y !== 'object' ) {
79+
b.fail( 'should return an object' );
80+
}
81+
}
82+
b.toc();
83+
if ( !isComplex128( y ) ) {
84+
b.fail( 'should return a Complex128' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
89+
function cdiv( z1, z2 ) {
90+
var re1;
91+
var re2;
92+
var im1;
93+
var im2;
94+
var a;
95+
var b;
96+
97+
re1 = real( z1 );
98+
re2 = real( z2 );
99+
im1 = imag( z1 );
100+
im2 = imag( z2 );
101+
102+
if ( abs( re2 ) >= abs( im2 ) ) {
103+
a = im2 / re2;
104+
b = re2 + ( im2 * a );
105+
return new Complex128( ( re1 + (im1 * a) )/b, (im1 - (re1*a) )/b );
106+
}
107+
a = re2 / im2;
108+
b = ( re2 * a ) + im2;
109+
return new Complex128( ( (re1*a) + im1 )/b, ( (im1*a) - re1 )/b );
110+
}
111+
});

0 commit comments

Comments
 (0)