# Replacing an element in table with indices other than i & j

I have a function F, of four other f functions, as shown below (1, 2, 3, 4 are indices here, while m is an integer)

F = f[m, 1]*f[m, 2]*f[m, 3]*f[m, 4]


I want to create a table using this function such that for each (i, j)th element f[m, i], f[m, j] and other f[m, k] (where k is a number other than i & j in (i,j)th element) elements are replaced by f[m-2, i], f[m-2,j] and f[m-1, k] respectively.

Replacing f[m, i] and f[m, j] is easy as follows, but I am not able to figure out how to represent the third replacement for indices other than i and j in F.

G = Table[F/.{f[m, i] -> f[m-2, i], f[m, j] -> f[m-2, j]}, {i, 1, 4}, {j, 1, 4}]


In my previous post, I got the suggestion to use "_" or "Except[i|j]" for k. These suggestion like

f[m, _] -> f[m-1, _]


successfully converts m to m-1 but then k is also replaced by symbols "_" or "Except[i|j]" in stead of giving a value.

For example f[m, 4] in {1, 2}th element of table gives f[m-1, _], not f[m-1, 4] as expected. I will appreciate any further help on this. thanks

• what is an acceptable range for m? Nov 15, 2017 at 8:34
• This table I have to use in "For" function later on where m = 2 - 6. Nov 15, 2017 at 9:11

When we type f[m,k] /. f[m,_] -> f[m-1,_] at the prompt, we get back f[m-1,_], which does not seem too surprising. The trick is to name the blank and use the name in place of the blank on the right hand side of the ->, like this:

f[m,k] /. f[m,x_] -> f[m-1,x]
(*  f[-1+m, k]  *)


However, that is still not exactly what we need, since x could have a value and we want the value of the blank named x, not the variable x. So, what we really want is to use RuleDelayed (:>), as

f[m,k] /. f[m,x_] :> f[m-1,x]
(*  f[-1+m, k]  *)


That works even when x has a value.

In other words, try this:

F = f[m, 1]*f[m, 2]*f[m, 3]*f[m, 4];

G = Table[F /. {f[m, i] -> f[m - 2, i], f[m, j] -> f[m - 2, j],
f[m, x_] :> f[m - 1, x]}, {i, 1, 4}, {j, 1, 4}]


Define the F function as follows

F[m_,i_,j_]:=Module[{rls},
rls = {
f[arg1_, arg2_] :> f[arg1 - 2, arg2] /; MemberQ[{i, j}, arg2],
f[arg1_, arg2_] :> f[arg1 - 1, arg2] /; Not[MemberQ[{i, j}, arg2]]
};

Fold[ReplaceAll[#1, #2] &, f[m, 1] f[m, 2] f[m, 3] f[m, 4], rls]
]


Evaluating F[m,1,3] returns

f[-2 + m, 1] f[-2 + m, 3] f[-1 + m, 2] f[-1 + m, 4]


Effectively, what the solution does is to apply repeatedly the transformation rules for f[m,i]; it first tackles the case where i,j's in the f's are equal to the inputs and then deals with all the remaining cases (ie those indexes not equal to either of the inputs).

You can use a single replacement rule as follows

G = Table[F /. f[m, k_] :> f[m - 1 - Boole[MemberQ[{i, j}, k]], k], {i, 1, 4}, {j, 1, 4}]


Also

H = Table[F /. f[m, k_] :> f[m - 1 - (k /. {i | j -> 1, _ -> 0}), k], {i, 1, 4}, {j, 1, 4}]


and

ClearAll[h]
h[i_, _, i_] := 2
h[_, i_, i_] := 2
h[_, _, _] := 1

M = Table[F /. f[m, k_] :> f[m - h[i, j, k], k], {i, 1, 4}, {j, 1, 4}];

G == H == M


True