README.md (2974B)
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 # evalpoly 22 23 > Compile a module for evaluating a [polynomial][@stdlib/math/base/tools/evalpoly]. 24 25 <section class="intro"> 26 27 </section> 28 29 <!-- /.intro --> 30 31 <section class="usage"> 32 33 ## Usage 34 35 ```javascript 36 var compile = require( '@stdlib/math/base/tools/evalpoly-compile' ); 37 ``` 38 39 #### compile( c ) 40 41 Compiles a module `string` containing an exported function which evaluates a [polynomial][@stdlib/math/base/tools/evalpoly] having coefficients `c`. 42 43 ```javascript 44 var str = compile( [ 3.0, 2.0, 1.0 ] ); 45 // returns <string> 46 ``` 47 48 In the example above, the output `string` would correspond to the following module: 49 50 ```javascript 51 'use strict'; 52 53 // MAIN // 54 55 /** 56 * Evaluates a polynomial. 57 * 58 * ## Notes 59 * 60 * - The implementation uses [Horner's rule][horners-method] for efficient computation. 61 * 62 * [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method 63 * 64 * 65 * @private 66 * @param {number} x - value at which to evaluate the polynomial 67 * @returns {number} evaluated polynomial 68 */ 69 function evalpoly( x ) { 70 if ( x === 0.0 ) { 71 return 3.0; 72 } 73 return 3.0 + (x * (2.0 + (x * 1.0))); // eslint-disable-line max-len 74 } 75 76 77 // EXPORTS // 78 79 module.exports = evalpoly; 80 ``` 81 82 The coefficients should be ordered in **ascending** degree, thus matching summation notation. 83 84 </section> 85 86 <!-- /.usage --> 87 88 <section class="notes"> 89 90 ## Notes 91 92 - The function is intended for **non-browser** environments for the purpose of generating module files. 93 94 </section> 95 96 <!-- /.notes --> 97 98 <section class="examples"> 99 100 ## Examples 101 102 <!-- eslint no-undef: "error" --> 103 104 ```javascript 105 var randu = require( '@stdlib/random/base/randu' ); 106 var round = require( '@stdlib/math/base/special/round' ); 107 var Float64Array = require( '@stdlib/array/float64' ); 108 var compile = require( '@stdlib/math/base/tools/evalpoly-compile' ); 109 110 var coef; 111 var sign; 112 var str; 113 var i; 114 115 // Create an array of random coefficients... 116 coef = new Float64Array( 10 ); 117 for ( i = 0; i < coef.length; i++ ) { 118 if ( randu() < 0.5 ) { 119 sign = -1.0; 120 } else { 121 sign = 1.0; 122 } 123 coef[ i ] = sign * round( randu()*100.0 ); 124 } 125 126 // Compile a module for evaluating a polynomial: 127 str = compile( coef ); 128 console.log( str ); 129 ``` 130 131 </section> 132 133 <!-- /.examples --> 134 135 <section class="links"> 136 137 [@stdlib/math/base/tools/evalpoly]: https://www.npmjs.com/package/@stdlib/math/tree/main/base/tools/evalpoly 138 139 </section> 140 141 <!-- /.links -->