Skip to content

Commit d8c4cd7

Browse files
committed
feat: add c implementation of stdlib/math/base/special/heaviside
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 70429b3 commit d8c4cd7

File tree

17 files changed

+1111
-21
lines changed

17 files changed

+1111
-21
lines changed

lib/node_modules/@stdlib/math/base/special/heaviside/README.md

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@license Apache-2.0
44
5-
Copyright (c) 2018 The Stdlib Authors.
5+
Copyright (c) 2025 The Stdlib Authors.
66
77
Licensed under the Apache License, Version 2.0 (the "License");
88
you may not use this file except in compliance with the License.
@@ -164,6 +164,105 @@ logEachMap( 'H(%0.4f) = %0.4f', x, heaviside );
164164

165165
<!-- /.examples -->
166166

167+
<!-- C interface documentation. -->
168+
169+
* * *
170+
171+
<section class "c">
172+
173+
## C APIs
174+
175+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
176+
177+
<section class="intro">
178+
179+
</section>
180+
181+
<!-- /.intro -->
182+
183+
<!-- C usage documentation. -->
184+
185+
<section class="usage">
186+
187+
### Usage
188+
189+
```c
190+
#include "stdlib/math/base/special/heaviside.h"
191+
```
192+
193+
#### stdlib_base_heaviside( x )
194+
195+
Evaluate the [Heaviside function][heaviside-function].
196+
197+
```c
198+
double out = stdlib_base_heaviside( 0.0, 0 );
199+
// returns 0.0
200+
201+
out = stdlib_base_heaviside( 3.141592653589793 / 2.0, 0 );
202+
// returns 1.0
203+
```
204+
205+
The function accepts the following arguments:
206+
207+
- **x**: `[in] double` input value.
208+
- **continuity**: `[in] int` input value.
209+
210+
```c
211+
double stdlib_base_heaviside( const double x, int continuity );
212+
```
213+
214+
</section>
215+
216+
<!-- /.usage -->
217+
218+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
219+
220+
<section class="notes">
221+
222+
</section>
223+
224+
<!-- /.notes -->
225+
226+
<!-- C API usage examples. -->
227+
228+
<section class="examples">
229+
230+
### Examples
231+
232+
```c
233+
#include "stdlib/math/base/special/heaviside.h"
234+
#include <stdio.h>
235+
236+
int main( void ) {
237+
const double x[] = { 0.0, -0.523, 0.785, -1.047, 3.14 };
238+
239+
double y;
240+
int i;
241+
for ( i = 0; i < 5; i++ ) {
242+
y = stdlib_base_heaviside( x[ i ], STDLIB_HEAVISIDE_CONTINUITY_HALF );
243+
printf( "heaviside(%lf, half-maximum) = %lf\n", x[ i ], y );
244+
}
245+
246+
for ( i = 0; i < 5; i++ ) {
247+
y = stdlib_base_heaviside( x[ i ], STDLIB_HEAVISIDE_CONTINUITY_LEFT );
248+
printf( "heaviside(%lf, left-continuous) = %lf\n", x[ i ], y );
249+
}
250+
251+
for ( i = 0; i < 5; i++ ) {
252+
y = stdlib_base_heaviside( x[ i ], STDLIB_HEAVISIDE_CONTINUITY_RIGHT );
253+
printf( "heaviside(%lf, right-continuous) = %lf\n", x[ i ], y );
254+
}
255+
}
256+
```
257+
258+
</section>
259+
260+
<!-- /.examples -->
261+
262+
</section>
263+
264+
<!-- /.c -->
265+
167266
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
168267

169268
<section class="related">
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var heaviside = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( heaviside instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
x = uniform( 100, -10.0, 10.0 );
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
y = heaviside( x[ i%x.length ] );
51+
if ( isnan( y ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
}
55+
b.toc();
56+
if ( isnan( y ) ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});

lib/node_modules/@stdlib/math/base/special/heaviside/benchmark/c/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#/
22
# @license Apache-2.0
33
#
4-
# Copyright (c) 2018 The Stdlib Authors.
4+
# Copyright (c) 2025 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.

lib/node_modules/@stdlib/math/base/special/heaviside/benchmark/c/benchmark.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2018 The Stdlib Authors.
4+
* Copyright (c) 2025 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.
@@ -21,9 +21,10 @@
2121
#include <math.h>
2222
#include <time.h>
2323
#include <sys/time.h>
24+
#include "stdlib/math/base/special/heaviside.h"
2425

2526
#define NAME "heaviside"
26-
#define ITERATIONS 1000000
27+
#define ITERATIONS 10000
2728
#define REPEATS 3
2829

2930
/**
@@ -83,25 +84,12 @@ static double rand_double( void ) {
8384
return (double)r / ( (double)RAND_MAX + 1.0 );
8485
}
8586

86-
/**
87-
* Computes the Heaviside function.
88-
*
89-
* @param x input value
90-
* @return function result
91-
*/
92-
double heaviside( double x ) {
93-
if ( x >= 0.0 ) {
94-
return 1.0;
95-
}
96-
return 0.0;
97-
}
98-
9987
/**
10088
* Runs a benchmark.
10189
*
10290
* @return elapsed time in seconds
10391
*/
104-
static double benchmark( void ) {
92+
static double benchmark( stdlib_heaviside_continuity_t continuity ) {
10593
double elapsed;
10694
double x[ 100 ];
10795
double y;
@@ -114,7 +102,7 @@ static double benchmark( void ) {
114102

115103
t = tic();
116104
for ( i = 0; i < ITERATIONS; i++ ) {
117-
y = heaviside( x[ i%100 ] );
105+
y = stdlib_base_heaviside( x[ i%100 ], continuity );
118106
if ( y != y ) {
119107
printf( "should not return NaN\n" );
120108
break;
@@ -140,7 +128,23 @@ int main( void ) {
140128
print_version();
141129
for ( i = 0; i < REPEATS; i++ ) {
142130
printf( "# c::%s\n", NAME );
143-
elapsed = benchmark();
131+
elapsed = benchmark(STDLIB_HEAVISIDE_CONTINUITY_LEFT);
132+
print_results( elapsed );
133+
printf( "ok %d benchmark finished\n", i+1 );
134+
}
135+
print_summary( REPEATS, REPEATS );
136+
137+
for ( i = 0; i < REPEATS; i++ ) {
138+
printf( "# c::%s\n", NAME );
139+
elapsed = benchmark(STDLIB_HEAVISIDE_CONTINUITY_HALF);
140+
print_results( elapsed );
141+
printf( "ok %d benchmark finished\n", i+1 );
142+
}
143+
print_summary( REPEATS, REPEATS );
144+
145+
for ( i = 0; i < REPEATS; i++ ) {
146+
printf( "# c::%s\n", NAME );
147+
elapsed = benchmark(STDLIB_HEAVISIDE_CONTINUITY_RIGHT);
144148
print_results( elapsed );
145149
printf( "ok %d benchmark finished\n", i+1 );
146150
}

0 commit comments

Comments
 (0)