Skip to content

Commit 7824100

Browse files
committed
feat: initial commit for feedback
1 parent 26d65cd commit 7824100

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
#ifndef STDLIB_MATH_BASE_SPECIAL_HEAVISIDE_H
20+
#define STDLIB_MATH_BASE_SPECIAL_HEAVISIDE_H
21+
22+
/*
23+
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
24+
*/
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
// Enumeration of function continuity:
30+
typedef enum STDLIB_MATH_CONTINUITY {
31+
// Half-maximum:
32+
STDLIB_MATH_HALF_MAXIMUM = 0,
33+
34+
// Left-continuous:
35+
STDLIB_MATH_LEFT_CONTINUOUS = 1,
36+
37+
// Right-continuous:
38+
STDLIB_MATH_RIGHT_CONTINUOUS = 2,
39+
40+
// Discontinuous:
41+
STDLIB_MATH_DISCONTINUOUS = 3
42+
} STDLIB_MATH_CONTINUITY;
43+
44+
/**
45+
* Evaluates the Heaviside function.
46+
*/
47+
double stdlib_base_heaviside( const double x, const STDLIB_MATH_CONTINUITY continuity );
48+
49+
#ifdef __cplusplus
50+
}
51+
#endif
52+
53+
#endif // !STDLIB_MATH_BASE_SPECIAL_HEAVISIDE_H
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 addon = require( './../src/addon.node' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Evaluates the Heaviside function.
30+
*
31+
* @private
32+
* @param {number} x - input value
33+
* @param {string} [continuity] - continuity option
34+
* @returns {number} function value
35+
*
36+
* @example
37+
* var v = heaviside( 0.0, 0 );
38+
* // returns 0.5
39+
*
40+
* @example
41+
* var v = heaviside( 0.0, 1 );
42+
* // returns 0.0
43+
*
44+
* @example
45+
* var v = heaviside( 0.0, 2 );
46+
* // returns 1.0
47+
*
48+
* @example
49+
* var v = heaviside( 0.0, 3 );
50+
* // returns NaN
51+
*
52+
* @example
53+
* var v = heaviside( NaN, 2 );
54+
* // returns NaN
55+
*/
56+
function heaviside( x, continuity ) {
57+
return addon( x, continuity );
58+
}
59+
60+
61+
// EXPORTS //
62+
63+
module.exports = heaviside;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
#include "stdlib/math/base/special/heaviside.h"
20+
#include "stdlib/napi/export.h"
21+
#include "stdlib/napi/argv.h"
22+
#include "stdlib/napi/argv_int32.h"
23+
#include "stdlib/napi/argv_double.h"
24+
#include "stdlib/napi/create_double.h"
25+
#include <node_api.h>
26+
27+
/**
28+
* Receives JavaScript callback invocation data.
29+
*
30+
* @param env environment under which the function is invoked
31+
* @param info callback data
32+
* @return Node-API value
33+
*/
34+
static napi_value addon( napi_env env, napi_callback_info info ) {
35+
STDLIB_NAPI_ARGV( env, info, argv, argc, 2 );
36+
STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 );
37+
STDLIB_NAPI_ARGV_INT32( env, continuity, argv, 1 );
38+
STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_base_heaviside( x, (STDLIB_MATH_CONTINUITY)continuity ), out );
39+
return out;
40+
}
41+
42+
STDLIB_NAPI_MODULE_EXPORT_FCN( addon )
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
#include "stdlib/math/base/special/heaviside.h"
20+
#include "stdlib/math/base/assert/is_nan.h"
21+
22+
/**
23+
* Evaluates the Heaviside function.
24+
*
25+
* @param x input value
26+
* @param continuity continuity option
27+
* @return function value
28+
*
29+
* @example
30+
* double y = stdlib_base_heaviside( 0.0, STDLIB_MATH_HALF_MAXIMUM );
31+
* // returns 0.5
32+
*/
33+
double stdlib_base_heaviside( const double x, const STDLIB_MATH_CONTINUITY continuity ) {
34+
if ( stdlib_base_is_nan( x ) ) {
35+
return 0.0 / 0.0; // NaN
36+
}
37+
if ( x > 0.0 ) {
38+
return 1.0;
39+
}
40+
// Handle `+-0`...
41+
if ( x == 0.0 ) {
42+
switch ( continuity ) {
43+
case STDLIB_MATH_HALF_MAXIMUM:
44+
return 0.5;
45+
case STDLIB_MATH_LEFT_CONTINUOUS:
46+
return 0.0;
47+
case STDLIB_MATH_RIGHT_CONTINUOUS:
48+
return 1.0;
49+
case STDLIB_MATH_DISCONTINUOUS:
50+
return 0.0 / 0.0; // NaN
51+
default:
52+
return 0.0 / 0.0; // NaN
53+
}
54+
}
55+
return 0.0;
56+
}

0 commit comments

Comments
 (0)