I have no idea what you’re actually trying to do, but this (inelegant) lambda
works:
tensor←{ ,[1 2] { { ,[3 4] 1 3 2 4 ⍉ ⍵} ⍣ (2 < ⍴⍴⍵) ⍵ } ⍺ ∘.× ⍵}
Basically, it conditionally executes the transpose and first ravel if the
result of the outer product is of rank greater than 2.
If you don’t mind having a stray variable floating around, this is more
readable:
tensor←{ ,[1 2] { ,[3 4] 1 3 2 4 ⍉ ⍵} ⍣ ( 2 < ⍴⍴T ) T ← ⍺ ∘.× ⍵}
Best regards!
Louis
> On 24 Jan 2016, at 21: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