Skip to content

Commit 42aab9b

Browse files
feat!: update math/base/special/cabsf to accept stdlib complex numbers
This commit updates the C API to operate on stdlib's single-precision complex number dtype. BREAKING CHANGE: use stdlib C complex64 dtype To migrate, users should provide a value having the type `stdlib_complex64_t`, rather than a built-in C99 single-precision complex dtype. This dtype is available via the package `@stdlib/complex/float32/ctor`. PR-URL: #3358 Closes: #3357 Co-authored-by: stdlib-bot <[email protected]> Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Karan Anand <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 75cbf89 commit 42aab9b

File tree

16 files changed

+664
-72
lines changed

16 files changed

+664
-72
lines changed

Diff for: lib/node_modules/@stdlib/math/base/special/cabsf/README.md

+22-11
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var cabsf = require( '@stdlib/math/base/special/cabsf' );
5555

5656
#### cabsf( z )
5757

58-
Computes an [absolute value][absolute-value] of a single-precision [complex][@stdlib/complex/float32/ctor] floating-point number.
58+
Computes the [absolute value][absolute-value] of a single-precision [complex][@stdlib/complex/float32/ctor] floating-point number.
5959

6060
```javascript
6161
var Complex64 = require( '@stdlib/complex/float32/ctor' );
@@ -125,21 +125,23 @@ for ( i = 0; i < 100; i++ ) {
125125

126126
#### stdlib_base_cabsf( z )
127127

128-
Computes an [absolute value][absolute-value] of a single-precision complex floating-point number.
128+
Computes the [absolute value][absolute-value] of a single-precision complex floating-point number.
129129

130130
```c
131-
#include <complex.h>
131+
#include "stdlib/complex/float32/ctor.h"
132132

133-
float y = stdlib_base_cabsf( 5.0+3.0*I );
133+
stdlib_complex64_t z = stdlib_complex64( 5.0f, 3.0f );
134+
135+
float y = stdlib_base_cabsf( z );
134136
// returns ~5.83f
135137
```
136138

137139
The function accepts the following arguments:
138140

139-
- **z**: `[in] float complex` input value.
141+
- **z**: `[in] stdlib_complex64_t` input value.
140142

141143
```c
142-
float stdlib_base_cabsf( const float complex z );
144+
float stdlib_base_cabsf( const stdlib_complex64_t z );
143145
```
144146
145147
</section>
@@ -162,19 +164,28 @@ float stdlib_base_cabsf( const float complex z );
162164
163165
```c
164166
#include "stdlib/math/base/special/cabsf.h"
167+
#include "stdlib/complex/float32/ctor.h"
168+
#include "stdlib/complex/float32/reim.h"
165169
#include <stdio.h>
166-
#include <complex.h>
167170
168171
int main( void ) {
169-
const float complex x[] = { 3.14f+1.0f*I, -3.14f-1.0f*I, 0.0f+0.0f*I, 0.0f/0.0f+0.0f/0.0f*I };
170-
171-
float complex v;
172+
const stdlib_complex64_t x[] = {
173+
stdlib_complex64( 3.14f, 1.0f ),
174+
stdlib_complex64( -3.14f, -1.0f ),
175+
stdlib_complex64( 0.0f, 0.0f ),
176+
stdlib_complex64( 0.0f/0.0f, 0.0f/0.0f )
177+
};
178+
179+
stdlib_complex64_t v;
180+
float re;
181+
float im;
172182
float y;
173183
int i;
174184
for ( i = 0; i < 4; i++ ) {
175185
v = x[ i ];
176186
y = stdlib_base_cabsf( v );
177-
printf( "f(%f + %f) = %f\n", crealf( v ), cimagf( v ), y );
187+
stdlib_complex64_reim( v, &re, &im );
188+
printf( "f(%f + %f) = %f\n", re, im, y );
178189
}
179190
}
180191
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var cabsf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( cabsf instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( pkg+'::native', opts, function benchmark( b ) {
43+
var values;
44+
var y;
45+
var i;
46+
47+
values = [
48+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
49+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
50+
];
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
y = cabsf( values[ i % values.length ] );
55+
if ( isnanf( y ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
}
59+
b.toc();
60+
if ( isnanf( y ) ) {
61+
b.fail( 'should not return NaN' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
});

Diff for: lib/node_modules/@stdlib/math/base/special/cabsf/benchmark/c/native/benchmark.c

+12-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
*/
1818

1919
#include "stdlib/math/base/special/cabsf.h"
20-
#include <complex.h>
20+
#include "stdlib/complex/float32/ctor.h"
21+
#include "stdlib/complex/float32/reim.h"
2122
#include <stdlib.h>
2223
#include <stdio.h>
2324
#include <math.h>
@@ -91,19 +92,23 @@ static float rand_float( void ) {
9192
* @return elapsed time in seconds
9293
*/
9394
static double benchmark( void ) {
94-
float complex z;
9595
double elapsed;
96+
float re[ 100 ];
97+
float im[ 100 ];
9698
double t;
97-
float re;
98-
float im;
9999
float y;
100100
int i;
101101

102+
for ( i = 0; i < 100; i++ ) {
103+
re[ i ] = ( 1000.0f*rand_float() ) - 500.0f;
104+
im[ i ] = ( 1000.0f*rand_float() ) - 500.0f;
105+
}
106+
107+
stdlib_complex64_t z;
108+
102109
t = tic();
103110
for ( i = 0; i < ITERATIONS; i++ ) {
104-
re = ( 1000.0f*rand_float() ) - 500.0f;
105-
im = ( 1000.0f*rand_float() ) - 500.0f;
106-
z = re + im*I;
111+
z = stdlib_complex64( re[ i%100 ], im[ i%100 ] );
107112
y = stdlib_base_cabsf( z );
108113
if ( y != y ) {
109114
printf( "should not return NaN\n" );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# @license Apache-2.0
2+
#
3+
# Copyright (c) 2025 The Stdlib Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# A `.gyp` file for building a Node.js native add-on.
18+
#
19+
# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md
20+
# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md
21+
{
22+
# List of files to include in this file:
23+
'includes': [
24+
'./include.gypi',
25+
],
26+
27+
# Define variables to be used throughout the configuration for all targets:
28+
'variables': {
29+
# Target name should match the add-on export name:
30+
'addon_target_name%': 'addon',
31+
32+
# Set variables based on the host OS:
33+
'conditions': [
34+
[
35+
'OS=="win"',
36+
{
37+
# Define the object file suffix:
38+
'obj': 'obj',
39+
},
40+
{
41+
# Define the object file suffix:
42+
'obj': 'o',
43+
}
44+
], # end condition (OS=="win")
45+
], # end conditions
46+
}, # end variables
47+
48+
# Define compile targets:
49+
'targets': [
50+
51+
# Target to generate an add-on:
52+
{
53+
# The target name should match the add-on export name:
54+
'target_name': '<(addon_target_name)',
55+
56+
# Define dependencies:
57+
'dependencies': [],
58+
59+
# Define directories which contain relevant include headers:
60+
'include_dirs': [
61+
# Local include directory:
62+
'<@(include_dirs)',
63+
],
64+
65+
# List of source files:
66+
'sources': [
67+
'<@(src_files)',
68+
],
69+
70+
# Settings which should be applied when a target's object files are used as linker input:
71+
'link_settings': {
72+
# Define libraries:
73+
'libraries': [
74+
'<@(libraries)',
75+
],
76+
77+
# Define library directories:
78+
'library_dirs': [
79+
'<@(library_dirs)',
80+
],
81+
},
82+
83+
# C/C++ compiler flags:
84+
'cflags': [
85+
# Enable commonly used warning options:
86+
'-Wall',
87+
88+
# Aggressive optimization:
89+
'-O3',
90+
],
91+
92+
# C specific compiler flags:
93+
'cflags_c': [
94+
# Specify the C standard to which a program is expected to conform:
95+
'-std=c99',
96+
],
97+
98+
# C++ specific compiler flags:
99+
'cflags_cpp': [
100+
# Specify the C++ standard to which a program is expected to conform:
101+
'-std=c++11',
102+
],
103+
104+
# Linker flags:
105+
'ldflags': [],
106+
107+
# Apply conditions based on the host OS:
108+
'conditions': [
109+
[
110+
'OS=="mac"',
111+
{
112+
# Linker flags:
113+
'ldflags': [
114+
'-undefined dynamic_lookup',
115+
'-Wl,-no-pie',
116+
'-Wl,-search_paths_first',
117+
],
118+
},
119+
], # end condition (OS=="mac")
120+
[
121+
'OS!="win"',
122+
{
123+
# C/C++ flags:
124+
'cflags': [
125+
# Generate platform-independent code:
126+
'-fPIC',
127+
],
128+
},
129+
], # end condition (OS!="win")
130+
], # end conditions
131+
}, # end target <(addon_target_name)
132+
133+
# Target to copy a generated add-on to a standard location:
134+
{
135+
'target_name': 'copy_addon',
136+
137+
# Declare that the output of this target is not linked:
138+
'type': 'none',
139+
140+
# Define dependencies:
141+
'dependencies': [
142+
# Require that the add-on be generated before building this target:
143+
'<(addon_target_name)',
144+
],
145+
146+
# Define a list of actions:
147+
'actions': [
148+
{
149+
'action_name': 'copy_addon',
150+
'message': 'Copying addon...',
151+
152+
# Explicitly list the inputs in the command-line invocation below:
153+
'inputs': [],
154+
155+
# Declare the expected outputs:
156+
'outputs': [
157+
'<(addon_output_dir)/<(addon_target_name).node',
158+
],
159+
160+
# Define the command-line invocation:
161+
'action': [
162+
'cp',
163+
'<(PRODUCT_DIR)/<(addon_target_name).node',
164+
'<(addon_output_dir)/<(addon_target_name).node',
165+
],
166+
},
167+
], # end actions
168+
}, # end target copy_addon
169+
], # end targets
170+
}

Diff for: lib/node_modules/@stdlib/math/base/special/cabsf/examples/c/example.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,27 @@
1717
*/
1818

1919
#include "stdlib/math/base/special/cabsf.h"
20+
#include "stdlib/complex/float32/ctor.h"
21+
#include "stdlib/complex/float32/reim.h"
2022
#include <stdio.h>
21-
#include <complex.h>
2223

2324
int main( void ) {
24-
float complex x[] = { 3.14f+1.0f*I, -3.14f-1.0f*I, 0.0f+0.0f*I, 0.0f/0.0f+0.0f/0.0f*I };
25+
const stdlib_complex64_t x[] = {
26+
stdlib_complex64( 3.14f, 1.0f ),
27+
stdlib_complex64( -3.14f, -1.0f ),
28+
stdlib_complex64( 0.0f, 0.0f ),
29+
stdlib_complex64( 0.0f/0.0f, 0.0f/0.0f )
30+
};
2531

26-
float complex v;
32+
stdlib_complex64_t v;
33+
float re;
34+
float im;
2735
float y;
2836
int i;
2937
for ( i = 0; i < 4; i++ ) {
3038
v = x[ i ];
3139
y = stdlib_base_cabsf( v );
32-
printf( "f(%f + %f) = %f\n", crealf( v ), cimagf( v ), y );
40+
stdlib_complex64_reim( v, &re, &im );
41+
printf( "f(%f + %f) = %f\n", re, im, y );
3342
}
3443
}

0 commit comments

Comments
 (0)