gpt4 book ai didi

矩阵乘法的符号表示法

转载 作者:行者123 更新时间:2023-12-04 23:13:18 25 4
gpt4 key购买 nike

在 numpy 中定义矩阵和点积是标准的,如下所示

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([[7, 8, 9, 10], [11, 12, 13, 14], [15, 16, 17, 18]])
print(a.shape)
print(b.shape)
print(a.dot(b).shape)

按预期输出:
(2, 3)
(3, 4)
(2, 4)

但令我惊讶的是,以下内容在 sympy 中失败
import sympy as sp
a = sp.Matrix([[1, 2, 3], [4, 5, 6]])
b = sp.Matrix([[7, 8, 9, 10], [11, 12, 13, 14], [15, 16, 17, 18]])
print(a.shape)
print(b.shape)
print(a.dot(b).shape)

哪个输出
(2, 3)
(3, 4)

---------------------------------------------------------------------------
ShapeError Traceback (most recent call last)
<ipython-input-30-50c934c7fbaf> in <module>()
4 print(a.shape)
5 print(b.shape)
----> 6 print(a.dot(b).shape)

~/miniconda3/lib/python3.6/site-packages/sympy/matrices/matrices.py in dot(self, b)
2389 mat = mat.T
2390 b = b.T
-> 2391 prod = flatten((mat * b).tolist())
2392 if len(prod) == 1:
2393 return prod[0]

~/miniconda3/lib/python3.6/site-packages/sympy/core/decorators.py in binary_op_wrapper(self, other)
130 else:
131 return f(self)
--> 132 return func(self, other)
133 return binary_op_wrapper
134 return priority_decorator

~/miniconda3/lib/python3.6/site-packages/sympy/matrices/common.py in __mul__(self, other)
2006 if self.shape[1] != other.shape[0]:
2007 raise ShapeError("Matrix size mismatch: %s * %s." % (
-> 2008 self.shape, other.shape))
2009
2010 # honest sympy matrices defer to their class's routine

ShapeError: Matrix size mismatch: (3, 2) * (4, 3).

这让我很困惑!

为什么 numpy 和 sympy 不一致?

为什么我在 sympy 的文档中找不到这种行为的警告?

如何在 sympy 中正确计算两个矩阵的点积?

我可以建议 sympy 的文档中有一个关于 numpy 和 sympy 之间语法差异的条目。 (我很乐意做出贡献,但我不知道这些差异)

最佳答案

我将在 Github 上发布来自 SymPy 开发人员的回复:

As a general rule, in NumPy, all operations vectorize over arrays. In SymPy, operations do their usual mathematical meaning. This means that SymPy is generally more restrictive to keeping things mathematically "pure".

The most obvious one first: in NumPy, everything is numeric. Functions take a number or array of numbers and produce a number or array of numbers. In SymPy, everything is symbolic. Expressions can use symbolic variables that remain unevaluated by default. For example, np.exp(np.pi) produces a number, but sympy.exp(sympy.pi) produces an unevaluated expression (which can be evaluated to a number with sympy.exp(sympy.pi).evalf()).

In general, SymPy functions will not work on NumPy arrays and NumPy functions will not work on SymPy expressions. If you want to mix SymPy and NumPy, it is recommended to use lambdify (start with a SymPy expression using only SymPy functions, then use lambdify to convert it to an equivalent NumPy function, and use that on NumPy arrays with NumPy numeric types). I've discussed this on StackOverflow many times.

'*' is matrix multiplication. Note that @ (Python 3.5+) does matrix multiplication in both NumPy and SymPy.

sympy.Matrix is always rank 2. NumPy arrays can be any rank. Note that dot/@ in NumPy work on any rank array. There is some weird behavior on rank 1 arrays (it basically treats them as both column or row vectors, depending on the context).

Calling a mathematical function on a matrix in SymPy, if it works, performs the analytic matrix valuation of that function. For example, exp(M) computes the matrix exponential. Matrix.applyfunc can be used to apply functions elementwise. In NumPy, exp(A) takes the exponential of each element of A (use scipy.linalg.expm to take a matrix exponential of a NumPy array).

np.dot does matrix multiplication. In SymPy, dot does a dot product (takes two 1xn or nx1 matrices and produces a scalar). sympy.dot errors (actually currently in master gives a deprecation warning) if the arguments are not 1xn.

A 1x1 matrix is not considered the same thing as a scalar in SymPy. 1 + Matrix([[1]]) is an error.

关于矩阵乘法的符号表示法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49940433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com