在 Wolfram Mathematica 中释放 HoldAll?
我想为变量赋值(例如 c 表示光速或 G 表示万有引力常数),但直到最后一步为止都以符号方式计算公式。
如何以最短的方式做到这一点?
Replace 非常长且重复,而 HoldForm 如果嵌套则可能需要多个 RealeaseHold。
还有其他一些功能吗?
I want to assign values to variables (like c for speed of light or G for gravitational constant) but have formulas calculated symbolically until last step.
How is it possible to do this in shortest way?
Replace is very long and duplicating while HoldForm can require multiple RealeaseHold if nested.
Is there some other functions for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我们可以为常量定义 N 个值。例如:
这定义了
c
的数值。我们可以定义一个使用常量的函数:当我们正常计算这个函数时,它会以符号形式留下
c
:但是如果我们应用
N
,则c< /code> 得到数值计算:
We can define
N
values for our constants. For example:This defines a numerical value for
c
. We can define a function that uses the constant:When we evaluate this function normally, it leaves
c
in symbolic form:But if we apply
N
, thenc
gets evaluated numerically:一个例子会有所帮助。但如果我理解你,那么你
就可以编写
用所涉及符号的新值来评估符号表达式。
该表达式可以始终保持符号性,您可以简单地对其中的符号求值不同的值。
an example will help. But if I understood you, then you have
then you can write
to evaluate the symbolic expression with new values for the symbols involved.
The expression can remain symbolic all the time, and you can simply evaluates it for different values for the symbols in it.
我认为这个问题有两个部分。
(1)我们是否应该强制Mathematica以符号方式进行所有计算。这是(几乎总是)错误的。 Mathematica 可以处理任意精度的数字,因此我们应该更愿意告诉它物理常量的精度(当它们超过
$MachinePrecision
时),并让它选择最有效的方法来解决问题。(2)我们如何以符号形式打印中间步骤。为此,请使用
HoldForm[expr]
,然后应该按照您的指示给出评估结果。
I think this question has two parts.
(1) Whether we should force Mathematica to do all calculations symbolically. This is (almost always) wrong. Mathematica can do arbitrary precision numerics, so we should prefer to tell it the precision of our physical constants (when they exceed
$MachinePrecision
) and let it choose the most efficient way to solve the problem.(2) How do we print intermediate steps in symbolic form. For this, use
HoldForm[expr]
, and thenshould give you the evaluation results as you indicate.
对于“ReleaseHoldAll”函数,
MapAll
(缩写形式//@
)将函数映射到表达式的所有部分。因此,您可以使用:其中
expr
是包含任意级别的Hold
、HoldForm
等的表达式。Regarding a "ReleaseHoldAll" function,
MapAll
(short form//@
) maps a function to all parts of an expression. Therefore, you can use:where
expr
is your expression containingHold
,HoldForm
, etc., at any level.有时在mathematica中使用替换运算符会产生奇怪的属性。这与您应用它的上下文有关。上面的答案可能会很好用,但我个人总是使用 Block[{variable=number}, code] 命令,这使得变量在 Block 括号内充当全局变量,但是一旦在外部进行评估,变量将保持未声明状态。
像这样使用它:
给出输出:
并将答案全局设置为输出的值,以便您可以在 :
结果之后使用它:
There are strange attributes to using the replacement operator in mathematica sometimes. This has to do with the context in which you apply it. The above answer will probably work well, but personally I always use Block[{variable=number}, code] command, which makes the variables act as global within the Block brackets, but once the evaluation proceeded outside, the variables remain undeclared.
use it like this:
gives output:
and also sets answer globally to the value of the output so you can use it after :
results in: