I am struggling to find a way to “automate” this type of function call:
x = [rand() rand(); rand() rand()]
kron(x, x) #n=2
kron(x, x, x) #n=3
kron(x, x, x, x) #n=4
Basically, having a 2x2 matrix “x” I would like to use the kronecker product of x with itself n times.
Is there a way to achieve this, perhaps in a manner like:
Of course not with the repeat function, but perhaps with something else.
Thank you! “…” looks like an interesting and useful operator… haven’t encountered (or used) in other programming languages.
Hey @Zackyzz , glad you got your question answered! Also, welcome to the Julia Community – saw it was your first post here on Discourse! Hope you enjoy your time here!
Yea, this syntax is interesting – it is called splatting and you can learn more about it here: Essentials · The Julia Language… (I am a fan of it but sometimes, it is not as efficient as maps or otherwise).
I am a big fan of “splat” and “slurp” as Julia’s most comical terminology.
In the case of
kron and other associative operations, you can also do
reduce(kron, fill(x, n)). That’s useful to know for many other associative functions like
The reduce trick is useful when
n is large or not known at compile time to avoid the performance issues of splatting and slurping (
kronn(x,n)=n==2 ? kron(x,x) : kron(x, kronn(x,n-1))
kronn(x,n)= n==1 ? x : kron(x, kronn(x,n-1))
this is correct the above are incorrect (kron is not commutative)
kronn(x,n)= n==1 ? x : kron(kronn(x,n-1),x)