time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (4426B)


      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 # Lucas Polynomial
     22 
     23 > Evaluate a [Lucas polynomial][fibonacci-polynomials].
     24 
     25 <section class="intro">
     26 
     27 A [Lucas polynomial][fibonacci-polynomials] is expressed according to the following recurrence relation
     28 
     29 <!-- <equation class="equation" label="eq:lucas_polynomial" align="center" raw="L_n(x) = \begin{cases}2 & \textrm{if}\ n = 0\\x & \textrm{if}\ n = 1\\x \cdot L_{n-1}(x) + L_{n-2}(x) & \textrm{if}\ n \geq 2\end{cases}" alt="Lucas polynomial."> -->
     30 
     31 <div class="equation" align="center" data-raw-text="L_n(x) = \begin{cases}2 &amp; \textrm{if}\ n = 0\\x &amp; \textrm{if}\ n = 1\\x \cdot L_{n-1}(x) + L_{n-2}(x) &amp; \textrm{if}\ n \geq 2\end{cases}" data-equation="eq:lucas_polynomial">
     32     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@7e0a95722efd9c771b129597380c63dc6715508b/lib/node_modules/@stdlib/math/base/tools/lucaspoly/docs/img/equation_lucas_polynomial.svg" alt="Lucas polynomial.">
     33     <br>
     34 </div>
     35 
     36 <!-- </equation> -->
     37 
     38 Alternatively, if `L(n,k)` is the coefficient of `x^k` in `L_n(x)`, then
     39 
     40 <!-- <equation class="equation" label="eq:lucas_polynomial_sum" align="center" raw="L_n(x) = \sum_{k = 0}^n L(n,k) x^k" alt="Lucas polynomial expressed as a sum."> -->
     41 
     42 <div class="equation" align="center" data-raw-text="L_n(x) = \sum_{k = 0}^n L(n,k) x^k" data-equation="eq:lucas_polynomial_sum">
     43     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@7e0a95722efd9c771b129597380c63dc6715508b/lib/node_modules/@stdlib/math/base/tools/lucaspoly/docs/img/equation_lucas_polynomial_sum.svg" alt="Lucas polynomial expressed as a sum.">
     44     <br>
     45 </div>
     46 
     47 <!-- </equation> -->
     48 
     49 We can extend [Lucas polynomials][fibonacci-polynomials] to negative `n` using the identity
     50 
     51 <!-- <equation class="equation" label="eq:negalucas_polynomial" align="center" raw="L_{-n}(x) = (-1)^{n} L_n(x)" alt="NegaLucas polynomial."> -->
     52 
     53 <div class="equation" align="center" data-raw-text="L_{-n}(x) = (-1)^{n} L_n(x)" data-equation="eq:negalucas_polynomial">
     54     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@7e0a95722efd9c771b129597380c63dc6715508b/lib/node_modules/@stdlib/math/base/tools/lucaspoly/docs/img/equation_negalucas_polynomial.svg" alt="NegaLucas polynomial.">
     55     <br>
     56 </div>
     57 
     58 <!-- </equation> -->
     59 
     60 </section>
     61 
     62 <!-- /.intro -->
     63 
     64 <section class="usage">
     65 
     66 ## Usage
     67 
     68 ```javascript
     69 var lucaspoly = require( '@stdlib/math/base/tools/lucaspoly' );
     70 ```
     71 
     72 #### lucaspoly( n, x )
     73 
     74 Evaluates a [Lucas polynomial][fibonacci-polynomials] at a value `x`.
     75 
     76 ```javascript
     77 var v = lucaspoly( 5, 2.0 ); // => 2^5 + 5*2^3 + 5*2
     78 // returns 82.0
     79 ```
     80 
     81 #### lucaspoly.factory( n )
     82 
     83 Uses code generation to generate a `function` for evaluating a [Lucas polynomial][fibonacci-polynomials].
     84 
     85 ```javascript
     86 var polyval = lucaspoly.factory( 5 );
     87 
     88 var v = polyval( 1.0 ); // => 1^5 + 5*1^3 + 5
     89 // returns 11.0
     90 
     91 v = polyval( 2.0 ); // => 2^5 + 5*2^3 + 5*2
     92 // returns 82.0
     93 ```
     94 
     95 </section>
     96 
     97 <!-- /.usage -->
     98 
     99 <section class="notes">
    100 
    101 ## Notes
    102 
    103 -   For hot code paths, a compiled function will be more performant than `lucaspoly()`.
    104 -   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.
    105 
    106 </section>
    107 
    108 <!-- /.notes -->
    109 
    110 <section class="examples">
    111 
    112 ## Examples
    113 
    114 <!-- eslint no-undef: "error" -->
    115 
    116 ```javascript
    117 var lucaspoly = require( '@stdlib/math/base/tools/lucaspoly' );
    118 
    119 var i;
    120 
    121 // Compute the negaLucas and Lucas numbers...
    122 for ( i = -76; i < 77; i++ ) {
    123     console.log( 'L_%d = %d', i, lucaspoly( i, 1.0 ) );
    124 }
    125 ```
    126 
    127 </section>
    128 
    129 <!-- /.examples -->
    130 
    131 <section class="links">
    132 
    133 [fibonacci-polynomials]: https://en.wikipedia.org/wiki/Fibonacci_polynomials
    134 
    135 [mdn-csp]: https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
    136 
    137 </section>
    138 
    139 <!-- /.links -->