Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if expression contains a Complex expression?

Is there a way to check if an expression contains complex expressions / imaginary numbers?

The documentation says that you can't check if an expression contains I because of how it is interpreted. I have also tried ImaginaryQ[expr_] := expr != Conjugate[expr] and Simplify[expr] =!= Simplify[Conjugate[expr]], but it does not yield accurate results. I have also tried to use MemberQ[expr, Complex], but that does not seem to work either.

I posted some examples into a notebook: http://www.eacousineau.com/download/complex-test.nb

like image 685
eacousineau Avatar asked Dec 03 '25 21:12

eacousineau


1 Answers

How about

ImaginaryQ[expr_] := ! FreeQ[expr, _Complex]

Using it on two of your examples:

imExpr = a Sin[a + 2 I];
ImaginaryQ@imExpr
(* True *)

reExpr = a Sin[a^2 + a];
ImaginaryQ@reExpr
(* False *)
like image 139
abcd Avatar answered Dec 06 '25 14:12

abcd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!