具有相关边界行程计数的嵌套循环
出于好奇,我尝试执行以下操作,结果对我来说并不那么明显; 假设我有运行时边界的嵌套循环,例如:
t = 0 // trip count
for l in 0:N
for k in 0:N
for j in max(l,k):N
for i in k:j+1
t += 1
t is loop trip count
是否有一个通用算法/方式(显然比 N^4 更好)来计算循环行程计数?
如果没有,我很想知道你将如何处理这个特定的循环。上面的循环是对称的(它是对称 4 阶张量上的循环),我也对检测循环对称性的方法感兴趣。
我正在假设迭代边界仅取决于常量或先前的循环变量。链接/期刊文章,如果您知道一篇,那就太好了。
just out of curiosity I tried to do the following, which turned out to be not so obvious to me;
Suppose I have nested loops with runtime bounds, for example:
t = 0 // trip count
for l in 0:N
for k in 0:N
for j in max(l,k):N
for i in k:j+1
t += 1
t is loop trip count
is there a general algorithm/way (better than N^4 obviously) to calculate loop trip count?
if not, I would be curious to know how you would approach just this particular loop. the above loop is symmetric (it's loops over symmetric rank-4 tensor), and I am also interested in methods to detect loop symmetry.
I am working on the assumption that the iteration bounds depend only on constant or previous loop variables. link/journal article, If you know of one, would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信内循环会运行
多次。
我并没有真正直接解决这个问题,我拟合了一个 4 阶多项式表达式来精确计算 N 从 1 到 50 的 t,希望我能得到精确的拟合。
为了计算精确的 t 我使用了
它应该相当于实际运行循环。
数据拟合,对数比例 http://img714.imageshack.us/img714/2313/plot3 .png
N 从 1 到 50 的拟合完全匹配,使用这两种方法计算 N=100 的结果为 13258775。
编辑:
该练习是使用开源代数系统 maxima 完成的,这是实际的源(输出被丢弃):
I believe the inner loop will run
times.
I did not really solve the problem directly, I fitted a 4-th order polynomial expression to exactly calculated t for N from 1 to 50 hoping that I'll get exact fit.
To calculate exact t I used
which should be the equivalent of actually running your loops.
data fit, log scale http://img714.imageshack.us/img714/2313/plot3.png
The fit for N from 1 to 50 matches exactly and calculating it for N=100 gives 13258775 using both methods.
EDIT:
The exercise was done using open source algebra system maxima, here's the actual source (output discarded):
如果您想知道内部循环
将执行多少次,只需计算:
N - max(l, k)
假设开放范围,N + 1 - max(l, k) )
假设封闭范围。例如,如果:
那么它将运行 7、8、9、10(封闭范围),因此实际上是
10 + 1 - 7 = 4
次。If you want to know how many times the inner loop:
Would be executed, just compute:
N - max(l, k)
assuming open range,N + 1 - max(l, k)
assuming closed range.For example, if:
then it will run on 7, 8, 9, 10 (closed range), so indeed
10 + 1 - 7 = 4
times.答案是否,只要循环边界可以以任意方式依赖于外部变量,因为这将提供获得积分级数的闭合形式公式的通用方法。
要了解这一点,请考虑以下事项:
行程计数 t(N) 等于总和 t(N) = f(0)+f(1)+f(2)+f(3)+...+f(N -1)。
因此,如果无论 f() 如何,您都可以获得 t(N) 的闭合形式公式,那么您就找到了一种生成闭合形式的非常通用的方法,我会说太笼统了,因为这里对应于积分,并且 < em>众所周知,并非所有积分都接受封闭形式公式。
the answer is no, as long as the loop bounds can depend from the outer variables in an arbitrary fashionm as this would provide a general means for getting closed form formulations of integral series.
To see this, consider the following:
The trip count t(N) equals the sum t(N) = f(0)+f(1)+f(2)+f(3)+...+f(N-1).
So if you can get a closed form formulation for t(N) regardless of f(), you have found a very general method of producing closed forms, too general I would say, because what you have here correspond to an integral, and it's known that not all integrals admit closed form formulations.