I have a 3x3 matrix of functions. It looks like


(It's symmetric on the first two arguments.) I want to replace all of the k1[i,j] functions with different functions. What I want to do is, basically

  k1[a, b] -> (1/c^4 k1c4[a, b][#] &), {a, 1, 3}, {b, 1, 3}]]

or with slightly different syntax

   k1[a, b] -> Function[xx,1/c^4 k1c4[a, b][xx]], {a, 1, 3}, {b, 1, 3}]]

Neither of these work. Instead of a matrix


I get a matrix


which is meaningless because a and b aren't defined anywhere.

How can I make the replacement list I want using something like Table?


1 Answer 1

SetAttributes[k1, Orderless]; 
kk1[xx_] := Array[k1[##][xx] &, {3, 3}]

{{k1[1, 1][xx], k1[1, 2][xx], k1[1, 3][xx]},
{k1[1, 2][xx], k1[2, 2][xx], k1[2, 3][xx]},
{k1[1, 3][xx], k1[2, 3][xx], k1[3, 3][xx]}}

You can use With in the first argument of Table to inject values of the iterator variables a and b into the rule elements:

rules = Flatten[ Table[With[{a = a, b = b}, k1[a, b] -> (1/c^4 k1c4[a, b][#] &)], 
 {a, 1, 3}, {b, 1, 3}]];

kk1[[x]] /. rules

enter image description here

A simpler alternative is to use Patterns to define the replacement rule:

rule = k1[a__][xx] :>  k1c4[a][xx] / c^4;
kk1[xx] /. rule

same result


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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