开发者

Blanks in the denominator of a replacement rule

Mathematica 7.0 seems to dislike having blanks in the denominator. Can anyone explain why this is?

Input:

ClearAll["Global`*"];
(*Without blanks:*)
a^2 / b^2 /. a^2 / b^2 -> d
(*with:*)
a^2 / b^2 /. a^c_ / b^c_ -> d
(*Without blanks:*)
a^2 / b^2 /. (a / b)^2 -> d
(*With:*)
a^2 / b^2 /. (a / b)^c_ -> d
(*Without blanks:*)    
a^开发者_StackOverflow中文版2 / b^2 /. a^2 * b^(-2) -> d
(*With:*)
a^2 / b^2 /. a^c_ * b^(-c_) -> d

Output:

d
a^2/b^2
d
a^2/b^2
d
a^2/b^2

I'm trying to work this for a more complicated problem. The substitution that I want to make is in an expression of the form:

(a ^ c_. * Coefficient1_. / b ^ c_. / Coefficient2_.)  +  (a ^ d_. * Coefficient3_. / b ^ d_. / Coefficient4_.)

Where the coefficients may involve sums, products, and quotients of variables that may or may not includea and b.

Possibly relevant:

The FullForm shows that the power in the denominator is stored as a product of -1 and c:

Input:

FullForm[a^2/b^2]
FullForm[a^c_/b^c_]
FullForm[ (a / b)^2 ]
FullForm[(a / b)^c_ ]
FullForm[a^2 * b^(-2) ]
FullForm[a^c_ * b^(-c_)]

Output:

Times[Power[a,2],Power[b,-2]]
Times[Power[a,Pattern[c,Blank[]]],Power[b,Times[-1,Pattern[c,Blank[]]]]]
Times[Power[a,2],Power[b,-2]]
Power[Times[a,Power[b,-1]],Pattern[c,Blank[]]]
Times[Power[a,2],Power[b,-2]]
Times[Power[a,Pattern[c,Blank[]]],Power[b,Times[-1,Pattern[c,Blank[]]]]]

Edit: Bolded change to my actual case.


Generally speaking you should try to avoid doing mathematical manipulation using ReplaceAll which is a structural tool.

As opposed to FullForm, I will use TreeForm to illustrate these expressions:

a^2/b^2   // TreeForm
a^c_/b^c_ // TreeForm

Blanks in the denominator of a replacement rule

Blanks in the denominator of a replacement rule

You can see that while these expressions are mathematically similar, they are structurally quite different. You may be able to hammer out a functioning replacement rule for a specific case, but you will usually be better off using the Formula Manipulation (or Polynomial Algebra) tools that Mathematica provides.

If you carefully describe the mathematical manipulation you wish to achieve, I will attempt to provide a better solution.


As belisarius humorously points out in a comment, trying to force Mathematica to "see" or display expressions the way you do is often largely futile. This is one of the reasons that the opening statement above is true.


I agree with everything Mr.Wizard wrote. Having said that, a replacement rule that would work in this specific case would be:

a^2/b^2 /. (Times[Power[a,c_],Power[b,e_]]/; e == -c )-> d

or

a^2/b^2 /.  (a^c_ b^e_/; e == -c )-> d

Note that I added the constraint /; e == -c so that I effectively have a -c_ without actually creating the corresponding Times[-1,c_] expression


The primary reason a^2 / b^2 /. a^c_ / b^c_ -> d doesn't work is your using Rule (->) not RuleDelayed (:>). The second reason, as you found with FullForm, is that a/b is interpreted as Times[a, Power[b,-1]], so it is best to not use division. Making these changes,

a^2 / b^2 /. a^n_ b^m_ :> {n,m}

returns {2, -2}. Usually, you'll want to have a default value, so that

a / b^2 /. a^n_. b^m_. :> {n,m}

returns {1,-2}.

Edit: to ensure that the two exponents are equal, requires the addition of the Condition (/;)

a^2 / b^2 /. a^n_. b^m_. /; n == m :> n

Note: by using _. this will also catch a/b.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜