有没有一种方法可以在没有for循环的情况下在numpy中实现递归? [英] Is there a way to implement recurrence in numpy without for-loops?

查看:76
本文介绍了有没有一种方法可以在没有for循环的情况下在numpy中实现递归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题.有一个矩阵X,我需要生成一个矩阵H,以使矩阵Hi_th行的值由矩阵Xi_th行和矩阵的(i-1)_th行确定H.

I have the following problem. There is a matrix X and I need to generate a matrix H such that values of i_th row in matrix H are determined by i_th row of the matrix X and (i-1)_th row of matrix H.

H_{i} = F(X_{i}, H_{i-1})

要计算矩阵H的第一行,我们使用特殊的矩阵外行(可以说是零行).

To calculate the first row of matrix H we use a special out-of-the-matrix row (row zero, so to say).

有没有一种方法可以有效地以向量化形式实现这种重复,而无需使用for循环?

Is there a way to implement this recurrence efficiently, in a vectorized form, without using for loops?

推荐答案

除了显式的for循环外,没有其他方法(通常).这是因为无法在行之间并行执行此任务(因为每一行都依赖于其他行).

There is no other way (in general) except for an explicit for loop. This is because there is no way to parallelize this task across the rows (since every row depends on some other row).

使得这种情况变得更加困难的是,您可以轻松地生成混沌行为,例如后勤地图:x_{n+1} = r * x_n * (1 - x_{n-1}).

What makes this even harder is that you can easily generate chaotic behavior, for example with the seemingly innocent looking logistic map: x_{n+1} = r * x_n * (1 - x_{n-1}).

仅当您设法找到一个封闭的表格时,您才能找到解决此问题的方法,从根本上消除了重复关系.但这必须针对每个递归关系进行,而且我很确定您甚至不能保证存在封闭形式...

You can only find a way around this if you manage to find a closed form, essentially eliminating the recurrence relation. But this has to be done for each recurrence relation and I am pretty sure you are not even guaranteed that a closed form exists...

这篇关于有没有一种方法可以在没有for循环的情况下在numpy中实现递归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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