Mathematica has some strange way of sorting terms. It seems that it is using a canonical sort on expressions, e.g. $\mathbb{i} \sin[x] \cos[y] \cos[z]$ is returned as $\mathbb{i} \cos[y] \cos[z] \sin[x]$. I interpret this behaviour that the function names ($\cos$ and $\sin$) have precedence over their arguments in the output sequence.
This behaviour makes output sometimes difficult to interpret complicated terms involving sums and products of transcendent functions. These terms are sorted according to the transcendent function names and not according to their arguments as I'm used to.
However, the core of my question is related to the problem of simplifying a given complex function e.g.
$\frac{1}{3} (V_{xx}-V_{xy}) \left(\cos \left(\frac{\pi k_1}{2}\right) \sin \left(\frac{\pi k_2}{2}\right) \sin \left(\frac{\pi k_3}{2}\right)+i \sin \left(\frac{\pi k_1}{2}\right) \cos \left(\frac{\pi k_2}{2}\right) \cos \left(\frac{\pi k_3}{2}\right)\right)-\frac{4}{3} \left(\frac{V_{xx}}{4}+\frac{V_{xy}}{2}\right) \left(\cos \left(\frac{\pi k_1}{2}\right) \sin \left(\frac{\pi k_2}{2}\right) \sin \left(\frac{\pi k_3}{2}\right)+i \sin \left(\frac{\pi k_1}{2}\right) \cos \left(\frac{\pi k_2}{2}\right) \cos \left(\frac{\pi k_3}{2}\right)\right)$
by substituting the definition of another complex function
$g_2=-\cos \left(\frac{\pi k_1}{2}\right) \sin \left(\frac{\pi k_2}{2}\right) \sin \left(\frac{\pi k_3}{2}\right)+i \sin \left(\frac{\pi k_1}{2}\right) \cos \left(\frac{\pi k_2}{2}\right) \cos \left(\frac{\pi k_3}{2}\right)$
"As one readily can see" (I love these textbook statements!), the resulting function is $V_{xy} g_2^*$.
However the use of complex functions is complicating the substitution task in Mathematica. A simple substitution using patterns like
-Cos[x_] Sin[y_] Sin[z_] + I Sin[x_] Cos[y_] Cos[z_] -> g
does not work for two reasons:
- The sequence of trigonometric functions is not preserved as expected in the canonical sorting of expressions. Thus the patterns x_, y_ and z_ are matching different arguments. This is in this case not a problem because I'm not using the named patterns in the substitution, but I can think of multiple issues where this would be one.
- The second, more severe problem is the case of conjugate, and negated functions as shown in the above example.
There is of course one solution, which is defining 4 patterns covering all possible complex cases of the function definition $g_2$ as
{
-Cos[x_] Sin[y_] Sin[z_] + I Sin[x_] Cos[y_] Cos[z_] -> g2,
+Cos[x_] Sin[y_] Sin[z_] - I Sin[x_] Cos[y_] Cos[z_] -> -g2,
-Cos[x_] Sin[y_] Sin[z_] - I Sin[x_] Cos[y_] Cos[z_] -> Conjugate[g2],
+Cos[x_] Sin[y_] Sin[z_] + I Sin[x_] Cos[y_] Cos[z_] -> -Conjugate[g2]
}
but I'm looking for a less cumbersome approach, since in my problem I have 12 different complex functions to substitute which multiplies up to 48 substitution lines with the above approach.
Any idea how to implement this in a more elegant way?