Skip to content

Commit ff48331

Browse files
feat: add math/base/special/dirac-deltaf
PR-URL: #3363 Ref: #649 Co-authored-by: stdlib-bot <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 76642bf commit ff48331

File tree

28 files changed

+2153
-0
lines changed

28 files changed

+2153
-0
lines changed
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
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+
# Dirac Delta
22+
23+
> Evaluate the [Dirac delta function][dirac-delta-function] in single-precision floating-point format.
24+
25+
<section class="intro">
26+
27+
The [Dirac delta function][dirac-delta-function] may be loosely defined as
28+
29+
<!-- <equation class="equation" label="eq:dirac_delta" align="center" raw="\delta = \begin{cases} \infty & \textrm{if}\ x = 0 \\ 0 & \textrm{if}\ x \neq 0\end{cases}" alt="Dirac delta function."> -->
30+
31+
```math
32+
\delta = \begin{cases} \infty & \textrm{if}\ x = 0 \\ 0 & \textrm{if}\ x \neq 0\end{cases}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\delta = \begin{cases} \infty &amp; \textrm{if}\ x = 0 \\ 0 &amp; \textrm{if}\ x \neq 0\end{cases}" data-equation="eq:dirac_delta">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/dirac-delta/docs/img/equation_dirac_delta.svg" alt="Dirac delta function.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
and is constrained to satisfy the identity
43+
44+
<!-- <equation class="equation" label="eq:dirac_delta_integral" align="center" raw="\int^{+\infty}_{-\infty} \delta(x)\ dx = 1" alt="Dirac delta function integral."> -->
45+
46+
```math
47+
\int^{+\infty}_{-\infty} \delta(x)\ dx = 1
48+
```
49+
50+
<!-- <div class="equation" align="center" data-raw-text="\int^{+\infty}_{-\infty} \delta(x)\ dx = 1" data-equation="eq:dirac_delta_integral">
51+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/dirac-delta/docs/img/equation_dirac_delta_integral.svg" alt="Dirac delta function integral.">
52+
<br>
53+
</div> -->
54+
55+
<!-- </equation> -->
56+
57+
Note that the [Dirac delta function][dirac-delta-function] is **not** a function in the traditional sense, as any real-valued function which is zero everywhere except at a single point, must have an integral equal to `0`.
58+
59+
</section>
60+
61+
<!-- /.intro -->
62+
63+
<section class="usage">
64+
65+
## Usage
66+
67+
```javascript
68+
var diracDeltaf = require( '@stdlib/math/base/special/dirac-deltaf' );
69+
```
70+
71+
#### diracDeltaf( x )
72+
73+
Evaluates the [Dirac delta function][dirac-delta-function] in single-precision floating-point format.
74+
75+
```javascript
76+
var v = diracDeltaf( 0.0 );
77+
// returns Infinity
78+
79+
v = diracDeltaf( 3.14 );
80+
// returns 0.0
81+
82+
v = diracDeltaf( NaN );
83+
// returns NaN
84+
```
85+
86+
</section>
87+
88+
<!-- /.usage -->
89+
90+
<section class="examples">
91+
92+
## Examples
93+
94+
<!-- eslint no-undef: "error" -->
95+
96+
```javascript
97+
var linspace = require( '@stdlib/array/base/linspace' );
98+
var diracDeltaf = require( '@stdlib/math/base/special/dirac-deltaf' );
99+
100+
var x = linspace( -1.0, 1.0, 101 );
101+
102+
var i;
103+
for ( i = 0; i < x.length; i++ ) {
104+
console.log( 'dirac(%d) = %d', x[ i ], diracDeltaf( x[ i ] ) );
105+
}
106+
```
107+
108+
</section>
109+
110+
<!-- /.examples -->
111+
112+
<!-- C interface documentation. -->
113+
114+
* * *
115+
116+
<section class="c">
117+
118+
## C APIs
119+
120+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
121+
122+
<section class="intro">
123+
124+
</section>
125+
126+
<!-- /.intro -->
127+
128+
<!-- C usage documentation. -->
129+
130+
<section class="usage">
131+
132+
### Usage
133+
134+
```c
135+
#include "stdlib/math/base/special/dirac_deltaf.h"
136+
```
137+
138+
#### stdlib_base_dirac_deltaf( x )
139+
140+
Evaluates the [Dirac delta function][dirac-delta-function] in single-precision floating-point format.
141+
142+
```c
143+
float x = stdlib_base_dirac_deltaf( 0.0f );
144+
// returns Infinity
145+
146+
x = stdlib_base_dirac_deltaf( 3.14f );
147+
// returns 0.0f
148+
```
149+
150+
The function accepts the following arguments:
151+
152+
- **x**: `[in] float` input value.
153+
154+
```c
155+
float stdlib_base_dirac_delta( const float x );
156+
```
157+
158+
</section>
159+
160+
<!-- /.usage -->
161+
162+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
163+
164+
<section class="notes">
165+
166+
</section>
167+
168+
<!-- /.notes -->
169+
170+
<!-- C API usage examples. -->
171+
172+
<section class="examples">
173+
174+
### Examples
175+
176+
```c
177+
#include "stdlib/math/base/special/dirac_deltaf.h"
178+
#include <stdlib.h>
179+
#include <stdio.h>
180+
181+
int main( void ) {
182+
const float x[] = { -1.0f, -0.5f, 0.0f, 0.5f, 1.0f, 3.14f, 2.0f };
183+
184+
float v;
185+
int i;
186+
for ( i = 0; i < 7; i++ ) {
187+
v = stdlib_base_dirac_deltaf( x[ i ] );
188+
printf( "dirac(%f) = %f\n", x[ i ], v );
189+
}
190+
}
191+
```
192+
193+
</section>
194+
195+
<!-- /.examples -->
196+
197+
</section>
198+
199+
<!-- /.c -->
200+
201+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
202+
203+
<section class="related">
204+
205+
</section>
206+
207+
<!-- /.related -->
208+
209+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
210+
211+
<section class="links">
212+
213+
[dirac-delta-function]: https://en.wikipedia.org/wiki/Dirac_delta_function
214+
215+
<!-- <related-links> -->
216+
217+
<!-- </related-links> -->
218+
219+
</section>
220+
221+
<!-- /.links -->
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 randu = require( '@stdlib/random/array/discrete-uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pkg = require( './../package.json' ).name;
27+
var diracDeltaf = 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, 50 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
y = diracDeltaf( x[ i % x.length ] );
42+
if ( isnanf( y ) ) {
43+
b.fail( 'should not return NaN' );
44+
}
45+
}
46+
b.toc();
47+
if ( isnanf( y ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 randu = require( '@stdlib/random/array/discrete-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 diracDeltaf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( diracDeltaf 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 = randu( 100, -50, 50 );
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
y = diracDeltaf( x[ i % x.length ] );
51+
if ( isnanf( y ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
}
55+
b.toc();
56+
if ( isnanf( y ) ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});

0 commit comments

Comments
 (0)