Suppose I have two sparse arrays
L = SparseArray[{{1, 1} -> 1, {1, 5} -> 4, {5, 1} -> 1, {5, 5} ->
2, {10, 10} -> 2, {10, 14} -> 2, {10, 18} -> 1, {14, 10} ->
2, {14, 14} -> 2, {14, 18} -> 1, {18, 10} -> -1, {18, 14} ->
1, {18, 18} -> 1, {19, 19} ->
1, {19, 23} -> -1, {19, 27} -> -1, {23, 19} -> 1, {23, 23} ->
2, {23, 27} -> 1, {27, 19} -> 1, {27, 23} -> 1, {27, 27} -> 1}];
and
R = SparseArray[{{1, 1} -> -1, {1, 5} -> 3, {5, 1} -> 1, {5, 5} ->
2, {10, 10} -> 2, {10, 14} -> 2, {10, 18} -> 1, {14, 10} ->
2, {14, 14} -> 2, {14, 18} -> -1, {18, 10} -> -2, {18, 14} ->
4, {18, 18} -> 1, {19, 19} ->
1, {19, 23} -> -1, {19, 27} -> -3, {23, 19} -> 1, {23, 23} ->
2, {23, 27} -> 1, {27, 19} -> 1, {27, 23} -> 1, {27, 27} -> 1}];
My problem is to construct another sparse matrix whose entries are coming from the above matrices with some specific properties. (Well I just need to get the ArrayRules
, obviously). For instance, I need to get the sparse matrix P
whose nonzero entries are union of the positive entries entries of L
and R
, i.e. if any position of L
(resp. R
) has a positive entry, P
should also have a positive entry. Now the union of the positions can be taken easily by
Union[Cases[ArrayRules[L, (r : (_ -> x_ /; Positive[x])) -> r], Cases[ArrayRules[R, (r : (_ -> x_ /; Positive[x])) -> r]]
But here is the problem. Notice that, the entry {1,5}->4
in the first list and {1,5}->3
in the second list (and sometimes the opposite). If such a situation (or say collision) happens, then the position {1,5}
of the output sparse matrix should have the maximum of the all possible values (here 4). Is it possible to write it neatly? I also need to make a sparse matrix N
, whose non zero entries are common nonzero entries of L
and R
, with the caveat that for a collision, as defined above, the corresponding entry is the minimum of all possible entries.
Advanced thanks for any help.