How about:

tensor←{((⍴⍺)×⍴⍵)⍴(⍋⍋0 1⍴⍨(⍴⍴⍺)+⍴⍴⍵)⍉⍺∘.×⍵}

I think this is a more elegant generalisation than using ⍣. It should
also work in 3 or more dimensions.

The left argument of the dyadic transpose needs to be 1 2 (for
vectors), 1 3 2 4 (for matrices), 1 4 2 5 3 6 (for cubes) etc. This is
an interleaving of the numbers 1 2 3 with 4 5 6. For the ⍋⍋ trick used
to do the interleaving, see: https://dfns.dyalog.com/n_ripple.htm

Jay.

On 24 January 2016 at 20:40, Tobia C. <[email protected]> wrote:
> I'm studying tensor products[1] and I came up with these two definitions,
> resp. for the operation between vectors and between matrices:
>
> tensor1←{,⍺∘.×⍵}
> tensor2←{,[1 2],[3 4]1 3 2 4⍉⍺∘.×⍵}
>
> Jay's version is more efficient, because it performs a single reshape, but
> it needs (⍴⍺)×⍴⍵, not (⍴⍺)+⍴⍵:
>
> tensor2←{((⍴⍺)×⍴⍵)⍴1 3 2 4⍉⍺∘.×⍵}
>
> It only works in Wikipedia's example because (2+2)=2×2
>
> Can anybody suggest an idiomatic way to combine tensor1 and tensor2 into a
> single function? (Without using multi-statement functions or non-GNU APL
> syntax.)
>
> Tobia
>
> [1] on this very interesting book: Yanofsky, Mannucci - Quantum Computing
> for Computer Scientists

Reply via email to