Factorial: Difference between revisions
No edit summary |
m (Text replacement - "<source" to "<syntaxhighlight") |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{Built-in|Factorial|!}} is a [[monadic]] [[scalar function]] which gives the [[wikipedia:factorial|factorial]] of a non-negative integer. Factorial takes its [[glyph]] < | {{Built-in|Factorial|!}} is a [[monadic]] [[scalar function]] which gives the [[wikipedia:factorial|factorial]] of a non-negative integer. Factorial takes its [[glyph]] <syntaxhighlight lang=apl inline>!</syntaxhighlight> from [[Comparison_with_traditional_mathematics#Prefix|traditional mathematics]] but, like all [[monadic function]]s, takes its argument on the right <syntaxhighlight lang=apl inline>!Y</syntaxhighlight> instead of traditional mathematics' <math>Y!</math>. It shares the glyph with the dyadic arithmetic function [[Binomial]]. | ||
== Examples == | == Examples == | ||
Line 5: | Line 5: | ||
The factorial of a positive integer n is defined as the [[times|product]] of [[Index Generator|1 to n]] inclusive. | The factorial of a positive integer n is defined as the [[times|product]] of [[Index Generator|1 to n]] inclusive. | ||
< | <syntaxhighlight lang=apl> | ||
!0 1 2 3 4 | !0 1 2 3 4 | ||
1 1 2 6 24 | 1 1 2 6 24 | ||
×/⍳4 | ×/⍳4 | ||
24 | 24 | ||
</ | </syntaxhighlight> | ||
== Extended definition == | == Extended definition == | ||
In multiple implementations, this function has an extended definition using the [[wikipedia:Gamma function|Gamma function]] <math>\Gamma(n)</math>, so that it is defined for real and [[complex]] numbers. Because <math>\Gamma(n)</math> equals <math>(n-1)!</math>, < | In multiple implementations, this function has an extended definition using the [[wikipedia:Gamma function|Gamma function]] <math>\Gamma(n)</math>, so that it is defined for real and [[complex]] numbers. Because <math>\Gamma(n)</math> equals <math>(n-1)!</math>, <syntaxhighlight lang=apl inline>!Y</syntaxhighlight> is defined as <math>\Gamma(Y+1)</math>. | ||
< | <syntaxhighlight lang=apl> | ||
!¯1.2 0.5 2.7 | !¯1.2 0.5 2.7 | ||
¯5.821148569 0.8862269255 4.170651784 | ¯5.821148569 0.8862269255 4.170651784 | ||
!2J1 ¯2J¯1 | !2J1 ¯2J¯1 | ||
0.962865153J1.339097176 ¯0.1715329199J¯0.3264827482 | 0.962865153J1.339097176 ¯0.1715329199J¯0.3264827482 | ||
</ | </syntaxhighlight>{{Works in|[[Dyalog APL]]}} | ||
The Gamma function diverges at 0 or negative numbers, so < | The Gamma function diverges at 0 or negative numbers, so <syntaxhighlight lang=apl inline>!Y</syntaxhighlight> is undefined at negative integers. | ||
< | <syntaxhighlight lang=apl> | ||
!¯1 | !¯1 | ||
DOMAIN ERROR | DOMAIN ERROR | ||
!¯1 | !¯1 | ||
∧ | ∧ | ||
</ | </syntaxhighlight>{{Works in|[[Dyalog APL]]}} | ||
In [[J]], where literal [[infinity]] is supported, negative integer factorial evaluates to positive infinity < | In [[J]], where literal [[infinity]] is supported, negative integer factorial evaluates to positive infinity <syntaxhighlight lang=j inline>_</syntaxhighlight> (if the argument is odd) or negative infinity <syntaxhighlight lang=j inline>__</syntaxhighlight> (if even). This corresponds to the positive-side limit of the Gamma function. | ||
< | <syntaxhighlight lang=j> | ||
!_1 _2 _3 _4 | !_1 _2 _3 _4 | ||
_ __ _ __ | _ __ _ __ | ||
</ | </syntaxhighlight>{{Works in|[[J]]}} | ||
== External links == | == External links == | ||
Line 43: | Line 43: | ||
=== Documentation === | === Documentation === | ||
* [ | * [https://help.dyalog.com/latest/#Language/Primitive%20Functions/Factorial.htm Dyalog] | ||
* [http://microapl.com/apl_help/ch_020_020_250.htm APLX] | * [http://microapl.com/apl_help/ch_020_020_250.htm APLX] | ||
* [https://www.jsoftware.com/help/dictionary/d410.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/bang NuVoc] | * [https://www.jsoftware.com/help/dictionary/d410.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/bang NuVoc] | ||
{{APL built-ins}}[[Category:Primitive functions]][[Category:Scalar monadic functions]] | {{APL built-ins}}[[Category:Primitive functions]][[Category:Scalar monadic functions]] |
Latest revision as of 21:21, 10 September 2022
!
|
Factorial (!
) is a monadic scalar function which gives the factorial of a non-negative integer. Factorial takes its glyph !
from traditional mathematics but, like all monadic functions, takes its argument on the right !Y
instead of traditional mathematics' . It shares the glyph with the dyadic arithmetic function Binomial.
Examples
The factorial of a positive integer n is defined as the product of 1 to n inclusive.
!0 1 2 3 4 1 1 2 6 24 ×/⍳4 24
Extended definition
In multiple implementations, this function has an extended definition using the Gamma function , so that it is defined for real and complex numbers. Because equals , !Y
is defined as .
!¯1.2 0.5 2.7 ¯5.821148569 0.8862269255 4.170651784 !2J1 ¯2J¯1 0.962865153J1.339097176 ¯0.1715329199J¯0.3264827482
The Gamma function diverges at 0 or negative numbers, so !Y
is undefined at negative integers.
!¯1 DOMAIN ERROR !¯1 ∧
In J, where literal infinity is supported, negative integer factorial evaluates to positive infinity _
(if the argument is odd) or negative infinity __
(if even). This corresponds to the positive-side limit of the Gamma function.
!_1 _2 _3 _4 _ __ _ __