README.md (5458B)
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 > Evaluate a [rational function][rational-function]. 24 25 <section class="intro"> 26 27 A [rational function][rational-function] `f(x)` is defined as 28 29 <!-- <equation class="equation" label="eq:rational_function" align="center" raw="f(x) = \frac{P(x)}{Q(x)}" alt="Rational function definition."> --> 30 31 <div class="equation" align="center" data-raw-text="f(x) = \frac{P(x)}{Q(x)}" data-equation="eq:rational_function"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@7e0a95722efd9c771b129597380c63dc6715508b/lib/node_modules/@stdlib/math/base/tools/evalrational/docs/img/equation_rational_function.svg" alt="Rational function definition."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 where both `P(x)` and `Q(x)` are polynomials in `x`. A [polynomial][polynomial] in `x` can be expressed 39 40 <!-- <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."> --> 41 42 <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"> 43 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@7e0a95722efd9c771b129597380c63dc6715508b/lib/node_modules/@stdlib/math/base/tools/evalrational/docs/img/equation_polynomial.svg" alt="Polynomial expression."> 44 <br> 45 </div> 46 47 <!-- </equation> --> 48 49 where `c_n, c_{n-1}, ..., c_0` are constants. 50 51 </section> 52 53 <!-- /.intro --> 54 55 <section class="usage"> 56 57 ## Usage 58 59 ```javascript 60 var evalrational = require( '@stdlib/math/base/tools/evalrational' ); 61 ``` 62 63 #### evalrational( P, Q, x ) 64 65 Evaluates a [rational function][rational-function] at a value `x`. The coefficients `P` and `Q` are expected to be arrays of the **same** length. 66 67 ```javascript 68 var P = [ -6.0, -5.0 ]; 69 var Q = [ 3.0, 0.5 ]; 70 71 var v = evalrational( P, Q, 6.0 ); // => ( -6*6^0 - 5*6^1 ) / ( 3*6^0 + 0.5*6^1 ) = (-6-30)/(3+3) 72 // returns -6.0 73 ``` 74 75 For polynomials of different degree, the coefficient array for the lower degree [polynomial][polynomial] should be padded with zeros. 76 77 ```javascript 78 // 2x^3 + 4x^2 - 5x^1 - 6x^0 => degree 4 79 var P = [ -6.0, -5.0, 4.0, 2.0 ]; 80 81 // 0.5x^1 + 3x^0 => degree 2 82 var Q = [ 3.0, 0.5, 0.0, 0.0 ]; // zero-padded 83 84 var v = evalrational( P, Q, 6.0 ); // => ( -6*6^0 - 5*6^1 + 4*6^2 + 2*6^3 ) / ( 3*6^0 + 0.5*6^1 + 0*6^2 + 0*6^3 ) = (-6-30+144+432)/(3+3) 85 // returns 90.0 86 ``` 87 88 Coefficients should be ordered in **ascending** degree, thus matching summation notation. 89 90 #### evalrational.factory( P, Q ) 91 92 Uses code generation to in-line coefficients and return a `function` for evaluating a [rational function][rational-function]. 93 94 ```javascript 95 var P = [ 20.0, 8.0, 3.0 ]; 96 var Q = [ 10.0, 9.0, 1.0 ]; 97 98 var rational = evalrational.factory( P, Q ); 99 100 var v = rational( 10.0 ); // => (20*10^0 + 8*10^1 + 3*10^2) / (10*10^0 + 9*10^1 + 1*10^2) = (20+80+300)/(10+90+100) 101 // returns 2.0 102 103 v = rational( 2.0 ); // => (20*2^0 + 8*2^1 + 3*2^2) / (10*2^0 + 9*2^1 + 1*2^2) = (20+16+12)/(10+18+4) 104 // returns 1.5 105 ``` 106 107 </section> 108 109 <!-- /.usage --> 110 111 <section class="notes"> 112 113 ## Notes 114 115 - For hot code paths in which coefficients are invariant, a compiled function will be more performant than `evalrational()`. 116 - 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. 117 118 </section> 119 120 <!-- /.notes --> 121 122 <section class="examples"> 123 ## Examples 124 125 <!-- eslint no-undef: "error" --> 126 127 ```javascript 128 var randu = require( '@stdlib/random/base/randu' ); 129 var round = require( '@stdlib/math/base/special/round' ); 130 var Float64Array = require( '@stdlib/array/float64' ); 131 var evalrational = require( '@stdlib/math/base/tools/evalrational' ); 132 133 var rational; 134 var sign; 135 var len; 136 var P; 137 var Q; 138 var v; 139 var i; 140 141 // Create two arrays of random coefficients... 142 len = 10; 143 P = new Float64Array( len ); 144 Q = new Float64Array( len ); 145 for ( i = 0; i < len; i++ ) { 146 if ( randu() < 0.5 ) { 147 sign = -1.0; 148 } else { 149 sign = 1.0; 150 } 151 P[ i ] = sign * round( randu()*100 ); 152 Q[ i ] = sign * round( randu()*100 ); 153 } 154 155 // Evaluate the rational function at random values... 156 for ( i = 0; i < 100; i++ ) { 157 v = randu() * 100.0; 158 console.log( 'f(%d) = %d', v, evalrational( P, Q, v ) ); 159 } 160 161 // Generate an `evalrational` function... 162 rational = evalrational.factory( P, Q ); 163 for ( i = 0; i < 100; i++ ) { 164 v = (randu()*100.0) - 50.0; 165 console.log( 'f(%d) = %d', v, rational( v ) ); 166 } 167 ``` 168 169 </section> 170 171 <!-- /.examples --> 172 173 <section class="links"> 174 175 [polynomial]: https://en.wikipedia.org/wiki/Polynomial 176 177 [rational-function]: https://en.wikipedia.org/wiki/Rational_function 178 179 [mdn-csp]: https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP 180 181 </section> 182 183 <!-- /.links -->