下标变量
有没有什么方法可以强制 Mathematica 独立于无下标变量来处理下标变量?更具体地说。比如说,我有以下定义:
Subscript[b, 1] = {{1, 2}}
Subscript[b, 2] = {{3, 4}}
b = Join[Subscript[b, 1], Subscript[b, 2]]
现在,当我使用
Subscript[b, 1]
Mathematica 时,当我希望它们成为三个独立值时,会将其替换为
Subscript[{{1, 2}, {3, 4}},1]
,因此更改 b 不会影响 Subscript[b, ..]。是否可以?
Is there any way to force Mathematica to treat subscripted variables independently of their unsubscripted counterparts? More specifically. Say, I have the following definitions:
Subscript[b, 1] = {{1, 2}}
Subscript[b, 2] = {{3, 4}}
b = Join[Subscript[b, 1], Subscript[b, 2]]
Now when I use
Subscript[b, 1]
Mathematica will substitute it with
Subscript[{{1, 2}, {3, 4}},1]
when I want these to be three independent values, so changing b will not affect Subscript[b, ..]. Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在对上一个SO问题的回答中,Mathematica 符号和语法模式 ,telefunkenvf14提到他是
,这本质上就是这个问题的目的。
WReach 指出Notation package 可以使用
Symbolize
非常简单地完成此操作,其中(如 Daniel 的回答)不要太担心
Box上面的
结构,因为您可以使用Notation
调色板更简单地输入这些内容。检查一切是否按预期工作:
注意:以上所有代码均已复制为输入文本,因此排版
SubscriptBox
已转换为输入表单Subscript
。但是,Symbolize
在框级别工作,因此测试需要转换回二维形式。为此,请选择代码(或单元格)并将其转换为标准形式 使用Cell
菜单或快捷方式Ctrl-Shift-N
。包含上述所有代码的笔记本应该看起来像In an answer to a previous SO question, Mathematica Notation and syntax mods, telefunkenvf14 mentioned that he was
which is essentially what this question is about.
WReach pointed out that the Notation package can do this quite simply using
Symbolize
Where (as in Daniel's answer) don't worry too much about the
Box
structure above as you can use theNotation
palette to enter this stuff in more simply.Check that it all works as wanted:
and
Note: all of the above code has been copied as Input Text, so the typeset
SubscriptBox
s have been converted to the input formSubscript
s. However, theSymbolize
works at the box level, so the tests need to be converted back to their 2d forms. To do this, select the code (or cells) and convert it to standard form by using theCell
menu or the shortcutCtrl-Shift-N
. The notebook with all the above code should look like如果您不想使用
Notation
包(请参阅 Daniel 的 和我的答案),但想要复制Symbolize
的行为,然后事情就变得有点棘手了。在阅读此SO答案后,我尝试这样做,但是遇到麻烦就放弃了。我会将代码作为社区 wiki 放在这里,以便其他人可以尝试完成它!
首先,您要截取输入的下标框结构并将其解释为“唯一”符号。以下代码
使输入的
x_i
成为名为"$sUbsCript$x$SPLIT$i"
的符号。不能保证是唯一的符号名称...但这将是一个相当不寻常的名称!备注:
1) 该代码不会获取以
FullForm
编写的下标。2)只有当下标的两个部分都是“简单”时,这个定义才会触发 - 没有空格,括号,运算符等...
接下来,因为这个符号名称太难看了,这里有一个可选的东西,可以在被问到时让它变得更好for (这可能应该改变)
最后,我们希望这个下标符号能够很好地打印出来。通常我们会使用
MakeBoxes
定义来执行此操作 - 但在本例中我们不能这样做,因为Symbol
具有属性Locked
:(< br>相反,我们将侵入
$PrePrint
来查找这些疯狂命名的符号并将它们作为下标写回:最后,如果您尝试将某些内容分配给下标符号,那么所有这些都会失败。 。我还没有尝试解决这个问题!
一些测试 - 请注意,您必须将
Subscript
转换为实际的框才能使代码正常工作。通过转换为标准格式来完成此操作:Ctrl-Shift-N。如果
Set
或SetDelayed
生成OwnValues
,则它不适用于它们,并且不适用于Information
确实适用于产生
DownValues
的定义If you don't want to use the
Notation
package (see Daniel's and my answers) but want to copy the behaviour ofSymbolize
, then it gets a little tricky.I had a go at doing this after I reading this SO answer but ran into troubles and gave up. I'll put the code here as community wiki so other people can try to finish it!
First you want to intercept an inputted subscript box structure and make it be interpreted as a "unique" symbol. The following code
makes an inputted
x_i
become a symbol named"$sUbsCript$x$SPLIT$i"
. Not a guaranteed unique symbol name... but it would a fairly unusual one!Notes:
1) that this code will not pick up subscripts written in
FullForm
.2) this definition only fires off if both parts of the subscript are "simple" - no spaces, brackets, operators, etc...
Next, because this symbol name is so ugly, here's an optional something to make it nicer when it's asked for (this probably should be changed)
Finally, we want this subscript symbol to print out nicely. Normally we'd do this using a
MakeBoxes
definition -- but we can't in this case becauseSymbol
has the attributeLocked
:(Instead, we'll hack in a
$PrePrint
to find these crazily named symbols and write them back as subscripts:Finally the place where all of this falls down is if you try to assign something to a subscripted symbol. I haven't tried working around this yet!
Some tests - note that you'll have to convert the
Subscript
s to actual boxes for the code to work. Do this by converting to StandardForm: Ctrl-Shift-N.It doesn't work with
Set
orSetDelayed
if they generateOwnValues
and it doesn't work withInformation
It does work with definitions that produce
DownValues
这是我用来执行此操作的一些代码。它也应该适合你:
有了这个,像
这样的定义在内部存储如下:
但它们显示为下标。
乍一看,我认为这可能就是西蒙的目标。
如果您的应用程序允许,您可能希望考虑采用类似 Mathematica 的命名约定,例如 FullDescriptiveCamelCase 变量名而不是下标变量。它最终将使您的代码更加可移植,并且最终会成为第二天性。
Here's some code I used to use to do this. It should work for you too:
With this, definitions like
are internally stored like this:
But they display as subscripts.
From a quick look I think this may be what Simon was aiming for.
If your application allows it, you may wish to consider adopting Mathematica-like naming conventions such as FullyDescriptiveCamelCase variable names instead of subscripted variables. It will make your code more portable in the end, and it does become second nature eventually.
可以使用同名包中的符号。
不要介意下面的代码,你没有弄清楚 RowBox 的结构。只需使用调色板模板并在左侧输入 Subscript[b,j_],然后在右侧输入 bb[j_]。因此,“实际”变量现在是 bb[1] 等,您可以安全地分配给 b。
输出[3]= {{1, 2}}
输出[4]= {{3, 4}}
输出[5]= {{1, 2}, {3, 4}}
输出[6]= {{1 , 2}}
你可能会得到更准确的回复,这是我第一次使用 Notation 包。
丹尼尔·利希布劳
沃尔夫勒姆研究公司
Could use Notation from the package of same name.
Don't mind the code below, you don't figure out that RowBox structure. Just use the palette template and type Subscript[b,j_] into the left side, and, say, bb[j_], into the right. So the "actual"variables are now bb[1] etc., and you can assign safely to b.
Out[3]= {{1, 2}}
Out[4]= {{3, 4}}
Out[5]= {{1, 2}, {3, 4}}
Out[6]= {{1, 2}}
You'll probably get more accurate replies, this is the first I ever mucked with the Notation package.
Daniel Lichtblau
Wolfram Research
符号是
b
,而不是Subscript[b, _]
。当你定义:
就像为任何函数 f 定义一个下值。喜欢做:
那么,你所做的
当然就是为符号b赋值。
现在
正如预期的那样。
所以,我想简短的回答是否定的。至少不是以一种直接的方式。
编辑
虽然上述内容是正确的(我相信),但我不知道 Notation 包有办法规避默认行为。其他答案包含详细信息。
The symbol is
b
, and notSubscript[b, _]
.When you define:
is like defining a downvalue for any function f. Like doing:
So, what you are doing is
Which of course assigns a value to the symbol b.
and now
As expected.
So, I guess the short answer is no. At least not in a straightforward way.
Edit
While the above is true (I believe), I wasn't aware that the Notation package has a way to circumvent the default behavior. Other answers contain the details.
我通过将各种回复粘贴到 Mathematica 笔记本中来详细研究了下标变量的线程(并尝试使用版本 7 和 8)。然而,我发现在某些情况下,下标变量的显式表示为
Subscript[a,b]
并不能给出这些答案中包含的正确答案。然而,当我明确使用下标( a_b )的二维表示法时,答案正如预期的那样。难道当将下标符号粘贴到电子邮件中时,它们会被表示为下标[a,b]
。 (当然,我应该补充一点,对于每个单独的贡献,我在使用Quit[ ]
之后重新启动了 Mathematica )。I studied the thread on subscripted variables in detail by pasting the various replies into a Mathematica notebook (and tried it with version 7 and 8). However, what I found out that in some cases the explicit representation of subscripted variables as
Subscript[a,b]
does not give the correct answer as contained in these answers. However, when I used explicitly the 2d notation for subscript ( a_b ) the answer was as expected. Could it be that when pasting subscripted symbols into an email they are represented asSubscript[a,b]
. (Of course, I should add that for each individual contributions I started Mathematica fresh - after usingQuit[ ]
).