在 Haskell 中计算递归关系
你好,StackOverflow。
假设我有两个用于计算的递归关系 S(i,j)
我想计算值 < em>S(0,0)、S(0,1)、S(1,0)、S(2,0) 等等......以渐近最优的方式。用铅笔和纸几分钟就可以发现它展开成树状结构,可以通过多种方式横向移动。现在,树以后不太可能有用,所以现在我希望生成像 [[S(00)],[S(10),S(01)],[S(20) 这样的嵌套列表),S(21),S(12),S(02)],...]
。我创建了一个函数来生成 S(i,0) (或 S(0,j),取决于第一个参数)的平面列表:
osrr xpa p predexp = os00 : os00 * (xpa + rp) : zipWith3 osrr' [1..] (tail osrr) osrr
where
osrr' n a b = xpa * a + rp * n * b
os00 = sqrt (pi/p) * predexp
rp = recip (2*p)
但是,我是,不知道如何进一步进行。
Greetings, StackOverflow.
Let's say I have two following recurrence relations for computing S(i,j)
I would like to compute values S(0,0), S(0,1), S(1,0), S(2,0), etc... in asymptotically optimal way. Few minutes with pencil and paper reveal that it unfolds into treelike structure which can be transversed in several ways. Now, it's unlikely tree will be useful later on, so for now I'm looking to produce nested list like [[S(00)],[S(10),S(01)],[S(20),S(21),S(12),S(02)],...]
. I have created a function to produce a flat list of S(i,0) (or S(0,j), depending on first argument):
osrr xpa p predexp = os00 : os00 * (xpa + rp) : zipWith3 osrr' [1..] (tail osrr) osrr
where
osrr' n a b = xpa * a + rp * n * b
os00 = sqrt (pi/p) * predexp
rp = recip (2*p)
I am, however, at loss as how to proceed further.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议以直接递归风格编写它并使用记忆来创建遍历:
该库将创建一个无限表来存储您已经计算的值。由于备忘录构造函数位于
p
参数下,因此该表在p
范围内存在;即 osrr 1 2 3 将创建一个表用于计算 A(2,3),然后清理它。您可以通过部分应用来重用特定p
的表:现在
osrr1
将在其所有调用之间共享该表(根据您的情况,可能会也可能不会)你想要什么)。I would suggest writing it in a direct recursive style and using memoization to create your traversal:
The library will create an infinite table to store values you have already computed. Because the memo constructors are under the
p
parameter, the table exists for the scope ofp
; i.e. osrr 1 2 3 will create a table for the purpose of computing A(2,3), and then clean it up. You can reuse the table for a specificp
by partially applying:Now
osrr1
will share the table between all its calls (which, depending on your situation, may or may not be what you want).首先,肯定存在一些您没有告诉我们的边界条件。
一旦有了这些,尝试将解决方案表述为递归定义的数组。只要您知道 i 和 j 的上限,此方法就有效。否则,请使用备忘录组合器。
First, there must be some boundary conditions that you've not told us about.
Once you have those, try stating the solution as a recursively defined array. This works as long as you know an upper bound on i and j. Otherwise, use memo combinators.