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