制作定制的InputForm和ShortInputForm
我经常希望看到 Mathematica 图形对象的内部表示,而不是在 FullForm
中,而是在更具可读性的 InputForm
中,并且能够选择双击部分代码,然后轻松将此代码复制到新的输入 Cell
中。但默认的 InputForm
不允许这样做,因为 InputForm
默认显示为 String
,而不是 Mathematica'的代码。有没有办法让 InputForm
显示为 Mathematica 的代码?
我还经常希望看到这种 InputForm
的缩短版本,其中所有长坐标列表都显示为第一个坐标,后跟用 Skeleton
包裹的跳过坐标值的数量,所有空的列表
被删除,所有数字也被缩短以显示不超过6位数字。最好仅使用 6 位坐标来表示,但对于诸如 Hue
之类的颜色指令,仅显示 2 位有效数字。例如,
Plot[{Sin[x], .5 Sin[2 x]}, {x, 0, 2 \[Pi]},
Filling -> {1 -> {2}}] // ShortInputForm
应该给出:(
Graphics[GraphicsComplex[{{1.28228`*^-7, 1.28228*^-7}, <<1133>>},
{{{EdgeForm[], Directive[{Opacity[0.2], Hue[0.67, 0.6, 0.6]}],
GraphicsGroup[{Polygon[{{1133, <<578>>}}]}]},
{EdgeForm[], Directive[{Opacity[0.2], Hue[0.67, 0.6, 0.6]}],
GraphicsGroup[{Polygon[{{432, <<556>>}}]}]}}, {{Hue[0.67, 0.6,
0.6], Line[{1, <<431>>}]}, {Hue[0.91, 0.6, 0.6],
Line[{432, <<701>>}]}}}], {AspectRatio -> GoldenRatio^(-1),
Axes -> True, AxesOrigin -> {0, 0},
Method -> {"AxesInFront" -> True},
PlotRange -> {{0, 2*Pi}, {-1., 1}},
PlotRangeClipping -> True,
PlotRangePadding -> {Scaled[0.02], Scaled[0.02]}}]
注意 -0.9999998592131705
转换为 -1.
,1.2822827157509358*^-7
转换为 1.28228*^-7
和 Hue[0.9060679774997897, 0.6, 0.6]
转换为 Hue[0.91, 0.6, 0.6]
)。
通过这种方式,我希望将 InputForm
的输出作为 Mathematica 的代码,并且还有一个 ShortInputForm
函数,该函数将给出缩短的版本这段代码。有人可以帮助我吗?
关于问题的第一部分,我找到了一种方法来实现我想要的:
Plot[{Sin[x], .5 Sin[2 x]}, {x, 0, 2 \[Pi]}, Filling -> {1 -> {2}}] //
InputForm // StandardForm
I often wish to see the internal representation of Mathematica's graphical objects not in the FullForm
but in much more readable InputForm
having the ability to select parts of the code by double-clicking on it and easily copy this code to a new input Cell
. But the default InputForm
does not allow this since InputForm
is displayed by default as a String
, not as Mathematica's code. Is there a way to have InputForm
displayed as Mathematica's code?
I also often wish to see a shortened version of such InputForm
where all long lists of coordinates are displayed as the first coordinate followed by number of skipped coordinate values wrapped with Skeleton
, all empty Lists
removed and all numbers are also shortened for displaying no more than 6 digits. It would be even better to use 6 digits only for coordinates but for color directives such as Hue
display only 2 significant digits. For example,
Plot[{Sin[x], .5 Sin[2 x]}, {x, 0, 2 \[Pi]},
Filling -> {1 -> {2}}] // ShortInputForm
should give:
Graphics[GraphicsComplex[{{1.28228`*^-7, 1.28228*^-7}, <<1133>>},
{{{EdgeForm[], Directive[{Opacity[0.2], Hue[0.67, 0.6, 0.6]}],
GraphicsGroup[{Polygon[{{1133, <<578>>}}]}]},
{EdgeForm[], Directive[{Opacity[0.2], Hue[0.67, 0.6, 0.6]}],
GraphicsGroup[{Polygon[{{432, <<556>>}}]}]}}, {{Hue[0.67, 0.6,
0.6], Line[{1, <<431>>}]}, {Hue[0.91, 0.6, 0.6],
Line[{432, <<701>>}]}}}], {AspectRatio -> GoldenRatio^(-1),
Axes -> True, AxesOrigin -> {0, 0},
Method -> {"AxesInFront" -> True},
PlotRange -> {{0, 2*Pi}, {-1., 1}},
PlotRangeClipping -> True,
PlotRangePadding -> {Scaled[0.02], Scaled[0.02]}}]
(note that -0.9999998592131705
is converted to -1.
, 1.2822827157509358*^-7
is converted to 1.28228*^-7
and Hue[0.9060679774997897, 0.6, 0.6]
is converted to Hue[0.91, 0.6, 0.6]
).
In this way, I wish to have the output of InputForm
as Mathematica's code and also have a ShortInputForm
function which will give the shortened version of this code. Can anybody help me?
As to the first part of the question, I have found one way to achieve what I want:
Plot[{Sin[x], .5 Sin[2 x]}, {x, 0, 2 \[Pi]}, Filling -> {1 -> {2}}] //
InputForm // StandardForm
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新
shortInputForm
函数的最新版本可以在此处找到。原始帖子
这是另一个更好的解决方案(与 Mathematica 5 兼容):
它是如何工作的
这个解决方案基于简单的想法:我们需要阻止图形等内容的转换>、
Point
以及其他 排版表达式,以便将它们显示在内部表单中(作为适合输入的表达式)。令人高兴的是,如果我们这样做,结果会发现StandardForm
输出只是原始表达式的格式化(二维)InputForm
。这正是所需要的!但如何做到这一点呢?
首先,此转换是由
FormatValues
为
Symbol
定义,例如Graphics
、Point
等。可以获取此类的完整列表Symbol
通过评估以下内容:我的第一个想法是
阻止
所有这些Symbol
(并且它有效!):但是这种方法会导致对所有这些
Symbol
进行评估,并评估$ContextPath
中所有Symbol
的所有FormatValues
。我认为应该避免。阻止这些
FormatValues
的其他方法只是从$ContextPath
中删除上下文"System`"
。但只有当这些Symbol
尚未解析为"System`"
上下文时,它才有效。因此,我们首先需要将表达式转换为String
,然后从$ContextPath
中删除"System`"
上下文,最后将字符串向后转换为原来的表达。然后所有新的Symbol
都将与当前的$Context
(以及Graphics
、Point
等)相关联,因为它们不在$ContextPath
中)。为了防止上下文阴影冲突和乱七八糟的"Global`"
上下文,我将$Context
切换到"myTemp`"
,如有必要,可以轻松清除它。这就是
myInputForm
的工作原理。现在介绍一下
shortInputForm
。这个想法不仅是显示myInputForm
的缩短版本,而且还保留选择部分缩短代码并将其复制到新输入单元格并使用此代码的能力,因为它将是没有缩写的完整代码。在版本 6 及更高版本中,可以通过Interpretation
实现后者。为了与Mathematica
6 之前的版本兼容,我添加了一段代码,如果$VersionNumber
小于 6,则删除此功能。这是我工作时遇到的唯一问题与
Interpretation
的区别在于它没有SequenceHold
属性,因此我们不能简单地将Sequence
指定为Interpretation
的第二个参数。但是这个问题可以通过将序列包装在List
中,然后对其进行Apply
ingSequence
来轻松避免:请注意,我需要指定确切的上下文对于我使用的所有内置
Symbol
,因为在调用它们时"System`"
上下文不在$ContextPath
中。这结束了我在开发这些功能时做出的非标准决定。欢迎提出建议和意见!
UPDATE
The most recent version of the
shortInputForm
function can be found here.Original post
Here is another, even better solution (compatible with Mathematica 5):
How it works
This solution is based on simple idea: we need to block conversion of such things as
Graphics
,Point
and others to typeset expressions in order to get them displayed in the internal form (as expressions suitable for input). Happily, if we do this, the resultingStandardForm
output is found to be just formatted (two-dimensional)InputForm
of the original expression. This is just what is needed!But how to do this?
First of all, this conversion is made by
FormatValues
defined forSymbol
s likeGraphics
,Point
etc. One can get full list of suchSymbol
s by evaluating the following:My first idea was just
Block
all theseSymbol
s (and it works!):But this method leads to the evaluation of all these
Symbol
s and also evaluates allFormatValues
for allSymbol
s in the$ContextPath
. I think it should be avoided.Other way to block these
FormatValues
is just to remove context"System`"
from the$ContextPath
. But it works only if theseSymbol
s are not resolved yet to the"System`"
context. So we need first to convert our expression toString
, then remove"System`"
context from the$ContextPath
and finally convert the string backward to the original expression. Then all newSymbol
s will be associated with the current$Context
(andGraphics
,Point
etc. - too, since they are not in the$ContextPath
). For preventing context shadowing conflicts and littering the"Global`"
context I switch$Context
to"myTemp`"
which can be easily cleared if necessary.This is how
myInputForm
works.Now about
shortInputForm
. The idea is not just to display a shortened version ofmyInputForm
but also preserve the ability to select and copy parts of the shortened code into new input cell and use this code as it would be the full code without abbreviations. In version 6 and higher it is possible to achieve the latter withInterpretation
. For compatibility with pre-6 versions ofMathematica
I have added a piece of code that removes this ability if$VersionNumber
is less than 6.The only problem that I faced when working with
Interpretation
is that it has noSequenceHold
attribute and so we cannot simply specifySequence
as the second argument forInterpretation
. But this problem can easily be avoided by wrapping sequence inList
and thenApply
ingSequence
to it:Note that I need to specify the exact context for all built-in
Symbol
s I use because at the moment of calling them the"System`"
context is not in the$ContextPath
.This ends the non-standard decisions taken me in the development of these functions. Suggestions and comments are welcome!
此时我得出以下解决方案:
现在:
At this moment I have come to the following solution:
Now: