|
| 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 --> |
0 commit comments