Skip to content

Commit 2b5f804

Browse files
committed
Refactor to use stdlib complex number APIs
1 parent 3e1e5c3 commit 2b5f804

File tree

3 files changed

+130
-47
lines changed

3 files changed

+130
-47
lines changed

lib/node_modules/@stdlib/math/base/napi/binary/README.md

+74-26
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,27 @@ void stdlib_math_base_napi_ff_f( napi_env env, napi_callback_info info, float (*
188188
Invokes a binary function accepting and returning double-precision complex floating-point numbers.
189189

190190
```c
191+
#include "stdlib/complex/float64.h"
192+
#include "stdlib/complex/reim.h"
191193
#include <node_api.h>
192-
#include <complex.h>
193194

194195
// ...
195196

196-
static double complex add( const double complex x, const double complex y ) {
197-
double re = creal( x ) + creal( y );
198-
double im = cimag( x ) + cimag( y );
199-
return re + im*I;
197+
static stdlib_complex128_t add( const stdlib_complex128_t x, const stdlib_complex128_t y ) {
198+
double xre;
199+
double xim;
200+
double yre;
201+
double yim;
202+
double re;
203+
double im;
204+
205+
stdlib_reim( x, &xre, &xim );
206+
stdlib_reim( y, &yre, &yim );
207+
208+
re = xre + yre;
209+
im = xim + yim;
210+
211+
return stdlib_complex128( re, im );
200212
}
201213

202214
// ...
@@ -219,26 +231,38 @@ The function accepts the following arguments:
219231
220232
- **env**: `[in] napi_env` environment under which the function is invoked.
221233
- **info**: `[in] napi_callback_info` callback data.
222-
- **fcn**: `[in] double complex (*fcn)( double complex, double complex )` binary function.
234+
- **fcn**: `[in] stdlib_complex128_t (*fcn)( stdlib_complex128_t, stdlib_complex128_t )` binary function.
223235
224236
```c
225-
void stdlib_math_base_napi_zz_z( napi_env env, napi_callback_info info, double complex (*fcn)( double complex, double complex ) );
237+
void stdlib_math_base_napi_zz_z( napi_env env, napi_callback_info info, stdlib_complex128_t (*fcn)( stdlib_complex128_t, stdlib_complex128_t ) );
226238
```
227239

228240
#### stdlib_math_base_napi_cc_c( env, info, fcn )
229241

230242
Invokes a binary function accepting and returning single-precision complex floating-point numbers.
231243

232244
```c
245+
#include "stdlib/complex/float64.h"
246+
#include "stdlib/complex/reim.h"
233247
#include <node_api.h>
234-
#include <complex.h>
235248

236249
// ...
237250

238-
static float complex add( const float complex x, const float complex y ) {
239-
float re = crealf( x ) + crealf( y );
240-
float im = cimagf( x ) + cimagf( y );
241-
return re + im*I;
251+
static stdlib_complex64_t add( const stdlib_complex64_t x, const stdlib_complex64_t y ) {
252+
float xre;
253+
float xim;
254+
float yre;
255+
float yim;
256+
float re;
257+
float im;
258+
259+
stdlib_reimf( x, &xre, &xim );
260+
stdlib_reimf( y, &yre, &yim );
261+
262+
re = xre + yre;
263+
im = xim + yim;
264+
265+
return stdlib_complex64( re, im );
242266
}
243267

244268
// ...
@@ -261,10 +285,10 @@ The function accepts the following arguments:
261285
262286
- **env**: `[in] napi_env` environment under which the function is invoked.
263287
- **info**: `[in] napi_callback_info` callback data.
264-
- **fcn**: `[in] float complex (*fcn)( float complex, float complex )` binary function.
288+
- **fcn**: `[in] stdlib_complex64_t (*fcn)( stdlib_complex64_t, stdlib_complex64_t )` binary function.
265289
266290
```c
267-
void stdlib_math_base_napi_cc_c( napi_env env, napi_callback_info info, float complex (*fcn)( float complex, float complex ) );
291+
void stdlib_math_base_napi_cc_c( napi_env env, napi_callback_info info, stdlib_complex64_t (*fcn)( stdlib_complex64_t, stdlib_complex64_t ) );
268292
```
269293

270294
#### STDLIB_MATH_BASE_NAPI_MODULE_DD_D( fcn )
@@ -314,12 +338,24 @@ When used, this macro should be used **instead of** `NAPI_MODULE`. The macro inc
314338
Macro for registering a Node-API module exporting an interface for invoking a binary function accepting and returning double-precision complex floating-point numbers.
315339

316340
```c
317-
#include <complex.h>
341+
#include "stdlib/complex/float64.h"
342+
#include "stdlib/complex/reim.h"
318343

319-
static double complex add( const double complex x, const double complex y ) {
320-
double re = creal( x ) + creal( y );
321-
double im = cimag( x ) + cimag( y );
322-
return re + im*I;
344+
static stdlib_complex128_t add( const stdlib_complex128_t x, const stdlib_complex128_t y ) {
345+
double xre;
346+
double xim;
347+
double yre;
348+
double yim;
349+
double re;
350+
double im;
351+
352+
stdlib_reim( x, &xre, &xim );
353+
stdlib_reim( y, &yre, &yim );
354+
355+
re = xre + yre;
356+
im = xim + yim;
357+
358+
return stdlib_complex128( re, im );
323359
}
324360

325361
// ...
@@ -330,7 +366,7 @@ STDLIB_MATH_BASE_NAPI_MODULE_ZZ_Z( add );
330366
331367
The macro expects the following arguments:
332368
333-
- **fcn**: `double complex (*fcn)( double complex, double complex )` binary function.
369+
- **fcn**: `stdlib_complex128_t (*fcn)( stdlib_complex128_t, stdlib_complex128_t )` binary function.
334370
335371
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
336372
@@ -339,12 +375,24 @@ When used, this macro should be used **instead of** `NAPI_MODULE`. The macro inc
339375
Macro for registering a Node-API module exporting an interface for invoking a binary function accepting and returning single-precision complex floating-point numbers.
340376
341377
```c
342-
#include <complex.h>
378+
#include "stdlib/complex/float32.h"
379+
#include "stdlib/complex/reimf.h"
380+
381+
static stdlib_complex64_t add( const stdlib_complex64_t x, const stdlib_complex64_t y ) {
382+
float xre;
383+
float xim;
384+
float yre;
385+
float yim;
386+
float re;
387+
float im;
388+
389+
stdlib_reimf( x, &xre, &xim );
390+
stdlib_reimf( y, &yre, &yim );
391+
392+
re = xre + yre;
393+
im = xim + yim;
343394
344-
static float complex add( const float complex x, const float complex y ) {
345-
float re = crealf( x ) + crealf( y );
346-
float im = cimagf( x ) + cimagf( y );
347-
return re + im*I;
395+
return stdlib_complex64( re, im );
348396
}
349397
350398
// ...
@@ -355,7 +403,7 @@ STDLIB_MATH_BASE_NAPI_MODULE_CC_C( add );
355403

356404
The macro expects the following arguments:
357405

358-
- **fcn**: `float complex (*fcn)( float complex, float complex )` binary function.
406+
- **fcn**: `stdlib_complex64_t (*fcn)( stdlib_complex64_t, stdlib_complex64_t )` binary function.
359407

360408
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
361409

lib/node_modules/@stdlib/math/base/napi/binary/include/stdlib/math/base/napi/binary.h

+38-13
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
#ifndef STDLIB_MATH_BASE_NAPI_BINARY_H
2020
#define STDLIB_MATH_BASE_NAPI_BINARY_H
2121

22+
#include "stdlib/complex/float32.h"
23+
#include "stdlib/complex/float64.h"
2224
#include <node_api.h>
2325
#include <assert.h>
24-
#include <complex.h>
2526

2627
/**
2728
* Macro for registering a Node-API module exporting an interface invoking a binary function accepting and returning double-precision floating-point numbers.
@@ -109,12 +110,24 @@
109110
* @param fcn binary function
110111
*
111112
* @example
112-
* #include <complex.h>
113+
* #include "stdlib/complex/float64.h"
114+
* #include "stdlib/complex/reim.h"
113115
*
114-
* static double complex add( const double complex x, const double complex y ) {
115-
* double re = creal( x ) + creal( y );
116-
* double im = cimag( x ) + cimag( y );
117-
* return re + im*I;
116+
* static stdlib_complex128_t add( const stdlib_complex128_t x, const stdlib_complex128_t y ) {
117+
* double xre;
118+
* double xim;
119+
* double yre;
120+
* double yim;
121+
* double re;
122+
* double im;
123+
*
124+
* stdlib_reim( x, &xre, &xim );
125+
* stdlib_reim( y, &yre, &yim );
126+
*
127+
* re = xre + yre;
128+
* im = xim + yim;
129+
*
130+
* return stdlib_complex128( re, im );
118131
* }
119132
*
120133
* // ...
@@ -153,12 +166,24 @@
153166
* @param fcn binary function
154167
*
155168
* @example
156-
* #include <complex.h>
169+
* #include "stdlib/complex/float32.h"
170+
* #include "stdlib/complex/reimf.h"
171+
*
172+
* static stdlib_complex64_t add( const stdlib_complex64_t x, const stdlib_complex64_t y ) {
173+
* float xre;
174+
* float xim;
175+
* float yre;
176+
* float yim;
177+
* float re;
178+
* float im;
179+
*
180+
* stdlib_reimf( x, &xre, &xim );
181+
* stdlib_reimf( y, &yre, &yim );
182+
*
183+
* re = xre + yre;
184+
* im = xim + yim;
157185
*
158-
* static float complex add( const float complex x, const float complex y ) {
159-
* float re = crealf( x ) + crealf( y );
160-
* float im = cimagf( x ) + cimagf( y );
161-
* return re + im*I;
186+
* return stdlib_complex64( re, im );
162187
* }
163188
*
164189
* // ...
@@ -211,12 +236,12 @@ napi_value stdlib_math_base_napi_ff_f( napi_env env, napi_callback_info info, fl
211236
/**
212237
* Invokes a binary function accepting and returning double-precision complex floating-point numbers.
213238
*/
214-
napi_value stdlib_math_base_napi_zz_z( napi_env env, napi_callback_info info, double complex (*fcn)( double complex, double complex ) );
239+
napi_value stdlib_math_base_napi_zz_z( napi_env env, napi_callback_info info, stdlib_complex128_t (*fcn)( stdlib_complex128_t, stdlib_complex128_t ) );
215240

216241
/**
217242
* Invokes a binary function accepting and returning single-precision complex floating-point numbers.
218243
*/
219-
napi_value stdlib_math_base_napi_cc_c( napi_env env, napi_callback_info info, float complex (*fcn)( float complex, float complex ) );
244+
napi_value stdlib_math_base_napi_cc_c( napi_env env, napi_callback_info info, stdlib_complex64_t (*fcn)( stdlib_complex64_t, stdlib_complex64_t ) );
220245

221246
#ifdef __cplusplus
222247
}

lib/node_modules/@stdlib/math/base/napi/binary/src/main.c

+18-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
*/
1818

1919
#include "stdlib/math/base/napi/binary.h"
20+
#include "stdlib/complex/float64.h"
21+
#include "stdlib/complex/float32.h"
22+
#include "stdlib/complex/reim.h"
23+
#include "stdlib/complex/reimf.h"
2024
#include <node_api.h>
2125
#include <stdint.h>
2226
#include <assert.h>
@@ -162,7 +166,7 @@ napi_value stdlib_math_base_napi_ff_f( napi_env env, napi_callback_info info, fl
162166
* @param fcn binary function
163167
* @return function return value as a Node-API complex-like object
164168
*/
165-
napi_value stdlib_math_base_napi_zz_z( napi_env env, napi_callback_info info, double complex (*fcn)( double complex, double complex ) ) {
169+
napi_value stdlib_math_base_napi_zz_z( napi_env env, napi_callback_info info, stdlib_complex128_t (*fcn)( stdlib_complex128_t, stdlib_complex128_t ) ) {
166170
napi_status status;
167171

168172
size_t argc = 2;
@@ -277,21 +281,24 @@ napi_value stdlib_math_base_napi_zz_z( napi_env env, napi_callback_info info, do
277281
status = napi_get_value_double( env, yim, &im1 );
278282
assert( status == napi_ok );
279283

280-
double complex v = fcn( re0 + im0*I, re1 + im1*I );
284+
stdlib_complex128_t v = fcn( stdlib_complex128( re0, im0 ), stdlib_complex128( re1, im1 ) );
285+
double re;
286+
double im;
287+
stdlib_reim( v, &re, &im );
281288

282289
napi_value obj;
283290
status = napi_create_object( env, &obj );
284291
assert( status == napi_ok );
285292

286293
napi_value vre;
287-
status = napi_create_double( env, creal( v ), &vre );
294+
status = napi_create_double( env, re, &vre );
288295
assert( status == napi_ok );
289296

290297
status = napi_set_named_property( env, obj, "re", vre );
291298
assert( status == napi_ok );
292299

293300
napi_value vim;
294-
status = napi_create_double( env, cimag( v ), &vim );
301+
status = napi_create_double( env, im, &vim );
295302
assert( status == napi_ok );
296303

297304
status = napi_set_named_property( env, obj, "im", vim );
@@ -315,7 +322,7 @@ napi_value stdlib_math_base_napi_zz_z( napi_env env, napi_callback_info info, do
315322
* @param fcn binary function
316323
* @return function return value as a Node-API complex-like object
317324
*/
318-
napi_value stdlib_math_base_napi_cc_c( napi_env env, napi_callback_info info, float complex (*fcn)( float complex, float complex ) ) {
325+
napi_value stdlib_math_base_napi_cc_c( napi_env env, napi_callback_info info, stdlib_complex64_t (*fcn)( stdlib_complex64_t, stdlib_complex64_t ) ) {
319326
napi_status status;
320327

321328
size_t argc = 2;
@@ -430,21 +437,24 @@ napi_value stdlib_math_base_napi_cc_c( napi_env env, napi_callback_info info, fl
430437
status = napi_get_value_double( env, yim, &im1 );
431438
assert( status == napi_ok );
432439

433-
float complex v = fcn( (float)re0 + (float)im0*I, (float)re1 + (float)im1*I );
440+
stdlib_complex64_t v = fcn( stdlib_complex64( (float)re0, (float)im0 ), stdlib_complex64( (float)re1, (float)im1 ) );
441+
float re;
442+
float im;
443+
stdlib_reimf( v, &re, &im );
434444

435445
napi_value obj;
436446
status = napi_create_object( env, &obj );
437447
assert( status == napi_ok );
438448

439449
napi_value vre;
440-
status = napi_create_double( env, (double)crealf( v ), &vre );
450+
status = napi_create_double( env, (double)re, &vre );
441451
assert( status == napi_ok );
442452

443453
status = napi_set_named_property( env, obj, "re", vre );
444454
assert( status == napi_ok );
445455

446456
napi_value vim;
447-
status = napi_create_double( env, (double)cimagf( v ), &vim );
457+
status = napi_create_double( env, (double)im, &vim );
448458
assert( status == napi_ok );
449459

450460
status = napi_set_named_property( env, obj, "im", vim );

0 commit comments

Comments
 (0)