Skip to content

Commit 4866c10

Browse files
gururaj1512kgryte
authored andcommitted
feat: add complex/float32/base/neg
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 5883c68 commit 4866c10

File tree

28 files changed

+2605
-0
lines changed

28 files changed

+2605
-0
lines changed
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 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+
# cnegf
22+
23+
> Negate a single-precision complex floating-point number.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var cnegf = require( '@stdlib/complex/float32/base/neg' );
41+
```
42+
43+
#### cnegf( z )
44+
45+
Negates a single-precision complex floating-point number.
46+
47+
```javascript
48+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
49+
var realf = require( '@stdlib/complex/float32/real' );
50+
var imagf = require( '@stdlib/complex/float32/imag' );
51+
52+
var z = new Complex64( -4.0, 5.0 );
53+
54+
var out = cnegf( z );
55+
// returns <Complex64>
56+
57+
var re = realf( out );
58+
// returns 4.0
59+
60+
var im = imagf( out );
61+
// returns -5.0
62+
63+
z = new Complex64( 0.0, 0.0 );
64+
65+
out = cnegf( z );
66+
// returns <Complex64>
67+
68+
re = realf( out );
69+
// returns -0.0
70+
71+
im = imagf( out );
72+
// returns -0.0
73+
74+
z = new Complex64( NaN, NaN );
75+
76+
out = cnegf( z );
77+
// returns <Complex64>
78+
79+
re = realf( out );
80+
// returns NaN
81+
82+
im = imagf( out );
83+
// returns NaN
84+
```
85+
86+
</section>
87+
88+
<!-- /.usage -->
89+
90+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
91+
92+
<section class="notes">
93+
94+
</section>
95+
96+
<!-- /.notes -->
97+
98+
<!-- Package usage examples. -->
99+
100+
<section class="examples">
101+
102+
## Examples
103+
104+
<!-- eslint no-undef: "error" -->
105+
106+
```javascript
107+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
108+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
109+
var cnegf = require( '@stdlib/complex/float32/base/neg' );
110+
111+
function randomComplex() {
112+
var re = discreteUniform( -50, 50 );
113+
var im = discreteUniform( -50, 50 );
114+
return new Complex64( re, im );
115+
}
116+
117+
var z;
118+
var o;
119+
var i;
120+
121+
for ( i = 0; i < 100; i++ ) {
122+
z = randomComplex();
123+
o = cnegf( z );
124+
console.log( 'negate(%s) = %s', z.toString(), o.toString() );
125+
}
126+
```
127+
128+
</section>
129+
130+
<!-- /.examples -->
131+
132+
<!-- C interface documentation. -->
133+
134+
* * *
135+
136+
<section class="c">
137+
138+
## C APIs
139+
140+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
141+
142+
<section class="intro">
143+
144+
</section>
145+
146+
<!-- /.intro -->
147+
148+
<!-- C usage documentation. -->
149+
150+
<section class="usage">
151+
152+
### Usage
153+
154+
```c
155+
#include "stdlib/complex/float32/base/neg.h"
156+
```
157+
158+
#### stdlib_base_complex64_neg( z )
159+
160+
Negates a single-precision complex floating-point number.
161+
162+
```c
163+
#include "stdlib/complex/float32/ctor.h"
164+
#include "stdlib/complex/float32/real.h"
165+
#include "stdlib/complex/float32/imag.h"
166+
167+
stdlib_complex64_t z = stdlib_complex64( 3.0f, -2.0f );
168+
stdlib_complex64_t out = stdlib_base_complex64_neg( z );
169+
170+
float re = stdlib_complex64_real( out );
171+
// returns -3.0f
172+
173+
float im = stdlib_complex64_imag( out );
174+
// returns 2.0f
175+
```
176+
177+
The function accepts the following arguments:
178+
179+
- **z**: `[in] stdlib_complex64_t` input value.
180+
181+
```c
182+
stdlib_complex64_t stdlib_base_complex64_neg( const stdlib_complex64_t z );
183+
```
184+
185+
</section>
186+
187+
<!-- /.usage -->
188+
189+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
190+
191+
<section class="notes">
192+
193+
</section>
194+
195+
<!-- /.notes -->
196+
197+
<!-- C API usage examples. -->
198+
199+
<section class="examples">
200+
201+
### Examples
202+
203+
```c
204+
#include "stdlib/complex/float32/base/neg.h"
205+
#include "stdlib/complex/float32/ctor.h"
206+
#include "stdlib/complex/float32/reim.h"
207+
#include <stdio.h>
208+
209+
int main( void ) {
210+
const stdlib_complex64_t x[] = {
211+
stdlib_complex64( 3.14f, 1.5f ),
212+
stdlib_complex64( -3.14f, 1.5f ),
213+
stdlib_complex64( 0.0f, -0.0f ),
214+
stdlib_complex64( 0.0f/0.0f, 0.0f/0.0f )
215+
};
216+
217+
stdlib_complex64_t v;
218+
stdlib_complex64_t y;
219+
float re;
220+
float im;
221+
int i;
222+
for ( i = 0; i < 4; i++ ) {
223+
v = x[ i ];
224+
stdlib_complex64_reim( v, &re, &im );
225+
printf( "z = %f + %fi\n", re, im );
226+
227+
y = stdlib_base_complex64_neg( v );
228+
stdlib_complex64_reim( y, &re, &im );
229+
printf( "cnegf(z) = %f + %fi\n", re, im );
230+
}
231+
}
232+
```
233+
234+
</section>
235+
236+
<!-- /.examples -->
237+
238+
</section>
239+
240+
<!-- /.c -->
241+
242+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
243+
244+
<section class="references">
245+
246+
</section>
247+
248+
<!-- /.references -->
249+
250+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
251+
252+
<section class="related">
253+
254+
* * *
255+
256+
## See Also
257+
258+
- <span class="package-name">[`@stdlib/complex/float64/base/neg`][@stdlib/complex/float64/base/neg]</span><span class="delimiter">: </span><span class="description">negate a double-precision complex floating-point number.</span>
259+
- <span class="package-name">[`@stdlib/math/base/special/cabsf`][@stdlib/math/base/special/cabsf]</span><span class="delimiter">: </span><span class="description">compute the absolute value of a single-precision complex floating-point number.</span>
260+
261+
</section>
262+
263+
<!-- /.related -->
264+
265+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
266+
267+
<section class="links">
268+
269+
<!-- <related-links> -->
270+
271+
[@stdlib/complex/float64/base/neg]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/float64/base/neg
272+
273+
[@stdlib/math/base/special/cabsf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cabsf
274+
275+
<!-- </related-links> -->
276+
277+
</section>
278+
279+
<!-- /.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) 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 bench = require( '@stdlib/bench' );
24+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var realf = require( '@stdlib/complex/float32/real' );
27+
var imagf = require( '@stdlib/complex/float32/imag' );
28+
var uniform = require( '@stdlib/random/base/uniform' );
29+
var pkg = require( './../package.json' ).name;
30+
var cnegf = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg, function benchmark( b ) {
36+
var values;
37+
var z;
38+
var y;
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+
y = cnegf( z );
50+
if ( typeof y !== 'object' ) {
51+
b.fail( 'should return an object' );
52+
}
53+
}
54+
b.toc();
55+
if ( isnanf( realf( y ) ) || isnanf( imagf( y ) ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});

0 commit comments

Comments
 (0)