【问题标题】:Mathematica: How to prevent evaluation of In[-1]?Mathematica:如何防止对 In[-1] 进行评估?
【发布时间】:2011-02-17 09:18:22
【问题描述】:

当我尝试使用 In[-1] 在 Wolfram Mathematica 5.2 中获取最后一个输入表达式时,我得到了评估输出:

In[1]:= On[In]
2 + 2
In[-1]

Out[2]= 4

During evaluation of In[1]:= In::trace: In[-1] --> In[$Line-1]. >>

During evaluation of In[1]:= In::trace: In[$Line-1] --> In[2]. >>

During evaluation of In[1]:= In::trace: In[2] --> 2+2. >>

Out[3]= 4

如何获得未计算形式的输入表达式?

【问题讨论】:

  • 我完全不知道您要使用哪种语言或程序来完成此任务。
  • @Michel 这是 Wolfram Mathematica 5.2。这是在标签中,现在在问题的标题和正文中。

标签: wolfram-mathematica


【解决方案1】:

你可以试试InString[]

In[1]:= 2 + 2
Out[1]= 4

In[2]:= InString[-1]
Out[2]= "\\(2 + 2\\)"

【讨论】:

  • 谢谢。 DisplayForm@ToExpression[InString[-1]] 工作正常。
【解决方案2】:

这是另一种方式:

In[1]:= 2 + 2
With[{line = $Line - 1}, HoldForm[In[line]] /. DownValues[In]]

Out[1]= 4

Out[2]= 2+2

编辑:

此方法不适用于Unevaluated

In[1]:= Unevaluated[2 + 2]
With[{line = $Line - 1}, HoldForm[In[line]]] /. (DownValues[In])
Out[1]= Unevaluated[2 + 2]
Out[2]= 2 + 2

我开了一个separate question on this

【讨论】:

    猜你喜欢
    • 2012-08-30
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多