1
$\begingroup$

Say if I have two lists of vectors of the same length, for example:

u = {{1,0,0},{0,1,0},{0,0,1}}
v = {{1,0,1},{1,1,0},{1,1,1}}

I want to add (modulo 2) u1 + v and then u2 + v and then u3 + v, so I want to obtain:

u1 + v = {{0,0,1},{1,0,0},{1,1,0}}
u2 + v = {{1,1,1},{1,0,0},{1,1,0}}
u3 + v = {{1,0,0},{1,1,1},{1,1,0}}

The only way I can do this so far is:

Mod[u[[1]]+v[[#]],2]&/@Range[Length[u]]
Mod[u[[2]]+v[[#]],2]&/@Range[Length[u]]
Mod[u[[3]]+v[[#]],2]&/@Range[Length[u]]

However, this involves manual/clunky computation. Is it possible to iterate through the positions of u as well as iterating through the position of v, so that I would be able to calculate this in one go without writing out all the equations?

This is a frequent problem I encounter where I want to define two pure functions to be mapped over different lists in the same line of code. Is it possible?

$\endgroup$
5
  • 7
    $\begingroup$ Does Outer[Mod[Plus[##], 2] &, u, v, 1] do what you want? $\endgroup$ Sep 20, 2023 at 14:46
  • 3
    $\begingroup$ For your current attempt: You can map over the elements of v themselves, without having to take the roundtrip over indices: Mod[u[[1]]+#,2]&/@v $\endgroup$
    – Lukas Lang
    Sep 20, 2023 at 14:55
  • $\begingroup$ @LukasLang That is certainly a better way of doing it! I wonder then if there is a way to iterate through the index of u simultaneously? $\endgroup$
    – am567
    Sep 20, 2023 at 15:08
  • $\begingroup$ @J.M.'slackofA.I. This is exactly what I was looking to do, thank you! I am still interested as to whether it can be done in the format I'd tried, although my suspicion is that I was just going about it the wrong way. $\endgroup$
    – am567
    Sep 20, 2023 at 15:10
  • 2
    $\begingroup$ Also: Table[Mod[Plus[u[[i]], v[[j]]], 2], {i, #1}, {j, #2}] & @@ Rest@Dimensions@{u, v} $\endgroup$ Sep 20, 2023 at 19:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.