Advertisement
lumeng

Untitled

Jan 2nd, 2019
173
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. = 在 Wolfram 语言里的涵义和很多别的编程语言里不太一样的。 你这样做等于在重新设置 Symbol 的运算规则,比如
  2.  
  3. In[26]:= Symbol["foobar"] = 2
  4. During evaluation of In[26]:= Set::write: Tag Symbol in Symbol[foobar] is Protected.
  5. Out[26]= 2
  6.  
  7. 也会给一样的报错。因为 Symbol 是系统符号,其运算规则被系统保护了,不能随意重新定义。另如重新定义 Sin 的运算规则也会报错:
  8.  
  9. In[28]:= Sin[Pi] = 2
  10. During evaluation of In[28]:= Set::write: Tag Sin in Sin[\[Pi]] is Protected.
  11. Out[28]= 2
  12.  
  13. 这样解释如果你无法理解,可以暂时跳过。 这个概念也许你可以深入看看 down values(https://reference.wolfram.com/language/ref/DownValues.html.zh)
  14.  
  15.  
  16. 如果你真的需要这样定义一堆 f?? 的符号,也许可以这样:先把不要的那些 f?? 符号清除掉:
  17.  
  18. In[23]:= Names["Global`*"]
  19. Out[23]= {"dir", "f11", "f12", "f13", "f21", "f22", "f23", "f31", \
  20. "f32", "f33", "i", "j", "s"}
  21. In[24]:= Remove /@ %[[2 ;; 10]]
  22. Out[24]= {Null, Null, Null, Null, Null, Null, Null, Null, Null}
  23. In[25]:= Names["Global`*"]
  24. Out[25]= {"dir", "i", "j", "s"}
  25.  
  26. 有可能用 Quit 重启 Mathematica 运算核也可以,看你同一个 session 里有否别的需要保持的结果。然后用 With 先执行 Symbol 产生 f?? 这些符号,然后定义它们的值:
  27.  
  28. In[19]:= Table[With[{s = Symbol["f" <> ToString[i] <> ToString[j]]}, s = i*j];, {i, 3}, {j, 3}]
  29. Out[19]= {{Null, Null, Null}, {Null, Null, Null}, {Null, Null, Null}}
  30. In[20]:= f11
  31. Out[20]= 1
  32.  
  33. 知乎的计算机程序排版功能实在太差,浪费了我很多时间,居然还无法保存回复,而且还不允许我修改原来的回答。这类编程问题最好到更成熟和专门的论坛问 (https://mathematica.stackexchange.com/, https://community.wolfram.com/),得到好回答的机会更大、速度更快。
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement