1

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})

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

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

2
  • Depends on F, really. I doubt there is a general method. Commented Feb 19, 2018 at 11:11
  • In beginning physics vector is introduced as a way of thread the coordinates of a point as one object as opposed to 3 numbers. That idea carries over into numpy. In proper vectorization we don't usually care about the order of evaluation - numpy is supposed take care of those details. So a calculation that depends on the order of evaluation doesn't neatly fit. Operations like cumsum are closest we get to a compiled sequential operation. Commented Feb 19, 2018 at 18:21

1 Answer 1

2

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).

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...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.