Namespace holding implementations of variations on the factorial function.
Namespace holding implementations of variations on the factorial function.
(->bigint x)
If x
is a fixed-precision integer, returns a emmy.util/bigint
version of x
. Else, acts as identity.
This is useful in cases where you may want to multiply x
by other large
numbers, but don't want to try and convert something that can't overflow,
like a symbol, into bigint
.
If `x` is a fixed-precision integer, returns a [[emmy.util/bigint]] version of `x`. Else, acts as identity. This is useful in cases where you may want to multiply `x` by other large numbers, but don't want to try and convert something that can't overflow, like a symbol, into `bigint`.
(bell n)
Returns the n
th Bell number, i.e.,
the number of ways a set of n
elements can be partitioned into nonempty
subsets.
The n
th Bell number is denoted $B_n$.
Returns the `n`th [Bell number](https://en.wikipedia.org/wiki/Bell_number), i.e., the number of ways a set of `n` elements can be partitioned into nonempty subsets. The `n`th Bell number is denoted $B_n$.
(binomial-coefficient n k)
Returns the binomial coefficient, i.e., the coefficient of the $x^k$ term in the polynomial expansion of the binomial power $(1 + x)^n$.
This quantity is sometimes pronounced "n choose k".
For negative n
or k
, binomial-coefficient
matches the behavior
provided by Mathematica, described at this
page. Given negative
n
, returns
;; for k >= 0
(* (expt -1 k)
(binomial-coefficient (+ (- n) k -1) k))
;; for k >= 0
(* (expt -1 (- n k))
(binomial-coefficient (+ (- k) -1) (- n k)))
;; otherwise:
0
Returns the [binomial coefficient](https://en.wikipedia.org/wiki/Binomial_coefficient), i.e., the coefficient of the $x^k$ term in the polynomial expansion of the binomial power $(1 + x)^n$. This quantity is sometimes pronounced "n choose k". For negative `n` or `k`, [[binomial-coefficient]] matches the behavior provided by Mathematica, described at [this page](https://mathworld.wolfram.com/BinomialCoefficient.html). Given negative `n`, returns ```clj ;; for k >= 0 (* (expt -1 k) (binomial-coefficient (+ (- n) k -1) k)) ;; for k >= 0 (* (expt -1 (- n k)) (binomial-coefficient (+ (- k) -1) (- n k))) ;; otherwise: 0 ```
(double-factorial n)
Returns the product of all integers from 1 up to n
that have the same
parity (odd or even) as n
.
([[double-factorial]] 0)
is defined as an empty product and equal to 1.
double-factorial
with argument n
is equivalent to ([[multi-factorial]] n 2)
, but slightly more general in that it can handle negative values of
n
.
If n
is negative and even, returns ##Inf
.
If n
is negative and odd, returns (/ (double-factorial (+ n 2)) (+ n 2))
.
For justification, see the Wikipedia page on the extension of double factorial to negative arguments.
Returns the product of all integers from 1 up to `n` that have the same parity (odd or even) as `n`. `([[double-factorial]] 0)` is defined as an empty product and equal to 1. [[double-factorial]] with argument `n` is equivalent to `([[multi-factorial]] n 2)`, but slightly more general in that it can handle negative values of `n`. If `n` is negative and even, returns `##Inf`. If `n` is negative and odd, returns `(/ (double-factorial (+ n 2)) (+ n 2))`. For justification, see the [Wikipedia page on the extension of double factorial to negative arguments](https://en.wikipedia.org/wiki/Double_factorial#Negative_arguments).
(factorial n)
Returns the factorial of n
, i.e., the product of 1 to n
(inclusive).
factorial
will return a platform-specific emmy.util/bigint
given
some n
that causes integer overflow.
Returns the factorial of `n`, i.e., the product of 1 to `n` (inclusive). [[factorial]] will return a platform-specific [[emmy.util/bigint]] given some `n` that causes integer overflow.
Alias for falling-factorial
.
Alias for [[falling-factorial]].
(falling-factorial a b)
generic falling-factorial.
Returns the falling
factorial, of
a
to the b
, defined as the polynomial
$$(a)_b = a^{\underline{b}} = a(a - 1)(a - 2) \cdots (a - b - 1)$$
Given a negative b
, ([[falling-factorial]] a b)
is equivalent
to (invert ([[rising-factorial]] (inc a) (- b)))
, or ##Inf
if the
denominator evaluates to 0.
The coefficients that appear in the expansions of falling-factorial
called
with a symbolic first argument and positive integral second argument are the
Stirling numbers of the first kind (see stirling-first-kind
).
generic falling-factorial. Returns the [falling factorial](https://en.wikipedia.org/wiki/Falling_and_rising_factorials), of `a` to the `b`, defined as the polynomial $$(a)_b = a^{\underline{b}} = a(a - 1)(a - 2) \cdots (a - b - 1)$$ Given a negative `b`, `([[falling-factorial]] a b)` is equivalent to `(invert ([[rising-factorial]] (inc a) (- b)))`, or `##Inf` if the denominator evaluates to 0. The coefficients that appear in the expansions of [[falling-factorial]] called with a symbolic first argument and positive integral second argument are the Stirling numbers of the first kind (see [[stirling-first-kind]]).
(multi-factorial n k)
Returns the product of the positive integers up to n
that are congruent
to (mod n k)
.
When k
equals 1, equivalent to ([[factorial]] n)
.
See the [Wikipedia page on generalizations
of double-factorial
](https://en.wikipedia.org/wiki/Double_factorial#Generalizations)
for more detail.
If you need to extend multi-factorial
to negative n
or k
, that page
has suggestions for generalization.
Returns the product of the positive integers up to `n` that are congruent to `(mod n k)`. When `k` equals 1, equivalent to `([[factorial]] n)`. See the [Wikipedia page on generalizations of [[double-factorial]]](https://en.wikipedia.org/wiki/Double_factorial#Generalizations) for more detail. If you need to extend [[multi-factorial]] to negative `n` or `k`, that page has suggestions for generalization.
Alias for falling-factorial
.
Alias for [[falling-factorial]].
(rising-factorial a b)
generic rising-factorial.
Returns the rising
factorial, of
a
to the b
, defined as the polynomial
$$(a)^b = a^{\overline{b}} = a(a + 1)(a + 2) \cdots (a + b - 1)$$
Given a negative b
, ([[rising-factorial]] a b)
is equivalent
to (invert ([[falling-factorial]] (dec a) (- b)))
, or ##Inf
if the
denominator evaluates to 0.
generic rising-factorial. Returns the [rising factorial](https://en.wikipedia.org/wiki/Falling_and_rising_factorials), of `a` to the `b`, defined as the polynomial $$(a)^b = a^{\overline{b}} = a(a + 1)(a + 2) \cdots (a + b - 1)$$ Given a negative `b`, `([[rising-factorial]] a b)` is equivalent to `(invert ([[falling-factorial]] (dec a) (- b)))`, or `##Inf` if the denominator evaluates to 0.
(stirling-first-kind n k & {:keys [unsigned?]})
Given n
and k
, returns the number of permutations of n
elements which
contain exactly k
permutation
cycles. This is called
the Stirling number s(n, k) of the first
kind.
By default, returns the signed Stirling number of the first
kind.
Pass the :unsigned? true
keyword option to retrieve the signed Stirling
number. (Or take the absolute value of the result...)
(stirling-first-kind 13 2)
;;=> -1486442880
(stirling-first-kind 13 2 :unsigned? true)
;;=> 1486442880
Given `n` and `k`, returns the number of permutations of `n` elements which contain exactly `k` [permutation cycles](https://mathworld.wolfram.com/PermutationCycle.html). This is called the [Stirling number s(n, k) of the first kind](https://en.wikipedia.org/wiki/Stirling_numbers_of_the_first_kind). By default, returns the [signed Stirling number of the first kind](https://en.wikipedia.org/wiki/Stirling_numbers_of_the_first_kind#Signs). Pass the `:unsigned? true` keyword option to retrieve the signed Stirling number. (Or take the absolute value of the result...) ```clj (stirling-first-kind 13 2) ;;=> -1486442880 (stirling-first-kind 13 2 :unsigned? true) ;;=> 1486442880 ```
(stirling-second-kind n k)
Returns $S(n,k)$, the number of ways to partition a set of n
objects into k
non-empty subsets.
This is called a Stirling number of the second kind.
Returns $S(n,k)$, the number of ways to partition a set of `n` objects into `k` non-empty subsets. This is called a [Stirling number of the second kind](https://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind).
(subfactorial n)
Returns the number of permutations of n
objects in which no object appears in
its original position. (Each of these permutations is called
a 'derangement' of the set.)
Returns the number of permutations of `n` objects in which no object appears in its original position. (Each of these permutations is called a ['derangement'](https://en.wikipedia.org/wiki/Derangement) of the set.) ## References - [Subfactorial page at Wolfram Mathworld](https://mathworld.wolfram.com/Subfactorial.html) - John Cook, [Variations on Factorial](https://www.johndcook.com/blog/2010/09/21/variations-on-factorial/) - John Cook, [Subfactorial](https://www.johndcook.com/blog/2010/04/06/subfactorial/) - ['Derangement' on Wikipedia](https://en.wikipedia.org/wiki/Derangement)
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close