Skip to content

Commit 7e1f49e

Browse files
gururaj1512kgryte
authored andcommitted
feat: add complex/float64/base/sub
Ref: #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 3da41b7 commit 7e1f49e

File tree

28 files changed

+2442
-0
lines changed

28 files changed

+2442
-0
lines changed
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
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+
# csub
22+
23+
> Subtract 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 csub = require( '@stdlib/complex/float64/base/sub' );
37+
```
38+
39+
#### csub( z1, z2 )
40+
41+
Subtracts 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( 5.0, 3.0 );
49+
var z2 = new Complex128( -2.0, 1.0 );
50+
51+
var v = csub( z1, z2 );
52+
// returns <Complex128>
53+
54+
var re = real( v );
55+
// returns 7.0
56+
57+
var im = imag( v );
58+
// returns 2.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' ).factory;
74+
var csub = require( '@stdlib/complex/float64/base/sub' );
75+
76+
var rand;
77+
var z1;
78+
var z2;
79+
var z3;
80+
var i;
81+
82+
rand = discreteUniform( -50, 50 );
83+
for ( i = 0; i < 100; i++ ) {
84+
z1 = new Complex128( rand(), rand() );
85+
z2 = new Complex128( rand(), rand() );
86+
z3 = csub( z1, z2 );
87+
console.log( '(%s) - (%s) = %s', z1.toString(), z2.toString(), z3.toString() );
88+
}
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/complex/float64/base/sub.h"
119+
```
120+
121+
#### stdlib_base_complex128_sub( z1, z2 )
122+
123+
Subtracts two double-precision complex floating-point numbers.
124+
125+
```c
126+
#include "stdlib/complex/float64/ctor.h"
127+
#include "stdlib/complex/float64/real.h"
128+
#include "stdlib/complex/float64/imag.h"
129+
130+
stdlib_complex128_t z1 = stdlib_complex128( 5.0, 3.0 );
131+
stdlib_complex128_t z2 = stdlib_complex128( -2.0, 1.0 );
132+
133+
stdlib_complex128_t out = stdlib_base_complex128_sub( z1, z2 );
134+
135+
double re = stdlib_complex128_real( out );
136+
// returns 7.0
137+
138+
double im = stdlib_complex128_imag( out );
139+
// returns 2.0
140+
```
141+
142+
The function accepts the following arguments:
143+
144+
- **z1**: `[in] stdlib_complex128_t` input value.
145+
- **z2**: `[in] stdlib_complex128_t` input value.
146+
147+
```c
148+
stdlib_complex128_t stdlib_base_complex128_sub( const stdlib_complex128_t z1, const stdlib_complex128_t z2 );
149+
```
150+
151+
</section>
152+
153+
<!-- /.usage -->
154+
155+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
156+
157+
<section class="notes">
158+
159+
</section>
160+
161+
<!-- /.notes -->
162+
163+
<!-- C API usage examples. -->
164+
165+
<section class="examples">
166+
167+
### Examples
168+
169+
```c
170+
#include "stdlib/complex/float64/base/sub.h"
171+
#include "stdlib/complex/float64/ctor.h"
172+
#include "stdlib/complex/float64/reim.h"
173+
#include <stdio.h>
174+
175+
int main( void ) {
176+
const stdlib_complex128_t x[] = {
177+
stdlib_complex128( 3.14, 1.5 ),
178+
stdlib_complex128( -3.14, 1.5 ),
179+
stdlib_complex128( 0.0, -0.0 ),
180+
stdlib_complex128( 0.0/0.0, 0.0/0.0 )
181+
};
182+
183+
stdlib_complex128_t v;
184+
stdlib_complex128_t y;
185+
double re;
186+
double im;
187+
int i;
188+
for ( i = 0; i < 4; i++ ) {
189+
v = x[ i ];
190+
stdlib_complex128_reim( v, &re, &im );
191+
printf( "z = %lf + %lfi\n", re, im );
192+
193+
y = stdlib_base_complex128_sub( v, v );
194+
stdlib_complex128_reim( y, &re, &im );
195+
printf( "csub(z, z) = %lf + %lfi\n", re, im );
196+
}
197+
}
198+
```
199+
200+
</section>
201+
202+
<!-- /.examples -->
203+
204+
</section>
205+
206+
<!-- /.c -->
207+
208+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
209+
210+
<section class="related">
211+
212+
* * *
213+
214+
## See Also
215+
216+
- <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>
217+
- <span class="package-name">[`@stdlib/complex/float64/base/div`][@stdlib/complex/float64/base/div]</span><span class="delimiter">: </span><span class="description">divide two complex numbers.</span>
218+
- <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>
219+
220+
</section>
221+
222+
<!-- /.related -->
223+
224+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
225+
226+
<section class="links">
227+
228+
<!-- <related-links> -->
229+
230+
[@stdlib/complex/float64/base/add]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/float64/base/add
231+
232+
[@stdlib/complex/float64/base/div]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/float64/base/div
233+
234+
[@stdlib/complex/float64/base/mul]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/float64/base/mul
235+
236+
<!-- </related-links> -->
237+
238+
</section>
239+
240+
<!-- /.links -->
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
27+
var real = require( '@stdlib/complex/float64/real' );
28+
var imag = require( '@stdlib/complex/float64/imag' );
29+
var pkg = require( './../package.json' ).name;
30+
var csub = 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 Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
43+
new Complex128( 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 = csub( z, z );
50+
if ( typeof out !== 'object' ) {
51+
b.fail( 'should return an object' );
52+
}
53+
}
54+
b.toc();
55+
if ( isnan( real( out ) ) || isnan( imag( out ) ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/base/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
28+
var real = require( '@stdlib/complex/float64/real' );
29+
var imag = require( '@stdlib/complex/float64/imag' );
30+
var tryRequire = require( '@stdlib/utils/try-require' );
31+
var pkg = require( './../package.json' ).name;
32+
33+
34+
// VARIABLES //
35+
36+
var csub = tryRequire( resolve( __dirname, './../lib/native.js' ) );
37+
var opts = {
38+
'skip': ( csub 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 Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
52+
new Complex128( 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 = csub( z, z );
59+
if ( typeof out !== 'object' ) {
60+
b.fail( 'should return an object' );
61+
}
62+
}
63+
b.toc();
64+
if ( isnan( real( out ) ) || isnan( imag( out ) ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});

0 commit comments

Comments
 (0)