I have an expression a which include parts of the form A[i,j], where i,j take a range of values. What I want to do is use a replacement rule on the whole expression whenever such a term is present. Currently my code looks something like
If[(! FreeQ[a, A[i_,j_]]), ReplaceRepeated[a, {a:-> f[a, i, j]}]] where f[a,i,j] is something that I want to depend on the specific values i,j that appear in a.
The initial check using FreeQ is working, i.e. it identifies whenever A[i_,j_] is present, for arbitrary i,j. However, I'm then not sure how to proceed, because I can't find a way to identify the specific values of i,j that match the pattern.
For example, if a=... + A[1,2] + ..., then I want the replacement a -> f[a,1,2]. My code currently would replace a -> f[a,i,j], and I can't work out how to obtain the specific values of i,j.
Edit: Some more concrete examples are
a = A[1,2]B[1,2] + C[1,2]D[1,2] a = (A[2,4]B[2,4] + C[2,4]D[2,4])^2 (A[3,2]B[3,2] + C[3,2]D[3,2]) which I would like to turn into
f[1,2] f[2,4]^2 f[3,2] respectively. Essentially I know that if A[i,j] appears, then so will the B, C, D terms, and in that case I want to simplify the expressions.
In this case, the above code looks like
If[(! FreeQ[a, A[i_,j_]]), ReplaceRepeated[a, {(A[i,j]B[i,j] + C[i,j]D[i,j]):-> f[a, i, j]}]] Edit: A longer explanation about the second example. For the example
a = (A[2,4]B[2,4] + C[2,4]D[2,4])^2 (A[3,2]B[3,2] + C[3,2]D[3,2]) I am unable to directly use normal replacement rules, since the expression appears expanded, i.e. as
A[2, 4]^2 A[3, 2] B[2, 4]^2 B[3, 2] + 2 A[2, 4] A[3, 2] B[2, 4] B[3, 2] C[2, 4] D[2, 4] + A[3, 2] B[3, 2] C[2, 4]^2 D[2, 4]^2 + A[2, 4]^2 B[2, 4]^2 C[3, 2] D[3, 2] + 2 A[2, 4] B[2, 4] C[2, 4] C[3, 2] D[2, 4] D[3, 2] + C[2, 4]^2 C[3, 2] D[2, 4]^2 D[3, 2] When it is expanded like this, the replacement rule outlined in my code above can't be used directly.
To get around this, I have a function that factorises the expression as
(p // replacement rule) (a/p // Together) where using e.g. p = A[2,4]B[2,4] + C[2,4]D[2,4] will give me one of the factors I want. But to do this, I need to know which labels i,j are present, so that I can let them appear in p as required. This is why I am trying to extract the labels in this way, and can't use a direct replacement.
f[a,1,2]orA[[i,j]]$\endgroup$aas well as the desired outputs? $\endgroup$