@stdlib/math-base-napi-unary 0.0.4 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/main.c CHANGED
@@ -17,9 +17,14 @@
17
17
  */
18
18
 
19
19
  #include "stdlib/math/base/napi/unary.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"
20
24
  #include <node_api.h>
21
25
  #include <stdint.h>
22
26
  #include <assert.h>
27
+ #include <stdbool.h>
23
28
 
24
29
  /**
25
30
  * Invokes a unary function accepting and returning double-precision floating-point numbers.
@@ -33,7 +38,7 @@
33
38
  * @param env environment under which the function is invoked
34
39
  * @param info callback data
35
40
  * @param fcn unary function
36
- * @return function return value as an N-API double-precision floating-point number
41
+ * @return function return value as a Node-API double-precision floating-point number
37
42
  */
38
43
  napi_value stdlib_math_base_napi_d_d( napi_env env, napi_callback_info info, double (*fcn)( double ) ) {
39
44
  napi_status status;
@@ -81,7 +86,7 @@ napi_value stdlib_math_base_napi_d_d( napi_env env, napi_callback_info info, dou
81
86
  * @param env environment under which the function is invoked
82
87
  * @param info callback data
83
88
  * @param fcn unary function
84
- * @return function return value as an N-API double-precision floating-point number
89
+ * @return function return value as a Node-API double-precision floating-point number
85
90
  */
86
91
  napi_value stdlib_math_base_napi_f_f( napi_env env, napi_callback_info info, float (*fcn)( float ) ) {
87
92
  napi_status status;
@@ -117,6 +122,388 @@ napi_value stdlib_math_base_napi_f_f( napi_env env, napi_callback_info info, flo
117
122
  return v;
118
123
  }
119
124
 
125
+ /**
126
+ * Invokes a unary function accepting and returning double-precision complex floating-point numbers.
127
+ *
128
+ * ## Notes
129
+ *
130
+ * - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
131
+ *
132
+ * - `x`: input value.
133
+ *
134
+ * @param env environment under which the function is invoked
135
+ * @param info callback data
136
+ * @param fcn unary function
137
+ * @return function return value as a Node-API complex-like object
138
+ */
139
+ napi_value stdlib_math_base_napi_z_z( napi_env env, napi_callback_info info, stdlib_complex128_t (*fcn)( stdlib_complex128_t ) ) {
140
+ napi_status status;
141
+
142
+ size_t argc = 1;
143
+ napi_value argv[ 1 ];
144
+ status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
145
+ assert( status == napi_ok );
146
+
147
+ if ( argc < 1 ) {
148
+ status = napi_throw_error( env, NULL, "invalid invocation. Must provide a complex number." );
149
+ assert( status == napi_ok );
150
+ return NULL;
151
+ }
152
+
153
+ bool hprop;
154
+ status = napi_has_named_property( env, argv[ 0 ], "re", &hprop );
155
+ assert( status == napi_ok );
156
+ if ( !hprop ) {
157
+ status = napi_throw_type_error( env, NULL, "invalid argument. Argument must have a real component." );
158
+ assert( status == napi_ok );
159
+ return NULL;
160
+ }
161
+
162
+ napi_value xre;
163
+ status = napi_get_named_property( env, argv[ 0 ], "re", &xre );
164
+ assert( status == napi_ok );
165
+
166
+ napi_valuetype xretype;
167
+ status = napi_typeof( env, xre, &xretype );
168
+ assert( status == napi_ok );
169
+ if ( xretype != napi_number ) {
170
+ status = napi_throw_type_error( env, NULL, "invalid argument. Argument must have a real component which is a number." );
171
+ assert( status == napi_ok );
172
+ return NULL;
173
+ }
174
+
175
+ status = napi_has_named_property( env, argv[ 0 ], "im", &hprop );
176
+ assert( status == napi_ok );
177
+ if ( !hprop ) {
178
+ status = napi_throw_type_error( env, NULL, "invalid argument. Argument must have an imaginary component." );
179
+ assert( status == napi_ok );
180
+ return NULL;
181
+ }
182
+
183
+ napi_value xim;
184
+ status = napi_get_named_property( env, argv[ 0 ], "im", &xim );
185
+ assert( status == napi_ok );
186
+
187
+ napi_valuetype ximtype;
188
+ status = napi_typeof( env, xim, &ximtype );
189
+ assert( status == napi_ok );
190
+ if ( ximtype != napi_number ) {
191
+ status = napi_throw_type_error( env, NULL, "invalid argument. Argument must have an imaginary component which a number." );
192
+ assert( status == napi_ok );
193
+ return NULL;
194
+ }
195
+
196
+ double re0;
197
+ status = napi_get_value_double( env, xre, &re0 );
198
+ assert( status == napi_ok );
199
+
200
+ double im0;
201
+ status = napi_get_value_double( env, xim, &im0 );
202
+ assert( status == napi_ok );
203
+
204
+ stdlib_complex128_t v = fcn( stdlib_complex128( re0, im0 ) );
205
+ double re;
206
+ double im;
207
+ stdlib_reim( v, &re, &im );
208
+
209
+ napi_value obj;
210
+ status = napi_create_object( env, &obj );
211
+ assert( status == napi_ok );
212
+
213
+ napi_value vre;
214
+ status = napi_create_double( env, re, &vre );
215
+ assert( status == napi_ok );
216
+
217
+ status = napi_set_named_property( env, obj, "re", vre );
218
+ assert( status == napi_ok );
219
+
220
+ napi_value vim;
221
+ status = napi_create_double( env, im, &vim );
222
+ assert( status == napi_ok );
223
+
224
+ status = napi_set_named_property( env, obj, "im", vim );
225
+ assert( status == napi_ok );
226
+
227
+ return obj;
228
+ }
229
+
230
+ /**
231
+ * Invokes a unary function accepting a double-precision complex floating-point number and returning a double-precision floating-point number.
232
+ *
233
+ * ## Notes
234
+ *
235
+ * - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
236
+ *
237
+ * - `x`: input value.
238
+ *
239
+ * @param env environment under which the function is invoked
240
+ * @param info callback data
241
+ * @param fcn unary function
242
+ * @return function return value as a Node-API complex-like object
243
+ */
244
+ napi_value stdlib_math_base_napi_z_d( napi_env env, napi_callback_info info, double (*fcn)( stdlib_complex128_t ) ) {
245
+ napi_status status;
246
+
247
+ size_t argc = 1;
248
+ napi_value argv[ 1 ];
249
+ status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
250
+ assert( status == napi_ok );
251
+
252
+ if ( argc < 1 ) {
253
+ status = napi_throw_error( env, NULL, "invalid invocation. Must provide a complex number." );
254
+ assert( status == napi_ok );
255
+ return NULL;
256
+ }
257
+
258
+ bool hprop;
259
+ status = napi_has_named_property( env, argv[ 0 ], "re", &hprop );
260
+ assert( status == napi_ok );
261
+ if ( !hprop ) {
262
+ status = napi_throw_type_error( env, NULL, "invalid argument. Argument must have a real component." );
263
+ assert( status == napi_ok );
264
+ return NULL;
265
+ }
266
+
267
+ napi_value xre;
268
+ status = napi_get_named_property( env, argv[ 0 ], "re", &xre );
269
+ assert( status == napi_ok );
270
+
271
+ napi_valuetype xretype;
272
+ status = napi_typeof( env, xre, &xretype );
273
+ assert( status == napi_ok );
274
+ if ( xretype != napi_number ) {
275
+ status = napi_throw_type_error( env, NULL, "invalid argument. Argument must have a real component which is a number." );
276
+ assert( status == napi_ok );
277
+ return NULL;
278
+ }
279
+
280
+ status = napi_has_named_property( env, argv[ 0 ], "im", &hprop );
281
+ assert( status == napi_ok );
282
+ if ( !hprop ) {
283
+ status = napi_throw_type_error( env, NULL, "invalid argument. Argument must have an imaginary component." );
284
+ assert( status == napi_ok );
285
+ return NULL;
286
+ }
287
+
288
+ napi_value xim;
289
+ status = napi_get_named_property( env, argv[ 0 ], "im", &xim );
290
+ assert( status == napi_ok );
291
+
292
+ napi_valuetype ximtype;
293
+ status = napi_typeof( env, xim, &ximtype );
294
+ assert( status == napi_ok );
295
+ if ( ximtype != napi_number ) {
296
+ status = napi_throw_type_error( env, NULL, "invalid argument. Argument must have an imaginary component which a number." );
297
+ assert( status == napi_ok );
298
+ return NULL;
299
+ }
300
+
301
+ double re;
302
+ status = napi_get_value_double( env, xre, &re );
303
+ assert( status == napi_ok );
304
+
305
+ double im;
306
+ status = napi_get_value_double( env, xim, &im );
307
+ assert( status == napi_ok );
308
+
309
+ napi_value v;
310
+ status = napi_create_double( env, fcn( stdlib_complex128( re, im ) ), &v );
311
+ assert( status == napi_ok );
312
+
313
+ return v;
314
+ }
315
+
316
+ /**
317
+ * Invokes a unary function accepting and returning single-precision complex floating-point numbers.
318
+ *
319
+ * ## Notes
320
+ *
321
+ * - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
322
+ *
323
+ * - `x`: input value.
324
+ *
325
+ * @param env environment under which the function is invoked
326
+ * @param info callback data
327
+ * @param fcn unary function
328
+ * @return function return value as a Node-API complex-like object
329
+ */
330
+ napi_value stdlib_math_base_napi_c_c( napi_env env, napi_callback_info info, stdlib_complex64_t (*fcn)( stdlib_complex64_t ) ) {
331
+ napi_status status;
332
+
333
+ size_t argc = 1;
334
+ napi_value argv[ 1 ];
335
+ status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
336
+ assert( status == napi_ok );
337
+
338
+ if ( argc < 1 ) {
339
+ status = napi_throw_error( env, NULL, "invalid invocation. Must provide a complex number." );
340
+ assert( status == napi_ok );
341
+ return NULL;
342
+ }
343
+
344
+ bool hprop;
345
+ status = napi_has_named_property( env, argv[ 0 ], "re", &hprop );
346
+ assert( status == napi_ok );
347
+ if ( !hprop ) {
348
+ status = napi_throw_type_error( env, NULL, "invalid argument. Argument must have a real component." );
349
+ assert( status == napi_ok );
350
+ return NULL;
351
+ }
352
+
353
+ napi_value xre;
354
+ status = napi_get_named_property( env, argv[ 0 ], "re", &xre );
355
+ assert( status == napi_ok );
356
+
357
+ napi_valuetype xretype;
358
+ status = napi_typeof( env, xre, &xretype );
359
+ assert( status == napi_ok );
360
+ if ( xretype != napi_number ) {
361
+ status = napi_throw_type_error( env, NULL, "invalid argument. Argument must have a real component which is a number." );
362
+ assert( status == napi_ok );
363
+ return NULL;
364
+ }
365
+
366
+ status = napi_has_named_property( env, argv[ 0 ], "im", &hprop );
367
+ assert( status == napi_ok );
368
+ if ( !hprop ) {
369
+ status = napi_throw_type_error( env, NULL, "invalid argument. Argument must have an imaginary component." );
370
+ assert( status == napi_ok );
371
+ return NULL;
372
+ }
373
+
374
+ napi_value xim;
375
+ status = napi_get_named_property( env, argv[ 0 ], "im", &xim );
376
+ assert( status == napi_ok );
377
+
378
+ napi_valuetype ximtype;
379
+ status = napi_typeof( env, xim, &ximtype );
380
+ assert( status == napi_ok );
381
+ if ( ximtype != napi_number ) {
382
+ status = napi_throw_type_error( env, NULL, "invalid argument. Argument must have an imaginary component which a number." );
383
+ assert( status == napi_ok );
384
+ return NULL;
385
+ }
386
+
387
+ double re0;
388
+ status = napi_get_value_double( env, xre, &re0 );
389
+ assert( status == napi_ok );
390
+
391
+ double im0;
392
+ status = napi_get_value_double( env, xim, &im0 );
393
+ assert( status == napi_ok );
394
+
395
+ stdlib_complex64_t v = fcn( stdlib_complex64( (float)re0, (float)im0 ) );
396
+ float re;
397
+ float im;
398
+ stdlib_reimf( v, &re, &im );
399
+
400
+ napi_value obj;
401
+ status = napi_create_object( env, &obj );
402
+ assert( status == napi_ok );
403
+
404
+ napi_value vre;
405
+ status = napi_create_double( env, (double)re, &vre );
406
+ assert( status == napi_ok );
407
+
408
+ status = napi_set_named_property( env, obj, "re", vre );
409
+ assert( status == napi_ok );
410
+
411
+ napi_value vim;
412
+ status = napi_create_double( env, (double)im, &vim );
413
+ assert( status == napi_ok );
414
+
415
+ status = napi_set_named_property( env, obj, "im", vim );
416
+ assert( status == napi_ok );
417
+
418
+ return obj;
419
+ }
420
+
421
+ /**
422
+ * Invokes a unary function accepting a single-precision complex floating-point number and returning a single-precision floating-point number.
423
+ *
424
+ * ## Notes
425
+ *
426
+ * - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
427
+ *
428
+ * - `x`: input value.
429
+ *
430
+ * @param env environment under which the function is invoked
431
+ * @param info callback data
432
+ * @param fcn unary function
433
+ * @return function return value as a Node-API complex-like object
434
+ */
435
+ napi_value stdlib_math_base_napi_c_f( napi_env env, napi_callback_info info, float (*fcn)( stdlib_complex64_t ) ) {
436
+ napi_status status;
437
+
438
+ size_t argc = 1;
439
+ napi_value argv[ 1 ];
440
+ status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
441
+ assert( status == napi_ok );
442
+
443
+ if ( argc < 1 ) {
444
+ status = napi_throw_error( env, NULL, "invalid invocation. Must provide a complex number." );
445
+ assert( status == napi_ok );
446
+ return NULL;
447
+ }
448
+
449
+ bool hprop;
450
+ status = napi_has_named_property( env, argv[ 0 ], "re", &hprop );
451
+ assert( status == napi_ok );
452
+ if ( !hprop ) {
453
+ status = napi_throw_type_error( env, NULL, "invalid argument. Argument must have a real component." );
454
+ assert( status == napi_ok );
455
+ return NULL;
456
+ }
457
+
458
+ napi_value xre;
459
+ status = napi_get_named_property( env, argv[ 0 ], "re", &xre );
460
+ assert( status == napi_ok );
461
+
462
+ napi_valuetype xretype;
463
+ status = napi_typeof( env, xre, &xretype );
464
+ assert( status == napi_ok );
465
+ if ( xretype != napi_number ) {
466
+ status = napi_throw_type_error( env, NULL, "invalid argument. Argument must have a real component which is a number." );
467
+ assert( status == napi_ok );
468
+ return NULL;
469
+ }
470
+
471
+ status = napi_has_named_property( env, argv[ 0 ], "im", &hprop );
472
+ assert( status == napi_ok );
473
+ if ( !hprop ) {
474
+ status = napi_throw_type_error( env, NULL, "invalid argument. Argument must have an imaginary component." );
475
+ assert( status == napi_ok );
476
+ return NULL;
477
+ }
478
+
479
+ napi_value xim;
480
+ status = napi_get_named_property( env, argv[ 0 ], "im", &xim );
481
+ assert( status == napi_ok );
482
+
483
+ napi_valuetype ximtype;
484
+ status = napi_typeof( env, xim, &ximtype );
485
+ assert( status == napi_ok );
486
+ if ( ximtype != napi_number ) {
487
+ status = napi_throw_type_error( env, NULL, "invalid argument. Argument must have an imaginary component which a number." );
488
+ assert( status == napi_ok );
489
+ return NULL;
490
+ }
491
+
492
+ double re;
493
+ status = napi_get_value_double( env, xre, &re );
494
+ assert( status == napi_ok );
495
+
496
+ double im;
497
+ status = napi_get_value_double( env, xim, &im );
498
+ assert( status == napi_ok );
499
+
500
+ napi_value v;
501
+ status = napi_create_double( env, (double)fcn( stdlib_complex64( (float)re, (float)im ) ), &v );
502
+ assert( status == napi_ok );
503
+
504
+ return v;
505
+ }
506
+
120
507
  /**
121
508
  * Invokes a unary function accepting and returning 32-bit signed integers.
122
509
  *
@@ -129,7 +516,7 @@ napi_value stdlib_math_base_napi_f_f( napi_env env, napi_callback_info info, flo
129
516
  * @param env environment under which the function is invoked
130
517
  * @param info callback data
131
518
  * @param fcn unary function
132
- * @return function return value as an N-API 32-bit signed integer
519
+ * @return function return value as a Node-API 32-bit signed integer
133
520
  */
134
521
  napi_value stdlib_math_base_napi_i_i( napi_env env, napi_callback_info info, int32_t (*fcn)( int32_t ) ) {
135
522
  napi_status status;
package/CHANGELOG.md DELETED
@@ -1,5 +0,0 @@
1
- # CHANGELOG
2
-
3
- > Package changelog.
4
-
5
- See [GitHub Releases](https://github.com/stdlib-js/math-base-napi-unary/releases) for the changelog.
package/binding.gyp DELETED
@@ -1,170 +0,0 @@
1
- # @license Apache-2.0
2
- #
3
- # Copyright (c) 2020 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
- }