Skip to content

refactor: update math/base/special/kernel-tan #5017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// MODULES //

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

bench( pkg+':k=1', function benchmark( b ) {
var len;
var x;
var y;
var i;

len = 100;
x = linspace( -PI/4.0, PI/4.0, len );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu() * PI/2.0 ) - ( PI/4.0 );
y = kernelTan( x, 0.0, 1 );
y = kernelTan( x[ i % len ], 0.0, 1 );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -52,14 +55,17 @@ bench( pkg+':k=1', function benchmark( b ) {
});

bench( pkg+':k=-1', function benchmark( b ) {
var len;
var x;
var y;
var i;

len = 100;
x = linspace( -PI/4.0, PI/4.0, len );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu() * PI/2.0 ) - ( PI/4.0 );
y = kernelTan( x, 0.0, -1 );
y = kernelTan( x[ i % len ], 0.0, -1 );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

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

bench( pkg+'::native:k=1', opts, function benchmark( b ) {
var len;
var x;
var y;
var i;

len = 100;
x = linspace( -PI/4.0, PI/4.0, len );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu() * PI/2.0 ) - ( PI/4.0 );
y = kernelTan( x, 0.0, 1 );
y = kernelTan( x[ i % len ], 0.0, 1 );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -61,14 +64,17 @@ bench( pkg+'::native:k=1', opts, function benchmark( b ) {
});

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

len = 100;
x = linspace( -PI/4.0, PI/4.0, len );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu() * PI/2.0 ) - ( PI/4.0 );
y = kernelTan( x, 0.0, -1 );
y = kernelTan( x[ i % len ], 0.0, -1 );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,18 @@ static double rand_double( void ) {
*/
static double benchmark( void ) {
double elapsed;
double x;
double x[ 100 ];
double z;
double t;
int i;

for ( i = 0; i < 100; i++ ) {
x[ i ] = ( ( rand_double() * 2.0 ) - 1.0 ) * 0.7853981633974483;
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = ( ( rand_double() * 2.0 ) - 1.0 ) * 0.7853981633974483;
z = stdlib_base_kernel_tan( x, 0.0, 1 );
z = stdlib_base_kernel_tan( x[ i % 100 ], 0.0, 1 );
if ( z != z ) {
printf( "should not return NaN\n" );
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"options": {},
"options": {
"task": "build"
},
"fields": [
{
"field": "src",
Expand All @@ -24,6 +26,46 @@
],
"confs": [
{
"task": "build",
"src": [
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": [
"@stdlib/number/float64/base/get-high-word",
"@stdlib/number/float64/base/set-low-word",
"@stdlib/napi/export",
"@stdlib/napi/argv",
"@stdlib/napi/argv-double",
"@stdlib/napi/argv-int32",
"@stdlib/napi/create-double"
]
},
{
"task": "benchmark",
"src": [
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": [
"@stdlib/number/float64/base/get-high-word",
"@stdlib/number/float64/base/set-low-word"
]
},
{
"task": "examples",
"src": [
"./src/main.c"
],
Expand Down
97 changes: 13 additions & 84 deletions lib/node_modules/@stdlib/math/base/special/kernel-tan/src/addon.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2022 The Stdlib Authors.
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,9 +17,12 @@
*/

#include "stdlib/math/base/special/kernel_tan.h"
#include "stdlib/napi/argv.h"
#include "stdlib/napi/argv_double.h"
#include "stdlib/napi/argv_int32.h"
#include "stdlib/napi/create_double.h"
#include "stdlib/napi/export.h"
#include <node_api.h>
#include <stdint.h>
#include <assert.h>

/**
* Receives JavaScript callback invocation data.
Expand All @@ -29,86 +32,12 @@
* @return Node-API value
*/
static napi_value addon( napi_env env, napi_callback_info info ) {
napi_status status;

// Get callback arguments:
size_t argc = 3;
napi_value argv[ 3 ];
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
assert( status == napi_ok );

// Check whether we were provided the correct number of arguments:
if ( argc < 3 ) {
status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." );
assert( status == napi_ok );
return NULL;
}
if ( argc > 3 ) {
status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." );
assert( status == napi_ok );
return NULL;
}

napi_valuetype vtype0;
status = napi_typeof( env, argv[ 0 ], &vtype0 );
assert( status == napi_ok );
if ( vtype0 != napi_number ) {
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
assert( status == napi_ok );
return NULL;
}

napi_valuetype vtype1;
status = napi_typeof( env, argv[ 1 ], &vtype1 );
assert( status == napi_ok );
if ( vtype1 != napi_number ) {
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." );
assert( status == napi_ok );
return NULL;
}

napi_valuetype vtype2;
status = napi_typeof( env, argv[ 2 ], &vtype2 );
assert( status == napi_ok );
if ( vtype1 != napi_number ) {
status = napi_throw_type_error( env, NULL, "invalid argument. third argument must be a number." );
assert( status == napi_ok );
return NULL;
}

double x;
status = napi_get_value_double( env, argv[ 0 ], &x );
assert( status == napi_ok );

double y;
status = napi_get_value_double( env, argv[ 1 ], &y );
assert( status == napi_ok );

int32_t k;
status = napi_get_value_int32( env, argv[ 2 ], &k );
assert( status == napi_ok );

double out = stdlib_base_kernel_tan( x, y, k );

napi_value v;
status = napi_create_double( env, out, &v );
assert( status == napi_ok );

return v;
}

/**
* Initializes a Node-API module.
*
* @param env environment under which the function is invoked
* @param exports exports object
* @return main export
*/
static napi_value init( napi_env env, napi_value exports ) {
napi_value fcn;
napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn );
assert( status == napi_ok );
return fcn;
STDLIB_NAPI_ARGV( env, info, argv, argc, 3 );
STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 );
STDLIB_NAPI_ARGV_DOUBLE( env, y, argv, 1 );
STDLIB_NAPI_ARGV_INT32( env, k, argv, 2 );
STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_base_kernel_tan( x, y, k ), out );
return out;
}

NAPI_MODULE( NODE_GYP_MODULE_NAME, init )
STDLIB_NAPI_MODULE_EXPORT_FCN( addon )
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@ tape( 'main export is a function', function test( t ) {

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

v = kernelTan( 4.0, NaN, 1.0 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

v = kernelTan( NaN, NaN, 1.0 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

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

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

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

t.end();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ tape( 'main export is a function', opts, function test( t ) {

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

v = kernelTan( 4.0, NaN, 1.0 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

v = kernelTan( NaN, NaN, 1.0 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

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

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

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

t.end();
});
Expand Down