Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best way to tensor reshaping #46

Open
ho-oto opened this issue Jun 11, 2018 · 2 comments
Open

Best way to tensor reshaping #46

ho-oto opened this issue Jun 11, 2018 · 2 comments

Comments

@ho-oto
Copy link
Contributor

ho-oto commented Jun 11, 2018

I often use @tensor macro and reshape together, e.g.:

@tensor A[a,e,c,g,f] := B[a,b] * C[b,c,d] * D[d,e,f,g]
A = reshape(A, size(A)[1]*size(A)[2]*size(A)[3], size(A)[4]*size(A)[5]) 

I think this code is inelegant and wasting memory. I want to use more elegant format like

@tensor A[(a,e,c),(g,f)] := B[a,b] * C[b,c,d] * D[d,e,f,g]

instead.
Or, some good way just I don't know already exists?

@Jutho
Copy link
Owner

Jutho commented Jun 12, 2018

This would indeed be a possibility which I have also considered, but I have not implemented it yet. Maybe at some point. One issue that holds me back is how to do/specify the reverse reshaping, i.e. where a single dimension of the tensor gets split into two or more, with easy syntax.

@mcabbott
Copy link

mcabbott commented Jan 8, 2019

I made a package which does this, and some other things. The notation you suggest works, as does the more compact \ for combining indices:

using TensorOperations; using TensorSlice
B = rand(2,3); C = rand(3,4,5); D = rand(5,6,7,8);
@tensor A[a,e,c,g,f] := B[a,b] * C[b,c,d] * D[d,e,f,g];

@shape Z[a\e\c, g\f] := A[a,e,c,g,f];

Unlike @tensor it doesn’t do any of the work, it just writes the same reshape() command you did. Which I believe should not allocate much memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants