ivy icon indicating copy to clipboard operation
ivy copied to clipboard

element-wise each?

Open tcolgate opened this issue 1 year ago • 1 comments

Ivy's @ cannot (I think) currently do element-wise each. e.g. In TryAPL:

(⍳ 4 4) ,¨ (⍳ 4 4)
┌───────┬───────┬───────┬───────┐
│1 1 1 1│1 2 1 2│1 3 1 3│1 4 1 4│
├───────┼───────┼───────┼───────┤
│2 1 2 1│2 2 2 2│2 3 2 3│2 4 2 4│
├───────┼───────┼───────┼───────┤
│3 1 3 1│3 2 3 2│3 3 3 3│3 4 3 4│
├───────┼───────┼───────┼───────┤
│4 1 4 1│4 2 4 2│4 3 4 3│4 4 4 4│
└───────┴───────┴───────┴───────┘

So A f¨ B implies pairwise (A[1 1] f B[1 1]) (B[1 2] f B[2 2])..... In ivy A f@ B does (A f B[1]) A f B[2]. @f@ does a full 'cross product' of all elements.

Most of the built in ops will operate elementwise anyway, but for those that don't (like ,), or for user defined ops, I've had to resort to reshaping as a flat vector, pairing them up, applying a unary function that takes the pairs, and then reshaping the result.

Is an elementwise version of @ achievable?

tcolgate avatar Jan 10 '25 11:01 tcolgate

Indeed, this is what I meant by https://github.com/robpike/ivy/issues/69#issuecomment-2539900591. I'd like to think more about this before suggesting anything.

rsc avatar Jan 10 '25 17:01 rsc