Skip to content

Commit 9b72f0c

Browse files
anandkaranubckgrytestdlib-bot
authored
feat: add math/base/special/csignumf
PR-URL: #6361 Ref: #649 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent baf9f87 commit 9b72f0c

File tree

29 files changed

+2262
-0
lines changed

29 files changed

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

0 commit comments

Comments
 (0)