当然,在有递推公式的前提下,实际上用MATLAB也很容易实现:
第一类斯特林数:
%MATLAB function, Calculation the Type I Stirling Number
%if input one argumnet, returns a matrix.
%and if input two arguments, returns a number.
%By Castor,2012-09-07
function M=StirlingS1(n,k)
if n==1
M=1;
else
M=zeros(n,n);
M(1,1)=1;
M(2,1)=-1;
M(2,2)=1;
for i=2:n %row
M(i,1)=(-1)*(i-1)*M(i-1,1);
M(i,i)=1;
for j=2:i %column
M(i,j)=M(i-1,j-1)-(i-1)*M(i-1,j);
end %inner for
end %outer for
end %inner if
if nargin==2
if k>n
%set it zero,
M=0;
%or throw an error:
%error('see StirlingS1: the second argument should be no larger than the first argument!');
end
M=M(n,k);
end
end第二类斯特林数:
%MATLAB function, Calculation the Type II Stirling Number
%if input one argumnet, returns a matrix.
%and if input two arguments, returns a number.
%By Castor,2012-09-07
function M=StirlingS2(n,k)
if n==1
M=1;
else
M=zeros(n,n);
M(1,1)=1;
M(2,1)=1;
M(2,2)=1;
for i=2:n %row
M(i,1)=1;
M(i,i)=1;
for j=2:i %column
M(i,j)=M(i-1,j-1)+j*M(i-1,j);
end %inner for
end %outer for
end %inner if
if nargin==2
if k>n
%set it zero,
M=0;
%or throw an error:
%error('see StirlingS2: the second argument should be no larger than the first argument!');
end
M=M(n,k);
end
end
函数将根据提供的参数个数,决定返回的是矩阵还是一个数值,对于矩阵,行表示的是n,列表示的是S(n),i
评论