[英]Recursion of integrated Legendre polynomials
I am writing these recursion in python and don't get why the official solution is different than mine.我正在用 python 编写这些递归,但不明白为什么官方解决方案与我的不同。 The trivial cases for n = 1, 2 are clear. n = 1, 2 的琐碎情况很清楚。 This is my approach:这是我的方法:
return ((2*(k-1)-1)*x*leg(k-1) - ((k-1)-2)*leg(k-2)) / k
This is the official solution:这是官方的解决方案:
return ((2*k-1)*x*leg(k-1) - (k-1)*leg(k-2)) / k
Why are they decreasing k to call the function, but in the first part the coefficient (2*k-1) not?为什么他们减少 k 来调用函数,但在第一部分中系数 (2*k-1) 不是? And why is the coefficient in the second part changed to (k-1)?为什么第二部分的系数变成了(k-1)?
EDIT编辑
So generally, afaiu, your issue stems from the formula (in your attached image does show L_{k+1}(x)
) while they do implement L_{k}(x)
without the intermediate derivation that shows how to obtain L_{k}(x)
from L_{k+1}(x)
.所以一般来说,afaiu,你的问题源于公式(在你的附图中确实显示了L_{k+1}(x)
),而他们确实实现了L_{k}(x)
而没有显示如何获得L_{k}(x)
的中间推导L_{k}(x)
来自L_{k+1}(x)
。
I further think that there is some confusion here, so I will sightly deviate from the notation.我进一步认为这里有一些混乱,所以我会稍微偏离符号。 Let m = k+1
in what follows.下面让m = k+1
。
We then obtain through straight forward substitution:然后我们通过直接替换得到:
m * L(x, m) = (2*(m+1)-1) * x * L(x, m-1) - ((m-1)-2) * L(x, m-2) # for m >= 3
which yields这产生
L(x, m) = ( (2*m + 2 - 1) * x * L(x, m-1) - ((m-3) * L(x, m-2) ) / m
and in python syntax, this is:在 python 语法中,这是:
def L(x, m):
if m == 1:
return x
elif m == 2:
return 0.5 * (x**2 - 1)
else: # do this for all m >= 3
return ( (2*m + 1) * x * L(x, m-1) - ((m-3) * L(x, m-2) ) / m
Why are they decreasing k to call the function, but in the first part the coefficient (2*k-1) not?为什么他们减少 k 来调用函数,但在第一部分中系数 (2*k-1) 不是?
IMHO they did, follow my derivation.恕我直言,他们做到了,按照我的推导。
And why is the coefficient in the second part changed to (k-1)?为什么第二部分的系数变成了(k-1)?
I honestly do not know;老实说,我不知道; to me, it seems like they made a mistake during substitution, ie they must have put m+1
instead of m-1
.对我来说,似乎他们在替换过程中犯了一个错误,即他们必须把m+1
而不是m-1
。
>>> (2*(k-1)-1)
Does first compute k-1
multiplies it by 2
and then subtracts 1
which is indifferent from 2*k-1
.是否第一计算k-1
乘以它由2
,然后减去1
,其是从淡漠2*k-1
。 For example:例如:
k = 5
does yield with your solution (2*(5-1)-1) = 7
and from the official solution (2*5-1) = 9
. k = 5
确实产生了您的解决方案(2*(5-1)-1) = 7
和来自官方解决方案(2*5-1) = 9
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.