在 SymPy 中自动填充矩阵元素

发布于 2024-11-27 09:29:40 字数 296 浏览 1 评论 0原文

有没有办法在 SymPy 中隐式定义符号矩阵的元素,遵循以下规则: symbol 后跟矩阵(或数字对)中的 subindices

例如,我想定义一个名为 M 的 3 x 2 矩阵,我希望 SymPy 自动创建它并将其填充为:

M = 
[ M_11 M_12]
[ M_21 M_22]
[ M_31 M_32]

如果没有办法隐式执行此操作,那么最简单的方法是什么方法来做到这一点明确地(例如循环)?

Is there a way to implicitly define the elements of a symbolic matrix in SymPy following a rule such as: symbol followed by subindices in the matrix (or pairs of numbers)

For example, I would like to define a 3 x 2 matrix called M, and I would like SymPy to automatically create it and populate it as:

M = 
[ M_11 M_12]
[ M_21 M_22]
[ M_31 M_32]

If there is no way to do this implicitly, what would be the easiest way to do this explicitly (e.g. looping)?

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

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

发布评论

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

评论(2

夜光 2024-12-04 09:29:40

考虑使用 MatrixSymbol 而不是 Matrix 对象。 MatrixSymbol 表示矩阵,无需显式元素。

In [1]: M = MatrixSymbol('M', 3, 2)

In [2]: M  # Just an expression
Out[2]: M

In [3]: Matrix(M)  # Turn it into an explicit matrix if you desire
Out[3]: 
⎡M₀₀  M₀₁⎤
⎢        ⎥
⎢M₁₀  M₁₁⎥
⎢        ⎥
⎣M₂₀  M₂₁⎦


In [4]: M.T * M   # Still just an expression
Out[4]: 
 T  
M ⋅M

In [5]: Matrix(M.T * M)  # Fully evaluate
Out[5]: 
⎡       2      2      2                                  ⎤
⎢    M₀₀  + M₁₀  + M₂₀        M₀₀⋅M₀₁ + M₁₀⋅M₁₁ + M₂₀⋅M₂₁⎥
⎢                                                        ⎥
⎢                                    2      2      2     ⎥
⎣M₀₁⋅M₀₀ + M₁₁⋅M₁₀ + M₂₁⋅M₂₀      M₀₁  + M₁₁  + M₂₁      ⎦

Consider using the MatrixSymbol rather than Matrix object. MatrixSymbol represents matrices without the need for explicit elements.

In [1]: M = MatrixSymbol('M', 3, 2)

In [2]: M  # Just an expression
Out[2]: M

In [3]: Matrix(M)  # Turn it into an explicit matrix if you desire
Out[3]: 
⎡M₀₀  M₀₁⎤
⎢        ⎥
⎢M₁₀  M₁₁⎥
⎢        ⎥
⎣M₂₀  M₂₁⎦


In [4]: M.T * M   # Still just an expression
Out[4]: 
 T  
M ⋅M

In [5]: Matrix(M.T * M)  # Fully evaluate
Out[5]: 
⎡       2      2      2                                  ⎤
⎢    M₀₀  + M₁₀  + M₂₀        M₀₀⋅M₀₁ + M₁₀⋅M₁₁ + M₂₀⋅M₂₁⎥
⎢                                                        ⎥
⎢                                    2      2      2     ⎥
⎣M₀₁⋅M₀₀ + M₁₁⋅M₁₀ + M₂₁⋅M₂₀      M₀₁  + M₁₁  + M₂₁      ⎦
我早已燃尽 2024-12-04 09:29:40

像这样的事情怎么样:

import sympy

M = sympy.Matrix(3, 2, lambda i,j:sympy.var('M_%d%d' % (i+1,j+1)))

编辑:我想我应该添加一个小解释。 sympy.Matrix() 的前两个参数将矩阵定义为 3x2(如您指定的)。第三个参数是一个lambda函数,它本质上是在一行中定义函数的简写方式,而不是使用def正式定义它。该函数将变量ij作为输入,它们通常是矩阵的索引。对于传递到 lambda 的每对 (i,j) (即,对于矩阵的每个元素),我们创建一个新的符号变量 M_ij sympy.var() 将字符串作为输入,定义新符号变量的名称。我们使用格式字符串 'M_%d%d' 即时生成此字符串,并用 (i+1,j+1) 填充它。我们将 1 添加到 ij 中,因为您希望矩阵为 1 索引,而不是像 Python 中的标准那样为 0 索引。

How about something like this:

import sympy

M = sympy.Matrix(3, 2, lambda i,j:sympy.var('M_%d%d' % (i+1,j+1)))

Edit: I suppose I should add a small explanation. The first two arguments to sympy.Matrix() are defining the matrix as 3x2 (as you specified). The third argument is a lambda function, which is essentially a shorthand way of defining a function in one line, rather than formally defining it with def. This function takes variables i and j as input, which conveniently are the indices of the matrix. For each pair (i,j) which are passed into the lambda (i.e., for each element of the matrix), we are creating a new symbolic variable M_ij. sympy.var() takes a string as input which defines the name of the new symbolic variable. We generate this string on-the-fly using the format string 'M_%d%d' and filling it with (i+1,j+1). We are adding 1 to i and j because you want the matrix to be 1-indexed, rather than 0-indexed as is the standard in Python.

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