在 Mathematica 中清除数值 [英] Clearing numerical values in Mathematica

查看:32
本文介绍了在 Mathematica 中清除数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理相当大的 Mathematica 项目,但出现的问题是我必须间歇性地检查数值结果,但想轻松地将我的所有构造恢复为分析形式.

I am working on fairly large Mathematica projects and the problem arises that I have to intermittently check numerical results but want to easily revert to having all my constructs in analytical form.

代码相当流畅我不想在任何地方使用范围结构,因为它们会增加工作开销.是否有一种简单的方法可以识别和清除所有数字分配?

The code is fairly fluid I don't want to use scoping constructs everywhere as they add work overhead. Is there an easy way for identifying and clearing all assignments that are numerical?

我确实知道范围界定是正确执行此操作的方法;-).然而,对于我的工作流程,我真的只是在寻找一个肮脏的技巧来在事后取消所有数字分配,而不是有远见来放下一个块.

I really do know that scoping is the way to do this correctly ;-). However, for my workflow I am really just looking for a dirty trick to nix all numerical assignments after the fact instead of having the foresight to put down a Block.

推荐答案

如果您的作业在顶级,您可以使用以下内容:

If your assignments are on the top level, you can use something like this:

a = 1;
b = c;
d = 3;
e = d + b;

Cases[DownValues[In],
   HoldPattern[lhs_ = rhs_?NumericQ] | 
   HoldPattern[(lhs_ = rhs_?NumericQ;)] :> Unset[lhs],
3]

如果您有足够的历史长度$HistoryLength(默认为无穷大),这将起作用.但是请注意,在上面的示例中,e 被分配了 3+c,而 3 在这里没有被撤销.所以,这个问题在表述上确实是模棱两可的,因为有些数字可以使它成为定义.避免这种情况的一种方法是使用 SetDelayed 进行分配,而不是使用 Set.

This will work if you have a sufficient history length $HistoryLength (defaults to infinity). Note however that, in the above example, e was assigned 3+c, and 3 here was not undone. So, the problem is really ambiguous in formulation, because some numbers could make it into definitions. One way to avoid this is to use SetDelayed for assignments, rather than Set.

另一种选择是分析 Global' 上下文中的名称(如果这是您的符号所在的上下文),然后说 OwnValues符号的 DownValues,以与上述类似的方式,并删除纯数字 rhs 的定义

Another alternative would be to analyze the names in say Global' context (if that is the context where your symbols live), and then say OwnValues and DownValues of the symbols, in a fashion similar to the above, and remove definitions with purely numerical r.h.s.

但是 IMO 这两种方法都不可靠.我仍然会使用范围结构并尝试隔离数字.一种可能性是将您的最终代码包装在 Block 中,并在此 Block 中分配数值.这似乎是一种更清洁的方法.工作开销很小 - 您只需要记住要将值分配给哪些符号.Block 会自动确保在它之外,符号没有定义.

But IMO neither of these approaches are robust. I'd still use scoping constructs and try to isolate numerics. One possibility is to wrap you final code in Block, and assign numerical values inside this Block. This seems a much cleaner approach. The work overhead is minimal - you just have to remember which symbols you want to assign the values to. Block will automatically ensure that outside it, the symbols will have no definitions.

编辑

另一种可能性是使用本地规则.例如,可以定义 rule[a] = a->1;rule[d]=d->3 而不是上面的赋值.然后您可以应用这些规则,将它们提取为说DownValues[rule][[All, 2]],每当你想用一些数字参数进行测试时.

Yet another possibility is to use local rules. For example, one could define rule[a] = a->1; rule[d]=d->3 instead of the assignments above. You could then apply these rules, extracting them as say DownValues[rule][[All, 2]], whenever you want to test with some numerical arguments.

这篇关于在 Mathematica 中清除数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆