Skip to content

Commit 3c7bd5e

Browse files
C0ldSmi1ekgryte
andauthored
feat: add math/base/tools/normhermitepolyf
PR-URL: #2151 Closes: #2029 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 91f84da commit 3c7bd5e

32 files changed

+2304
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# Normalized Hermite Polynomial
22+
23+
> Evaluate a normalized [Hermite polynomial][hermite-polynomial] using single-precision floating-point arithmetic.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
The normalized (aka "probabilist") [Hermite polynomials][hermite-polynomial] are given by
30+
31+
<!-- <equation class="equation" label="eq:normalized_hermite_polynomials" align="center" raw="He_{n}(x)=(-1)^{n} e^{\frac{x^2}{2}} \frac{\mathrm d^{n}}{\mathrm d x^{n}} e^{-\frac{x^2}{2}}" alt="Equation for normalized Hermite polynomials."> -->
32+
33+
```math
34+
He_{n}(x)=(-1)^{n} e^{\frac{x^2}{2}} \frac{\mathrm d^{n}}{\mathrm d x^{n}} e^{-\frac{x^2}{2}}
35+
```
36+
37+
<!-- <div class="equation" align="center" data-raw-text="He_{n}(x)=(-1)^{n} e^{\frac{x^2}{2}} \frac{\mathrm{d}^{n}}{\mathrm{d}x^n} e^{-\frac{x^2}{2}}" data-equation="eq:normalized_hermite_polynomials">
38+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bea0101eb61892f160eec8d97dc79188fd937523/lib/node_modules/@stdlib/math/base/tools/normhermitepoly/docs/img/equation_normalized_hermite_polynomials.svg" alt="Equation for normalized Hermite polynomials.">
39+
<br>
40+
</div> -->
41+
42+
<!-- </equation> -->
43+
44+
</section>
45+
46+
<!-- /.intro -->
47+
48+
<!-- Package usage documentation. -->
49+
50+
<section class="usage">
51+
52+
## Usage
53+
54+
```javascript
55+
var normhermitepolyf = require( '@stdlib/math/base/tools/normhermitepolyf' );
56+
```
57+
58+
#### normhermitepolyf( n, x )
59+
60+
Evaluates a normalized [Hermite polynomial][hermite-polynomial] of degree `n` using single-precision floating-point arithmetic.
61+
62+
```javascript
63+
var v = normhermitepolyf( 1, 1.0 );
64+
// returns 1.0
65+
66+
v = normhermitepolyf( 1, 0.5 );
67+
// returns 0.5
68+
69+
v = normhermitepolyf( 0, 0.5 );
70+
// returns 1.0
71+
72+
v = normhermitepolyf( 2, 0.5 );
73+
// returns -0.75
74+
75+
v = normhermitepolyf( -1, 0.5 );
76+
// returns NaN
77+
```
78+
79+
#### normhermitepolyf.factory( n )
80+
81+
Returns a function for evaluating a normalized [Hermite polynomial][hermite-polynomial] of degree `n` using single-precision floating-point arithmetic.
82+
83+
```javascript
84+
var polyval = normhermitepolyf.factory( 2 );
85+
86+
var v = polyval( 0.5 );
87+
// returns -0.75
88+
```
89+
90+
</section>
91+
92+
<!-- /.usage -->
93+
94+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
95+
96+
<section class="notes">
97+
98+
</section>
99+
100+
<!-- /.notes -->
101+
102+
<!-- Package usage examples. -->
103+
104+
<section class="examples">
105+
106+
## Examples
107+
108+
<!-- eslint no-undef: "error" -->
109+
110+
```javascript
111+
var uniform = require( '@stdlib/random/array/uniform' );
112+
var zeros = require( '@stdlib/array/zeros' );
113+
var smap = require( '@stdlib/strided/base/smap' );
114+
var logEach = require( '@stdlib/console/log-each' );
115+
var normhermitepolyf = require( '@stdlib/math/base/tools/normhermitepolyf' );
116+
117+
// Generate random values at which to evaluate a polynomial:
118+
var x = uniform( 10, -50.0, 50.0, {
119+
'dtype': 'float32'
120+
});
121+
122+
// Create a polynomial function of degree 1:
123+
var f = normhermitepolyf.factory( 1 );
124+
125+
// Allocate an output array:
126+
var y = zeros( x.length, 'float32' );
127+
128+
// Evaluate the polynomial:
129+
smap( x.length, x, 1, y, 1, f );
130+
logEach( 'He_%d(%.3f) = %.3f', 1, x, y );
131+
132+
// Create a polynomial function of degree 2:
133+
f = normhermitepolyf.factory( 2 );
134+
135+
// Allocate an output array:
136+
y = zeros( x.length, 'float32' );
137+
138+
// Evaluate the polynomial:
139+
smap( x.length, x, 1, y, 1, f );
140+
logEach( 'He_%d(%.3f) = %.3f', 2, x, y );
141+
142+
// Create a polynomial function of degree 3:
143+
f = normhermitepolyf.factory( 3 );
144+
145+
// Allocate an output array:
146+
y = zeros( x.length, 'float32' );
147+
148+
// Evaluate the polynomial:
149+
smap( x.length, x, 1, y, 1, f );
150+
logEach( 'He_%d(%.3f) = %.3f', 3, x, y );
151+
```
152+
153+
</section>
154+
155+
<!-- /.examples -->
156+
157+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
158+
159+
<section class="references">
160+
161+
</section>
162+
163+
<!-- /.references -->
164+
165+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
166+
167+
<section class="related">
168+
169+
</section>
170+
171+
<!-- /.related -->
172+
173+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
174+
175+
<section class="links">
176+
177+
[hermite-polynomial]: https://en.wikipedia.org/wiki/Hermite_polynomials
178+
179+
</section>
180+
181+
<!-- /.links -->
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var pkg = require( './../package.json' ).name;
28+
var factory = require( './../lib/factory.js' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg+'::create:factory', function benchmark( b ) {
34+
var n;
35+
var f;
36+
var i;
37+
38+
n = discreteUniform( 10, 1, 10, {
39+
'dtype': 'float32'
40+
});
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
f = factory( n[ i%n.length ] );
45+
if ( typeof f !== 'function' ) {
46+
b.fail( 'should return a function' );
47+
}
48+
}
49+
b.toc();
50+
if ( typeof f !== 'function' ) {
51+
b.fail( 'should return a function' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
56+
57+
bench( pkg+'::evaluate:factory', function benchmark( b ) {
58+
var x;
59+
var v;
60+
var f;
61+
var i;
62+
63+
f = factory( 2 );
64+
x = uniform( 10, -5.0, 5.0, {
65+
'dtype': 'float32'
66+
});
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
v = f( x[ i%x.length ] );
71+
if ( isnanf( v ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
}
75+
b.toc();
76+
if ( isnanf( v ) ) {
77+
b.fail( 'should not return NaN' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 normhermitepolyf = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var opts;
34+
var x;
35+
var y;
36+
var i;
37+
38+
opts = {
39+
'dtype': 'float32'
40+
};
41+
x = uniform( 10, -5.0, 5.0, opts );
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
y = normhermitepolyf( 2, x[ i%x.length ] );
46+
if ( isnanf( y ) ) {
47+
b.fail( 'should not return NaN' );
48+
}
49+
}
50+
b.toc();
51+
if ( isnanf( y ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});

0 commit comments

Comments
 (0)