系数方程列表/矩阵(方程组)
我尝试将方程(方程组)中的系数提取到列表(矩阵)中。我尝试过 CoefficientList[poly, {var1, var2, ...}] 但没有成功。
这个简单的例子应该可以解释我的问题:
Eq1 = a D[U[x1, x2], {x1, 2}] + b D[V[x1, x2], {x2, 2}]
有什么建议吗?
编辑:
Daniel 的 Lichtblau 解决方案非常清晰(谢谢),但是如果方程看起来像这样呢?
Eq1 = a D[U[x1, x2], {x1, 2}] + b D[V[x1, x2], {x2, 2}] + c W[x1, x2]
一个简单的例子可以解决如下:
有没有更优雅的解决方案? (特别是对于更复杂的表达式)
Ps我不明白为什么,但这个解决方案给了我正确的结果。
I try to extract coefficient from equations (system of equations) into list (matrix). I have tried CoefficientList[poly, {var1, var2, ...}]
but without success.
This simple example should explain my problem:
Eq1 = a D[U[x1, x2], {x1, 2}] + b D[V[x1, x2], {x2, 2}]
Any advice?
Edit:
Daniel's Lichtblau solution is very clear (thanks you), but what if the equation that looks like this?
Eq1 = a D[U[x1, x2], {x1, 2}] + b D[V[x1, x2], {x2, 2}] + c W[x1, x2]
A simple example can be resolved as follows:
Is there any more elegant solution? (particularly for more complex expressions)
Ps I can't understand why, but this solution gives me the correct result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,偏导数用 Derivative 表示,因此模式需要与之匹配。另外,我认为您不想使用
CoefficientList
因为它会接受两个表达式都出现的术语。总而言之,以下内容应该有效:这里
(Coefficient[Eq1, #] &)
是一个匿名函数,用于查找参数的系数,并且/@
映射将其添加到右侧的列表中。华泰
Firstly the partial derivatives are represented with
Derivative
, so the pattern needs to match that. Also, I don't think you want to useCoefficientList
as that would accept terms where both your expressions appear. All in all, the following should work:Here
(Coefficient[Eq1, #] &)
is an anonymous function that finds the coefficient of the argument, and/@
maps it to the list on the right.HTH
CoefficientArrays 通常可用于提取某些变量集中的线性系统的系数。在这种情况下,我们首先需要获取变量列表。
CoefficientArrays 返回 {常量, 系数} 形式的结果。它使用稀疏数组,因此我将使用 Normal 转换为显式列表。
Out[672]= {0, {b, a}}
Daniel Lichtblau
沃尔夫勒姆研究公司
CoefficientArrays is often useful for extracting coefficients to linear systems in some set of variables. In this case we first need to get the list of variables.
CoefficientArrays returns a result of the form {constants, coefficients}. it uses sparse arrays so I'll convert to explicit lists with Normal.
Out[672]= {0, {b, a}}
Daniel Lichtblau
Wolfram Research