I want to define a 2D (numpy) array such that cell(row,col) == row+col (or any other function of those 2 variables: row and column)
I’m looking for a functional solution, and my hope is that would not use any memory and that the resulting construct could be passed to any standard array manipulation function (matrix mult…)
Something doable in C++ with template metaprogramming.
Use
numpy.fromfunction(function, shape). From the docs:So, for your case:
(Of course, this does use memory — there isn’t a way to have a function which acts as if it’s a numpy array but isn’t actually in memory. However, for a somewhat similar case, see the
memmapnumpy class, which accesses a file stored on disk as a numpy array — docs here.)