I'm trying to do something simple in numpy, and I'm sure there should be an easy way of doing it.
Basically, I have a list of n
vectors with various lengths. If v1[i]
is the i
'th entry of the first vector then I want to find a n
-dimensional array, A, such that
A[i,j,k...] = v1[i] v2[j] v3[k] ...
My problem is that:
outer
only takes two vector arguments.
einsum
requires a parameter like "abcd..." which seems unnecessary.
kron
requires what seems like rather complex reshaping, and takes only two arguments.
I'd like to avoid as much complexity as possible, so as to avoid introducing bugs. So preferably I would like a single command.
So far, the best I have some up with is:
vs = [v1, v2, v3 ...]
shape = map(len, vs)
# specify the orientation of each vector
newshapes = diag(array(shape)-1)+1
reshaped = [x.reshape(y) for x,y in zip(vs, newshapes)]
# direct product
A = reduce(lambda a,b: a*b, reshaped, 1)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…