What does 'lazy' mean?

When I checked the usage of the function ‘transpose’, I got

Lazy transpose. Mutating the returned object should appropriately mutate A. Often, but not always, yields Transpose(A), where Transpose is a lazy transpose wrapper. Note that this operation is recursive.

What does ‘lazy’ mean here? I saw a lot of ‘lazy’ and ‘eager’, but do not know what they mean.

1 Like

This is about Python, but the same ideas apply: What is Lazy Evaluation in Python? | by Xiaoxu Gao | Towards Data Science


In this particular case, it means the transpose operation doesn’t actually do anything at the time it is called, except to return the array wrapped in a Transpose object.

However, when you index into the returned transpose object, it will transparently reverse the order of the indexes, making it appear as though the underlying array has been transposed.

It is essentially a ‘view’ into the underlying array where the indices have been swapped.

So it is called lazy because it doesn’t do anything until you actually index into the result: it waits till the last minute.

The ‘eager’ alternative here is permutedims. This will allocate an entirely new array and transpose the values. It’s the opposite of lazy because it does all the work up front, at the expense of memory allocations and copies.

Which one is right for you? It’s really a question of performance and as always benchmark. But as a rule of thumb: if you find yourself accessing the transposed array more than a few times it’s probably best to use the eager alternative, permutedims.


Paradoxically, permutedims(::AbstractVector) goes against this idiom and is a non-materializing reshape. The main difference between permutedims and transpose is that the latter is recursive.