简体   繁体   English

使用 Sympy 的矩阵导数

[英]Derivatives of a matrix using Sympy

I am learning Sympy to know the Symbolic operations in Python.我正在学习 Sympy 以了解 Python 中的符号操作。 I want to find out the derivative of a matrix.我想找出矩阵的导数。

示例矩阵

How could I derivate the matrix in respect to b.我如何推导关于 b 的矩阵。

import sympy as sp
B = sp.Matrix([[(a*c),(b**2)],[(b*d),(d*a)]])
B

This can not help me to give my answer. 不能帮助我给出我的答案。

Assuming you want the usual matrix-by-scalar derivative and that you're using sympy 1.7, then the following should work:假设您想要通常的 矩阵标量导数并且您使用的是 sympy 1.7,那么以下应该可以工作:

import sympy as sp
a, b, c, d = sp.symbols("a b c d")
B = sp.Matrix([[(a*c),(b**2)],[(b*d),(d*a)]])
B.diff(b)

Returns:回报:

Matrix([
[0, 2*b],
[d,   0]])

Which seems right to me.这对我来说似乎是正确的。 More here .更多在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM