Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Checking for a given phrase in an expression

  • To: mathgroup at smc.vnet.net
  • Subject: [mg60782] Re: [mg60744] Checking for a given phrase in an expression
  • From: Andrzej Kozlowski <andrzej at yhc.att.ne.jp>
  • Date: Wed, 28 Sep 2005 01:41:36 -0400 (EDT)
  • References: <200509270745.DAA18926@smc.vnet.net> <B8D01E27-0C24-437F-B196-C353CF3C1C9E@mimuw.edu.pl>
  • Reply-to: Andrzej Kozlowski <andrzej at akikoz.net>
  • Sender: owner-wri-mathgroup at wolfram.com


On 27 Sep 2005, at 23:06, Andrzej Kozlowski wrote:


>
> On 27 Sep 2005, at 16:45, lost.and.lonely.physicist at gmail.com wrote:
>
>
>
>> Hello everyone
>>
>> Is it possible to ask Mathematica to check if a given phrase  
>> occurs in
>> an expression? Say I have
>>
>> aa = 1 + x + x y + Log[Sin[z]]
>>
>> and
>>
>> bb = x + Log[Sin[z]]
>>
>> Can Mathematica tell me such a phrase bb exists in aa? Can it tell me
>> its 'position' within the expression aa?
>>
>> Thanks for the help!
>>
>>
>>
>>
>
>
> Your question is actually  too vague to have an unambiguous answer.  
> One can define a function like this:
>
> SubExpressionQ[a_, b_] := Block[{f}, (a /. b -> f) =!= a]
>
> This will test if something is a "subexpression' of something else  
> in the sense of Matheamtica's pattern matching. Thus in your case:
>
>
> aa = 1 + x + x*y + Log[Sin[z]]; bb = x + Log[Sin[z]];
>
>
> SubExpressionQ[aa, bb]
>
>
> True
>
>
> However, this concept of subexpression will not necessarily always  
> correspond to what you might expect. For example just take
>
> s = (x/y)^2
>
> You might expect that x/y is a subexpression ("phrase") of s, but  
> in fact:
>
>
> SubExpressionQ[s,x/y]
>
> False
>
> To understand why you need to look at the FullForm of the two  
> expressions:
>
>
> FullForm[s]
>
> Times[Power[x,2],Power[y,-2]]
>
>
> FullForm[x/y]
>
> Times[x,Power[y,-1]]
>
> and now you can see why one is not a "phrase" in the other. As I  
> wrote at the beginning, the meaning of "phrase in something"is  
> ambiguous. There are various ways to interpret it. One way is the  
> above, which is based on the way Mathematica evaluates and  
> represents expressions. Another possible approach would be to  
> convert both expressions to strings and use Mathematica's string  
> matching abilities to  test if the two expressions convert to  
> substrings of one another. This approach will of course not always  
> agree with the other.
> Given all that there is also no clear sense in which you can speak  
> of a "position" of one expression in anohter, particularly that the  
> commutative and associative laws for addition and multiplication  
> are used in pattern matching .
>
> Andrzej Kozlowski
>
>


There is another, minor but annoying problem with the function  
SubExpressionQ I defined above:


SubExpressionQ[a_, b_] := Block[{f}, (a /. b -> f) =!= a]

which will occur if the expression b is  just the symbol f


SubExpressionQ[1+f+f^2,f]

False

This can however be easily avoided, for example as follows:


Clear[SubExpressionQ]

SubExpressionQ[a_, b_] := (a /. b -> Unique[]) =!= a

Now


SubExpressionQ[1+f+f^2,f]


True

etc.

Andrzej Kozlowski





  • Prev by Date: SVG Authoring Tool Chain Report
  • Next by Date: Grassmann Calculus / Indexed Objects / Simplify
  • Previous by thread: Re: Checking for a given phrase in an expression
  • Next by thread: Re: Checking for a given phrase in an expression