I'm looking to build a $7\times3$ matrix using ChineseRemainder. Trying to use

${0,1,2,3,4,5,6} \pmod 7$


${3,15,21} \pmod {30}$

so that the first element of the matrix is the answer to

$0 \pmod 7$


$3 \pmod {30}$

or $63$. Thus in ChineseRemainder form,

ChineseRemainder[{0, 3}, {7, 30}]

How would I go about writing this? I figured I'd have to use Outer with ChineseRemainder but can't seem to make it work.

  • 3
    $\begingroup$ Perhaps Outer[ChineseRemainder[{##}, {7, 30}] &, {0, 1, 2, 3, 4, 5, 6}, {3, 15, 21}] ? $\endgroup$
    – ilian
    Apr 11, 2016 at 16:54
  • $\begingroup$ @ilian That did it! Added MatrixForm and it spit out what I needed! Could you maybe put this as an answer and include a description of what the ## and & do in coordination with Outer? I'll gladly mark as best answer. $\endgroup$ Apr 11, 2016 at 17:49

2 Answers 2


As already mentioned in the question, we can use Outer, for example (with a $3 \times 2$ matrix)

Outer[f, {x1, x2, x3}, {y1, y2}]

{{f[x1, y1], f[x1, y2]}, {f[x2, y1], f[x2, y2]}, {f[x3, y1], f[x3, y2]}}

so we just need a function f for which f[x, y] returns ChineseRemainder[{x, y}, {7, 30}].

That function could be defined simply as

f[x_, y_] := ChineseRemainder[{x, y}, {7, 30}]

and then the desired result is

Outer[f, {0, 1, 2, 3, 4, 5, 6}, {3, 15, 21}]

It is slightly shorter to use a pure (or anonymous) Function where ##, or SlotSequence, denotes the sequence of arguments, e.g.

g[{##}, {m, n}] & [x, y]

(* g[{x, y}, {m, n}] *)

where the head g could be, say ChineseRemainder. Putting this together,

mat = Outer[ChineseRemainder[{##}, {7, 30}] &, {0, 1, 2, 3, 4, 5, 6}, {3, 15, 21}]

and of course mat can be displayed as a matrix by MatrixForm[mat].


Tuples can be a useful alternative to Outer,e.g. the 7, 30 case:

r = Range[0, 6];
c = {3, 15, 21};
res = ChineseRemainder[{#1, #2}, {7, 30}] & @@@ Tuples[{r, c}];
TableForm[Partition[res, 3], TableHeadings -> {r, c}]

enter image description here

  • $\begingroup$ This is great! Very neat, orderly, and well labeled! I'm a little confused tho, on what exactly it does, and at present wouldn't be build something similar on my own. I get r and c (and might change r as follows "n=7; r=Range[0,n-1];" then use "{n,30}" instead of {7,30} so both will be set as I set a value for "n".) For "res", are #1 and #2 referring to the above lines for "r" and "c"? What does "& @@@ Tuples" do? Does "Partition" define the number of columns? I know I could look this up, but there's enough here I don't fully get as a newbie, I'd be a little overwhelmed. Thanks $\endgroup$ Apr 12, 2016 at 19:14
  • 1
    $\begingroup$ @Elem-Teach-w-Bach-n-Math-Ed thank you. Apologies for delay in replying: timezone. Tuples just does what outer does but flattens the list. @@@ is useful. It changes the head of list. ,e.g. f@@@{{a,b},{c,d}}->{f[a,b],f[c,d]}. Just dive in and play. There are many ways to get things done. Sometimes terse looks nice but is inefficient. I learn a lot from MSE. So have fun. :) $\endgroup$
    – ubpdqn
    Apr 12, 2016 at 22:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.