MathGroup Archive 2008

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

Search the Archive

Re: how to test where a list contains constant(s) or not

  • To: mathgroup at smc.vnet.net
  • Subject: [mg91985] Re: how to test where a list contains constant(s) or not
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Tue, 16 Sep 2008 19:22:31 -0400 (EDT)
  • References: <gal3iu$e0a$1@smc.vnet.net>

Hi,
> case1: { a, b, c, Pi }   gives true because of Pi
> case2: { a, b, c, 0.0001} gives true because of 0.0001
> case3: { a, b, c,  2 + Pi I } gives ture becase of 2 + Pi I
> case4: { a, b, c} gives false
> 
> is this function right ?
> 
> ComplexQ[z_] := NumericQ[ z ] || ( NumericQ[ z ] && SameQ[ Head[ z ],
> Complex] )
> IsConstantsIn[ lstList_ ] :=
> 		Module[ { intLength },
> 			intLength = Length@Select[ lstList, ComplexQ[ # ]& ];
> 			If[ intLength > 0, Return[ True ], Return[ False ] ];
> 			Return[ False ];
> 			]

It probably does what you want, but I think it is way too complicated.
Note that Complex numbers are Numeric by default, so there is no need
for your ComplexQ-definition.

If you think about seriously using Mathematica you should get familiar
with its pattern matcher, otherwise you are missing its strongest part
:-). If I correctly understood what you are after, the following leaves
all the work for mathematica:

IsConstantsIn[{___, _?NumericQ, ___}] := True
IsConstantsIn[___] := False

the first definition gives True, if the argument to IsConstantsIn is a
list containing at least one numeric element at any position. The second
definition ensures that IsConstantsIn will return False in any other
case. To handle other cases, e.g. expressions that are not lists you can
easily extend by adding additional definitions. Finally I think the name
of your function could probably changed to reflect more clearly what
exactly it is doing, e.g. IsNumericIn or HasNumericElement. Mathematica
has the posibility to define attributes to symbols, among these
attributes is Constant, so IsCOnstantsIn would mislead me in thinking it
has something to do with that...

hth,

albert


  • Prev by Date: Re: Functional programming?
  • Next by Date: Re: One fundamental Gudermannian identity not verified
  • Previous by thread: Re: how to test where a list contains constant(s) or not
  • Next by thread: Label style in PieChart?