Skip to content

Commit a2133de

Browse files
vivekmaurya001kgrytestdlib-bot
authored
feat: add math/base/special/heavisidef
PR-URL: #3374 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 9241c28 commit a2133de

File tree

14 files changed

+1070
-0
lines changed

14 files changed

+1070
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Heaviside Function
22+
23+
> Evaluate the [Heaviside function][heaviside-function] for a single-precision floating-point number.
24+
25+
<section class="intro">
26+
27+
The [Heaviside function][heaviside-function] is defined as
28+
29+
<!-- <equation class="equation" label="eq:heaviside_function" align="center" raw="H(x) = \begin{cases} 1 & \textrm{if}\ x \gt 0 \\ 0 & \textrm{if}\ x \lt 0\end{cases}" alt="Heaviside function."> -->
30+
31+
```math
32+
H(x) = \begin{cases} 1 & \textrm{if}\ x \gt 0 \\ 0 & \textrm{if}\ x \lt 0\end{cases}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="H(x) = \begin{cases} 1 &amp; \textrm{if}\ x \gt 0 \\ 0 &amp; \textrm{if}\ x \lt 0\end{cases}" data-equation="eq:heaviside_function">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/heaviside/docs/img/equation_heaviside_function.svg" alt="Heaviside function.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
and is discontinuous at `0`. Depending on the context, the [Heaviside function][heaviside-function] may be defined as a continuous function. To define the [Heaviside function][heaviside-function] such that the function has rotational symmetry,
43+
44+
<!-- <equation class="equation" label="eq:heaviside_function_half_maximum" align="center" raw="H(x) = \begin{cases} x & \textrm{if}\ x \gt 0 \\ \frac{1}{2} & \textrm{if}\ x = 0 \\ 0 & \textrm{if}\ x \lt 0\end{cases}" alt="Heaviside function half-maximum."> -->
45+
46+
```math
47+
H(x) = \begin{cases} x & \textrm{if}\ x \gt 0 \\ \frac{1}{2} & \textrm{if}\ x = 0 \\ 0 & \textrm{if}\ x \lt 0\end{cases}
48+
```
49+
50+
<!-- <div class="equation" align="center" data-raw-text="H(x) = \begin{cases} x &amp; \textrm{if}\ x \gt 0 \\ \frac{1}{2} &amp; \textrm{if}\ x = 0 \\ 0 &amp; \textrm{if}\ x \lt 0\end{cases}" data-equation="eq:heaviside_function_half_maximum">
51+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/heaviside/docs/img/equation_heaviside_function_half_maximum.svg" alt="Heaviside function half-maximum.">
52+
<br>
53+
</div> -->
54+
55+
<!-- </equation> -->
56+
57+
To define the [Heaviside function][heaviside-function] as a left-continuous function,
58+
59+
<!-- <equation class="equation" label="eq:heaviside_function_left_continuous" align="center" raw="H(x) = \begin{cases} x & \textrm{if}\ x \gt 0 \\ 0 & \textrm{if}\ x \leq 0\end{cases}" alt="Heaviside function left-continuous."> -->
60+
61+
```math
62+
H(x) = \begin{cases} x & \textrm{if}\ x \gt 0 \\ 0 & \textrm{if}\ x \leq 0\end{cases}
63+
```
64+
65+
<!-- <div class="equation" align="center" data-raw-text="H(x) = \begin{cases} x &amp; \textrm{if}\ x \gt 0 \\ 0 &amp; \textrm{if}\ x \leq 0\end{cases}" data-equation="eq:heaviside_function_left_continuous">
66+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/heaviside/docs/img/equation_heaviside_function_left_continuous.svg" alt="Heaviside function left-continuous.">
67+
<br>
68+
</div> -->
69+
70+
<!-- </equation> -->
71+
72+
To define the [Heaviside function][heaviside-function] as a right-continuous function,
73+
74+
<!-- <equation class="equation" label="eq:heaviside_function_right_continuous" align="center" raw="H(x) = \begin{cases} x & \textrm{if}\ x \geq 0 \\ 0 & \textrm{if}\ x \lt 0\end{cases}" alt="Heaviside function right-continuous."> -->
75+
76+
```math
77+
H(x) = \begin{cases} x & \textrm{if}\ x \geq 0 \\ 0 & \textrm{if}\ x \lt 0\end{cases}
78+
```
79+
80+
<!-- <div class="equation" align="center" data-raw-text="H(x) = \begin{cases} x &amp; \textrm{if}\ x \geq 0 \\ 0 &amp; \textrm{if}\ x \lt 0\end{cases}" data-equation="eq:heaviside_function_right_continuous">
81+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/heaviside/docs/img/equation_heaviside_function_right_continuous.svg" alt="Heaviside function right-continuous.">
82+
<br>
83+
</div> -->
84+
85+
<!-- </equation> -->
86+
87+
</section>
88+
89+
<!-- /.intro -->
90+
91+
<section class="usage">
92+
93+
## Usage
94+
95+
```javascript
96+
var heavisidef = require( '@stdlib/math/base/special/heavisidef' );
97+
```
98+
99+
#### heavisidef( x\[, continuity] )
100+
101+
Evaluates the [Heaviside function][heaviside-function] for a single-precision floating-point number.
102+
103+
```javascript
104+
var v = heavisidef( 3.14 );
105+
// returns 1.0
106+
107+
v = heavisidef( -3.14 );
108+
// returns 0.0
109+
110+
v = heavisidef( NaN );
111+
// returns NaN
112+
```
113+
114+
The `continuity` parameter may be one of the following values:
115+
116+
- `'half-maximum'`: if `x == 0`, the function returns `0.5`.
117+
- `'left-continuous'`: if `x == 0`, the function returns `0.0`.
118+
- `'right-continuous'`: if `x == 0`, the function returns `1.0`.
119+
120+
By default, the function is discontinuous at `0`.
121+
122+
```javascript
123+
var v = heavisidef( 0.0 );
124+
// returns NaN
125+
```
126+
127+
To define the [Heaviside function][heaviside-function] as a continuous function, set the `continuity` parameter.
128+
129+
```javascript
130+
var v = heavisidef( 0.0, 'half-maximum' );
131+
// returns 0.5
132+
133+
v = heavisidef( 0.0, 'left-continuous' );
134+
// returns 0.0
135+
136+
v = heavisidef( 0.0, 'right-continuous' );
137+
// returns 1.0
138+
```
139+
140+
</section>
141+
142+
<!-- /.usage -->
143+
144+
<section class="examples">
145+
146+
## Examples
147+
148+
<!-- eslint no-undef: "error" -->
149+
150+
```javascript
151+
var linspace = require( '@stdlib/array/base/linspace' );
152+
var heavisidef = require( '@stdlib/math/base/special/heavisidef' );
153+
154+
var x = linspace( -10.0, 10.0, 101 );
155+
156+
var i;
157+
for ( i = 0; i < x.length; i++ ) {
158+
console.log( 'H(%d) = %f', x[ i ], heavisidef( x[ i ] ) );
159+
}
160+
```
161+
162+
</section>
163+
164+
<!-- /.examples -->
165+
166+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
167+
168+
<section class="related">
169+
170+
</section>
171+
172+
<!-- /.related -->
173+
174+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
175+
176+
<section class="links">
177+
178+
[heaviside-function]: https://en.wikipedia.org/wiki/Heaviside_step_function
179+
180+
</section>
181+
182+
<!-- /.links -->
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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 bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pkg = require( './../package.json' ).name;
27+
var heavisidef = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var i;
36+
37+
x = uniform( 100, -50.0, 50.0 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
y = heavisidef( x[ i % x.length ] );
42+
if ( y > 1.0 ) {
43+
b.fail( 'should not return a value greater than 1' );
44+
}
45+
}
46+
b.toc();
47+
if ( y > 1.0 ) {
48+
b.fail( 'should not return a value greater than 1' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
53+
54+
bench( pkg+'::left-continuous', function benchmark( b ) {
55+
var x;
56+
var y;
57+
var i;
58+
59+
x = uniform( 100, -50.0, 50.0 );
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
y = heavisidef( x[ i % x.length ], 'left-continuous' );
64+
if ( isnanf( y ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
}
68+
b.toc();
69+
if ( isnanf( y ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
});
75+
76+
bench( pkg+'::right-continuous', function benchmark( b ) {
77+
var x;
78+
var y;
79+
var i;
80+
81+
x = uniform( 100, -50.0, 50.0 );
82+
83+
b.tic();
84+
for ( i = 0; i < b.iterations; i++ ) {
85+
y = heavisidef( x[ i % x.length ], 'right-continuous' );
86+
if ( isnanf( y ) ) {
87+
b.fail( 'should not return NaN' );
88+
}
89+
}
90+
b.toc();
91+
if ( isnanf( y ) ) {
92+
b.fail( 'should not return NaN' );
93+
}
94+
b.pass( 'benchmark finished' );
95+
b.end();
96+
});
97+
98+
bench( pkg+'::half-maximum', function benchmark( b ) {
99+
var x;
100+
var y;
101+
var i;
102+
103+
x = uniform( 100, -50.0, 50.0 );
104+
105+
b.tic();
106+
for ( i = 0; i < b.iterations; i++ ) {
107+
y = heavisidef( x[ i % x.length ], 'half-maximum' );
108+
if ( isnanf( y ) ) {
109+
b.fail( 'should not return NaN' );
110+
}
111+
}
112+
b.toc();
113+
if ( isnanf( y ) ) {
114+
b.fail( 'should not return NaN' );
115+
}
116+
b.pass( 'benchmark finished' );
117+
b.end();
118+
});
Lines changed: 50 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)