Blanks in the denominator of a replacement rule

假装没事ソ 提交于 2019-12-23 17:18:37

问题


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^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.


回答1:


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




回答2:


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

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.




回答3:


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.



来源:https://stackoverflow.com/questions/7168040/blanks-in-the-denominator-of-a-replacement-rule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!