0

如何使用f(x,y)sql 中的函数来计算这个系列:

f(x,y)=x-y^3/3!+x^5/5!-y^7/7!+....

我写了阶乘函数并使用了while循环和计数直到参数<=32和convert(varchar(50),@answer)

它可以在没有stackoverflow错误的情况下计算,但是我如何在这个集合中使用这个函数(或proc)?

我们如何在不使用阶乘函数的情况下模拟这个集合?

例如

x^5/5!  

是模拟

x^3/3!*x^2/5*4   .....

谢谢你帮助我:)

4

2 回答 2

1

如果您只需要这个特定的无限级数,它会收敛到所有 x 的 sin() 和 sinh() 值的组合。(检查我的数学以确定,使用泰勒级数来表示 sin 和 sinh。)

f(x,y) = (sin(x)-sinh(-x))/2 + (sin(y)+sinh(-y))/2

这表达了你想要的没有循环的结果,但不幸的是,双曲正弦函数 sinh() 在 T-SQL 中不可用。您可以通过为其创建 CLR 用户定义函数来使 .NET math.sinh 函数对 SQL Server 可用。(您也可以将整个函数 f(x,y) 设为 CLR 函数。)

于 2012-04-28T17:40:27.467 回答
0

我假设一系列因素(显示为 3、5、7)随着奇数集的增加而增加。在此解决方案中,我使用的是公用表表达式,这意味着您必须使用 SQL Server 2005 或更高版本。

Declare @x float;
Declare @y float;

-- this is the first factor evaluated
-- e.g., in the example, 3 is the first factor
-- values less that one effectively set the min
-- to one.
Declare @FactorMin int;

-- this is the maximum number of iterations
-- i.e., 3, 5, 7, 
Declare @FactorMax int

Set @x = 20;
Set @y = 20;
Set @FactorMin = 3;
Set @FactorMax = 15;

With Numbers As
    (
    Select 1 As Value
    Union All
    Select Value + 1
    From Numbers
    Where Value < @FactorMax
    )
    , OddNumbers As
    (
    Select Value
        , Row_Number() Over( Order By Value ) As Position
    From Numbers
    Where Value % 2 = 1
        And Value Between @FactorMin And @FactorMax
    )
    , Factorials As
    (
    Select O.Value, O.Position
        , Exp(Sum(Log(N1.Value))) As Factorial
        , Case When O.Position % 2 = 1 Then -1 * @y Else @x End As XOrY
    From OddNumbers As O
        Cross Join Numbers As N1
    Where N1.Value <= O.Value
    Group By O.Value, O.Position
    )
Select Sum( Z.Value )
From    (
        Select @x As Value
        Union All   
        Select Power(XOrY, Value) / Factorial
        From Factorials 
        ) As Z  
Option (MaxRecursion 0);
于 2012-04-28T17:05:37.677 回答