内省 Mathematica 中生成的消息
有什么方法可以获取 Mathematica 中表达式求值期间生成的实际消息吗?假设我正在数值求解 ODE,它会崩溃,就像这样
In[1] := sol = NDSolve[{x'[t] == -15 x[t], x[0] == 1}, x, {t, 0, 1},
Method -> "ExplicitEuler"];
在这种情况下,我会收到 NDSolve::mxst
错误,告诉我在 处达到了 10000 步的最大数量>t == 0.08671962566152185
。现在,如果我查看 $MessageList
变量,我只会收到消息名称;特别是,NDSolve
决定退出时有关 t
值的信息已丢失。
现在,我始终可以使用标准附加包之一中的 InterpolatingFunctionDomain 函数从 sol 获取该信息,但如果我能以某种方式将其从消息中提取出来,这会很有帮助的。
Is there any way to get at the actual messages generated during the evaluation of an expression in Mathematica? Say I'm numerically solving an ODE and it blows up, like so
In[1] := sol = NDSolve[{x'[t] == -15 x[t], x[0] == 1}, x, {t, 0, 1},
Method -> "ExplicitEuler"];
In this case, I'll get the NDSolve::mxst
error, telling me the maximum number of 10000 steps was reached at t == 0.08671962566152185
. Now, if I look at the $MessageList
variable, I only receive the message name; in particular, the information about the value of t
where NDSolve
decided to quit has been lost.
Now, I can always get that information from sol
using the InterpolatingFunctionDomain
function from one of the standard add-on packages, but if I can somehow pull it out of the message, it would be quite helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您也许可以使用 $MessagePrePrint 设置一个函数来存储每条消息以供以后检索。
You might be able to use $MessagePrePrint to set up a function which would store away each of the messages for later retrieval.
我不知道这是否可行,但如果您唯一想知道的是出错时特定参数的值,那么获取它们的一种笨拙方法是在全局范围内使用虚拟值定义这些变量。这适用于循环计数器,但我不知道它是否在
NDSolve
中工作。另一个拼凑方法是使 t 动态 并使用 t 计算单元格。更优雅(可能也是正确)的方法是使用 Reap 和播种。
I don't know if this will work, but if the only thing you want to know are the values of specific parameters at the point of error then a kludgy way of getting them would be to define those variables with dummy values globally. This works with loop counters, but I don't know if it works from within
NDSolve
. Another kludge would be to make t Dynamic and have an evaluated cell with t.A more elegant (and probably the correct) approach would be to use Reap and Sow.