工控网首页
>

应用设计

>

SPC分析中用到的伽玛函数

SPC分析中用到的伽玛函数

2023/9/7 17:50:17

function [Fun_SPC_Gamma](@x float)

returns float
begin

 

    if @x <= 0
        return 1
    declare @gamma float 
    set @gamma = 0.577215664901532860606512090

 

    if @x <0.001
        return 1.0 / (@x * (1.0 + @gamma * @x));

 

    if @x < 12
    begin

 

        declare @y float
        set @y =@x

 

        declare @n int 
        set @n = 0

 

        declare @arg_was_less_than_one bit
        if @y < 1.0
            set @arg_was_less_than_one = 1
        else
            set @arg_was_less_than_one = 0

 

        if @arg_was_less_than_one = 1
            set @y = @y + 1
        else
        begin

             

            set @n= cast(Floor(@y) as int) - 1
            set @y =@y - @n
        end

 

        declare @tbP table
        (
            id int,
            const float
        )     
        insert into @tbP values(1,-1.71618513886549492533811E+0)
        insert into @tbP values(2,2.47656508055759199108314E+1)
        insert into @tbP values(3,-3.79804256470945635097577E+2)
        insert into @tbP values(4,6.29331155312818442661052E+2)
        insert into @tbP values(5,8.66966202790413211295064E+2)
        insert into @tbP values(6,-3.14512729688483675254357E+4)
        insert into @tbP values(7,-3.61444134186911729807069E+4)
        insert into @tbP values(8,6.64561438202405440627855E+4)

 

        declare @tbQ table
        (
            id int,
            const float
        )     
        insert into @tbQ values(1,-3.08402300119738975254353E+1)
        insert into @tbQ values(2,3.15350626979604161529144E+2)
        insert into @tbQ values(3,-1.01515636749021914166146E+3)
        insert into @tbQ values(4,-3.10777167157231109440444E+3)
        insert into @tbQ values(5,2.25381184209801510330112E+4)
        insert into @tbQ values(6,4.75584627752788110767815E+3)
        insert into @tbQ values(7,-1.34659959864969306392456E+5)
        insert into @tbQ values(8,-1.15132259675553483497211E+5)

 

        declare @num float
        declare @den float
        set @num = 0
        set @den = 1

 

        declare @i int 
        declare @z float
        set @z = @y - 1

 

        set @i = 1

 

        while @i <=8
        begin

 

            declare @p float
            declare @q float

 

            select
                @p = const
            from @tbP t 
            where id = @i

 

            select 
                @q = const
            from @tbQ t
            where id = @i

 

            set @num = (@num + @p) * @z
            set @den = @den * @z + @q

 

            set @i = @i + 1
        end

 

        declare @result float
        set @result = @num/@den + 1.0

 

        if @arg_was_less_than_one = 1
        begin
            set @result = @result/(@y - 1)
        end
        else
        begin

             

            declare @j int
            set @j = 0
            while @j< @n 
            begin
                set @result  = @result * @y
                set @y =@y + 1
                set @j = @j + 1
            end

 

        end

 

        return @result
    end

 

    if @x > 171.624
        return 1

 

    return Exp(dbo.Fun_SPC_LogGamma(@x))

 

end

审核编辑(
王静
)
投诉建议

提交

查看更多评论