# Generate an array with custom index

Apologies if this is a duplicate. I am trying (but failing) to generate the following n x n matrix in Mathematica: where G[tj, t0] is a function along the main diagonal and G[tj, tj] is a function filling the remainder.

I'm trying to use Map, but am having difficulties specifying the arguments.

For example, using n = 5:

Array[g[#, #2] &, {5, 5}, {{0, 1}, {0, 1}}]


Another possibility using a single Table:

n = 5;
Table[
If[i==j, OverTilde[G], G][t[i], t[Mod[i-j, n]]],
{i, 0, n-1},
{j, 0, n-1}
] //MatrixForm //TeXForm


$$\left( \begin{array}{ccccc} \tilde{G}(t(0),t(0)) & G(t(0),t(4)) & G(t(0),t(3)) & G(t(0),t(2)) & G(t(0),t(1)) \\ G(t(1),t(1)) & \tilde{G}(t(1),t(0)) & G(t(1),t(4)) & G(t(1),t(3)) & G(t(1),t(2)) \\ G(t(2),t(2)) & G(t(2),t(1)) & \tilde{G}(t(2),t(0)) & G(t(2),t(4)) & G(t(2),t(3)) \\ G(t(3),t(3)) & G(t(3),t(2)) & G(t(3),t(1)) & \tilde{G}(t(3),t(0)) & G(t(3),t(4)) \\ G(t(4),t(4)) & G(t(4),t(3)) & G(t(4),t(2)) & G(t(4),t(1)) & \tilde{G}(t(4),t(0)) \\ \end{array} \right)$$

Update: Slightly streamlined version:

n = 5;
a = Array[Subscript[t, #] & /@ {##} &, {n, n}, {{0, n - 1}, {n - 1, 0}}];

m2 = MapIndexed[G @@@ RotateRight[#, #2[]] &, a] /.
p:G[_, _[_, 0]] :> Operate[OverTilde, p]

m2 == m


True

n = 5;
ta = Array[Subscript[t, #] &, n, 0];
tb = RotateRight[ta, #] & /@ Range[0, n - 1];
m = Apply[G, Transpose[Transpose[{ta, #}] & /@ tb], {-3}];
m = ReplacePart[m, {i_, i_} :> (m[[i, i]] /. G -> OverTilde[G])];

TeXForm @ MatrixForm @ m


$$\left( \begin{array}{ccccc} \tilde{G}\left(t_0,t_0\right) & G\left(t_0,t_4\right) & G\left(t_0,t_3\right) & G\left(t_0,t_2\right) & G\left(t_0,t_1\right) \\ G\left(t_1,t_1\right) & \tilde{G}\left(t_1,t_0\right) & G\left(t_1,t_4\right) & G\left(t_1,t_3\right) & G\left(t_1,t_2\right) \\ G\left(t_2,t_2\right) & G\left(t_2,t_1\right) & \tilde{G}\left(t_2,t_0\right) & G\left(t_2,t_4\right) & G\left(t_2,t_3\right) \\ G\left(t_3,t_3\right) & G\left(t_3,t_2\right) & G\left(t_3,t_1\right) & \tilde{G}\left(t_3,t_0\right) & G\left(t_3,t_4\right) \\ G\left(t_4,t_4\right) & G\left(t_4,t_3\right) & G\left(t_4,t_2\right) & G\left(t_4,t_1\right) & \tilde{G}\left(t_4,t_0\right) \\ \end{array} \right)$$

Something like this?

Table[
Table[G[t[j], t[k]], {k, j, 0, -1}]~Join~Table[G[t[j], t[i]], {i, n - 1, j + 1, -1}],
{j, 0, n - 1}
]


For example, n = 4

{
{G[t, t], G[t, t], G[t, t], G[t, t]},
{G[t, t], G[t, t], G[t, t], G[t, t]},
{G[t, t], G[t, t], G[t, t], G[t, t]},
{G[t, t], G[t, t], G[t, t], G[t, t]}
}


which seems to follow the pattern.

Using nested Tables like this usually isn't suggested, so I'll keeping working on a better method, but for now it seems to work.

You can also use SparseArray:

n=4;
Normal@SparseArray[
{
{i_,i_}->OverTilde[G][i-1,0],
{i_,j_}->G[i-1,Mod[i-j,n]]
},
{n,n}
]//MatrixForm//TeXForm


$$\left( \begin{array}{cccc} \tilde{G}(0,0) & G(0,3) & G(0,2) & G(0,1) \\ G(1,1) & \tilde{G}(1,0) & G(1,3) & G(1,2) \\ G(2,2) & G(2,1) & \tilde{G}(2,0) & G(2,3) \\ G(3,3) & G(3,2) & G(3,1) & \tilde{G}(3,0) \\ \end{array} \right)$$