# Condition on the index of an element $a_{i,j}$

This question may seem to be easy. But I did not find the right way after several attempts

I need to put a condition in general for $a_{i,j}$ ,If the i is bigger than j puts $a_{j,i}$, other way puts $a_{i,j}$.

If[i > j, Subscript[a, j, i], Subscript[a, i, j]]


$\text{If}\left[i>j,a_{j,i},a_{i,j}\right]$

But I do not know how to implement it on a function. How it applies to a function as in the following example.

For example

If I have the following function

f= Subscript[a, 1, 3] Subscript[a, 3, 2] +Subscript[a, 1, 3] Subscript[a, 2,3] -Subscript[a, 5, 2] Subscript[a, 1, 2] Subscript[a, 3, 2]


$f=a_{1,3} a_{3,2}+a_{1,3} a_{2,3}-a_{5,2} a_{1,2} a_{3,2}$

This condition will work on turning index as follows,

$a_{3,2} \Rightarrow a_{2,3}$

$a_{5,2} \Rightarrow a_{2,5}$

then, we get f as following,

$f=2a_{1,3} a_{2,3}-a_{1,2} a_{2,3} a_{2,5}$

f =
Subscript[a, 1, 3] Subscript[a, 3, 2] +
Subscript[a, 1, 3] Subscript[a, 2, 3] -
Subscript[a, 5, 2] Subscript[a, 1, 2] Subscript[a, 3, 2];

f /. Subscript[a_, i_, j_] /; i > j :> Subscript[a, j, i] If you want to do this repeatedly you can define a function:

rep[fn_] := fn /. Subscript[a_, i_, j_] /; i > j :> Subscript[a, j, i]


Now it's just

rep @ f Avoid using Subscript if you can, and simply use a[1,2] etc. instead. Then you can define

a[i_, j_] /; i>j := a[j,i]


Now

a[1, 3] a[3, 2] + a[1, 3] a[2, 3] - a[5, 2] a[1, 2] a[3, 2]


returns

2 a[1, 3] a[2, 3] - a[1, 2] a[2, 3] a[2, 5]


An advantage of doing it this way is that things are transformed automatically, with no need to replace expressions repeatedly, applying functions to expressions etc.

• One can even add MakeBoxes[a[x__], _] := ToBoxes @ Subscript[a, x] to get the (apparently) desired output format. Aug 7 '17 at 15:50