0
$\begingroup$

I have function B[i,j] where i,j are integers. Then I create array:

b = Array[B, {3,3}]

now I set values of B[i,j] for some i,j:

B[1, 1] = -2.98675 
B[1, 2] =  -4.99337 
B[1, 3] = -2.13606 
B[2, 2] = -1.0732 
B[2, 3] = -2.63553
B[3, 3] = -1.446

now in array b values B[3,2], B[3,1] and B[2,1] have no value, but I would like to have array b symmetric (B[2,1]==B[1,2]). I cannot find the easy way how to tell Mathematica to make array b symmetric. This problem is easy to solve manualy, but for function C[i,j,k] in array c = Array[C,{3,3,3}], symmetric array would be appreatiated. I want to sum over these arrays.

$\endgroup$
1
$\begingroup$

You can yse simple construct with Do:

Do[B[i, j] = B[j, i], {i, 3}, {j, 3}]

In case of Array[S,{3,3,3}] this probably should be changed to:

Do[S[i, j, k] = S[j, i, k], {i, 3}, {j, 3}, {k, 3}]
$\endgroup$
  • $\begingroup$ It is quite tricky with the iterators i,j. But Do[B[i, j] = B[j, i], {j, 3}, {i, 3}] worked for me. $\endgroup$ – Juicce Oct 16 at 15:23
  • $\begingroup$ I checked with Array[B,{3,3}] // MatrixForm both variants of iterators and there is no difference. Anyway, thank you for accepting. $\endgroup$ – Alx Oct 16 at 15:27
  • $\begingroup$ And for Array[S,{3,3,3}] only Do[Cv[i, j, k] = Cv[j, i, k] = Cv[k, i, j], {k, 3}, {j, 3}, {i, 3}] works for me. $\endgroup$ – Juicce Oct 21 at 13:13
2
$\begingroup$

You can use SymmetrizedArray:

Normal @ SymmetrizedArray[
    {
    {1,1}->-2.98675,
    {1,2}->-4.99337,
    {1,3}->-2.13606,
    {2,2}->-1.0732,
    {2,3}->-2.63553,
    {3,3}->-1.446
    },
    {3,3},
    Symmetric[{1,2}]
]

{{-2.98675, -4.99337, -2.13606}, {-4.99337, -1.0732, -2.63553}, {-2.13606, -2.63553, -1.446}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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