Skip to content

Commit 685cf22

Browse files
feat(math): add math/base/special/heavisidef
1 parent a0ba090 commit 685cf22

File tree

14 files changed

+1078
-0
lines changed

14 files changed

+1078
-0
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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 numeric value.
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 numeric value.
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+
* * *
171+
172+
## See Also
173+
174+
- <span class="package-name">[`@stdlib/math/base/special/ramp`][@stdlib/math/base/special/ramp]</span><span class="delimiter">: </span><span class="description">evaluate the ramp function.</span>
175+
176+
</section>
177+
178+
<!-- /.related -->
179+
180+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
181+
182+
<section class="links">
183+
184+
[heaviside-function]: https://en.wikipedia.org/wiki/Heaviside_step_function
185+
186+
<!-- <related-links> -->
187+
188+
[@stdlib/math/base/special/ramp]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/ramp
189+
190+
<!-- </related-links> -->
191+
192+
</section>
193+
194+
<!-- /.links -->
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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 randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
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 = randu( 100, -50.0, 50.0 );
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
y = heavisidef( x[ i % x.length ] );
41+
if ( y > 1.0 ) {
42+
b.fail( 'should not return a value greater than 1' );
43+
}
44+
}
45+
b.toc();
46+
if ( y > 1.0 ) {
47+
b.fail( 'should not return a value greater than 1' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});
52+
53+
bench( pkg+'::left-continuous', function benchmark( b ) {
54+
var x;
55+
var y;
56+
var i;
57+
58+
x = randu( 100, -50.0, 50.0 );
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
y = heavisidef( x[ i % x.length ], 'left-continuous' );
62+
if ( isnan( y ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
}
66+
b.toc();
67+
if ( isnan( y ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
});
73+
74+
bench( pkg+'::right-continuous', function benchmark( b ) {
75+
var x;
76+
var y;
77+
var i;
78+
79+
x = randu( 100, -50.0, 50.0 );
80+
b.tic();
81+
for ( i = 0; i < b.iterations; i++ ) {
82+
y = heavisidef( x[ i % x.length ], 'right-continuous' );
83+
if ( isnan( y ) ) {
84+
b.fail( 'should not return NaN' );
85+
}
86+
}
87+
b.toc();
88+
if ( isnan( y ) ) {
89+
b.fail( 'should not return NaN' );
90+
}
91+
b.pass( 'benchmark finished' );
92+
b.end();
93+
});
94+
95+
bench( pkg+'::half-maximum', function benchmark( b ) {
96+
var x;
97+
var y;
98+
var i;
99+
100+
x = randu( 100, -50.0, 50.0 );
101+
b.tic();
102+
for ( i = 0; i < b.iterations; i++ ) {
103+
y = heavisidef( x[ i % x.length ], 'half-maximum' );
104+
if ( isnan( y ) ) {
105+
b.fail( 'should not return NaN' );
106+
}
107+
}
108+
b.toc();
109+
if ( isnan( y ) ) {
110+
b.fail( 'should not return NaN' );
111+
}
112+
b.pass( 'benchmark finished' );
113+
b.end();
114+
});
Lines changed: 50 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)