优化递归序列的计算 [英] Optimizing the computation of a recursive sequence

查看:139
本文介绍了优化递归序列的计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是最快的方式在研发计算定义为一个递推数列

  X [1];  -  X1
X [n]的&其中;  - 函数f(x [N-1])
 

我假设适当长度的矢量x preallocated。有没有不仅仅是一个循环更聪明的办法?

词:这个扩展到向量:

  X [,1];  -  X1
 ×〔中,n]&其中;  -  F(X [中,n-1])
 

解决方案

在这是否可以以任何方式完全量化的问题,我认为答案很可能是不。背后阵列编程的基本想法是,操作应用到整个组值在同一时间。同样为embarassingly并行计算的问题。在这种情况下,因为你的递归算法取决于每个先前状态,就没有的方式来获得速度从并行处理:它必须串行运行。

话虽这么说,加快你的程序通常建议适用。例如,外面的递归函数尽可能做尽可能多的计算。排序一切。 predefine您的数组长度,使他们没有足够的循环过程中成长。等等看到这一问题进行类似的讨论。也有在添Hesterberg的高效S加上编程<文章/ A>。

What is the fastest way in R to compute a recursive sequence defined as

x[1] <- x1 
x[n] <- f(x[n-1])

I am assuming that the vector x of proper length is preallocated. Is there a smarter way than just looping?

Variant: extend this to vectors:

 x[,1] <- x1 
 x[,n] <- f(x[,n-1])

解决方案

In terms of the question of whether this can be fully "vectorized" in any way, I think the answer is probably "no". The fundamental idea behind array programming is that operations apply to an entire set of values at the same time. Similarly for questions of "embarassingly parallel" computation. In this case, since your recursive algorithm depends on each prior state, there would be no way to gain speed from parallel processing: it must be run serially.

That being said, the usual advice for speeding up your program applies. For instance, do as much of the calculation outside of your recursive function as possible. Sort everything. Predefine your array lengths so they don't have to grow during the looping. Etc. See this question for a similar discussion. There is also a pseudocode example in Tim Hesterberg's article on efficient S-Plus Programming.

这篇关于优化递归序列的计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆