Skip to content

Commit 7ce2628

Browse files
refactor: update math/base/special/kernel-tan
PR-URL: #5017 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent d77378e commit 7ce2628

File tree

7 files changed

+96
-110
lines changed

7 files changed

+96
-110
lines changed

lib/node_modules/@stdlib/math/base/special/kernel-tan/benchmark/benchmark.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var linspace = require( '@stdlib/array/base/linspace' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var PI = require( '@stdlib/constants/float64/pi' );
2727
var pkg = require( './../package.json' ).name;
@@ -31,14 +31,17 @@ var kernelTan = require( './../lib' );
3131
// MAIN //
3232

3333
bench( pkg+':k=1', function benchmark( b ) {
34+
var len;
3435
var x;
3536
var y;
3637
var i;
3738

39+
len = 100;
40+
x = linspace( -PI/4.0, PI/4.0, len );
41+
3842
b.tic();
3943
for ( i = 0; i < b.iterations; i++ ) {
40-
x = ( randu() * PI/2.0 ) - ( PI/4.0 );
41-
y = kernelTan( x, 0.0, 1 );
44+
y = kernelTan( x[ i % len ], 0.0, 1 );
4245
if ( isnan( y ) ) {
4346
b.fail( 'should not return NaN' );
4447
}
@@ -52,14 +55,17 @@ bench( pkg+':k=1', function benchmark( b ) {
5255
});
5356

5457
bench( pkg+':k=-1', function benchmark( b ) {
58+
var len;
5559
var x;
5660
var y;
5761
var i;
5862

63+
len = 100;
64+
x = linspace( -PI/4.0, PI/4.0, len );
65+
5966
b.tic();
6067
for ( i = 0; i < b.iterations; i++ ) {
61-
x = ( randu() * PI/2.0 ) - ( PI/4.0 );
62-
y = kernelTan( x, 0.0, -1 );
68+
y = kernelTan( x[ i % len ], 0.0, -1 );
6369
if ( isnan( y ) ) {
6470
b.fail( 'should not return NaN' );
6571
}

lib/node_modules/@stdlib/math/base/special/kernel-tan/benchmark/benchmark.native.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
25+
var linspace = require( '@stdlib/array/base/linspace' );
2626
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2727
var PI = require( '@stdlib/constants/float64/pi' );
2828
var tryRequire = require( '@stdlib/utils/try-require' );
@@ -40,14 +40,17 @@ var opts = {
4040
// MAIN //
4141

4242
bench( pkg+'::native:k=1', opts, function benchmark( b ) {
43+
var len;
4344
var x;
4445
var y;
4546
var i;
4647

48+
len = 100;
49+
x = linspace( -PI/4.0, PI/4.0, len );
50+
4751
b.tic();
4852
for ( i = 0; i < b.iterations; i++ ) {
49-
x = ( randu() * PI/2.0 ) - ( PI/4.0 );
50-
y = kernelTan( x, 0.0, 1 );
53+
y = kernelTan( x[ i % len ], 0.0, 1 );
5154
if ( isnan( y ) ) {
5255
b.fail( 'should not return NaN' );
5356
}
@@ -61,14 +64,17 @@ bench( pkg+'::native:k=1', opts, function benchmark( b ) {
6164
});
6265

6366
bench( pkg+'::native:k=-1', opts, function benchmark( b ) {
67+
var len;
6468
var x;
6569
var y;
6670
var i;
6771

72+
len = 100;
73+
x = linspace( -PI/4.0, PI/4.0, len );
74+
6875
b.tic();
6976
for ( i = 0; i < b.iterations; i++ ) {
70-
x = ( randu() * PI/2.0 ) - ( PI/4.0 );
71-
y = kernelTan( x, 0.0, -1 );
77+
y = kernelTan( x[ i % len ], 0.0, -1 );
7278
if ( isnan( y ) ) {
7379
b.fail( 'should not return NaN' );
7480
}

lib/node_modules/@stdlib/math/base/special/kernel-tan/benchmark/c/native/benchmark.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,18 @@ static double rand_double( void ) {
9191
*/
9292
static double benchmark( void ) {
9393
double elapsed;
94-
double x;
94+
double x[ 100 ];
9595
double z;
9696
double t;
9797
int i;
9898

99+
for ( i = 0; i < 100; i++ ) {
100+
x[ i ] = ( ( rand_double() * 2.0 ) - 1.0 ) * 0.7853981633974483;
101+
}
102+
99103
t = tic();
100104
for ( i = 0; i < ITERATIONS; i++ ) {
101-
x = ( ( rand_double() * 2.0 ) - 1.0 ) * 0.7853981633974483;
102-
z = stdlib_base_kernel_tan( x, 0.0, 1 );
105+
z = stdlib_base_kernel_tan( x[ i % 100 ], 0.0, 1 );
103106
if ( z != z ) {
104107
printf( "should not return NaN\n" );
105108
break;

lib/node_modules/@stdlib/math/base/special/kernel-tan/manifest.json

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
2-
"options": {},
2+
"options": {
3+
"task": "build"
4+
},
35
"fields": [
46
{
57
"field": "src",
@@ -24,6 +26,46 @@
2426
],
2527
"confs": [
2628
{
29+
"task": "build",
30+
"src": [
31+
"./src/main.c"
32+
],
33+
"include": [
34+
"./include"
35+
],
36+
"libraries": [
37+
"-lm"
38+
],
39+
"libpath": [],
40+
"dependencies": [
41+
"@stdlib/number/float64/base/get-high-word",
42+
"@stdlib/number/float64/base/set-low-word",
43+
"@stdlib/napi/export",
44+
"@stdlib/napi/argv",
45+
"@stdlib/napi/argv-double",
46+
"@stdlib/napi/argv-int32",
47+
"@stdlib/napi/create-double"
48+
]
49+
},
50+
{
51+
"task": "benchmark",
52+
"src": [
53+
"./src/main.c"
54+
],
55+
"include": [
56+
"./include"
57+
],
58+
"libraries": [
59+
"-lm"
60+
],
61+
"libpath": [],
62+
"dependencies": [
63+
"@stdlib/number/float64/base/get-high-word",
64+
"@stdlib/number/float64/base/set-low-word"
65+
]
66+
},
67+
{
68+
"task": "examples",
2769
"src": [
2870
"./src/main.c"
2971
],
Lines changed: 13 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2022 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -17,9 +17,12 @@
1717
*/
1818

1919
#include "stdlib/math/base/special/kernel_tan.h"
20+
#include "stdlib/napi/argv.h"
21+
#include "stdlib/napi/argv_double.h"
22+
#include "stdlib/napi/argv_int32.h"
23+
#include "stdlib/napi/create_double.h"
24+
#include "stdlib/napi/export.h"
2025
#include <node_api.h>
21-
#include <stdint.h>
22-
#include <assert.h>
2326

2427
/**
2528
* Receives JavaScript callback invocation data.
@@ -29,86 +32,12 @@
2932
* @return Node-API value
3033
*/
3134
static napi_value addon( napi_env env, napi_callback_info info ) {
32-
napi_status status;
33-
34-
// Get callback arguments:
35-
size_t argc = 3;
36-
napi_value argv[ 3 ];
37-
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
38-
assert( status == napi_ok );
39-
40-
// Check whether we were provided the correct number of arguments:
41-
if ( argc < 3 ) {
42-
status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." );
43-
assert( status == napi_ok );
44-
return NULL;
45-
}
46-
if ( argc > 3 ) {
47-
status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." );
48-
assert( status == napi_ok );
49-
return NULL;
50-
}
51-
52-
napi_valuetype vtype0;
53-
status = napi_typeof( env, argv[ 0 ], &vtype0 );
54-
assert( status == napi_ok );
55-
if ( vtype0 != napi_number ) {
56-
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
57-
assert( status == napi_ok );
58-
return NULL;
59-
}
60-
61-
napi_valuetype vtype1;
62-
status = napi_typeof( env, argv[ 1 ], &vtype1 );
63-
assert( status == napi_ok );
64-
if ( vtype1 != napi_number ) {
65-
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." );
66-
assert( status == napi_ok );
67-
return NULL;
68-
}
69-
70-
napi_valuetype vtype2;
71-
status = napi_typeof( env, argv[ 2 ], &vtype2 );
72-
assert( status == napi_ok );
73-
if ( vtype1 != napi_number ) {
74-
status = napi_throw_type_error( env, NULL, "invalid argument. third argument must be a number." );
75-
assert( status == napi_ok );
76-
return NULL;
77-
}
78-
79-
double x;
80-
status = napi_get_value_double( env, argv[ 0 ], &x );
81-
assert( status == napi_ok );
82-
83-
double y;
84-
status = napi_get_value_double( env, argv[ 1 ], &y );
85-
assert( status == napi_ok );
86-
87-
int32_t k;
88-
status = napi_get_value_int32( env, argv[ 2 ], &k );
89-
assert( status == napi_ok );
90-
91-
double out = stdlib_base_kernel_tan( x, y, k );
92-
93-
napi_value v;
94-
status = napi_create_double( env, out, &v );
95-
assert( status == napi_ok );
96-
97-
return v;
98-
}
99-
100-
/**
101-
* Initializes a Node-API module.
102-
*
103-
* @param env environment under which the function is invoked
104-
* @param exports exports object
105-
* @return main export
106-
*/
107-
static napi_value init( napi_env env, napi_value exports ) {
108-
napi_value fcn;
109-
napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn );
110-
assert( status == napi_ok );
111-
return fcn;
35+
STDLIB_NAPI_ARGV( env, info, argv, argc, 3 );
36+
STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 );
37+
STDLIB_NAPI_ARGV_DOUBLE( env, y, argv, 1 );
38+
STDLIB_NAPI_ARGV_INT32( env, k, argv, 2 );
39+
STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_base_kernel_tan( x, y, k ), out );
40+
return out;
11241
}
11342

114-
NAPI_MODULE( NODE_GYP_MODULE_NAME, init )
43+
STDLIB_NAPI_MODULE_EXPORT_FCN( addon )

lib/node_modules/@stdlib/math/base/special/kernel-tan/test/test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@ tape( 'main export is a function', function test( t ) {
3939

4040
tape( 'the function returns `NaN` if provided `NaN` for `x` or `y`', function test( t ) {
4141
var v = kernelTan( NaN, 0.0, 1.0 );
42-
t.equal( isnan( v ), true, 'returns NaN' );
42+
t.equal( isnan( v ), true, 'returns expected value' );
4343

4444
v = kernelTan( 4.0, NaN, 1.0 );
45-
t.equal( isnan( v ), true, 'returns NaN' );
45+
t.equal( isnan( v ), true, 'returns expected value' );
4646

4747
v = kernelTan( NaN, NaN, 1.0 );
48-
t.equal( isnan( v ), true, 'returns NaN' );
48+
t.equal( isnan( v ), true, 'returns expected value' );
4949

5050
v = kernelTan( NaN, 0.0, -1.0 );
51-
t.equal( isnan( v ), true, 'returns NaN' );
51+
t.equal( isnan( v ), true, 'returns expected value' );
5252

5353
v = kernelTan( 4.0, NaN, -1.0 );
54-
t.equal( isnan( v ), true, 'returns NaN' );
54+
t.equal( isnan( v ), true, 'returns expected value' );
5555

5656
v = kernelTan( NaN, NaN, -1.0 );
57-
t.equal( isnan( v ), true, 'returns NaN' );
57+
t.equal( isnan( v ), true, 'returns expected value' );
5858

5959
t.end();
6060
});

lib/node_modules/@stdlib/math/base/special/kernel-tan/test/test.native.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,22 @@ tape( 'main export is a function', opts, function test( t ) {
4848

4949
tape( 'the function returns `NaN` if provided `NaN` for `x` or `y`', opts, function test( t ) {
5050
var v = kernelTan( NaN, 0.0, 1.0 );
51-
t.equal( isnan( v ), true, 'returns NaN' );
51+
t.equal( isnan( v ), true, 'returns expected value' );
5252

5353
v = kernelTan( 4.0, NaN, 1.0 );
54-
t.equal( isnan( v ), true, 'returns NaN' );
54+
t.equal( isnan( v ), true, 'returns expected value' );
5555

5656
v = kernelTan( NaN, NaN, 1.0 );
57-
t.equal( isnan( v ), true, 'returns NaN' );
57+
t.equal( isnan( v ), true, 'returns expected value' );
5858

5959
v = kernelTan( NaN, 0.0, -1.0 );
60-
t.equal( isnan( v ), true, 'returns NaN' );
60+
t.equal( isnan( v ), true, 'returns expected value' );
6161

6262
v = kernelTan( 4.0, NaN, -1.0 );
63-
t.equal( isnan( v ), true, 'returns NaN' );
63+
t.equal( isnan( v ), true, 'returns expected value' );
6464

6565
v = kernelTan( NaN, NaN, -1.0 );
66-
t.equal( isnan( v ), true, 'returns NaN' );
66+
t.equal( isnan( v ), true, 'returns expected value' );
6767

6868
t.end();
6969
});

0 commit comments

Comments
 (0)