Skip to content

Commit 9b48575

Browse files
feat: add C implementation for math/base/special/heavisidef
PR-URL: #7022 Ref: #649 Co-authored-by: stdlib-bot <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 35f5f7a commit 9b48575

File tree

23 files changed

+1782
-15
lines changed

23 files changed

+1782
-15
lines changed

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

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ var heavisidef = require( '@stdlib/math/base/special/heavisidef' );
155155
var opts = {
156156
'dtype': 'float32'
157157
};
158-
var x = uniform( 101, -10.0, 10.0, opts );
158+
var x = uniform( 100, -10.0, 10.0, opts );
159159

160160
logEachMap( 'H(%0.4f) = %0.4f', x, heavisidef );
161161
```
@@ -164,6 +164,104 @@ logEachMap( 'H(%0.4f) = %0.4f', x, heavisidef );
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/heavisidef.h"
191+
```
192+
193+
#### stdlib_base_heavisidef( x, continuity )
194+
195+
Evaluates the [Heaviside function][heaviside-function] for a single-precision floating-point number.
196+
197+
```c
198+
float y = stdlib_base_heavisidef( 0.0f, STDLIB_BASE_HEAVISIDEF_CONTINUITY_HALF_MAXIMUM );
199+
// returns 0.5f
200+
201+
y = stdlib_base_heavisidef( 0.0f, STDLIB_BASE_HEAVISIDEF_CONTINUITY_LEFT_CONTINUOUS );
202+
// returns 0.0f
203+
```
204+
205+
The function accepts the following arguments:
206+
207+
- **x**: `[in] float` input value.
208+
- **continuity**: `[in] STDLIB_BASE_HEAVISIDEF_CONTINUITY` continuity option.
209+
210+
The `continuity` parameter may be one of the following values:
211+
212+
- `STDLIB_BASE_HEAVISIDEF_CONTINUITY_HALF_MAXIMUM`: if `x == 0`, the function returns `0.5`.
213+
- `STDLIB_BASE_HEAVISIDEF_CONTINUITY_LEFT_CONTINUOUS`: if `x == 0`, the function returns `0.0`.
214+
- `STDLIB_BASE_HEAVISIDEF_CONTINUITY_RIGHT_CONTINUOUS`: if `x == 0`, the function returns `1.0`.
215+
- `STDLIB_BASE_HEAVISIDEF_CONTINUITY_DISCONTINUOUS`: if `x == 0`, the function returns `NaN`.
216+
217+
If provided a `continuity` argument which is not one of the enumeration constants listed above, the function returns `NaN` for `x == 0`, behaving like the discontinuous case.
218+
219+
```c
220+
float stdlib_base_heavisidef( const float x, const STDLIB_BASE_HEAVISIDEF_CONTINUITY continuity );
221+
```
222+
223+
</section>
224+
225+
<!-- /.usage -->
226+
227+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
228+
229+
<section class="notes">
230+
231+
</section>
232+
233+
<!-- /.notes -->
234+
235+
<!-- C API usage examples. -->
236+
237+
<section class="examples">
238+
239+
### Examples
240+
241+
```c
242+
#include "stdlib/math/base/special/heavisidef.h"
243+
#include <stdio.h>
244+
245+
int main( void ) {
246+
const float x[] = { -4.0f, -3.0f, -2.0f, -1.0f, 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
247+
248+
float y;
249+
int i;
250+
for ( i = 0; i < 10; i++ ) {
251+
y = stdlib_base_heavisidef( x[ i ], STDLIB_BASE_HEAVISIDEF_CONTINUITY_HALF_MAXIMUM );
252+
printf( "H(%f) = %f\n", x[ i ], y );
253+
}
254+
}
255+
```
256+
257+
</section>
258+
259+
<!-- /.examples -->
260+
261+
</section>
262+
263+
<!-- /.c -->
264+
167265
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
168266

169267
<section class="related">

lib/node_modules/@stdlib/math/base/special/heavisidef/benchmark/benchmark.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ bench( pkg, function benchmark( b ) {
3434
var y;
3535
var i;
3636

37-
x = uniform( 100, -50.0, 50.0 );
37+
x = uniform( 100, -50.0, 50.0, {
38+
'dtype': 'float32'
39+
});
3840

3941
b.tic();
4042
for ( i = 0; i < b.iterations; i++ ) {
41-
y = heavisidef( x[ i % x.length ] );
43+
y = heavisidef( x[ i%x.length ] );
4244
if ( y > 1.0 ) {
4345
b.fail( 'should not return a value greater than 1' );
4446
}
@@ -56,11 +58,13 @@ bench( pkg+'::left-continuous', function benchmark( b ) {
5658
var y;
5759
var i;
5860

59-
x = uniform( 100, -50.0, 50.0 );
61+
x = uniform( 100, -50.0, 50.0, {
62+
'dtype': 'float32'
63+
});
6064

6165
b.tic();
6266
for ( i = 0; i < b.iterations; i++ ) {
63-
y = heavisidef( x[ i % x.length ], 'left-continuous' );
67+
y = heavisidef( x[ i%x.length ], 'left-continuous' );
6468
if ( isnanf( y ) ) {
6569
b.fail( 'should not return NaN' );
6670
}
@@ -78,11 +82,13 @@ bench( pkg+'::right-continuous', function benchmark( b ) {
7882
var y;
7983
var i;
8084

81-
x = uniform( 100, -50.0, 50.0 );
85+
x = uniform( 100, -50.0, 50.0, {
86+
'dtype': 'float32'
87+
});
8288

8389
b.tic();
8490
for ( i = 0; i < b.iterations; i++ ) {
85-
y = heavisidef( x[ i % x.length ], 'right-continuous' );
91+
y = heavisidef( x[ i%x.length ], 'right-continuous' );
8692
if ( isnanf( y ) ) {
8793
b.fail( 'should not return NaN' );
8894
}
@@ -100,11 +106,13 @@ bench( pkg+'::half-maximum', function benchmark( b ) {
100106
var y;
101107
var i;
102108

103-
x = uniform( 100, -50.0, 50.0 );
109+
x = uniform( 100, -50.0, 50.0, {
110+
'dtype': 'float32'
111+
});
104112

105113
b.tic();
106114
for ( i = 0; i < b.iterations; i++ ) {
107-
y = heavisidef( x[ i % x.length ], 'half-maximum' );
115+
y = heavisidef( x[ i%x.length ], 'half-maximum' );
108116
if ( isnanf( y ) ) {
109117
b.fail( 'should not return NaN' );
110118
}
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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var heavisidef = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( heavisidef 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, -50.0, 50.0, {
47+
'dtype': 'float32'
48+
});
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
y = heavisidef( x[ i%x.length ] );
53+
if ( isnanf( y ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
}
57+
b.toc();
58+
if ( isnanf( y ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
20+
# VARIABLES #
21+
22+
ifndef VERBOSE
23+
QUIET := @
24+
else
25+
QUIET :=
26+
endif
27+
28+
# Determine the OS ([1][1], [2][2]).
29+
#
30+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
31+
# [2]: http://stackoverflow.com/a/27776822/2225624
32+
OS ?= $(shell uname)
33+
ifneq (, $(findstring MINGW,$(OS)))
34+
OS := WINNT
35+
else
36+
ifneq (, $(findstring MSYS,$(OS)))
37+
OS := WINNT
38+
else
39+
ifneq (, $(findstring CYGWIN,$(OS)))
40+
OS := WINNT
41+
else
42+
ifneq (, $(findstring Windows_NT,$(OS)))
43+
OS := WINNT
44+
endif
45+
endif
46+
endif
47+
endif
48+
49+
# Define the program used for compiling C source files:
50+
ifdef C_COMPILER
51+
CC := $(C_COMPILER)
52+
else
53+
CC := gcc
54+
endif
55+
56+
# Define the command-line options when compiling C files:
57+
CFLAGS ?= \
58+
-std=c99 \
59+
-O3 \
60+
-Wall \
61+
-pedantic
62+
63+
# Determine whether to generate position independent code ([1][1], [2][2]).
64+
#
65+
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
66+
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
67+
ifeq ($(OS), WINNT)
68+
fPIC ?=
69+
else
70+
fPIC ?= -fPIC
71+
endif
72+
73+
# List of C targets:
74+
c_targets := benchmark.out
75+
76+
77+
# RULES #
78+
79+
#/
80+
# Compiles C source files.
81+
#
82+
# @param {string} [C_COMPILER] - C compiler
83+
# @param {string} [CFLAGS] - C compiler flags
84+
# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code
85+
#
86+
# @example
87+
# make
88+
#
89+
# @example
90+
# make all
91+
#/
92+
all: $(c_targets)
93+
94+
.PHONY: all
95+
96+
#/
97+
# Compiles C source files.
98+
#
99+
# @private
100+
# @param {string} CC - C compiler
101+
# @param {string} CFLAGS - C compiler flags
102+
# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code
103+
#/
104+
$(c_targets): %.out: %.c
105+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $< -lm
106+
107+
#/
108+
# Runs compiled benchmarks.
109+
#
110+
# @example
111+
# make run
112+
#/
113+
run: $(c_targets)
114+
$(QUIET) ./$<
115+
116+
.PHONY: run
117+
118+
#/
119+
# Removes generated files.
120+
#
121+
# @example
122+
# make clean
123+
#/
124+
clean:
125+
$(QUIET) -rm -f *.o *.out
126+
127+
.PHONY: clean

0 commit comments

Comments
 (0)