Reputation: 13436
I have a matrix M
containing pairs (i.e. arrays of size 2). Given a pair p
, how can I get a 2D matrix M'
containing the result of the scalar product of p
and each element of M
?
(0, 0) (0, 1) 8 5
M = (1, 0) (1, 1) p = (2, 2) M' = 5 2
(2, 0) (2, 1) 4 1
M
and p
are defined :
M =. 3 2 2 $ 0 0 0 1 1 0 1 1 2 0 2 1
p =. 2 2
I have an implementation of scalar product in J :
sp =. +/ @: *: @: -
It works on pairs :
0 0 sp p
8
0 1 sp p
5
But not on the full matrix, because of bad length :
p sp M
|length error: sp
| p sp M
How should I deal with that ? This is probably easy for J geniuses, but I'm just a poor newbie.
Upvotes: 1
Views: 93
Reputation: 9133
Use
p sp"1 M
8 5
5 2
4 1
You want sp
here to read first-rank cells of M:
<"1 M
┌───┬───┐
│0 0│0 1│
├───┼───┤
│1 0│1 1│
├───┼───┤
│2 0│2 1│
└───┴───┘
so sp"1
will work on each cell (p sp 0 0
, p sp 0 1
, etc). sp
(infinite rank) tries to apply sp once for the whole matrix M.
Likewise sp"2
will apply sp
on second-rank cells of M:
<"2 M
┌───┬───┬───┐
│0 0│1 0│2 0│
│0 1│1 1│2 1│
└───┴───┴───┘
so
p sp"2 M
8 5
2 5
0 5
(the pairs: p sp 2 2 $ 0 0 0 1
, p sp 2 2 $ 1 0 1 1
and p sp 2 2 $ 2 0 2 1
)
Upvotes: 3