coefficients.js (2095B)
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 'use strict'; 20 21 // MODULES // 22 23 var binomcoef = require( './../../../../base/special/binomcoef' ); 24 var floor = require( './../../../../base/special/floor' ); 25 var ceil = require( './../../../../base/special/ceil' ); 26 var cache = require( './cache.js' ); 27 28 29 // MAIN // 30 31 /** 32 * Computes polynomial coefficients. 33 * 34 * ## Notes 35 * 36 * - Coefficients are computed via a (1,2)-Pascal triangle (i.e., Lucas triangle). For more details, see [Lucas polynomials][oeis-lucas-polynomials] and [Lucas triangle][oeis-lucas-triangle]. 37 * 38 * [oeis-lucas-polynomials]: https://oeis.org/wiki/Lucas_polynomials 39 * [oeis-lucas-triangle]: https://oeis.org/wiki/Lucas_triangle 40 * 41 * @private 42 * @param {NonNegativeInteger} n - Lucas polynomial for which to compute coefficients 43 * @returns {NonNegativeIntegerArray} polynomial coefficients 44 */ 45 function coefficients( n ) { 46 var coefs; 47 var half; 48 var high; 49 var low; 50 var p; 51 var a; 52 var b; 53 var m; 54 var i; 55 56 coefs = cache[ n ]; 57 if ( coefs === void 0 ) { 58 m = n + 1; 59 coefs = new Array( m ); 60 if ( n === 0 ) { 61 coefs[ 0 ] = 2.0; 62 } else { 63 for ( i = 0; i < m; i++ ) { 64 coefs[ i ] = 0.0; 65 } 66 half = n / 2; 67 high = ceil( half ); 68 low = floor( half ); 69 for ( i = 0; i <= low; i++ ) { 70 p = (2*i) + (n%2); 71 a = 2.0 * binomcoef( high+i-1, low-i-1 ); 72 b = binomcoef( high+i-1, low-i ); 73 coefs[ p ] += a + b; 74 } 75 } 76 // Memoize the coefficients: 77 cache[ n ] = coefs; 78 } 79 return coefs; 80 } 81 82 83 // EXPORTS // 84 85 module.exports = coefficients;