Factorial

From APL Wiki
Revision as of 21:17, 10 September 2022 by Adám Brudzewsky (talk | contribs) (Text replacement - "</source>" to "</syntaxhighlight>")
Jump to navigation Jump to search
!

Factorial (!) is a monadic scalar function which gives the factorial of a non-negative integer. Factorial takes its glyph <source lang=apl inline>!</syntaxhighlight> from traditional mathematics but, like all monadic functions, takes its argument on the right <source lang=apl inline>!Y</syntaxhighlight> 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.

<source lang=apl>

     !0 1 2 3 4

1 1 2 6 24

     ×/⍳4

24 </syntaxhighlight>

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 , <source lang=apl inline>!Y</syntaxhighlight> is defined as .

<source lang=apl>

     !¯1.2 0.5 2.7

¯5.821148569 0.8862269255 4.170651784

     !2J1 ¯2J¯1

0.962865153J1.339097176 ¯0.1715329199J¯0.3264827482

</syntaxhighlight>

Works in: Dyalog APL

The Gamma function diverges at 0 or negative numbers, so <source lang=apl inline>!Y</syntaxhighlight> is undefined at negative integers.

<source lang=apl>

     !¯1

DOMAIN ERROR

!¯1        
∧   

</syntaxhighlight>

Works in: Dyalog APL

In J, where literal infinity is supported, negative integer factorial evaluates to positive infinity <source lang=j inline>_</syntaxhighlight> (if the argument is odd) or negative infinity <source lang=j inline>__</syntaxhighlight> (if even). This corresponds to the positive-side limit of the Gamma function.

<source lang=j>

     !_1 _2 _3 _4

_ __ _ __

</syntaxhighlight>

Works in: J

External links

Documentation

APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)
Quad names Index originComparison toleranceMigration levelAtomic vector