Message Boards Message Boards

0
|
6057 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Pattern recognition of exponents 0 and 1.

Posted 9 years ago

Hello everybody, I stumbled across the following problem: I want to do the following substitution in some lengthy expression: f[ x^a_ y^b_ z^c_]->g[a,b,c] including the case a,b,c ={0,1}. Of course I could brute force code all the cases 0,1,n>1, but that would require 3^3 terms, and I will probably need to do this with a greater number of variables. So any smart idea is most welcome, and I might learn some coding trick in the process as I have relatively small experience with Mathematica. Cheers

3 Replies

To match 0 and 1 exponents for monomials you could multiply the whole expression by a general monomial, e.g. :

list = Flatten[Table[x ^i y^j z^k, {k, 0, 2}, {j, 0, 2}, {i, 0, 2}]]  
list x^p y^p z^p /. x^a_ y^b_ z^c_ :> f[a, b, c] /. p -> 0

I.M.

POSTED BY: Ivan Morozov

You could use the _. notation like in this example:

integrate[x_^n_., x_] := x^(n + 1)/(n + 1) /; FreeQ[n, x] && n != -1

here x^1 is also matched. By setting the proper defaults you can match most of them.

POSTED BY: Sander Huisman

In[154]:= exp = Table[Power[RandomChoice[{x, y, z}], i], {i, -3, 3}]

Out[154]= {1/y^3, 1/y^2, 1/y, 1, x, y^2, y^3}

Define a function that checks the form of the input expression and returns the exponent (power).

    pwrChk[expr_] := Switch[expr,
(*1. Address the case of the constant*)
(*-----> If input is*)1, (*then return*)0,
(*2. Address all regular powers of the form x^y. *)
(*----->If input is*)Power[x_, y_], (*then return*) FullForm[expr][[1, 2]],
      (*3. Address cases like x^1.*)
(*----->If input is*)expr, (*return*)1]

Try it on our sample list of expressions.

In[157]:= pwrChk[#] & /@ exp

Out[157]= {-3, -2, -1, 0, 1, 2, 3}

The answer is a list with the powers of the input expressions including cases where the power was a zero or one.

POSTED BY: Isaac Abraham
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