STIRLING1
Updated 2024-03-14 20:23:52.863000
Syntax
SELECT [westclintech].[wct].[STIRLING1] (
<@n, int,>
,<@k, int,>)
Description
Use the scalar function STIRLING1 to calculate the Stirling numbers of the first kind. The Stirling numbers of the first kind are the coefficients in the expansion of the falling factorial:
(x)_n = x(x-1)(x-2)\cdots(x-n+1)
Into the powers of the variable x;
(x)_n = \sum_{k=0}^n s(n,k) x^k
Stirling numbers of the first kind exhibit the following recursive relationship.
s(n,k)=s(n-1,k-1)-(n-1)s(n-1,k)
Arguments
@n
Number of items.
@k
Number chosen.
Return Type
float
Remarks
If n <= 0 then NULL is returned.
If k < 0 then NULL is returned.
If k > n then NULL is returned.
s(0,0) = 1.
s(n,0) = 0.
Examples
Example #1
Calculate the coefficients for the expansion of: x(x-1)(x-2)(x-3)(x-4)
SELECT wct.STIRLING1(4, 1) as [x],
wct.STIRLING1(4, 2) as [x^2],
wct.STIRLING1(4, 3) as [x^3],
wct.STIRLING1(4, 4) as [x^4];
This produces the following result.
{"columns":[{"field":"x","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"x^2","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"x^3","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"x^4","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"x":"-6","x^2":"11","x^3":"-6","x^4":"1"}]}
The expanded equation would thus be: x4 - 6x3 + 11x2 - 6x.
Example #2
A lower triangular representation of the first few Stirling numbers of the first kind.
SELECT [1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9],
[10]
FROM
(
SELECT n.SeriesValue as n,
k.seriesValue as k,
cast(wct.STIRLING1(n.SeriesValue, k.SeriesValue) as int) as S1
FROM wct.SeriesInt(1, 10, NULL, NULL, NULL) n
CROSS APPLY wct.SeriesInt(1, n.seriesvalue, NULL, NULL, NULL) k
) d
PIVOT
(
MAX(S1)
FOR k IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10])
) pvt
ORDER BY n;
This produces the following result.
{"columns":[{"field":"1","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"2"},{"field":"3"},{"field":"4"},{"field":"5"},{"field":"6"},{"field":"7"},{"field":"8"},{"field":"9"},{"field":"10"}],"rows":[{"1":"1","2":"NULL","3":"NULL","4":"NULL","5":"NULL","6":"NULL","7":"NULL","8":"NULL","9":"NULL","10":"NULL"},{"1":"-1","2":"1","3":"NULL","4":"NULL","5":"NULL","6":"NULL","7":"NULL","8":"NULL","9":"NULL","10":"NULL"},{"1":"2","2":"-3","3":"1","4":"NULL","5":"NULL","6":"NULL","7":"NULL","8":"NULL","9":"NULL","10":"NULL"},{"1":"-6","2":"11","3":"-6","4":"1","5":"NULL","6":"NULL","7":"NULL","8":"NULL","9":"NULL","10":"NULL"},{"1":"24","2":"-50","3":"35","4":"-10","5":"1","6":"NULL","7":"NULL","8":"NULL","9":"NULL","10":"NULL"},{"1":"-120","2":"274","3":"-225","4":"85","5":"-15","6":"1","7":"NULL","8":"NULL","9":"NULL","10":"NULL"},{"1":"720","2":"-1764","3":"1624","4":"-735","5":"175","6":"-21","7":"1","8":"NULL","9":"NULL","10":"NULL"},{"1":"-5040","2":"13068","3":"-13132","4":"6769","5":"-1960","6":"322","7":"-28","8":"1","9":"NULL","10":"NULL"},{"1":"40320","2":"-109584","3":"118124","4":"-67284","5":"22449","6":"-4536","7":"546","8":"-36","9":"1","10":"NULL"},{"1":"-362880","2":"1026576","3":"-1172700","4":"723680","5":"-269325","6":"63273","7":"-9450","8":"870","9":"-45","10":"1"}]}
See Also
FACTORIALPOWER - the step-h factorial power x(n,h)
LCHOOSE - Natural logarithm of the binomial coefficient
LPERMUTA - calculate the natural logarithm of the PERMUTATIONA function.