Skip to content

Commit 3eb7264

Browse files
committed
feat: add C implementation for math/base/special/heavisidef
--- 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: na - task: lint_license_headers status: passed ---
1 parent 041c0d8 commit 3eb7264

File tree

14 files changed

+242
-121
lines changed

14 files changed

+242
-121
lines changed

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
2525
var uniform = require( '@stdlib/random/array/uniform' );
26-
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2727
var tryRequire = require( '@stdlib/utils/try-require' );
2828
var pkg = require( './../package.json' ).name;
2929

3030

3131
// VARIABLES //
3232

33-
var heaviside = tryRequire( resolve( __dirname, './../lib/native.js' ) );
33+
var heavisidef = tryRequire( resolve( __dirname, './../lib/native.js' ) );
3434
var opts = {
35-
'skip': ( heaviside instanceof Error )
35+
'skip': ( heavisidef instanceof Error )
3636
};
3737

3838

@@ -43,17 +43,19 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4343
var y;
4444
var i;
4545

46-
x = uniform( 100, -50.0, 50.0 );
46+
x = uniform( 100, -50.0, 50.0, {
47+
'dtype': 'float32'
48+
});
4749

4850
b.tic();
4951
for ( i = 0; i < b.iterations; i++ ) {
50-
y = heaviside( x[ i%x.length ], 'half-maximum' );
51-
if ( isnan( y ) ) {
52+
y = heavisidef( x[ i%x.length ], 'half-maximum' );
53+
if ( isnanf( y ) ) {
5254
b.fail( 'should not return NaN' );
5355
}
5456
}
5557
b.toc();
56-
if ( isnan( y ) ) {
58+
if ( isnanf( y ) ) {
5759
b.fail( 'should not return NaN' );
5860
}
5961
b.pass( 'benchmark finished' );

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <time.h>
2323
#include <sys/time.h>
2424

25-
#define NAME "heaviside"
25+
#define NAME "heavisidef"
2626
#define ITERATIONS 1000000
2727
#define REPEATS 3
2828

@@ -78,9 +78,9 @@ static double tic( void ) {
7878
*
7979
* @return random number
8080
*/
81-
static double rand_double( void ) {
81+
static double rand_float( void ) {
8282
int r = rand();
83-
return (double)r / ( (double)RAND_MAX + 1.0 );
83+
return (float)r / ( (float)RAND_MAX + 1.0 );
8484
}
8585

8686
/**
@@ -89,11 +89,11 @@ static double rand_double( void ) {
8989
* @param x input value
9090
* @return function result
9191
*/
92-
double heaviside( double x ) {
93-
if ( x >= 0.0 ) {
94-
return 1.0;
92+
float heavisidef( float x ) {
93+
if ( x >= 0.0f ) {
94+
return 1.0f;
9595
}
96-
return 0.0;
96+
return 0.0f;
9797
}
9898

9999
/**
@@ -103,18 +103,18 @@ double heaviside( double x ) {
103103
*/
104104
static double benchmark( void ) {
105105
double elapsed;
106-
double x[ 100 ];
107-
double y;
106+
float x[ 100 ];
108107
double t;
108+
float y;
109109
int i;
110110

111111
for ( i = 0; i < 100; i++ ) {
112-
x[ i ] = ( 100.0*rand_double() ) - 50.0;
112+
x[ i ] = ( 100.0f*rand_float() ) - 50.0f;
113113
}
114114

115115
t = tic();
116116
for ( i = 0; i < ITERATIONS; i++ ) {
117-
y = heaviside( x[ i%100 ] );
117+
y = heavisidef( x[ i%100 ] );
118118
if ( y != y ) {
119119
printf( "should not return NaN\n" );
120120
break;

lib/node_modules/@stdlib/math/base/special/heavisidef/benchmark/c/native/benchmark.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include "stdlib/math/base/special/heaviside.h"
19+
#include "stdlib/math/base/special/heavisidef.h"
2020
#include <stdlib.h>
2121
#include <stdio.h>
2222
#include <math.h>
2323
#include <time.h>
2424
#include <sys/time.h>
2525

26-
#define NAME "heaviside"
26+
#define NAME "heavisidef"
2727
#define ITERATIONS 1000000
2828
#define REPEATS 3
2929

@@ -79,9 +79,9 @@ static double tic( void ) {
7979
*
8080
* @return random number
8181
*/
82-
static double rand_double( void ) {
82+
static double rand_float( void ) {
8383
int r = rand();
84-
return (double)r / ( (double)RAND_MAX + 1.0 );
84+
return (float)r / ( (float)RAND_MAX + 1.0f );
8585
}
8686

8787
/**
@@ -91,18 +91,18 @@ static double rand_double( void ) {
9191
*/
9292
static double benchmark( void ) {
9393
double elapsed;
94-
double x[ 100 ];
95-
double y;
94+
float x[ 100 ];
9695
double t;
96+
float y;
9797
int i;
9898

9999
for ( i = 0; i < 100; i++ ) {
100-
x[ i ] = ( 100.0*rand_double() ) - 50.0;
100+
x[ i ] = ( 100.0f*rand_float() ) - 50.0f;
101101
}
102102

103103
t = tic();
104104
for ( i = 0; i < ITERATIONS; i++ ) {
105-
y = stdlib_base_heaviside( x[ i%100 ], STDLIB_BASE_HEAVISIDE_CONTINUITY_HALF_MAXIMUM );
105+
y = stdlib_base_heavisidef( x[ i%100 ], STDLIB_BASE_HEAVISIDEF_CONTINUITY_HALF_MAXIMUM );
106106
if ( y != y ) {
107107
printf( "should not return NaN\n" );
108108
break;

lib/node_modules/@stdlib/math/base/special/heavisidef/examples/c/example.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include "stdlib/math/base/special/heaviside.h"
19+
#include "stdlib/math/base/special/heavisidef.h"
2020
#include <stdio.h>
2121

2222
int main( void ) {
23-
const double x[] = { -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 };
23+
const float x[] = { -4.0f, -3.0f, -2.0f, -1.0f, 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
2424

25-
double y;
25+
float y;
2626
int i;
2727
for ( i = 0; i < 10; i++ ) {
28-
y = stdlib_base_heaviside( x[ i ], STDLIB_BASE_HEAVISIDE_CONTINUITY_HALF_MAXIMUM );
29-
printf( "H(%lf) = %lf\n", x[ i ], y );
28+
y = stdlib_base_heavisidef( x[ i ], STDLIB_BASE_HEAVISIDEF_CONTINUITY_HALF_MAXIMUM );
29+
printf( "H(%f) = %f\n", x[ i ], y );
3030
}
3131
}

lib/node_modules/@stdlib/math/base/special/heavisidef/include/stdlib/math/base/special/heaviside.h renamed to lib/node_modules/@stdlib/math/base/special/heavisidef/include/stdlib/math/base/special/heavisidef.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
* limitations under the License.
1717
*/
1818

19-
#ifndef STDLIB_MATH_BASE_SPECIAL_HEAVISIDE_H
20-
#define STDLIB_MATH_BASE_SPECIAL_HEAVISIDE_H
19+
#ifndef STDLIB_MATH_BASE_SPECIAL_HEAVISIDEF_H
20+
#define STDLIB_MATH_BASE_SPECIAL_HEAVISIDEF_H
2121

2222
/*
2323
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
@@ -27,27 +27,27 @@ extern "C" {
2727
#endif
2828

2929
// Enumeration of function continuity:
30-
typedef enum STDLIB_BASE_HEAVISIDE_CONTINUITY {
30+
typedef enum STDLIB_BASE_HEAVISIDEF_CONTINUITY {
3131
// Half-maximum:
32-
STDLIB_BASE_HEAVISIDE_CONTINUITY_HALF_MAXIMUM = 0,
32+
STDLIB_BASE_HEAVISIDEF_CONTINUITY_HALF_MAXIMUM = 0,
3333

3434
// Left-continuous:
35-
STDLIB_BASE_HEAVISIDE_CONTINUITY_LEFT_CONTINUOUS = 1,
35+
STDLIB_BASE_HEAVISIDEF_CONTINUITY_LEFT_CONTINUOUS = 1,
3636

3737
// Right-continuous:
38-
STDLIB_BASE_HEAVISIDE_CONTINUITY_RIGHT_CONTINUOUS = 2,
38+
STDLIB_BASE_HEAVISIDEF_CONTINUITY_RIGHT_CONTINUOUS = 2,
3939

4040
// Discontinuous:
41-
STDLIB_BASE_HEAVISIDE_CONTINUITY_DISCONTINUOUS = 3
42-
} STDLIB_BASE_HEAVISIDE_CONTINUITY;
41+
STDLIB_BASE_HEAVISIDEF_CONTINUITY_DISCONTINUOUS = 3
42+
} STDLIB_BASE_HEAVISIDEF_CONTINUITY;
4343

4444
/**
45-
* Evaluates the Heaviside function.
45+
* Evaluates the Heaviside function for a single-precision floating-point number.
4646
*/
47-
double stdlib_base_heaviside( const double x, const STDLIB_BASE_HEAVISIDE_CONTINUITY continuity );
47+
float stdlib_base_heavisidef( const float x, const STDLIB_BASE_HEAVISIDEF_CONTINUITY continuity );
4848

4949
#ifdef __cplusplus
5050
}
5151
#endif
5252

53-
#endif // !STDLIB_MATH_BASE_SPECIAL_HEAVISIDE_H
53+
#endif // !STDLIB_MATH_BASE_SPECIAL_HEAVISIDEF_H

0 commit comments

Comments
 (0)