Message Boards Message Boards

0
|
4189 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Find an operator in a formula with Cases?

Hi, I would like to search through a formula to find if it contains a divide operator.

f= (a + b)/(c - d) e

Below the surface this is build as

f // FullForm

Times[Plus[a,b],Power[Plus[c,Times[-1,d]],-1],e]

I wrongly though that searching for a Divide operation I could use Cases looking for a pattern Power[__,-1] but this doesn't work. I only succeed when converting f in fullform to a string and then use StringCases.

f = f // FullForm // ToString
StringCases[f, __ ~~ "Power[" ~~ __ ~~ ", -1]" ~~ ___]

This returns the expression so I know it found the Divide operation. Basically I would like to search for any +,*,-,/ operator in any formula like above. Is there a way by using the formula itself with Cases?

Thx!

POSTED BY: l van Veen
2 Replies

Add a level specification so Cases looks 'deeper':

Cases[f, Power[x__, -1], \[Infinity]]
POSTED BY: Sander Huisman

Thanks Sander, I knew it was a basic thing and now it all came back :) (slight feeling of shame..). Amazing to see that sometimes jou look at the documentation and just miss the important part. I was seeing strings only and not an expression tree.

POSTED BY: l van Veen
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract