Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9634

Re: Deleting expressions containing a certain head

From A Retey <awnl@gmx-topmail.de>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Deleting expressions containing a certain head
Date 2012-04-14 07:11 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jmb7u6$r1v$1@smc.vnet.net> (permalink)
References <jm8p89$ip5$1@smc.vnet.net>

Show all headers | View raw


Hi,

> 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.

cheers,

albert

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Re: Deleting expressions containing a certain head "wb@wavebounce.com" <rob@wavebounce.com> - 2012-04-13 08:48 +0000
  Re: Deleting expressions containing a certain head A Retey <awnl@gmx-topmail.de> - 2012-04-14 07:11 +0000

csiph-web