5
$\begingroup$

Give real x,y and z, I am trying to calculate the matrix matF defined as

     b = {{x, -y}, {y, -x}}; 
      matF = FullSimplify[MatrixExp[(-I)*b*z]]
(*{{Cos[Sqrt[x^2 - y^2]*z] - (I*x*Sin[Sqrt[x^2 - y^2]*z])/Sqrt[(x - y)*(x + y)], 
   (I*y*Sin[Sqrt[x^2 - y^2]*z])/Sqrt[(x - y)*(x + y)]}, 
  {-((I*y*Sin[Sqrt[x^2 - y^2]*z])/Sqrt[(x - y)*(x + y)]), 
   Cos[Sqrt[x^2 - y^2]*z] + (I*x*Sin[Sqrt[x^2 - y^2]*z])/Sqrt[(x - y)*(x + y)]}}*)

The output has expressions like Sqrt[(x-y)(x+y)] and I expect the Mathematica to show me Sqrt[x^2-y^2]. Secondly, I want to define as new variable Sqrt[x^2-y^2]=u and see the entire expression in terms of this new variable.

$\endgroup$
3
  • 3
    $\begingroup$ Mathematica's simplification rules can be a bit frustrating at times. Please try matF /. (x - y) (x + y) -> x^2 - y^2 /. Power[x^2 - y^2, k_] :> u^(2 k). Also, in the future you might want to apply FullForm to see which replacements to make. Sometimes the internatl representation is quite different from the pretty-printed one. $\endgroup$ Commented Apr 7 at 13:42
  • $\begingroup$ Intersstingly, FullSimplify shows: (x-y)(x+y) and Simplify shows: x^2-y^2. If you use Simplify after FullSimplify it does what you want. $\endgroup$ Commented Apr 7 at 15:16
  • $\begingroup$ Simplify`SimplifyCount[(x - y) (x + y)] and Simplify`SimplifyCount[x^2 - y^2] show that the two expressions are equally simple. (Simplify tries to minimize this function. You can give your own function through the option ComplexityFunction.) $\endgroup$
    – Michael E2
    Commented Apr 7 at 15:32

2 Answers 2

8
$\begingroup$

The trouble is that the default ComplexityFunction returns 10 for both Simplify`SimplifyCount[(x - y) (x + y)] and Simplify`SimplifyCount[x^2 - y^2]. Almost any perturbation (in the right direction) will break the tie. Each of the options below return the same thing, with x^2-y^2 and not (x-y)(x+y). Some work because of the following differences in the counts of the operations (A-B is Plus[A, Times[-1, B]]):

expr #Plus #Times #Power
x^2-y^2 1 1 2
(x-y)(x+y) 2 2 0

Various ComplexityFunction options, roughly from most targeted to least:

Penalize the undesired form

FullSimplify[MatrixExp[(-I)*b*z], 
 ComplexityFunction -> (Simplify`SimplifyCount[#] + 
     Count[#, (x + y) (x - y), Infinity] &)]

Penalize factored polynomials

FullSimplify[MatrixExp[(-I)*b*z], 
 ComplexityFunction -> (Simplify`SimplifyCount[#] + 
     Count[#, p_Times /; PolynomialQ[p, {x, y}], Infinity] &)]

Reward (all) powers

FullSimplify[MatrixExp[(-I)*b*z], 
 ComplexityFunction -> (Simplify`SimplifyCount[#] - 
     Count[#, _Power, Infinity] &)]

Penalize factored forms

FullSimplify[MatrixExp[(-I)*b*z], 
 ComplexityFunction -> (Simplify`SimplifyCount[#] + 
     Count[#, _Times, Infinity] &)]

Penalize sums

FullSimplify[MatrixExp[(-I)*b*z], 
 ComplexityFunction -> (Simplify`SimplifyCount[#] + 
     Count[#, _Plus, Infinity] &)]

Side note: I generally try to avoid using replacement rules for algebraic operations. One exception is a rule of the form var -> expr, where var is a single variable. Such rules seem safe and will do the obvious. Sometimes I break with this practice with a rule of the form expr :> simp[expr], where simp[] is some (targeted) simplifying transformation. Examples:

FullSimplify[MatrixExp[(-I)*b*z]] /.
 p_ /; PolynomialQ[p, {x, y}] :> Expand[p]

FullSimplify[MatrixExp[(-I)*b*z]] /.
 (x - y) -> (x^2 - y^2)/(x + y)

(* the way I recommend above (extra Simplify needed): *)
FullSimplify[MatrixExp[(-I)*b*z]] /.
 x -> y + (x^2 - y^2)/(x + y) // Simplify

The problem with applying expr :> replacement to an expression is that sometimes the FullForm of expr does not match the FullForm of what looks like the same thing in part of the expression being simplified. Another problem can be that the rule works on the test case(s) but fails on an edge case.

$\endgroup$
1
  • 1
    $\begingroup$ This would make a nice series of sci-fi book titles: Vol 1: Penalize the undesired form; Vol 2: Penalize factored polynomials; Vol 3: Reward (all) powers; Vol 4: Penalize factored forms; Vol 5: Penalize sums. $\endgroup$
    – Red Banana
    Commented Apr 8 at 14:25
2
$\begingroup$
r1 = x^2 - y^2 -> u^2;
b = {{x, -y}, {y, -x}};
matF = FullSimplify[MatrixExp[(-I)*b*z]] // Together // Simplify

matF /. r1 // PowerExpand

$\left( \begin{array}{cc} \cos (u z)-\frac{i x \sin (u z)}{u} & \frac{i y \sin (u z)}{u} \\ -\frac{i y \sin (u z)}{u} & \cos (u z)+\frac{i x \sin (u z)}{u} \\ \end{array} \right)$

$\endgroup$

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.