Re: search for an operator in an expression
- To: mathgroup at smc.vnet.net
- Subject: [mg78261] Re: search for an operator in an expression
- From: "Jean-Marc Gulliet" <jeanmarc.gulliet at gmail.com>
- Date: Wed, 27 Jun 2007 05:24:36 -0400 (EDT)
- References: <f5iuqu$a99$1@smc.vnet.net> <467D2013.6040903@gmail.com>
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 Hi Daniel, If I have correctly understood what you want, the easiest way should be to check the head of your expression. (Remember that in Mathematica everything is an expression, that is something with a head and some elements: head[e1, e2, ...] .) For instance, say we have the following expression In[1]:= expr = Expand[(a + I*b)^5] Out[1]= a^5 + 5*I*a^4*b - 10*a^3*b^2 - 10*I*a^2*b^3 + 5*a*b^4 + I*b^5 Its head is Plus. In[2]:= Head[expr] Out[2]= Plus We can also look at the full form of the expression and see that it contains many sub-expressions (with head Times or Complex for example); however the head of the overall expression is Plus. In[3]:= FullForm[expr] Out[3]//FullForm= Plus[Power[a,5],Times[Complex[0, 5],Power[a,4],b],Times[-1,10,Power[a,3],Power[b,2]],Times[Complex[ 0,-10],Power[a,2],Power[b,3]],Times[5,a,Power[ b,4]],Times[Complex[0,1],Power[b,5]]] In[4]:= TreeForm[expr] (*TreeForm is mores appealing in version 6.0 *) To summarize, the differences among the list {a, b, c}, the sum a + b + c, and the product a * b * c are just in their respective heads. In[5]:= FullForm[{a, b, c}] Out[5]//FullForm= List[a,b,c] In[6]:= FullForm[a + b + c] Out[6]//FullForm= Plus[a,b,c] In[7]:= FullForm[a*b*c] Out[7]//FullForm= Times[a,b,c] Finally, Apply is used to change the head of an expression. Below we transform a product into a sum by changing its head. In[8]:= Plus @@ (a*b*c) Out[8]= a + b + c HTH, Jean-Marc