如何告诉 Mathematica 将 0 替换为 0 的 1 次方?
专家;
给定
f = (#1^#2) &
是否有一种方法可以定义上面的 'f' ,使得如果 #1 和 #2 都为零,那么纯函数 'f' 的值应该为 1 ?
这样当我写入时
f[0,0]
它会返回 1 而不是不确定?
顺便说一句,我知道我可以写,
f = (If[#1 == 0 && #2 == 0, 1, #1^#2]) &
但我想要一个通用规则或模式,所以我不必编写这些检查,因为纯函数可能会更复杂(其中有很多 # ),而且我不想做很多这些检查'if then else' 检查可能出现的每个可能的 0^0。
谢谢
更新:
也许我应该进一步澄清我这样做的原因。
我让用户从菜单中选择一个功能。其功能为
a x^n0 + b y^n1 + c x^n2 y^n3
其中,在上面,参数'n0'、'n1'、'n2'和'n3'也可以从滑块中选择,并且这些可以为零。
现在,“x”和“y”是坐标,它们也可以为零。
因此,在对上述函数求值时,有可能会遇到0^0。
自己做的时候,有很多情况需要检查。例如 'y^n3' 可以是 0^0 而不是另一个,y^n1 可以是 0^0 而不是另一个,x^n2 y^n3 可以是 0^0 而不是其他,等等。 ,所以我必须定义许多不同的情况。 (我认为有 16 种可能的情况)。
我正在努力避免这种情况。如果我可以告诉 Mathematica 在较低级别用 1 替换 0^0,那么生活会更简单。
2011 年 12 月 7 日更新 感谢大家的回答和评论,都非常有用,解决了我的问题,我从中学到了东西。
我选择了 Leonid 答案,因为它允许我用最少的额外编码来解决我的问题。
这是一个小例子,
Manipulate[Row[{format[x, n], "=", eval[x, n]}],
{{x, 0.0, "x="}, 0, 1, .1, Appearance -> "Labeled"},
{{n, 0.0, "n="}, 0, 1, .1, Appearance -> "Labeled"},
Initialization :>
(
format[x_, n_] := HoldForm["(" x ")"^n];
eval = Unevaluated[#1^#2] /. HoldPattern[0.0^0.0] :> 0.0 &
)
]
我在代码中到处使用实数(它是一个数值 pde 求解器),所以这就是为什么我在上面使用 0.0 而不是 0^0 来适应我正在做的事情。
Experts;
Given
f = (#1^#2) &
Is there a way to define 'f' above such that if #1 and #2 are both zero, then the value of the pure function 'f' should be 1 ?
so that when I write
f[0,0]
it will return 1 and not Indeterminate?
btw, I know I can write
f = (If[#1 == 0 && #2 == 0, 1, #1^#2]) &
But I wanted a general rule or pattern, so I do not have to write these checks, as the pure function can be more complicated (many # in it ) and I do not want to do many of these 'if then else' checks for each possible 0^0 that might show up.
thanks
Update:
May be I should clarify more why I am doing this.
I have a user selects a function from a menu. The function is
a x^n0 + b y^n1 + c x^n2 y^n3
Where in the above, the parameters 'n0', 'n1', 'n2' and 'n3' also can be selected from sliders, and these can be zero.
Now, 'x' and 'y' are coordinates, and these can be zero also.
Therefore, it is possible that 0^0 can be encountered when evaluating the above function.
There are many cases to check for, when doing it myself. For example 'y^n3' can be 0^0 and not the other, y^n1 can be 0^0 and not the other, x^n2 y^n3 can be both 0^0 and not the others, etc.., and so I have to define many different cases. (16 possible cases I think).
And I am trying to avoid this. If I can tell Mathematica to replace 0^0 by 1 at a lower level, then life will be simpler.
Update 12/7/11
Thanks for everyone's answers and comments, all are very useful and solve my problem and I learned from them.
I selected Leonid answer as it allowed me to solve my problem with least amount of additional coding.
Here is an small example
Manipulate[Row[{format[x, n], "=", eval[x, n]}],
{{x, 0.0, "x="}, 0, 1, .1, Appearance -> "Labeled"},
{{n, 0.0, "n="}, 0, 1, .1, Appearance -> "Labeled"},
Initialization :>
(
format[x_, n_] := HoldForm["(" x ")"^n];
eval = Unevaluated[#1^#2] /. HoldPattern[0.0^0.0] :> 0.0 &
)
]
I use real numbers everywhere in my code (it is a numerical pde solver), so that is why I used 0.0 in the above and not 0^0 to fit with what I am doing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当然,在Mathematica中有很多方法可以做事,但我经常使用的一个设计习惯是用递减的特异性来编写“函数”(实际上是一种模式)。 Mathematica 具有这样的属性:它会先应用更具体的模式,然后再应用不太具体的模式。
因此,对于您的情况,我只是写:
我假设您希望使用整数值,因为这是此类情况的通常上下文,例如在评估伯恩斯坦基函数时。
Of course there are many ways to do things in Mathematica, but a design idiom I often use is to write the "function" (actually, a pattern) with decreasing specificity. Mathematica has the property that it will apply more specific patterns before less specific.
So, for your case I'd just write:
I assume you expect to work with integer values since that's the usual context for this type of situation, e.g. when evaluating Bernstein basis functions.
我同意@Deep Yellow的答案,但是如果您坚持使用纯函数,这里有一种方法:
编辑
保持在纯函数的范围内,您在编辑中描述的情况可以得到解决与我对您的特定原始示例的解决方案相同。您可以通过一点点元编程来自动化此操作,定义以下函数转换器:
然后,我之前的代码可以重写为:
但是您可以更一般地这样做,例如:
I agree with the answer of @Deep Yellow, but if you insist on a pure function, here is one way:
EDIT
Staying within the realm of pure functions, the situation you described in your edit can be addressed in the same way as my solution to your specific original example. You can automate this with a tiny bit of metaprogramming, defining the following function transformer:
Then, my previous code can be rewritten as:
But you can is this more generally, for example:
您可以尝试这样写:
f = Quiet[Check[#1^#2,1]] &
。Quiet
将抑制“Power::indet: “遇到不确定表达式 0^0。”
消息,Check
将用替换结果>1
如果它是不确定的,最好使用像
s = Quiet[Check[#1, 1]]
这样的函数并将表达式包装在其中。You may try writing it like
f = Quiet[Check[#1^#2,1]] &
.Quiet
will suppress the"Power::indet: "Indeterminate expression 0^0 encountered."
message andCheck
will replace the result with1
if it is indeterminate.It is probably better to use some function like
s = Quiet[Check[#1, 1]]
and wrap your expressions in it.我有点惊讶这个微不足道的(尽管有点危险)修复没有在前面提到。如果您确实不希望表达式
0^0
在任何上下文中出现,而您 (a) 担心它出现,或者 (b) 希望它将其计算为某个值除了 1 之外,您可以简单地尝试一下。在复杂函数多次调用
x^n
形式的表达式(其中x
为实数)的情况下,我需要此修复n 是一个整数,在这种情况下0^0
应被视为x^0=1
的限制,因为x
变为 0。不过,值得注意的是,这样做会“污染”当前内核会话的
Power
,因此可能会破坏同时运行且条件 (a) 和 (b) 不成立的其他笔记本。由于Power
位于System??
上下文中,而不是Global??
中,因此可能很难分离不同笔记本的上下文以避免产生冲突通过此修复。I'm slightly surprised the trivial (albeit slightly dangerous) fix did not get mentioned earlier. If you really don't expect the expression
0^0
to come up in any context where you'd (a) be worried that it did, or (b) would like it to evaluate it to something other than 1, you can simply tryI needed this fix in a situation where a complicated function had a number of calls to expressions of the form
x^n
wherex
is real andn
is an integer, in which case0^0
should be seen as the limit ofx^0=1
asx
goes to 0.It's important to note, though, that doing this will 'contaminate'
Power
for the current kernel session, and may therefore break other notebooks which run concurrently and for which conditions (a) and (b) may not hold. SincePower
is located in theSystem՝
context instead ofGlobal՝
, it may be difficult to separate the contexts of different notebooks to avoid clashes produced by this fix.