-
-
Notifications
You must be signed in to change notification settings - Fork 844
feat: add C implementation for math/base/special/heaviside
#6823
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
base: develop
Are you sure you want to change the base?
Changes from 3 commits
d8c4cd7
a3796f7
86d0252
ad3eecc
4160ea6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,7 +2,7 @@ | |||||
|
||||||
@license Apache-2.0 | ||||||
|
||||||
Copyright (c) 2018 The Stdlib Authors. | ||||||
Copyright (c) 2025 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. | ||||||
|
@@ -164,6 +164,105 @@ logEachMap( 'H(%0.4f) = %0.4f', x, heaviside ); | |||||
|
||||||
<!-- /.examples --> | ||||||
|
||||||
<!-- C interface documentation. --> | ||||||
|
||||||
* * * | ||||||
|
||||||
<section class "c"> | ||||||
|
||||||
## C APIs | ||||||
|
||||||
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||||||
|
||||||
<section class="intro"> | ||||||
|
||||||
</section> | ||||||
|
||||||
<!-- /.intro --> | ||||||
|
||||||
<!-- C usage documentation. --> | ||||||
|
||||||
<section class="usage"> | ||||||
|
||||||
### Usage | ||||||
|
||||||
```c | ||||||
#include "stdlib/math/base/special/heaviside.h" | ||||||
``` | ||||||
|
||||||
#### stdlib_base_heaviside( x ) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||
|
||||||
Evaluate the [Heaviside function][heaviside-function]. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||
|
||||||
```c | ||||||
double out = stdlib_base_heaviside( 0.0, 0 ); | ||||||
// returns 0.0 | ||||||
|
||||||
out = stdlib_base_heaviside( 3.141592653589793 / 2.0, 0 ); | ||||||
// returns 1.0 | ||||||
``` | ||||||
|
||||||
The function accepts the following arguments: | ||||||
|
||||||
- **x**: `[in] double` input value. | ||||||
- **continuity**: `[in] int` input value. | ||||||
|
||||||
```c | ||||||
double stdlib_base_heaviside( const double x, int continuity ); | ||||||
``` | ||||||
|
||||||
</section> | ||||||
|
||||||
<!-- /.usage --> | ||||||
|
||||||
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||||||
|
||||||
<section class="notes"> | ||||||
|
||||||
</section> | ||||||
|
||||||
<!-- /.notes --> | ||||||
|
||||||
<!-- C API usage examples. --> | ||||||
|
||||||
<section class="examples"> | ||||||
|
||||||
### Examples | ||||||
|
||||||
```c | ||||||
#include "stdlib/math/base/special/heaviside.h" | ||||||
#include <stdio.h> | ||||||
|
||||||
int main( void ) { | ||||||
const double x[] = { 0.0, -0.523, 0.785, -1.047, 3.14 }; | ||||||
|
||||||
double y; | ||||||
int i; | ||||||
for ( i = 0; i < 5; i++ ) { | ||||||
y = stdlib_base_heaviside( x[ i ], STDLIB_HEAVISIDE_CONTINUITY_HALF ); | ||||||
printf( "heaviside(%lf, half-maximum) = %lf\n", x[ i ], y ); | ||||||
} | ||||||
|
||||||
for ( i = 0; i < 5; i++ ) { | ||||||
y = stdlib_base_heaviside( x[ i ], STDLIB_HEAVISIDE_CONTINUITY_LEFT ); | ||||||
printf( "heaviside(%lf, left-continuous) = %lf\n", x[ i ], y ); | ||||||
} | ||||||
|
||||||
for ( i = 0; i < 5; i++ ) { | ||||||
y = stdlib_base_heaviside( x[ i ], STDLIB_HEAVISIDE_CONTINUITY_RIGHT ); | ||||||
printf( "heaviside(%lf, right-continuous) = %lf\n", x[ i ], y ); | ||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
</section> | ||||||
|
||||||
<!-- /.examples --> | ||||||
|
||||||
</section> | ||||||
|
||||||
<!-- /.c --> | ||||||
|
||||||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||||||
|
||||||
<section class="related"> | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var resolve = require( 'path' ).resolve; | ||
var bench = require( '@stdlib/bench' ); | ||
var uniform = require( '@stdlib/random/array/uniform' ); | ||
var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
var tryRequire = require( '@stdlib/utils/try-require' ); | ||
var pkg = require( './../package.json' ).name; | ||
|
||
|
||
// VARIABLES // | ||
|
||
var heaviside = tryRequire( resolve( __dirname, './../lib/native.js' ) ); | ||
var opts = { | ||
'skip': ( heaviside instanceof Error ) | ||
}; | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg+'::native', opts, function benchmark( b ) { | ||
var x; | ||
var y; | ||
var i; | ||
|
||
x = uniform( 100, -10.0, 10.0 ); | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
y = heaviside( x[ i%x.length ] ); | ||
if ( isnan( y ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( isnan( y ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,7 @@ | ||||||
#/ | ||||||
# @license Apache-2.0 | ||||||
# | ||||||
# Copyright (c) 2018 The Stdlib Authors. | ||||||
# Copyright (c) 2025 The Stdlib Authors. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||
# | ||||||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
# you may not use this file except in compliance with the License. | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,7 @@ | ||||||
/** | ||||||
* @license Apache-2.0 | ||||||
* | ||||||
* Copyright (c) 2018 The Stdlib Authors. | ||||||
* Copyright (c) 2025 The Stdlib Authors. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||
* | ||||||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
* you may not use this file except in compliance with the License. | ||||||
|
@@ -21,9 +21,10 @@ | |||||
#include <math.h> | ||||||
#include <time.h> | ||||||
#include <sys/time.h> | ||||||
#include "stdlib/math/base/special/heaviside.h" | ||||||
|
||||||
#define NAME "heaviside" | ||||||
#define ITERATIONS 1000000 | ||||||
#define ITERATIONS 10000 | ||||||
#define REPEATS 3 | ||||||
|
||||||
/** | ||||||
|
@@ -83,25 +84,12 @@ static double rand_double( void ) { | |||||
return (double)r / ( (double)RAND_MAX + 1.0 ); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Computes the Heaviside function. | ||||||
* | ||||||
* @param x input value | ||||||
* @return function result | ||||||
*/ | ||||||
double heaviside( double x ) { | ||||||
if ( x >= 0.0 ) { | ||||||
return 1.0; | ||||||
} | ||||||
return 0.0; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Runs a benchmark. | ||||||
* | ||||||
* @return elapsed time in seconds | ||||||
*/ | ||||||
static double benchmark( void ) { | ||||||
static double benchmark( stdlib_heaviside_continuity_t continuity ) { | ||||||
double elapsed; | ||||||
double x[ 100 ]; | ||||||
double y; | ||||||
|
@@ -114,7 +102,7 @@ static double benchmark( void ) { | |||||
|
||||||
t = tic(); | ||||||
for ( i = 0; i < ITERATIONS; i++ ) { | ||||||
y = heaviside( x[ i%100 ] ); | ||||||
y = stdlib_base_heaviside( x[ i%100 ], continuity ); | ||||||
if ( y != y ) { | ||||||
printf( "should not return NaN\n" ); | ||||||
break; | ||||||
|
@@ -140,7 +128,23 @@ int main( void ) { | |||||
print_version(); | ||||||
for ( i = 0; i < REPEATS; i++ ) { | ||||||
printf( "# c::%s\n", NAME ); | ||||||
elapsed = benchmark(); | ||||||
elapsed = benchmark(STDLIB_HEAVISIDE_CONTINUITY_LEFT); | ||||||
print_results( elapsed ); | ||||||
printf( "ok %d benchmark finished\n", i+1 ); | ||||||
} | ||||||
print_summary( REPEATS, REPEATS ); | ||||||
|
||||||
for ( i = 0; i < REPEATS; i++ ) { | ||||||
printf( "# c::%s\n", NAME ); | ||||||
elapsed = benchmark(STDLIB_HEAVISIDE_CONTINUITY_HALF); | ||||||
print_results( elapsed ); | ||||||
printf( "ok %d benchmark finished\n", i+1 ); | ||||||
} | ||||||
print_summary( REPEATS, REPEATS ); | ||||||
|
||||||
for ( i = 0; i < REPEATS; i++ ) { | ||||||
printf( "# c::%s\n", NAME ); | ||||||
elapsed = benchmark(STDLIB_HEAVISIDE_CONTINUITY_RIGHT); | ||||||
print_results( elapsed ); | ||||||
printf( "ok %d benchmark finished\n", i+1 ); | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can avoid updating copyright years for pre-existing files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done