Also, here's an identity tensor generator for this version of tip: tid=:3 :0 (y,y)$,=,i.y )
Example use: (i.3 4 5)-:(i.3 4 5) 2 tip tid 4 5 1 I hope this helps, -- Raul On Tue, Jun 29, 2021 at 4:03 PM Raul Miller <rauldmil...@gmail.com> wrote: > > In http://jsoftware.com/pipermail/programming/2021-June/058315.html > Viktor Grigorov mentioned a need for a tensor product. > > I have been trying to think of a good simple expression, but there's > special cases. > > Still, I figured maybe someone might make use of this: > > tip=: {{ > assert. 1=#,m > select. m=. {.,m > case. 0 do. */ > NB. case. 1 do. +/ .* > NB. case. 2 do. > NB. +/@(,/)@(*"2 _) > NB. case. _ do. > NB. +/@,@(*"_) > case. do. > +/@(,/^:(m-1))@(*"(m,_)) > end. > }} > > Example use: > > (i.2 3) 1 tip i.3 4 > 20 23 26 29 > 56 68 80 92 > (i.2 3) +/ .* i.3 4 > 20 23 26 29 > 56 68 80 92 > (i.2 3 5) 2 tip i.3 5 7 > 7105 7210 7315 7420 7525 7630 7735 > 18130 18460 18790 19120 19450 19780 20110 > > Basically, the m argument to tip (tensor inner product) is the number > of pairs of dimensions in the argument arrays which should be summed > in the tensor product. > > You can use _ if you want all dimensions to be paired. > > The commented out lines in the definition could be removed or instated > (with the NB. toggled out) and that should make no difference in the > result, other than a slightly different intermediate representation of > the inner product. > > As you can see in my above quicky examples, the m argument to tip is > the number of (inner) dimensions which must match between the two > array arguments. These dimensions vanish from the shape of the result. > > I hope this makes sense to someone... > > -- > Raul ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm