time-to-botec

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

README.md (5413B)


      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 # continued-fraction
     22 
     23 > [Continued fraction][continued-fraction] approximation.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var continuedFraction = require( '@stdlib/math/base/tools/continued-fraction' );
     31 ```
     32 
     33 #### continuedFraction( generator\[, options ] )
     34 
     35 Evaluates the continued fraction described by the supplied `generator` argument. `generator` can be either a function which returns an array with two elements, the `a` and `b` terms of the fraction, or an ES6 [Generator object][es6-generator]. By default, the function computes
     36 
     37 <!-- <equation class="equation" label="eq:continued_fraction_a" align="center" raw="\frac{a_1}{b_1+\frac{a_2}{b_2+\frac{a_3}{b_3+\frac{a_4}{b_4}+\ldots}}}" alt="Continued fraction without leading b_0 term"> -->
     38 
     39 <div class="equation" align="center" data-raw-text="\frac{a_1}{b_1+\frac{a_2}{b_2+\frac{a_3}{b_3+\frac{a_4}{b_4}+\ldots}}}" data-equation="eq:continued_fraction_a">
     40     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/tools/continued-fraction/docs/img/equation_continued_fraction_a.svg" alt="Continued fraction without leading b_0 term">
     41     <br>
     42 </div>
     43 
     44 <!-- </equation> -->
     45 
     46 Using an ES6 [Generator object][es6-generator]:
     47 
     48 <!-- eslint-disable no-restricted-syntax -->
     49 
     50 ```javascript
     51 // Continued fraction for (e-1)^(-1):
     52 var gen = generator();
     53 var out = continuedFraction( gen );
     54 // returns ~0.582
     55 
     56 function* generator() {
     57     var i = 0;
     58     while ( true ) {
     59         i += 1;
     60         yield [ i, i ];
     61     }
     62 }
     63 ```
     64 
     65 Alternatively, one can use a closure to achieve the same goal:
     66 
     67 ```javascript
     68 // Continued fraction for (e-1)^(-1):
     69 var gen = generator();
     70 var out = continuedFraction( gen );
     71 // returns ~0.582
     72 
     73 function generator() {
     74     var i = 0;
     75     return gen;
     76 
     77     function gen() {
     78         i += 1;
     79         return [ i, i ];
     80     }
     81 }
     82 ```
     83 
     84 The function accepts the following `options`:
     85 
     86 -   **maxIter**: `integer` denoting the maximum number of times the supplied generator object will be called. Default: `1000000`.
     87 -   **tolerance**: `number` primitive specifying the used tolerance to assess convergence. Default: `2.22e-16`.
     88 -   **keep**: `boolean` primitive indicating whether to keep the `b0` term in the continued fraction. Default: `false`.
     89 
     90 To evaluate
     91 
     92 <!-- <equation class="equation" label="eq:continued_fraction_b" align="center" raw="b_0 + \frac{a_1}{b_1+\frac{a_2}{b_2+\frac{a_3}{b_3+\frac{a_4}{b_4}+\ldots}}}" alt="Continued fraction with leading b_0 term"> -->
     93 
     94 <div class="equation" align="center" data-raw-text="b_0 + \frac{a_1}{b_1+\frac{a_2}{b_2+\frac{a_3}{b_3+\frac{a_4}{b_4}+\ldots}}}" data-equation="eq:continued_fraction_b">
     95     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/tools/continued-fraction/docs/img/equation_continued_fraction_b.svg" alt="Continued fraction with leading b_0 term">
     96     <br>
     97 </div>
     98 
     99 <!-- </equation> -->
    100 
    101 set the `keep` option to `true`.
    102 
    103 ```javascript
    104 var out = continuedFraction( generator(), {
    105     'keep': true
    106 });
    107 // returns ~1.718
    108 
    109 function generator() {
    110     var i = 0;
    111     return gen;
    112 
    113     function gen() {
    114         i += 1;
    115         return [ i, i ];
    116     }
    117 }
    118 ```
    119 
    120 To change the maximum number of iterations, set the `maxIter` option.
    121 
    122 ```javascript
    123 var out = continuedFraction( generator(), {
    124     'maxIter': 10
    125 });
    126 // returns ~0.582
    127 
    128 function generator() {
    129     var i = 0;
    130     return gen;
    131 
    132     function gen() {
    133         i += 1;
    134         return [ i, i ];
    135     }
    136 }
    137 ```
    138 
    139 The default tolerance of `2.22e-16` to assess convergence can be changed via the `tolerance` option.
    140 
    141 ```javascript
    142 var out = continuedFraction( generator(), {
    143     'tolerance': 1e-1
    144 });
    145 // returns ~0.579
    146 
    147 function generator() {
    148     var i = 0;
    149     return gen;
    150 
    151     function gen() {
    152         i += 1;
    153         return [ i, i ];
    154     }
    155 }
    156 ```
    157 
    158 </section>
    159 
    160 <!-- /.usage -->
    161 
    162 <section class="examples">
    163 
    164 ## Examples
    165 
    166 <!-- eslint-disable no-restricted-syntax -->
    167 
    168 <!-- eslint no-undef: "error" -->
    169 
    170 ```javascript
    171 var continuedFraction = require( '@stdlib/math/base/tools/continued-fraction' );
    172 var out;
    173 
    174 function* generator() {
    175     while ( true ) {
    176         yield [ 1, 1 ];
    177     }
    178 }
    179 
    180 function closure() {
    181     var ones = [ 1, 1 ];
    182     return gen;
    183 
    184     function gen() {
    185         return ones;
    186     }
    187 }
    188 
    189 out = continuedFraction( generator(), {
    190     'keep': true
    191 });
    192 console.log( 'Golden ratio (generator): %d,', out );
    193 
    194 out = continuedFraction( closure(), {
    195     'keep': true
    196 });
    197 console.log( 'Golden ratio (closure): %d', out );
    198 ```
    199 
    200 </section>
    201 
    202 <!-- /.examples -->
    203 
    204 <section class="links">
    205 
    206 [continued-fraction]: https://en.wikipedia.org/wiki/Continued_fraction
    207 
    208 [es6-generator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
    209 
    210 </section>
    211 
    212 <!-- /.links -->