evalrational.js (3029B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 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 * ## Notice 20 * 21 * The original C++ code and copyright notice are from the [Boost library]{@link http://www.boost.org/doc/libs/1_60_0/boost/math/tools/rational.hpp}. The implementation has been modified for JavaScript. 22 * 23 * ```text 24 * (C) Copyright John Maddock 2006. 25 * 26 * Use, modification and distribution are subject to the 27 * Boost Software License, Version 1.0. (See accompanying file 28 * LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 29 * ``` 30 */ 31 32 'use strict'; 33 34 // MODULES // 35 36 var abs = require( './../../../../base/special/abs' ); 37 38 39 // MAIN // 40 41 /** 42 * Evaluates a rational function, i.e., the ratio of two polynomials described by the coefficients stored in \\(P\\) and \\(Q\\). 43 * 44 * ## Notes 45 * 46 * - Coefficients should be sorted in ascending degree. 47 * - The implementation uses [Horner's rule][horners-method] for efficient computation. 48 * 49 * [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method 50 * 51 * 52 * @param {NumericArray} P - numerator polynomial coefficients sorted in ascending degree 53 * @param {NumericArray} Q - denominator polynomial coefficients sorted in ascending degree 54 * @param {number} x - value at which to evaluate the rational function 55 * @returns {number} evaluated rational function 56 * 57 * @example 58 * var P = [ -6.0, -5.0 ]; 59 * var Q = [ 3.0, 0.5 ]; 60 * 61 * var v = evalrational( P, Q, 6.0 ); // => ( -6*6^0 - 5*6^1 ) / ( 3*6^0 + 0.5*6^1 ) = (-6-30)/(3+3) 62 * // returns -6.0 63 * 64 * @example 65 * // 2x^3 + 4x^2 - 5x^1 - 6x^0 => degree 4 66 * var P = [ -6.0, -5.0, 4.0, 2.0 ]; 67 * 68 * // 0.5x^1 + 3x^0 => degree 2 69 * var Q = [ 3.0, 0.5, 0.0, 0.0 ]; // zero-padded 70 * 71 * 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) 72 * // returns 90.0 73 */ 74 function evalrational( P, Q, x ) { 75 var len; 76 var s1; 77 var s2; 78 var i; 79 80 len = P.length; 81 if ( len === 0 ) { 82 return NaN; 83 } 84 if ( len !== Q.length ) { 85 return NaN; 86 } 87 if ( x === 0.0 || len === 1 ) { 88 return P[ 0 ] / Q[ 0 ]; 89 } 90 // Use Horner's method... 91 if ( abs( x ) <= 1.0 ) { 92 s1 = P[ len-1 ]; 93 s2 = Q[ len-1 ]; 94 for ( i = len-2; i >= 0; --i ) { 95 s1 *= x; 96 s2 *= x; 97 s1 += P[ i ]; 98 s2 += Q[ i ]; 99 } 100 } else { 101 x = 1.0 / x; // use inverse to avoid overflow 102 s1 = P[ 0 ]; 103 s2 = Q[ 0 ]; 104 for ( i = 1; i < len; ++i ) { 105 s1 *= x; 106 s2 *= x; 107 s1 += P[ i ]; 108 s2 += Q[ i ]; 109 } 110 } 111 return s1 / s2; 112 } 113 114 115 // EXPORTS // 116 117 module.exports = evalrational;