MathGroup Archive 2007

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

Search the Archive

Re: search for an operator in an expression

  • To: mathgroup at smc.vnet.net
  • Subject: [mg78357] Re: search for an operator in an expression
  • From: rob <josh2499 at hotmail.com>
  • Date: Fri, 29 Jun 2007 05:41:34 -0400 (EDT)
  • References: <f5iuqu$a99$1@smc.vnet.net> <f5o8jr$5jo$1@smc.vnet.net> <f5qhdc$2jt$1@smc.vnet.net> <f5tcb1$2ir$1@smc.vnet.net>

Thanks for taking time to help. I used your suggestion

Cases[expr, Plus[x__] :> Length[x]]

where expr = 2 + a*Exp[-b x] + Sin[c + b] + d

and I get {0,0,2,1}.  I could total that and add one to get 
the correct answer of 4.

Another fellow suggested
StringCount[ToString[expr], "+"]
which puts out the correct answer and I think I understand it.

But, forgetting my original question, I'd sure like to 
understand why your Cases[] gives what it does.

Looking up all the players it seems like your suggestion 
would give a list of the components of FullForm[expr} with 
the arguments of Plus replaced by their lengths. It probably 
  does what you intended but I sure don't understand why. 
The quickie explanations in the HELP sometimes make no sense 
to me.

I'm using V5.1/XP. Thanks again for responding.

Regards, Rob

dh wrote:

> Hi,
> 
> you must distinguish between OutputFormat, what you see, and FullForm, 
> 
> what Mathematica sees. Try: FullForm[yourExpression] then you will see 
> 
> that you have 1 operator Plus with multiple arguments. If you want to 
> 
> see the number of arguments, you could e.g. use:
> 
> Cases[expression, Plus[x__] :> Length[x]]
> 
> hope this helps, Daniel
> 
> 
> 
> rob wrote:
> 
> 
>>Sir, I tried your suggestion as follows:
> 
> 
> 
>>expr = 2 + a*Exp[-b x] + Sin[c + b] + d
> 
> 
> 
>>Count[expr, HoldPattern[Plus[__]], Infinity]
> 
> 
> 
>>which gives a result of "1". I count 4 "+" in expr. Please, 
> 
> 
>>what am I doing wrong? Thanks.
> 
> 
> 
>>dh wrote:
> 
> 
> 
>>>Hi Daniel,
> 
> 
> 
>>>you may use "Count". Take care to search on all "levels" and prevent 
> 
> 
> 
>>>evaluation of the operator. Asssuming your expression is in: expr, e.g.:
> 
> 
> 
>>>Count[expr, HoldPattern[ Plus[__] ], Infinity]
> 
> 
> 
>>>hope this helps, Daniel
> 
> 
> 
> 
> 
> 
> 
>>>Daniel wrote:
> 
> 
> 
> 
>>>>Is there a way to search for an operator in a symbolic expression? My wish is to show that there is only "+" in a very large expression. Rather than showing an expression on multiple pages I would like to perform a search that shows there is only "+" between the terms. Converting to a string does not seem to work since the expression is written on multiple lines(fractions, exponents etc..)
> 
> 
> 
> 
>>>>Thanks
> 
> 
> 
> 
> 
> 
> 
> 
> 


  • Prev by Date: managing version 6 in a locked-down environment
  • Next by Date: Re: Integration variables
  • Previous by thread: Re: search for an operator in an expression
  • Next by thread: Re: search for an operator in an expression