Einsum.jl icon indicating copy to clipboard operation
Einsum.jl copied to clipboard

Additional functionality for offsets in index expressions #6

Open ntezak opened this issue 7 years ago • 7 comments

This introduces additional capabilities for handling multiple different offsets for RHS index expressions as well as a modified convention for un-allocated negative indices.

ntezak avatar Sep 06 '16 19:09 ntezak

I would be happy to add documentation and tests.

ntezak avatar Sep 06 '16 19:09 ntezak

Actually, I think this convention for determining the allocation size is still not quite clean, let me think about it a little more.

ntezak avatar Sep 06 '16 19:09 ntezak

Documentation for the offsets would be greatly appreciated!

ahwillia avatar Sep 06 '16 20:09 ahwillia

Actually, I think this convention for determining the allocation size is still not quite clean

Yes, I agree. Cross-referencing from your examples in https://github.com/ahwillia/Einsum.jl/issues/6#issuecomment-245061487

I think I would prefer:

X = randn(10)

@einsum A[i] := X[i-5]
@test size(A) == (5,)
@test all(A .== X[6:end])

Does that make sense? Thanks so much for catching my oversights!

ahwillia avatar Sep 06 '16 20:09 ahwillia

Hey Alex, that would be an option but then there would be a marked difference between the behavior of a pre-allocated array (in which case I don't think we should shift the LHS indices) and a non-pre-allocated array. I think the simplest thing may just be to not allow any offset indices in combination with :=, i.e., non-pre-allocated expressions. What do you think?

ntezak avatar Sep 15 '16 17:09 ntezak

What are the desired rules for pre-allocated arrays?

B = collect(1:10)

# produce errors?
A = zeros(5)
@einsum A[i] = B[i+5]
@einsum A[i] = B[i-5]

# legal?
A = zeros(10)
@einsum A[i] = B[i+5]
A == [6, 7, 8, 9, 10, 0, 0, 0, 0, 0]

A = zeros(10)
@einsum A[i] = B[i-5]
A == [0, 0, 0, 0, 0, 1, 2, 3, 4, 5]

If this is the case, then I guess it would make sense to allocate A to be the size of B in all these cases and the behavior would be consistent.

Side note. This is silly, but would be kind of cool to support this:

B = collect(1:10)
A = zeros(10)
@einsum A[i] = B[i>>5]
A == [6, 7, 8, 9, 10, 1, 2, 3, 4, 5]

@einsum A[i] = B[i<<2]
A == [3, 4, 5, 6, 7, 8, 9, 10, 1, 2]

ahwillia avatar Sep 17 '16 07:09 ahwillia

In case someone is watching this: index shifts of the form @einsum A[i] := X[i+:offset] seem to be broken on Julia 1.0. This feature never made it into the readme but was in the tests... and in #29 I commented these out. It should still work as before on 0.6, but someone who needs this would have to fix it on 1.0.

mcabbott avatar Dec 28 '18 17:12 mcabbott