If I have a list of numbers l = {256/11, 258/11, 263/11, 263/11, -22, -22, 251/11, 0, 261/11, -22, 265/11, 259/11, 0, 259/11} which I want to evaluate mod 26, how do I do that?
I know I can do Mod[l, 26], but that just gives me {256/11, 258/11, 263/11, 263/11, 4, 4, 251/11, 0, 261/11, 4, 265/11, 259/11, 0, 259/11} without evaluating the fractions. How do I perform this calculation so that every element is an integer mod 26?
Asked
Active
Viewed 473 times
3
Matt G
- 349
- 2
- 9
2 Answers
5
PowerMod can help:
fracMod = Mod[Numerator[#] * PowerMod[Denominator@#, -1, 26], 26] &;
fracMod@{256/11, 258/11, 263/11, 263/11, -22, -22, 251/11, 0,
261/11, -22, 265/11, 259/11, 0, 259/11}
(* {2, 14, 5, 5, 4, 4, 11, 0, 19, 4, 17, 7, 0, 7} *)
Addendum -- General-purpose function
ClearAll[ratMod];
SetAttributes[ratMod, Listable];
ratMod[Rational[n_, d_], m_] := Mod[n*PowerMod[d, -1, m], m];
ratMod[n_Integer, m_] := Mod[n, m];
ratMod[m_][x_] := ratMod[x, m]; (* operator form *)
OP's example:
ratMod[26]@ {256/11, 258/11, 263/11, 263/11, -22, -22, 251/11, 0,
261/11, -22, 265/11, 259/11, 0, 259/11}
(* {2, 14, 5, 5, 4, 4, 11, 0, 19, 4, 17, 7, 0, 7} *)
Listability with the operator form requires Through:
ratMod[{13, 26}]@ {256/11, 258/11, 263/11, 263/11, -22, -22, 251/11, 0,
261/11, -22, 265/11, 259/11, 0, 259/11} // Through
(*
{{2, 1, 5, 5, 4, 4, 11, 0, 6, 4, 4, 7, 0, 7},
{2, 14, 5, 5, 4, 4, 11, 0, 19, 4, 17, 7, 0, 7}}
*)
Michael E2
- 235,386
- 17
- 334
- 747
3
I guess what Mod[a/b, c] means is that there is an integer z such that Mod[b*z,c] is equal to Mod[a, c]. Assuming this, consider the first fraction 256/11. You can find the integer z using
FindInstance[Mod[11*z, 26] == Mod[256, 26], z, Integers]
{{z -> 2}}
You can also replace FindInstance by Solve or by Reduce. You can turn this into a function straightforwardly and apply it to your list l:
frac[q_] := First@FindInstance[
Mod[Denominator[q]*z, 26] == Mod[Numerator[q], 26], z, Integers];
sol = frac[#] & /@ l;
z /. sol
{2, 14, 5, 5, 4, 4, 11, 0, 19, 4, 17, 7, 0, 7}
bill s
- 68,936
- 4
- 101
- 191