Sagemath:SyntaxError:关键字可以是表达式

发布于 2025-01-23 15:02:50 字数 952 浏览 3 评论 0原文

最后,我想替换qij^2 = qiji,j = 1,2,3,4 并得到错误:

SyntaxError: keyword can't be an expression

代码:

sage: f1 = 2*x1 + x2 + 1
sage: f2 = -x1 + 5*x2 - 2
sage: e1 = expand(f1^2)
sage: e2 = expand(f2^2)
sage: var('q11, q12, q13, q14, q21, q22, q23, q24')
(q11, q12, q13, q14, q21, q22, q23, q24)
sage: E1 = e1.substitute(x1=q11+(2q12)-q13-(2q14), x2=q21+(2q22)-q23-(2q24))
sage: E2 = e2.substitute(x1=q11+(2q12)-q13-(2q14), x2=q21+(2q22)-q23-(2q24))
sage: E1a = expand(E1)
sage: E2a = expand(E2)
sage: E1aa = E1a.substitute(q11^2=q11, q12^2=q12, q13^2=q13, q14^2=q14,
....:                       q21^2=q21, q22^2=q22, q23^2=q23, q24^2=q24)
sage: E2aa = E2a.substitute(q11^2=q11, q12^2=q12, q13^2=q13, q14^2=q14,
....:                       q21^2=q21, q22^2=q22, q23^2=q23, q24^2=q24)
sage: E1aa, E2aa

At the end I wanted to substitute qij^2=qij, i,j=1,2,3,4
and got the error:

SyntaxError: keyword can't be an expression

The code:

sage: f1 = 2*x1 + x2 + 1
sage: f2 = -x1 + 5*x2 - 2
sage: e1 = expand(f1^2)
sage: e2 = expand(f2^2)
sage: var('q11, q12, q13, q14, q21, q22, q23, q24')
(q11, q12, q13, q14, q21, q22, q23, q24)
sage: E1 = e1.substitute(x1=q11+(2q12)-q13-(2q14), x2=q21+(2q22)-q23-(2q24))
sage: E2 = e2.substitute(x1=q11+(2q12)-q13-(2q14), x2=q21+(2q22)-q23-(2q24))
sage: E1a = expand(E1)
sage: E2a = expand(E2)
sage: E1aa = E1a.substitute(q11^2=q11, q12^2=q12, q13^2=q13, q14^2=q14,
....:                       q21^2=q21, q22^2=q22, q23^2=q23, q24^2=q24)
sage: E2aa = E2a.substitute(q11^2=q11, q12^2=q12, q13^2=q13, q14^2=q14,
....:                       q21^2=q21, q22^2=q22, q23^2=q23, q24^2=q24)
sage: E1aa, E2aa

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断舍离 2025-01-30 15:02:50

如果您只想替换一个变量,则可以使用=语法:e1a.substitute(q11 =(whywather))。但是,如果您想代替更复杂的表达式,则需要使用其他语法:

E1a.substitute(q11^2=q1)    # will raise an error
E1a.substitute(q11^2==q1)   # should work
E1a.substitute({q11^2: q1}) # should work

e1a.substitute产生的帮助消息?提供了一些示例。

If you want to just substitute for a variable, you can use the = syntax: E1a.substitute(q11=(whatever)). If, though, you want to substitute for a more complicated expression, you need to use a different syntax:

E1a.substitute(q11^2=q1)    # will raise an error
E1a.substitute(q11^2==q1)   # should work
E1a.substitute({q11^2: q1}) # should work

The help message produced by E1a.substitute? gives some examples.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文