1

I've implemented a recursive function which takes as parameter a numpy array. Here the simplified version:

 def rec(arr):

     rec(arr[indices])

In every recursive call I use a part of the array indexed by some indices.

My question is about the memory load: how does python handle this? Does it makes a copy of the array at each call or not?

2
  • I do not see any relation between recursion and what your question is about. If you use a loops instead you think the memory usage would be different? Commented Nov 26, 2012 at 16:13
  • @Bakuriu: I guess the relevance of recursion is that a 15-level-deep recursion implies there are 15 segments of arr that are alive at the same time. With iteration, this may or may not be the case. But I agree that this question appears to be entirely about the memory use of arr[indices] and not about recursion per se. Commented Nov 26, 2012 at 16:20

1 Answer 1

6

It depends on the nature of indices. If it's a slice, there is no copy. If, on the other hand, you're using fancy indexing, then a copy is made.

I recommend reading Copies and Views in the NumPy tutorial (even though the section does not cover fancy indexing).

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

2 Comments

Indices is an array of indices which index a slice of arr. So it does not make a copy right?
@JohnEntropy: If the elements of indices are integers, that's fancy indexing, so a copy is made.

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.