Skip to content

Commit 10ef39d

Browse files
committed
feat: add complex/float32/base/add
Ref: #2260
1 parent 9fd67fc commit 10ef39d

File tree

28 files changed

+2422
-0
lines changed

28 files changed

+2422
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2021 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+
# add
22+
23+
> Add 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 add = require( '@stdlib/complex/float32/base/add' );
37+
```
38+
39+
#### add( z1, z2 )
40+
41+
Adds two single-precision complex floating-point numbers.
42+
43+
```javascript
44+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
45+
var realf = require( '@stdlib/complex/float32/real' );
46+
var imagf = require( '@stdlib/complex/float32/imag' );
47+
48+
var z = new Complex64( -1.5, 2.5 );
49+
50+
var v = add( z, z );
51+
// returns <Complex64>
52+
53+
var re = realf( v );
54+
// returns -3.0
55+
56+
var im = imagf( v );
57+
// returns 5.0
58+
```
59+
60+
</section>
61+
62+
<!-- /.usage -->
63+
64+
<section class="examples">
65+
66+
## Examples
67+
68+
<!-- eslint no-undef: "error" -->
69+
70+
```javascript
71+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
72+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
73+
var add = require( '@stdlib/complex/float32/base/add' );
74+
75+
var rand = discreteUniform( -50, 50 );
76+
77+
var z1;
78+
var z2;
79+
var z3;
80+
var i;
81+
for ( i = 0; i < 100; i++ ) {
82+
z1 = new Complex64( rand(), rand() );
83+
z2 = new Complex64( rand(), rand() );
84+
z3 = add( z1, z2 );
85+
console.log( '(%s) + (%s) = %s', z1.toString(), z2.toString(), z3.toString() );
86+
}
87+
```
88+
89+
</section>
90+
91+
<!-- /.examples -->
92+
93+
<!-- C interface documentation. -->
94+
95+
* * *
96+
97+
<section class="c">
98+
99+
## C APIs
100+
101+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
102+
103+
<section class="intro">
104+
105+
</section>
106+
107+
<!-- /.intro -->
108+
109+
<!-- C usage documentation. -->
110+
111+
<section class="usage">
112+
113+
### Usage
114+
115+
```c
116+
#include "stdlib/complex/float32/base/add.h"
117+
```
118+
119+
#### stdlib_base_complex64_add( z1, z2 )
120+
121+
Adds two single-precision complex floating-point numbers.
122+
123+
```c
124+
#include "stdlib/complex/float32/ctor.h"
125+
#include "stdlib/complex/float32/real.h"
126+
#include "stdlib/complex/float32/imag.h"
127+
128+
stdlib_complex64_t z = stdlib_complex64( 3.0f, -2.0f );
129+
130+
stdlib_complex64_t out = stdlib_base_complex64_add( z, z );
131+
132+
float re = stdlib_complex64_real( out );
133+
// returns 6.0f
134+
135+
float im = stdlib_complex64_imag( out );
136+
// returns -4.0f
137+
```
138+
139+
The function accepts the following arguments:
140+
141+
- **z1**: `[in] stdlib_complex64_t` input value.
142+
- **z2**: `[in] stdlib_complex64_t` input value.
143+
144+
```c
145+
stdlib_complex64_t stdlib_base_complex64_add( const stdlib_complex64_t z1, const stdlib_complex64_t z2 );
146+
```
147+
148+
</section>
149+
150+
<!-- /.usage -->
151+
152+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
153+
154+
<section class="notes">
155+
156+
</section>
157+
158+
<!-- /.notes -->
159+
160+
<!-- C API usage examples. -->
161+
162+
<section class="examples">
163+
164+
### Examples
165+
166+
```c
167+
#include "stdlib/complex/float32/base/add.h"
168+
#include "stdlib/complex/float32/ctor.h"
169+
#include "stdlib/complex/float32/reim.h"
170+
#include <stdio.h>
171+
172+
int main( void ) {
173+
const stdlib_complex64_t x[] = {
174+
stdlib_complex64( 3.14f, 1.5f ),
175+
stdlib_complex64( -3.14f, 1.5f ),
176+
stdlib_complex64( 0.0f, -0.0f ),
177+
stdlib_complex64( 0.0f/0.0f, 0.0f/0.0f )
178+
};
179+
180+
stdlib_complex64_t v;
181+
stdlib_complex64_t y;
182+
float re;
183+
float im;
184+
int i;
185+
for ( i = 0; i < 4; i++ ) {
186+
v = x[ i ];
187+
stdlib_complex64_reim( v, &re, &im );
188+
printf( "z = %f + %fi\n", re, im );
189+
190+
y = stdlib_base_complex64_add( v, v );
191+
stdlib_complex64_reim( y, &re, &im );
192+
printf( "add(z, z) = %f + %fi\n", re, im );
193+
}
194+
}
195+
```
196+
197+
</section>
198+
199+
<!-- /.examples -->
200+
201+
</section>
202+
203+
<!-- /.c -->
204+
205+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
206+
207+
<section class="related">
208+
209+
* * *
210+
211+
## See Also
212+
213+
- <span class="package-name">[`@stdlib/math/base/ops/cadd`][@stdlib/math/base/ops/cadd]</span><span class="delimiter">: </span><span class="description">add two double-precision complex floating-point numbers.</span>
214+
- <span class="package-name">[`@stdlib/math/base/ops/cmulf`][@stdlib/math/base/ops/cmulf]</span><span class="delimiter">: </span><span class="description">multiply two single-precision complex floating-point numbers.</span>
215+
- <span class="package-name">[`@stdlib/math/base/ops/csubf`][@stdlib/math/base/ops/csubf]</span><span class="delimiter">: </span><span class="description">subtract two single-precision complex floating-point numbers.</span>
216+
217+
</section>
218+
219+
<!-- /.related -->
220+
221+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
222+
223+
<section class="links">
224+
225+
<!-- <related-links> -->
226+
227+
[@stdlib/math/base/ops/cadd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/ops/cadd
228+
229+
[@stdlib/math/base/ops/cmulf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/ops/cmulf
230+
231+
[@stdlib/math/base/ops/csubf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/ops/csubf
232+
233+
<!-- </related-links> -->
234+
235+
</section>
236+
237+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 add = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg, function benchmark( b ) {
36+
var values;
37+
var out;
38+
var z;
39+
var i;
40+
41+
values = [
42+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
43+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
44+
];
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
z = values[ i%values.length ];
49+
out = add( z, z );
50+
if ( typeof out !== 'object' ) {
51+
b.fail( 'should return an object' );
52+
}
53+
}
54+
b.toc();
55+
if ( isnanf( realf( out ) ) || isnanf( imagf( out ) ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 Complex64 = require( '@stdlib/complex/float32/ctor' );
28+
var realf = require( '@stdlib/complex/float32/real' );
29+
var imagf = require( '@stdlib/complex/float32/imag' );
30+
var tryRequire = require( '@stdlib/utils/try-require' );
31+
var pkg = require( './../package.json' ).name;
32+
33+
34+
// VARIABLES //
35+
36+
var add = tryRequire( resolve( __dirname, './../lib/native.js' ) );
37+
var opts = {
38+
'skip': ( add instanceof Error )
39+
};
40+
41+
42+
// MAIN //
43+
44+
bench( pkg+'::native', opts, function benchmark( b ) {
45+
var values;
46+
var out;
47+
var z;
48+
var i;
49+
50+
values = [
51+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
52+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
53+
];
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
z = values[ i%values.length ];
58+
out = add( z, z );
59+
if ( typeof out !== 'object' ) {
60+
b.fail( 'should return an object' );
61+
}
62+
}
63+
b.toc();
64+
if ( isnanf( realf( out ) ) || isnanf( imagf( out ) ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});

0 commit comments

Comments
 (0)