Skip to content

Commit a6b331a

Browse files
committed
feat: add complex/float32/base/div
1 parent a39fad8 commit a6b331a

Some content is hidden

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

44 files changed

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

0 commit comments

Comments
 (0)