3
$\begingroup$

I wish to create a grid of 31x31 points with coordinates of the form $(k/30,n/30)$ within the unit square, and give each point a colour based on the 2-adic value of both its coördinates.

I plan to use Wolfram Mathematica, as this program has a simple function to calculate the 2-adic value, but I do not know how to generate a grid in which I can assign colours to the points. If we write $v(x)$ as the 2-adic value of $x$, the colouring should be as follows:

$(x,y)$ is coloured:

  • Blue if $v(x) \geq v(y)$ and $v(x) \geq v(1)=1$
  • Green if $v(x) < v(y)$ and $v(y) \geq v(1)=1$
  • Red if $v(x) < v(1)=1 $ and $ v(y) < v(1)=1$

Can someone help me?

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$ – bbgodfrey Sep 26 '15 at 14:00
  • $\begingroup$ Please add any code you have tried. $\endgroup$ – bbgodfrey Sep 26 '15 at 14:01
  • $\begingroup$ Take a look at ArrayPlot. $\endgroup$ – gwr Sep 26 '15 at 14:02
7
$\begingroup$

I don't know much about 2-adic values, hence I took the code for it from this mathworld article. Here are a couple of solutions to do it:

Using Graphics

PadicNorm[x_Integer, p_Integer?PrimeQ] := p^(-IntegerExponent[x, p])
PadicNorm[x_Rational, p_Integer?PrimeQ] := 
 PadicNorm[Numerator[x], p]/PadicNorm[Denominator[x], p]
v[x_] := PadicNorm[x, 2]
Graphics[{
  Table[{PointSize@0.016,
    Which[
     v[k/30] >= v[n/30] && v[k/30] >= 1, Blue,
     v[k/30] < v[n/30] && v[n/30] >= 1, Green,
     v[k/30] < 1 && v[n/30] < 1, Red
     ],
    Point[{k/30, n/30}]
    },
   {k, 0, 30}, {n, 0, 30}
   ]
  }
 ]

Which produces the following: enter image description here

Using squares instead of points

Using appropriate substitutions on a ConstantArray and selectively changing the background color:

PadicNorm[x_Integer, p_Integer?PrimeQ] := p^(-IntegerExponent[x, p])
PadicNorm[x_Rational, p_Integer?PrimeQ] := 
 PadicNorm[Numerator[x], p]/PadicNorm[Denominator[x], p]
vx[j_] := PadicNorm[(j - 1)/30, 2]
vy[i_] := PadicNorm[(31 - i)/30, 2]
ReplacePart[
  ConstantArray[0, {31, 31}],
  {{i_, j_} :> 
    Item[" ", Background -> Blue] /; vx[j] >= vy[i] && vx[j] >= 1,
   {i_, j_} :> 
    Item[" ", Background -> Green] /; vx[j] < vy[i] && vy[i] >= 1,
   {i_, j_} :> Item[" ", Background -> Red] /; vx[j] < 1 && vy[i] < 1}
  ] // Grid

The functions vx and vy serve to easily convert between the numbering of the elements of a matrix and the required cartesian way of ordering coordinates. Here is the output: enter image description here

Output squares using Graphics

Slight modification of the previously used code with Graphics:

PadicNorm[x_Integer, p_Integer?PrimeQ] := p^(-IntegerExponent[x, p])
PadicNorm[x_Rational, p_Integer?PrimeQ] := 
 PadicNorm[Numerator[x], p]/PadicNorm[Denominator[x], p]
v[x_] := PadicNorm[x, 2]
Graphics[{
  Table[
   {
    Which[
     v[k/30] >= v[n/30] && v[k/30] >= 1, Blue,
     v[k/30] < v[n/30] && v[n/30] >= 1, Green,
     v[k/30] < 1 && v[n/30] < 1, Red
     ],
    Rectangle[{k/30, n/30}, {(k + 1)/30, (n + 1)/30}]
    },
   {k, 0, 30}, {n, 0, 30}
   ]
  }
 ]

And this is the output: enter image description here

$\endgroup$
  • $\begingroup$ Thank you very much, this is what I needed! Yet, is there a way to, instead of colouring adjacent squares, colour loose points in the unit square? $\endgroup$ – Pepijn Sep 26 '15 at 15:20
  • $\begingroup$ @Pepijn of course. Sorry I didn't understand that that's what you needed. See the edit. $\endgroup$ – glS Sep 26 '15 at 15:26
  • $\begingroup$ Thank you very much, this is exactly what I needed! $\endgroup$ – Pepijn Sep 26 '15 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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