1
$\begingroup$

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:

  1. 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.
  2. 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?

$\endgroup$
0

1 Answer 1

2
$\begingroup$

EDIT I have edited in line with comment by Kuba

This can be seen as follows:

k = Pi #/2 & /@ {k1, k2, k3};
f = {Cos, Sin, Sin};
t1 = Times @@ MapThread[Compose,{f, k}];
t2 = Times @@ MapThread[Compose,{{Sin, Cos, Cos}, k}];
g = -t1 + t2 I;
cg = -t1 - t2 I;

Setting up your equation:

eqn = 1/3 (Vxx - Vxy) (t1 + t2 I) - 4/3 (Vxx/4 + Vxy/2) (t1 + t2 I);

Then,

Collect[Simplify[eqn], Vxy] /. {cg -> g2c}

yields g2cVxy as desired (where g2c is oomplex conjugate of g2)

$\endgroup$
7
  • $\begingroup$ just an offtopic remark, you can write t1 definition in more elegant way with Inner[Compose, f, k, Times] or Times @@ MapThread[Compose, {f, k}]. Compose is the same as #[#2] &. $\endgroup$
    – Kuba
    Commented Oct 11, 2013 at 7:13
  • $\begingroup$ @Kuba thank you, I guess I was too focussed on showing this was just an exercise in algebraic manipulation...both Inner and MapThread are nicer. $\endgroup$
    – ubpdqn
    Commented Oct 11, 2013 at 7:51
  • $\begingroup$ thanks for the quick feedback, I was too focused on finding a pattern which does the trick, and not thinking about procedural approaches to generate the substitution. However I have a followup question on the use of Compose. What is this function exactly doing? The manual just mentions that it has been superseded since Version 2.0 (!) by Composition, but Composition is failing when using it in the above code.... $\endgroup$
    – Rainer
    Commented Oct 11, 2013 at 11:51
  • $\begingroup$ @Rainer mathematica.stackexchange.com/q/28778/5478 $\endgroup$
    – Kuba
    Commented Oct 11, 2013 at 11:56
  • $\begingroup$ @Kuba thanks for the link, it's very clear. $\endgroup$
    – Rainer
    Commented Oct 11, 2013 at 12: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.