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: [mg78420] Re: [mg78103] search for an operator in an expression
  • From: DrMajorBob <drmajorbob at bigfoot.com>
  • Date: Sun, 1 Jul 2007 07:41:42 -0400 (EDT)
  • References: <59219.1182598370249.JavaMail.root@m35>
  • Reply-to: drmajorbob at bigfoot.com

Here's an attempt, with (perhaps) counterintuitive results:

expr=Sin[x]Sin[x^3+x]//TrigExpand
Cos[x^3]/2-1/2 Cos[x]^2 Cos[x^3]+1/2 Cos[x^3] Sin[x]^2+Cos[x] Sin[x]  
Sin[x^3]

expr//FullForm
Plus[Times[Rational[1,2],Cos[Power[x,3]]],Times[Rational[-1,2],Power[Cos[x],2],Cos[Power[x,3]]],Times[Rational[1,2],Cos[Power[x,3]],Power[Sin[x],2]],Times[Cos[x],Sin[x],Sin[Power[x,3]]]]

Count[expr,Plus[__]]
4

Cases[expr, Plus[__]]

{Cos[x^3]/2, -1/2 Cos[x]^2 Cos[x^3], 1/2 Cos[x^3] Sin[x]^2,
  Cos[x] Sin[x] Sin[x^3]}

The FullForm contains a single Plus with four arguments, which I'd  
normally think of as three additions. But Count and Cases find four  
instances of Plus.

But wait... there's even more!

Cases[expr, Plus[__], Infinity]

{1/2, x, 3, x^3, Cos[x^3], Cos[x^3]/2, -1/2, x, Cos[x], 2,
  Cos[x]^2, x, 3, x^3,
  Cos[x^3], -1/2 Cos[x]^2 Cos[x^3], 1/2, x, 3, x^3, Cos[x^3], x,
  Sin[x], 2, Sin[x]^2, 1/2 Cos[x^3] Sin[x]^2, x, Cos[x], x,
  Sin[x], x, 3, x^3, Sin[x^3], Cos[x] Sin[x] Sin[x^3]}

Count[expr, Plus[__], Infinity]

35

But there's also THIS:

StringCount[expr // FullForm // ToString, "Plus"]

1

or this:

StringCount[expr // ToString, "+"]

2

Take your pick!!

Bobby

On Sat, 23 Jun 2007 06:03:04 -0500, Daniel <danielmaxin at yahoo.com> 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
>
>



-- 
DrMajorBob at bigfoot.com



  • Prev by Date: Re: Iterpolation and Dates
  • Next by Date: Combinatorica loading time in V6
  • Previous by thread: Re: Re: search for an operator in an expression
  • Next by thread: Re: search for an operator in an expression