Skip to content

Commit df509a1

Browse files
committed
feat: add math/base/tools/evalpolyf
1 parent c742a6f commit df509a1

File tree

17 files changed

+1487
-0
lines changed

17 files changed

+1487
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
# evalpolyf
22+
23+
> Evaluate a [polynomial][polynomial] using single-precision floating-point arithmetic.
24+
25+
<section class="intro">
26+
27+
A [polynomial][polynomial] in a variable `x` can be expressed as
28+
29+
<!-- <equation class="equation" label="eq:polynomial" align="center" raw="c_nx^n + c_{n-1}x^{n-1} + \ldots + c_1x^1 + c_0 = \sum_{i=0}^{n} c_ix^i" alt="Polynomial expression."> -->
30+
31+
```math
32+
c_nx^n + c_{n-1}x^{n-1} + \ldots + c_1x^1 + c_0 = \sum_{i=0}^{n} c_ix^i
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="c_nx^n + c_{n-1}x^{n-1} + \ldots + c_1x^1 + c_0 = \sum_{i=0}^{n} c_ix^i" data-equation="eq:polynomial">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@7e0a95722efd9c771b129597380c63dc6715508b/lib/node_modules/@stdlib/math/base/tools/evalpoly/docs/img/equation_polynomial.svg" alt="Polynomial expression.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
where `c_n, c_{n-1}, ..., c_0` are constants.
43+
44+
</section>
45+
46+
<!-- /.intro -->
47+
48+
<section class="usage">
49+
50+
## Usage
51+
52+
```javascript
53+
var evalpolyf = require( '@stdlib/math/base/tools/evalpolyf' );
54+
```
55+
56+
#### evalpolyf( c, x )
57+
58+
Evaluates a [polynomial][polynomial] having coefficients `c` and degree `n` at a value `x`, where `n = c.length-1`.
59+
60+
```javascript
61+
var Float32Array = require( '@stdlib/array/float32' );
62+
63+
var v = evalpolyf( new Float32Array( [ 3.0, 2.0, 1.0 ] ), 10 ); // => 3*10^0 + 2*10^1 + 1*10^2
64+
// returns 123.0
65+
```
66+
67+
The coefficients should be ordered in **ascending** degree, thus matching summation notation.
68+
69+
#### evalpolyf.factory( c )
70+
71+
Uses code generation to in-line coefficients and return a function for evaluating a [polynomial][polynomial] using single-precision floating-point arithmetic.
72+
73+
```javascript
74+
var Float32Array = require( '@stdlib/array/float32' );
75+
76+
var polyval = evalpolyf.factory( new Float32Array( [ 3.0, 2.0, 1.0 ] ) );
77+
78+
var v = polyval( 10.0 ); // => 3*10^0 + 2*10^1 + 1*10^2
79+
// returns 123.0
80+
81+
v = polyval( 5.0 ); // => 3*5^0 + 2*5^1 + 1*5^2
82+
// returns 38.0
83+
```
84+
85+
</section>
86+
87+
<!-- /.usage -->
88+
89+
<section class="notes">
90+
91+
## Notes
92+
93+
- For hot code paths in which coefficients are invariant, a compiled function will be more performant than `evalpolyf()`.
94+
- While code generation can boost performance, its use may be problematic in browser contexts enforcing a strict [content security policy][mdn-csp] (CSP). If running in or targeting an environment with a CSP, avoid using code generation.
95+
96+
</section>
97+
98+
<!-- /.notes -->
99+
100+
<section class="examples">
101+
102+
## Examples
103+
104+
<!-- eslint no-undef: "error" -->
105+
106+
```javascript
107+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
108+
var uniform = require( '@stdlib/random/base/uniform' );
109+
var evalpolyf = require( '@stdlib/math/base/tools/evalpolyf' );
110+
111+
// Create an array of random coefficients:
112+
var coef = discreteUniform( 10, -100, 100, {
113+
'dtype': 'float32'
114+
});
115+
116+
// Evaluate the polynomial at random values:
117+
var v;
118+
var i;
119+
for ( i = 0; i < 100; i++ ) {
120+
v = uniform( 0.0, 100.0 );
121+
console.log( 'f(%d) = %d', v, evalpolyf( coef, v ) );
122+
}
123+
124+
// Generate an `evalpolyf` function:
125+
var polyval = evalpolyf.factory( coef );
126+
for ( i = 0; i < 100; i++ ) {
127+
v = uniform( -50.0, 50.0 );
128+
console.log( 'f(%d) = %d', v, polyval( v ) );
129+
}
130+
```
131+
132+
</section>
133+
134+
<!-- /.examples -->
135+
136+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
137+
138+
<section class="related">
139+
140+
</section>
141+
142+
<!-- /.related -->
143+
144+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
145+
146+
<section class="links">
147+
148+
[polynomial]: https://en.wikipedia.org/wiki/Polynomial
149+
150+
[mdn-csp]: https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
151+
152+
</section>
153+
154+
<!-- /.links -->
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var factory = require( './../lib/factory.js' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::create:factory', function benchmark( b ) {
33+
var values;
34+
var opts;
35+
var f;
36+
var i;
37+
38+
opts = {
39+
'dtype': 'float32'
40+
};
41+
values = [
42+
uniform( 10, 0.0, 100.0, opts ),
43+
uniform( 10, 0.0, 100.0, opts ),
44+
uniform( 10, 0.0, 100.0, opts )
45+
];
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
f = factory( values[ i%values.length ] );
49+
if ( typeof f !== 'function' ) {
50+
b.fail( 'should return a function' );
51+
}
52+
}
53+
b.toc();
54+
if ( typeof f !== 'function' ) {
55+
b.fail( 'should return a function' );
56+
}
57+
b.pass( 'benchmark finished' );
58+
b.end();
59+
});
60+
61+
bench( pkg+'::evaluate:factory', function benchmark( b ) {
62+
var values;
63+
var opts;
64+
var v;
65+
var f;
66+
var i;
67+
68+
opts = {
69+
'dtype': 'float32'
70+
};
71+
f = factory( uniform( 10, 0.0, 100.0, opts ) );
72+
values = uniform( 10, 0.0, 100.0, opts );
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
v = f( values[ i%values.length ] );
77+
if ( isnan( v ) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
}
81+
b.toc();
82+
if ( isnan( v ) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
});
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 pow = require( '@stdlib/math/base/special/pow' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pkg = require( './../package.json' ).name;
28+
var factory = require( './../lib/factory.js' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var polyval;
42+
var opts;
43+
44+
opts = {
45+
'dtype': 'float32'
46+
};
47+
polyval = factory( uniform( len, 0.0, 100.0, opts ) );
48+
return benchmark;
49+
50+
/**
51+
* Benchmark function.
52+
*
53+
* @private
54+
* @param {Benchmark} b - benchmark instance
55+
*/
56+
function benchmark( b ) {
57+
var x;
58+
var v;
59+
var i;
60+
61+
x = uniform( 10, 0.0, 100.0, opts );
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
v = polyval( x[ i%x.length ] );
66+
if ( isnan( v ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
if ( isnan( v ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
/**
83+
* Main execution sequence.
84+
*
85+
* @private
86+
*/
87+
function main() {
88+
var len;
89+
var min;
90+
var max;
91+
var f;
92+
var i;
93+
94+
min = 1; // 10^min
95+
max = 6; // 10^max
96+
97+
for ( i = min; i <= max; i++ ) {
98+
len = pow( 10, i );
99+
f = createBenchmark( len );
100+
bench( pkg+':factory:len='+len, f );
101+
}
102+
}
103+
104+
main();

0 commit comments

Comments
 (0)