英文:
subscripts in sympy and python
问题
我正在学习sympy和python,通过研究我在网上找到的示例代码。我遇到了这行代码
z = symbols("z_{0:2}")
它创建了变量z_0和z_1。这非常方便!现在,z_{0:2}的构造方式让我想起了基本python中的range函数。我的问题是:z_{0:2}是用于定义带下标变量的临时技巧,还是通用sympy/python构造的特殊实例?如果是的话,这个通用构造是什么,你能指导我去哪里学习更多关于它的在线文档吗?
我是一个完全的新手,感谢你的耐心。
英文:
I'm learning sympy and python by studying sample codes I found online. I came across this line of code
z = symbols("z_{0:2}")
which creates the variables z_0 and z_1. This is very handy! Now, the z_{0:2} construction reminds me of the range function in basic python. My question: Is this z_{0:2} an ad-hoc trick just for defining variables with subscripts, or is this a special instance of a general sympy/python construction? If so, what is this general construction, and can you point me to a couple of onlin documentation where I can learn more about this?
I'm a complete newbie; thanks for your patience.
答案1
得分: 4
symbols
函数的文档写得很好,有很多示例,所以我不会在这里重复介绍。
这个函数实现了"范围语法",以减少创建索引元素时的输入。请注意,我使用了"元素"一词,而不是"符号",因为symbols
函数可以用来创建普通符号、通配符符号(用于模式匹配)、未定义函数、索引对象等。例如:
s1, s2, s3, s4, s5 = symbols("s1:6")
f1, f2, f3, f4, f5 = symbols("f1:6", cls=Function)
i1, i2, i3, i4, i5 = symbols("i1:6", cls=Idx)
请注意:
- 在上面的示例中,我没有使用大括号。
- 实际名称和数字下标之间没有下划线字符。
- 如果您使用支持Latex渲染的交互式环境(例如Jupyter Notebook),您将看到上面的符号和函数将以下标形式渲染。这是
LatexPrinter
的默认行为。
下划线和大括号可用于创建更复杂的符号。例如,如果我想创建表示储罐内氢气温度的符号,我会这样做:
T = symbols("T_{H_{2}}^{tank}")
T
这在交互式环境中渲染得很好,但在非交互式环境中很难理解。
编辑以满足评论要求:这真的取决于您想要实现的目标。例如,使用符号列表:
N = 6
z = symbols("z_{0:%s}" % N) # 符号列表
sum([z[(-1)**n + 1] for n in range(N)])
# 输出: 3*z_{0} + 3*z_{2}
或者使用索引对象:
N = 5
z = IndexedBase("z")
expr = Sum(z[(-1)**n + 1], (n, 0, N))
print(expr)
# 输出: Sum(z[(-1)**n + 1], (n, 0, 5))
print(expr.doit())
# 输出: 3*z[0] + 3*z[2]
如果您查看屏幕上呈现的Latex,这两个示例的最后输出是相同的,但它们在概念上非常不同。
英文:
The documentation of the symbols
function is well written with plenty of examples, so I'm not going to repeat it here.
The "range syntax" is implemented by this function in order to reduce typing while creating indexed elements. Note that I used the word "elements" and not "symbols", because the symbols
function can be used to create ordinary symbols, wild symbols (for pattern matching), undefined functions, indexed objects, etc. For example:
s1, s2, s3, s4, s5 = symbols("s1:6")
f1, f2, f3, f4, f5 = symbols("f1:6", cls=Function)
i1, i2, i3, i4, i5 = symbols("i1:6", cls=Idx)
Note that:
- I haven't used curly parentheses in the above examples.
- there is no
_
(underscore) character between the actual name and the numeric subscript. - If you are using an interactive environment which render latex (for example, Jupyter Notebook), you will see that the above symbols and function will be rendered with a subscript. That's the default behavior of the
LatexPrinter
.
Underscores and curly parentheses can be used to create more complicated-looking symbols. For example, if I want to create a symbol representing the temperature of hydrogen inside a tank I would do something like this:
T = symbols("T_{H_{2}}^{tank}")
T
This renders nicely on an interactive environment, but it would be very hard to understand on a non-interactive environment.
EDIT to satisfy comment: it really depends on what you are trying to achieve. For example, using a list of symbols:
N = 6
z = symbols("z_{0:%s}" % N) # list of symbols
sum([z[(-1)**n + 1] for n in range(N)])
# out: 3*z_{0} + 3*z_{2}
or using an indexed object:
N = 5
z = IndexedBase("z")
expr = Sum(z[(-1)**n + 1], (n, 0, N))
print(expr)
# out: Sum(z[(-1)**n + 1], (n, 0, 5))
print(expr.doit())
# out: 3*z[0] + 3*z[2]
If you look at the latex rendered on the screen, the last output of both examples are identical, yet they are conceptually very different.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论