6

Does the Im function work with symbolic arguments?

Clear[ y, t, k, ω ]

A ( Cos[ k y ] + I Sin[ k y ] ) 2I Sin[ ω t ] //ComplexExpand

(* Output: 2 I A Cos[ k y] Sin[ t ω ] - 2 A Sin[ k y ] Sin[ t ω ] *)

Im[ % ]

(* Output: -2 Im[ A Sin[ k y ] Sin[ t ω ] ] + 2 Re[ A Cos[ k y ] Sin[ t ω ] ] *)

Expected output: -2 A Sin[ k y ] Sin[ t ω ]

Artes
  • 57,212
  • 12
  • 157
  • 245
Gary
  • 569
  • 3
  • 13
  • 2
    You could apply ComplexExpand to the last output again, e.g. ComplexExpand[Im[%]] instead of Im[%] – Heike May 01 '12 at 22:58
  • I'm amazed by all the good answers, but still a little puzzled. One says that ComplexExpand is not needed, the other that TargetFunctions is not needed. Both lead to succinct expressions that give the correct answer. What is it that makes one or the other, or neither, preferable? – Gary May 02 '12 at 14:02
  • @Artes, Sorry for not responding. I'm not very good at forum protocols, as I don't use them very often, each is a little different, and I find the locations of the buttons a little confusing. I think I forgot to set automatic email for this one. Even now, I am not sure that I have satisfied the request. I'm happy with the answers. – Gary Jul 28 '12 at 14:49

1 Answers1

11

You should assume that your variables are real, (if you want M to proceed further) because Mathematica treats variables in general as complex. One of many ways to do it :

 expr = A ((Cos[k y] + I Sin[k y]) 2 I Sin[t ω]); 
 Refine[ Im[ expr], (A | k y | t ω) ∈ Reals]
2 A Cos[k y] Sin[t ω]

We needn't use ComplexExpand defining expr, but in this case it is the simplest approach (pointed out by Heike) :

ComplexExpand @ Im @ expr

Some other ways of imposing assumptions :

Assuming[(A | k y | t ω) ∈ Reals, Refine[ Im[ expr] ] ]

another way yielding the same result :

Block[{$Assumptions = A ∈ Reals && k y ∈ Reals && t ω ∈ Reals},
       Refine @ Im[ expr] ]
 2 A Cos[k y] Sin[t ω]
Artes
  • 57,212
  • 12
  • 157
  • 245
  • ComplexExpand[] actually works nicely here: ComplexExpand[Im[A ((Cos[k y] + I Sin[k y])*2 I Sin[ω t])], TargetFunctions -> Im]; unfortunately the TargetFunctions option is not as well-known as I think it should be. – J. M.'s missing motivation May 02 '12 at 02:46
  • Note that, in place of the assumption like A ∈ Reals you can use the assumption _Symbol ∈ Reals to assume that all explicit variables are real. This allows you to encapsulate this in a function without having to manually pass a list of arguments. For an exhaustive discussion, see here. – Jess Riedel Sep 27 '17 at 18:58