Stream: helpdesk (published)

Topic: Tullio square elements


view this post on Zulip Dale Black (Feb 25 2021 at 23:38):

Is there a way to create a new array of squared elements using the @tullio einsum functionality?

My first attempt was:

@tullio M[x, y, z] :=  A[x, y, z] * A[x, y, z]

But this doesn't seem to work like I thought it would

view this post on Zulip Dale Black (Feb 25 2021 at 23:40):

I guess my question is can I do A .^ 2 using Tullio and einsum notation?

view this post on Zulip Mason Protter (Feb 25 2021 at 23:57):

What did you expect for that to do? It's the same as A .^ 2 for me.

julia> A = collect(reshape(1:(2^3), 2, 2, 2))
2×2×2 Array{Int64, 3}:
[:, :, 1] =
 1  3
 2  4

[:, :, 2] =
 5  7
 6  8

julia> using Tullio

julia> @tullio M[x, y, z] :=  A[x, y, z] * A[x, y, z]
2×2×2 Array{Int64, 3}:
[:, :, 1] =
 1   9
 4  16

[:, :, 2] =
 25  49
 36  64

julia> M == A .^ 2
true

view this post on Zulip Alex Ames (Feb 26 2021 at 00:22):

@tullio M[i,j,k] := A[i,j,k]^2 works just as well

view this post on Zulip Dale Black (Feb 26 2021 at 00:26):

Hmm, I cannot recreate the error so I am not sure what I was doing wrong originally. Thank you both though, this works


Last updated: Nov 22 2024 at 04:41 UTC