It might be a very basic question! Let say that I have an array of variables as the following

x = {{x11, x12, x13, x14}, {x21, x22, x23, x24}, {x31, x32, x33, x34}, {x41, x42, x43, x44}, {x51, x52, x53, x54}}.

I want to define a 5 by 5 symmetric matrix G so that each of its elements is an arbitrary function of all of the variables above, e.g.

G[1,3]=G13[x11,x12,x13,x14,x21,x22,x23,x24,x31,x32,x33,x34,x41,x42,x43,x44,x51,x52,x53,x54], and so on. What is the simplest way to do this?

Thank you in advance!


2 Answers 2

x = {{x11, x12, x13, x14}, {x21, x22, x23, x24}, {x31, x32, x33, 
    x34}, {x41, x42, x43, x44}, {x51, x52, x53, x54}};
n = 2;
m = SparseArray[{i_, j_} /; j > i -> 
    Subscript[g, FromDigits[{i, j}]][Sequence @@ Flatten@x], {n, n}];
diagnal = 
  SparseArray[{i_, i_} -> 
    Subscript[g, FromDigits[{i, i}]][Sequence @@ Flatten@x], {n, n}];
m + Transpose[m] + diagnal
% // Normal // MatrixForm
  • $\begingroup$ Thank you so much. $\endgroup$
    – Irane.Mir
    Commented Nov 5, 2020 at 12:38

This would be one option

g[M_List /; Length[Dimensions[M]] === 2]:=Array[{x,y}\[Function](Symbol[StringJoin[Flatten@{"g",ToString/@Sort[{x,y}]}]][##]&@@Flatten[M]),Dimensions[M]]

which for

x = {{x11, x12}, {x21, x22}};
g[x] // MatrixForm

results in


where I have considered a $2\times2$ matrix for a more compact output.

  • $\begingroup$ Thank you so much, it works perfectly for a 2×2 matrix. To get a 5×5 one, I changed 2-->5 in the matrix dimensions, but it didn't work. What else should I change? $\endgroup$
    – Irane.Mir
    Commented Nov 4, 2020 at 20:40
  • $\begingroup$ g[Array[Subscript[a, Row[{#1, #2}]] &, {5, 5}]] works for me as expected... $\endgroup$
    – N0va
    Commented Nov 4, 2020 at 21:29

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.