2

我有一个小问题。我已经搜索了很长时间,但没有找到答案(帮助,我是初学者)。我想在mathematica 9中制作自己的物理计算笔记本。笔记本应该易于使用,并且应该能够快速获得概览(因为我不想每次都使用数量)。我想像在教科书中一样显示我的方程式。例如,我的问题是 ElectronMass 所需的下标。它们需要成为一个符号,因为我想保护这些价值观。但它不起作用。我可以更改 m 并为 Electronmass 获得不同的值。这是我的笔记本:

Needs["Notation`"];
Symbolize[ParsedBoxWrapper[SubscriptBox["_", "_"]]]

Subscript[m, e] = Quantity["ElectronMass"];
Subscript[m, p] = Quantity["ProtonMass"];
Subscript[m, n] = Quantity["NeutronMass"];
Protect[Subscript[m, el], Subscript[m, p], Subscript[m, n]];

到目前为止一切都很好......但是后来发生了这种情况......

In[19]:= m = 5

Out[19]= 5

In[20]:= Subscript[m, e ]

Out[20]= Subscript[5, e]

相反,我想要

In[20]:= Subscript[m, e ]

Out[20]= Subscript[m, e]

只是将 Subscript[m, e] 与与“m”或“e”有关的任何事情独立对待,感谢您的帮助

4

3 回答 3

2

一个简单的解决方法就是保护您要使用的所有符号,所以而不是

Protect[Subscript[m, el], Subscript[m, p], Subscript[m, n]];

你需要写

Protect[m,el,n];

这样,一旦您设置了值

Subscript[m, n] = 2;
Subscript[m, el] = 4;

稍后在评估它们时,您可以保证返回正确的值。作为一个简短的说明,您可能希望为某些动机重新定义下标,例如

Subscript[a_, b_] := a[b]
于 2013-08-01T13:25:03.820 回答
1

除了 Artur 的方法之外,您还可以设置HoldAllSubscript阻止评估这些符号的方法。这不应该轻易完成,因为它会影响所有的使用,Subscript但如果你希望这种行为是普遍的,它可能正是你想要的。

SetAttributes[Subscript, HoldAll]

m = 5; e = 3;

Subscript[m, e]
(* Out= *) Quantity["ElectronMass"]

专用 StackExchange 站点:
在此处输入图像描述

于 2013-08-05T21:35:13.893 回答
0

In addition to the excellent Hold (your breath) and Protect (your precious variables) answers you got so far, I rather suggest you stop and think for a moment.

Is this a limitation/quirk of MMA or rather a limitation of the logic/style of your paper/notebook?

After all: you go at great lengths to define m's with subscripts to indicate specific masses. Then why on earth do you want to define a quantity (constant, perhaps) just m without subscrpts?

Is it a mass? then give it a subscript to indicate what kind of mass it is.

Is it not a mass? Then call it something else. Use a longer - more descrptive - variable name, or mm if you have really run out of creativity.

In general do not confuse your readers with similar symbols having widely different meanings.

Also , I would not type

Symbolize[ParsedBoxWrapper[SubscriptBox["_", "_"]]]

since this is way too general. I would type

Symbolize[ParsedBoxWrapper[SubscriptBox["m", "_"]]]

which restricts the kind of physical objects with pretty subscripts. Of course, you can always later add

Symbolize[ParsedBoxWrapper[SubscriptBox["q", "_"]]]

if you need to describe other subscripted quantities (say charges).,

于 2013-08-10T09:58:12.440 回答