替换规则分母中的空格 [英] Blanks in the denominator of a replacement rule
问题描述
Mathematica 7.0 似乎不喜欢分母中有空格.谁能解释一下这是为什么?
Mathematica 7.0 seems to dislike having blanks in the denominator. Can anyone explain why this is?
输入:
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
输出:
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_.)
其中系数可能涉及变量的总和、乘积和商,这些变量可能包括也可能不包括a
和b
.
Where the coefficients may involve sums, products, and quotients of variables that may or may not includea
and b
.
可能相关:
FullForm 显示分母中的幂存储为 -1 和 c 的乘积:
The FullForm shows that the power in the denominator is stored as a product of -1 and c:
输入:
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_)]
输出:
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[]]]]]
对我的实际案例进行了粗体更改.
Bolded change to my actual case.
推荐答案
我同意 Wizard 先生写的所有内容.话虽如此,适用于这种特定情况的替换规则是:
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
或
a^2/b^2 /. (a^c_ b^e_/; e == -c )-> d
请注意,我添加了约束 /;e == -c
这样我就可以有效地使用 -c_
而无需实际创建相应的 Times[-1,c_]
表达式
Note that I added the constraint /; e == -c
so that I effectively have a -c_
without actually creating the corresponding Times[-1,c_]
expression
这篇关于替换规则分母中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!