Mathematica Asked on April 5, 2021
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!
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
Correct answer by cvgmt on April 5, 2021
This would be one option
ClearAll["g*"];
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
{
{g11[x11,x12,x21,x22,x31,x32,x41,x42],g12[x11,x12,x21,x22,x31,x32,x41,x42]},
{g12[x11,x12,x21,x22,x31,x32,x41,x42],g22[x11,x12,x21,x22,x31,x32,x41,x42]}
}
where I have considered a $2times2$ matrix for a more compact output.
Answered by N0va on April 5, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP