MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Deleting expressions containing a certain head

  • To: mathgroup at
  • Subject: [mg126060] Re: Deleting expressions containing a certain head
  • From: A Retey <awnl at>
  • Date: Sat, 14 Apr 2012 03:09:47 -0400 (EDT)
  • Delivered-to:
  • References: <jm8p89$ip5$>


> I'm not sure this quite meets the challenge.For example, Exp[I Pi}
> is not Complex but it "has a complex number in it" and appears in the
> output. Perhaps this is as it should be.
> list = {2^(Complex[2, 3]) + 3, 2, 3 x};
> Select[list, FreeQ[#, Complex]&]

You are of course right that pattern matching for Head Complex might not 
be a robust method to match expressions that "have complex numbers in 
them". For your example it is a matter of evaluation order, though. 
Exp[I Pi] will be evaluated to -1 even before Select sees it, so I don't 
think it is a valid counter example unless it is provided as a held 
expression, like here:

Select[{a,1+I,Hold[Exp[I Pi]]},FreeQ[#, Complex] &]

one could make that work with e.g.:

Select[{a,1+I,Hold[Exp[I*Pi]]},FreeQ[#,Complex|HoldPattern[I]] &]

Note that we need HoldPattern here since otherwise I will immediately be 
evaluated to Complex[0,1]. But of course when working with unevaluated 
symbolic expressions the task "remove expressions that have complex 
numbers in them" is probably not very well specified anyway. I think for 
a great many use cases (probably even all where the task is meaningful 
at all) the approach with checking for the appearance of head Complex 
might be valid.

In the end it's of course for the OP to decide whether that is what he 
wanted. I understood his question was about removing expressions 
containing a certain head. If his ultimate goal was to remove 
expressions "which have a complex number in it" your warning is 
reasonable and the OP probably wants to make sure that this approach is 
good enough for his use case.



  • Prev by Date: Re: NDSolve is really slow in version 8 but not 7
  • Next by Date: Re: fyi, small note on using Mathematica for object based programming
  • Previous by thread: Re: Deleting expressions containing a certain head
  • Next by thread: HostMath - Generate and embed math equations or mathematical formulas in a blog or website