time-to-botec

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

README.md (4683B)


      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 # Factorial
     22 
     23 > [Factorial][factorial-function] function.
     24 
     25 <section class="intro">
     26 
     27 The [factorial][factorial-function] function may be defined as the product
     28 
     29 <!-- <equation class="equation" label="eq:factorial_function" align="center" raw="n! = \prod_{k=1}^n k" alt="Factorial function definition"> -->
     30 
     31 <div class="equation" align="center" data-raw-text="n! = \prod_{k=1}^n k" data-equation="eq:factorial_function">
     32     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/factorial/docs/img/equation_factorial_function.svg" alt="Factorial function definition">
     33     <br>
     34 </div>
     35 
     36 <!-- </equation> -->
     37 
     38 or according to the recurrence relation
     39 
     40 <!-- <equation class="equation" label="eq:factorial_recurrence_relation" align="center" raw="n! = \begin{cases}1 & \textrm{if } n = 0,\\(n-1)! \times n & \textrm{if } n > 1\end{cases}" alt="Factorial function recurrence relation"> -->
     41 
     42 <div class="equation" align="center" data-raw-text="n! = \begin{cases}1 &amp; \textrm{if } n = 0,\\(n-1)! \times n &amp; \textrm{if } n &gt; 1\end{cases}" data-equation="eq:factorial_recurrence_relation">
     43     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/factorial/docs/img/equation_factorial_recurrence_relation.svg" alt="Factorial function recurrence relation">
     44     <br>
     45 </div>
     46 
     47 <!-- </equation> -->
     48 
     49 Following the convention for an [empty product][empty-product], in all definitions,
     50 
     51 <!-- <equation class="equation" label="eq:zero_factorial" align="center" raw="0! = 1" alt="Zero factorial"> -->
     52 
     53 <div class="equation" align="center" data-raw-text="0! = 1" data-equation="eq:zero_factorial">
     54     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/factorial/docs/img/equation_zero_factorial.svg" alt="Zero factorial">
     55     <br>
     56 </div>
     57 
     58 <!-- </equation> -->
     59 
     60 The [Gamma][@stdlib/math/base/special/gamma] function extends the [factorial][factorial-function] function for non-integer values.
     61 
     62 <!-- <equation class="equation" label="eq:factorial_function_and_gamma" align="center" raw="n! = \Gamma(n+1)" alt="Factorial function extension via the Gamma function"> -->
     63 
     64 <div class="equation" align="center" data-raw-text="n! = \Gamma(n+1)" data-equation="eq:factorial_function_and_gamma">
     65     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/factorial/docs/img/equation_factorial_function_and_gamma.svg" alt="Factorial function extension via the Gamma function">
     66     <br>
     67 </div>
     68 
     69 <!-- </equation> -->
     70 
     71 The [factorial][factorial-function] of a **negative** integer is not defined.
     72 
     73 </section>
     74 
     75 <!-- /.intro -->
     76 
     77 <section class="usage">
     78 
     79 ## Usage
     80 
     81 ```javascript
     82 var factorial = require( '@stdlib/math/base/special/factorial' );
     83 ```
     84 
     85 #### factorial( x )
     86 
     87 Evaluates the [factorial][factorial-function] function.
     88 
     89 ```javascript
     90 var v = factorial( 3.0 );
     91 // returns 6.0
     92 
     93 v = factorial( -1.5 );
     94 // returns ~-3.545
     95 
     96 v = factorial( -0.5 );
     97 // returns ~1.772
     98 
     99 v = factorial( 0.5 );
    100 // returns ~0.886
    101 
    102 v = factorial( -10.0 );
    103 // returns NaN
    104 
    105 v = factorial( 171.0 );
    106 // returns Infinity
    107 
    108 v = factorial( NaN );
    109 // returns NaN
    110 ```
    111 
    112 </section>
    113 
    114 <!-- /.usage -->
    115 
    116 <section class="examples">
    117 
    118 ## Examples
    119 
    120 <!-- eslint no-undef: "error" -->
    121 
    122 ```javascript
    123 var incrspace = require( '@stdlib/array/incrspace' );
    124 var factorial = require( '@stdlib/math/base/special/factorial' );
    125 
    126 var x = incrspace( -10.0, 100.0, 1.0 );
    127 var v;
    128 var i;
    129 
    130 for ( i = 0; i < x.length; i++ ) {
    131     v = factorial( x[ i ] );
    132     console.log( 'x: %d, f(x): %d', x[ i ], v );
    133 }
    134 ```
    135 
    136 </section>
    137 
    138 <!-- /.examples -->
    139 
    140 <section class="links">
    141 
    142 [@stdlib/math/base/special/gamma]: https://www.npmjs.com/package/@stdlib/math/tree/main/base/special/gamma
    143 
    144 [factorial-function]: https://en.wikipedia.org/wiki/Factorial
    145 
    146 [empty-product]: https://en.wikipedia.org/wiki/Empty_product
    147 
    148 </section>
    149 
    150 <!-- /.links -->