1
$\begingroup$

Mathematica has tons of functions to perform algebraic manipulations. But is there a way to use it to check if I didn't make mistakes in my own algebraic manipulations.

As a real world example:

  • [step1] I have $\sin(\omega t + \pi /2)$
  • [step2] I rewrite that as $\sin(\omega t - \pi/4 +3\pi /4)$,
  • [step3] I apply a well known identity so I end up with $\sin(\omega t - \pi/4)\cos(3\pi /4) + \cos(\omega t - \pi/4)\sin(3\pi /4)$.

Will Mathematica be able to check: $$\text{step1}\Leftrightarrow\text{step2}\Leftrightarrow\text{step3}$$ I don't look for something smart, just verifying I didn't make a sign error or something like that.

$\endgroup$
4
  • $\begingroup$ Instead of attempting it by hand, leave the Cos[a +b] in your expression, then use TrigExpand. E.g. TrigExpand@Cos[a + b] returns the identity you sought. See also TrigReduce etc. $\endgroup$
    – MarcoB
    Commented Dec 13, 2019 at 19:56
  • $\begingroup$ @Marco, I rewrote the question so it may eventually be more clear. For specific answer to you comment, in [step2], TrigExpand[Sin[\[Omega] t -a + b]] will perform a full expansion of the expression, which was not something I wanted. $\endgroup$ Commented Dec 13, 2019 at 20:20
  • 2
    $\begingroup$ Simplify[step1-step3]? Or FullSimplify. When that doesn't work, I plug in random numbers with something like expr1 - expr2 /. x -> RandomReal[{-10, 10}, 1, WorkingPrecision -> 32], depending on what domain I want instead of {-10, 10}, how many variables there are, whether the expressions are listable. If listable, it's easy to check 100 random values with ... /. Thread[{x, y, z} -> RandomReal[{-2,2}, {3, 100}, WorkingPrecision -> 32]. $\endgroup$
    – Michael E2
    Commented Dec 13, 2019 at 20:34
  • $\begingroup$ Good idea @Michael. I was considering something like that but using == instead of a difference. I assume your solution is better suited to deal with rounding errors. $\endgroup$ Commented Dec 13, 2019 at 22:27

1 Answer 1

1
$\begingroup$

Quickly wrapping up Michael E2's idea:

s1 = Sin[w*t + \[Pi]/2];
s2 = Sin[w*t \[Minus] \[Pi]/4 + 3 \[Pi]/4];
s3 = Sin[w*t \[Minus] \[Pi]/4]*Cos[3 \[Pi]/4] + 
   Sin[3 \[Pi]/4]*Cos[w t - \[Pi]/4];
s4 = Sin[w*t + \[Pi]/2.1];

Clear[equal]; 
equal[s1_, s2_, n_ : 100, range_ : {-2, 2}] := Module[{
    vars = Quiet @ Union @ 
           Cases[s3, x_ /; ! ValueQ[x] && Context[x] == "Global`", {-1}, Heads -> True]},
  If[TrigExpand[s1] - TrigExpand[s2] == 0, True,
   tests = Thread[vars -> Table[Slot[i], {i, Length[vars]}]] & @@@ 
     RandomReal[range, {n, Length[vars]}, WorkingPrecision -> 32],
   Thread[s1 /. tests] == Thread[s2 /. tests]
   ]
  ]

equal @@@ {{s1, s2}, {s2, s3}, {s3, s4}}
(* {True, True, False} *)
$\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.