# Coloring with Hue for a function on a lattice grid

I wish to color a 2-dimensional lattice grid according to the value of a function at each lattice-node. More specifically, if I have 9 angles in a 3x3 array,

angles={{0, π, π}, {0, 0, π/2}, {π/2, 0, 3 π/3}}


then one can plot these angles on a lattice-grid by the following code in Mathematica:

angles = {{0, π, π}, {0, 0, π/2}, {π/2, 0, 3 π/3}};
GraphicsGrid[
Map[Graphics[{
LightGray, Circle[{0, 0}, 1],
Hue[#/(2 π), .6, .8], Thick, Arrowheads[Medium], Arrow[{{0, 0}, {Cos[#], Sin[#]}}]}] &,
angles, {2}]]


Here, the colouring is done using the Hue command according to the value of each angle. However, now I want to compute a function f[i,j]; to be more specific,

f[i_,j_]:=Cos[angles[[i + 1, j]] - angles[[i, j]]] + Cos[angles[[i, j+1]] - angles[[i, j]]];


with

angles[[n+1,i_]]:=angles[[1,i]];
angles[[i_,n+1]]:=angles[[i,1]];


i.e., the boundary conditions.

In the first code, Hue is used with the angles[[i,j]] (through #), which is probably straightforward. But is it possible to use f[i,j] instead, where f[i,j] is defined as above?

P.S. This question is related to: data visualization on a lattice grid

Thanks!

## dbm

P.S. The final code, thanks to BoLe's answers:

Clear["Global*"];
n := 10
angles = Table[RandomReal[{-Pi, Pi}], {i, n}, {j, n}];
f[here_, down_, right_] := Cos[down - here] + Cos[right - here]
g[list_, {i_, j_}] :=
Module[{m, n}, {m, n} = Dimensions[list]; {list[[i, j]],
If[i != m, list[[i + 1, j]], -list[[1, j]]],
If[j != n, list[[i, j + 1]], -list[[i, 1]]]}]
GraphicsGrid[
MapIndexed[
Graphics[{LightGray, Circle[{0, 0}, 1],
Hue[Rescale[f @@ g[angles, #2], {-2, 2}, {0, 1}]], Thin,
Arrowheads[Small], Arrow[{{0, 0}, {Cos[#], Sin[#]}}]}] &,
angles, {2}]]

-

Define f and apply it to every element of the matrix (that becomes three elements). Mod ensures periodic boundaries.

f[here_, down_, right_] := Cos[down - here] + Cos[right - here]

MapIndexed[
f @@ Extract[angles,
Mod[{#2, #2 + {1, 0}, #2 + {0, 1}}, 3, 1]] &,
angles, {2}]


(* @whuber example *)
angles = 2 Pi ImageData[

GraphicsGrid[Map[Graphics[{
Arrow[{{0, 0}, {Cos[#], Sin[#]}}]}] &, angles, {2}]]


Coloring according to f, which can be between -2 and 2 I think, rescaled to 0-to-1 for Hue.

GraphicsGrid[MapIndexed[Graphics[{
Hue[Rescale[
f @@ Extract[angles,
Mod[{#2, #2 + {1, 0}, #2 + {0, 1}}, 3, 1]],
{-2, 2}, {0, 1}]],
Arrow[{{0, 0}, {Cos[#], Sin[#]}}]}] &, angles, {2}]]


Replying to fourth comment: One way to code custom boundary conditions is to define a custom extraction function and place it in place of Extract.

g[list_, {i_, j_}] := Module[{m, n},
{m, n} = Dimensions[list]; {
list[[i, j]],
If[i != m, list[[i + 1, j]], -list[[1, j]]],
If[j != n, list[[i, j + 1]], -list[[i, 1]]]}]

GraphicsGrid[MapIndexed[Graphics[{
Hue[Rescale[f @@ g[angles, #2], {-2, 2}, {0, 1}]],
Arrow[{{0, 0}, {Cos[#], Sin[#]}}]}] &, angles, {2}]]

-
Thanks for the answer. I am not particularly interested in the function itself which I could have easily coded up. The thing is, I want to color the arrows according to the values of f at each site. i.e., instead of Hue[#/(2Pi)] in the main code, I want to insert something like Hue[F[[i,j]]]. Sorry for not explaining the question well enough. –  dbm Apr 25 '13 at 16:48
@dbm, you might want to take note of the fact that most of the coloring functions only accept arguments within the interval $[0,1]$ (well, Hue[] is periodic, but its primary domain is still $[0,1)$). Your f doesn't seem to have the correct scaling. Maybe you should specify your desired coloring function... –  Guess who it is. Apr 25 '13 at 16:50
@dbm See, I merely inserted the f-@@-applying lines from above for coloring. Feel modular. :> –  BoLe Apr 25 '13 at 17:09
@BoLe, The edited comment is almost what I wanted, except that it would be great if the rescaling is [-1,1] for the function. Yes, the function has random [-2,2]. I can just divide the function by 2, and hence it gets rescaled to [-1,1]. Moreover, I would still prefer to have boundary conditions explicitly defined as I also wish to use anti-periodic boundary conditions, i.e., angles[[i_,n+1]]:= -angles[[i,1]] and angles[[n+1,i_]]:= -angles[[1,i]]. Btw, could we put the Circles around the arrows back? –  dbm Apr 25 '13 at 17:16
@dbm I reckon you are able enough to mix all the offered code into something with circles. ;-) As for the custom boundary conditions ... you could always If` the code. –  BoLe Apr 25 '13 at 17:22