I have a Python function that, given a scalar or vector a, does some sort of symmetry expansion, e.g.,
import numpy
def expand(a):
zero = numpy.zeros_like(a)
return numpy.array([
[zero, a, a],
[a, zero, a],
[a, a, zero],
#
[zero, -a, -a],
[-a, zero, -a],
[-a, -a, zero],
])
In this example, the resulting array will have shape (6, 3, a.shape). Eventually I'll need shape (6, a.shape, 3) or (3, a.shape, 6) and contiguity, so I'll numpy.moveaxis() around. Even (3, 6, a.shape) would be an improvement. I can get this with
import numpy
def expand(a):
zero = numpy.zeros_like(a)
return numpy.array([
[zero, a, a, zero, -a, -a],
[a, zero, a, -a, zero, -a],
[a, a, zero, -a, -a, zero]
])
but this isn't as readable as the first version, especially if the transformation is more complex. (There always 3 columns.)
Is there a way to initialize out to have the right shape straight away? (Note that reshape()ing won't do it, the data would be in wrong order.)