减少数学中未确定的方程组

发布于 2024-10-17 04:22:47 字数 517 浏览 1 评论 0原文

我得到了以下方程(作为示例):

{2 w11 + 3 w21 == 2 w12, w11 == 4 w12 + 3 w22, w11 + 2 w21 + w22 == 0,
  2 w12 + w21 + 2 w22 == 0}

我想确定 w11、w12、w21、w22。然而,只需执行以下操作:

Solve[{3 w11 + 2 w21 == 5 w11 + 3 w12, w11 + w21 == 5 w21 + 3 w22, 
  3 w12 + 2 w22 == -2 w11 - w12, w12 + w22 == -2 w21 - w22}, {w11, 
  w12, w21, w22}]

因为方程组是欠定的。我有一个想法,即使用矩阵代数。但我需要自动将 w11、w12、w21、w22 前面的这些系数分组到一个矩阵(列表列表)中,然后从那里开始。但我不确定如何轻松地操纵这些方程来生成这样的矩阵。请帮忙,或者如果您有更好的想法,也请分享。

非常感谢。

I obtained the folloiwng equation (as an example):

{2 w11 + 3 w21 == 2 w12, w11 == 4 w12 + 3 w22, w11 + 2 w21 + w22 == 0,
  2 w12 + w21 + 2 w22 == 0}

And I want to determine w11, w12, w21, w22. However, simply do the following:

Solve[{3 w11 + 2 w21 == 5 w11 + 3 w12, w11 + w21 == 5 w21 + 3 w22, 
  3 w12 + 2 w22 == -2 w11 - w12, w12 + w22 == -2 w21 - w22}, {w11, 
  w12, w21, w22}]

Because the system of equations is under-determined. I have one thought, i.e. using matrix algebra. But I need to automatically group those coefficients in front of w11, w12, w21, w22 into a matrix (list of lists) then go from there. But I am not sure how to manipulate these equations easily to generate such a matrix. Please help, or if you have better ideas, please share too.

Many thanks.

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

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

发布评论

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

评论(2

源来凯始玺欢你 2024-10-24 04:22:47

有一个内置函数 CoefficientArrays 用于转换将线性(或多项式)方程组转换为矩阵形式。

你想要的矩阵是结果的第二部分:

In[7]:= vars = {w11, w12, w21, w22};

In[8]:= CoefficientArrays[{2 w11 + 3 w21 == 2 w12, 
   w11 == 4 w12 + 3 w22, w11 + 2 w21 + w22 == 0, 
   2 w12 + w21 + 2 w22 == 0}, vars] // Normal

Out[8]= {{0, 0, 0, 
  0}, {{2, -2, 3, 0}, {1, -4, 0, -3}, {1, 0, 2, 1}, {0, 2, 1, 2}}}

非齐次部分是结果的第一部分,一个向量:

In[9]:= CoefficientArrays[{3 w11 + 2 w12 == 5 w11 + 3 w21 + a, 
   w11 + w12 == 5 w12 + 3 w22 - c, 
   3 w21 + 2 w22 + b == a - 2 w11 - w21, 
   w21 + w22 == f - 2 w12 - w22}, vars] // Normal

Out[9]= {{-a, 
  c, -a + b, -f}, {{-2, 2, -3, 0}, {1, -4, 0, -3}, {2, 0, 4, 2}, {0, 
   2, 1, 2}}}

There is a built-in function CoefficientArrays for converting systems of linear (or polynomial) equations into a matrix form.

The matrix you want is the second part of the result:

In[7]:= vars = {w11, w12, w21, w22};

In[8]:= CoefficientArrays[{2 w11 + 3 w21 == 2 w12, 
   w11 == 4 w12 + 3 w22, w11 + 2 w21 + w22 == 0, 
   2 w12 + w21 + 2 w22 == 0}, vars] // Normal

Out[8]= {{0, 0, 0, 
  0}, {{2, -2, 3, 0}, {1, -4, 0, -3}, {1, 0, 2, 1}, {0, 2, 1, 2}}}

The inhomogeneous part is the first part of the result, a vector:

In[9]:= CoefficientArrays[{3 w11 + 2 w12 == 5 w11 + 3 w21 + a, 
   w11 + w12 == 5 w12 + 3 w22 - c, 
   3 w21 + 2 w22 + b == a - 2 w11 - w21, 
   w21 + w22 == f - 2 w12 - w22}, vars] // Normal

Out[9]= {{-a, 
  c, -a + b, -f}, {{-2, 2, -3, 0}, {1, -4, 0, -3}, {2, 0, 4, 2}, {0, 
   2, 1, 2}}}
绝影如岚 2024-10-24 04:22:47

这是您的方程和变量:

vars = {w11, w12, w21, w22};
eqs = {2 w11 + 3 w21 == 2 w12, w11 == 4 w12 + 3 w22, 
   w11 + 2 w21 + w22 == 0, 2 w12 + w21 + 2 w22 == 0};

这是矩阵:

In[48]:= matrix =  Transpose[ eqs /. Equal :> Subtract /. 
    Map[Thread[vars -> #] &, IdentityMatrix[Length[vars]]]]

Out[48]= {{2, -2, 3, 0}, {1, -4, 0, -3}, {1, 0, 2, 1}, {0, 2, 1, 2}}

编辑:

同样适用于您的第二组方程:

In[49]:= eqs = {3 w11 + 2 w21 == 5 w11 + 3 w12,  w11 + w21 == 5 w21 + 3 w22, 
  3 w12 + 2 w22 == -2 w11 - w12,  w12 + w22 == -2 w21 - w22};   

In[50]:= matrix = Transpose[ eqs /. Equal :> Subtract /. 
    Map[Thread[vars -> #] &, IdentityMatrix[Length[vars]]]]

Out[50]= {{-2, -3, 2, 0}, {1, 0, -4, -3}, {2, 4, 0, 2}, {0, 1, 2, 2}}

编辑:

根据要求扩展解决方案。首先,它是如何工作的:其思想是首先将所有变量移至左侧,这是通过用减法替换等于运算符来实现的:

In[69]:= eqs = {3 w11 + 2 w21 == 5 w11 + 3 w12,  w11 + w21 == 5 w21 + 3 w22, 
     3 w12 + 2 w22 == -2 w11 - w12,  w12 + w22 == -2 w21 - w22};

In[70]:= eqs /。等于:> Subtract

Out[70]= {-2 w11 - 3 w12 + 2 w21, w11 - 4 w21 - 3 w22, 2 w11 + 4 w12 + 2 w22, w12 + 2 w21 + 2 w22}

构建规则使得对于任何一组规则,只有一个变量设置为 1,其余变量设置为 0:

 In[71]:= Map[Thread[vars -> #] &, IdentityMatrix[Length[vars]]]

 Out[71]= {{w11 -> 1, w12 -> 0, w21 -> 0, w22 -> 0}, {w11 -> 0, w12 -> 1, w21 -> 0, w22 -> 0}, 
        {w11 -> 0, w12 -> 0, w21 -> 1, w22 -> 0}, {w11 -> 0, w12 -> 0, w21 -> 0, w22 -> 1}}

这允许计算系数:

In[72]:= eqs /. Equal :> Subtract /. Map[Thread[vars -> #] &, IdentityMatrix[Length[vars]]]

Out[72]= {{-2, 1, 2, 0}, {-3, 0, 4, 1}, {2, -4, 0, 2}, {0, -3, 2, 2}}

在检查规则如何工作时,很容易看出我们需要应用 Transpose到结果。

现在,您的第二个请求需要更多工作:

In[53]:= eqs = {3 w11 + 2 w12 == 5 w11 + 3 w21 + a, w11 + w12 == 5 w12 + 3 w22 - c, 
   3 w21 + 2 w22 + b == a - 2 w11 - w21, w21 + w22 == f - 2 w12 - w22};

In[55]:= modifiedEqs = With[{alts = Alternatives @@ vars},
   eqs //. {lhs_ == HoldPattern[Plus[left___, x_, right___]] /; !FreeQ[x, alts] :> 
                    lhs - x == left + right,
            HoldPattern[Plus[left___, x_, right___] == rhs_] /; FreeQ[x, alts] :> 
           (left + right == rhs - x)}]

Out[55]= {-2 w11 + 2 w12 - 3 w21 == a, w11 - 4 w12 - 3 w22 == -c,  
     2 w11 + 4 w21 + 2 w22 == a - b,   2 w12 + w21 + 2 w22 == f}

In[68]:= matrix = {Transpose[# /. (lhs_ == rhs_) :> lhs /. 
    Map[Thread[vars -> #] &, IdentityMatrix[Length[vars]]]], #[[All,2]]} &[modifiedEqs]

Out[68]= {{{-2, 2, -3, 0}, {1, -4, 0, -3}, {2, 0, 4, 2}, {0, 2, 1,  2}}, {a, -c, a - b, f}}

主​​要区别是我们需要额外的步骤来分离常量并将它们带到 rhs 您可能会发现自己弄清楚其工作原理的细节更有用。

编辑:

是的,我忘了提及:要理解解决方案,您应该知道在嵌套列表中应用规则时会发生什么 - 在这种情况下,较大列表中的每个规则列表都会生成表达式的转换副本,例如:

In[73]:= {a, b, c} /. {{a -> 1}, {b -> 1}, {c -> 1}}

Out[73]= {{1, b, c}, {a, 1, c}, {a, b, 1}}

HTH

Here are your equations and variables:

vars = {w11, w12, w21, w22};
eqs = {2 w11 + 3 w21 == 2 w12, w11 == 4 w12 + 3 w22, 
   w11 + 2 w21 + w22 == 0, 2 w12 + w21 + 2 w22 == 0};

Here is the matrix:

In[48]:= matrix =  Transpose[ eqs /. Equal :> Subtract /. 
    Map[Thread[vars -> #] &, IdentityMatrix[Length[vars]]]]

Out[48]= {{2, -2, 3, 0}, {1, -4, 0, -3}, {1, 0, 2, 1}, {0, 2, 1, 2}}

EDIT:

The same works for your second group of equations:

In[49]:= eqs = {3 w11 + 2 w21 == 5 w11 + 3 w12,  w11 + w21 == 5 w21 + 3 w22, 
  3 w12 + 2 w22 == -2 w11 - w12,  w12 + w22 == -2 w21 - w22};   

In[50]:= matrix = Transpose[ eqs /. Equal :> Subtract /. 
    Map[Thread[vars -> #] &, IdentityMatrix[Length[vars]]]]

Out[50]= {{-2, -3, 2, 0}, {1, 0, -4, -3}, {2, 4, 0, 2}, {0, 1, 2, 2}}

EDIT:

Expanding on the solution, upon request. First, how it works: the idea is to first bring all variables to the left, which is achieved by replacing the equals operator with subtraction:

In[69]:= eqs = {3 w11 + 2 w21 == 5 w11 + 3 w12,  w11 + w21 == 5 w21 + 3 w22, 
     3 w12 + 2 w22 == -2 w11 - w12,  w12 + w22 == -2 w21 - w22};

In[70]:= eqs /. Equal :> Subtract

Out[70]= {-2 w11 - 3 w12 + 2 w21, w11 - 4 w21 - 3 w22, 2 w11 + 4 w12 + 2 w22, w12 + 2 w21 + 2 w22}

The rules are constructed so that for any group of rules, only one variable is set to 1, and the rest to 0:

 In[71]:= Map[Thread[vars -> #] &, IdentityMatrix[Length[vars]]]

 Out[71]= {{w11 -> 1, w12 -> 0, w21 -> 0, w22 -> 0}, {w11 -> 0, w12 -> 1, w21 -> 0, w22 -> 0}, 
        {w11 -> 0, w12 -> 0, w21 -> 1, w22 -> 0}, {w11 -> 0, w12 -> 0, w21 -> 0, w22 -> 1}}

This allows to compute the coefficients:

In[72]:= eqs /. Equal :> Subtract /. Map[Thread[vars -> #] &, IdentityMatrix[Length[vars]]]

Out[72]= {{-2, 1, 2, 0}, {-3, 0, 4, 1}, {2, -4, 0, 2}, {0, -3, 2, 2}}

Upon inspecting how the rules work, it is easy to see that we need to apply Transpose to the result.

Now, your second request requires more work:

In[53]:= eqs = {3 w11 + 2 w12 == 5 w11 + 3 w21 + a, w11 + w12 == 5 w12 + 3 w22 - c, 
   3 w21 + 2 w22 + b == a - 2 w11 - w21, w21 + w22 == f - 2 w12 - w22};

In[55]:= modifiedEqs = With[{alts = Alternatives @@ vars},
   eqs //. {lhs_ == HoldPattern[Plus[left___, x_, right___]] /; !FreeQ[x, alts] :> 
                    lhs - x == left + right,
            HoldPattern[Plus[left___, x_, right___] == rhs_] /; FreeQ[x, alts] :> 
           (left + right == rhs - x)}]

Out[55]= {-2 w11 + 2 w12 - 3 w21 == a, w11 - 4 w12 - 3 w22 == -c,  
     2 w11 + 4 w21 + 2 w22 == a - b,   2 w12 + w21 + 2 w22 == f}

In[68]:= matrix = {Transpose[# /. (lhs_ == rhs_) :> lhs /. 
    Map[Thread[vars -> #] &, IdentityMatrix[Length[vars]]]], #[[All,2]]} &[modifiedEqs]

Out[68]= {{{-2, 2, -3, 0}, {1, -4, 0, -3}, {2, 0, 4, 2}, {0, 2, 1,  2}}, {a, -c, a - b, f}}

The main difference is that we need an extra step to separate the constants and bring them to the r.h.s. You may find it more useful to figure out the details of how this works yourself.

Edit:

Yes, I forgot to mention: to understand the solution, you should know what happens when you apply rules in nested lists - in this case, each list of rules inside a larger lists results in a transformed copy of an expression, for example:

In[73]:= {a, b, c} /. {{a -> 1}, {b -> 1}, {c -> 1}}

Out[73]= {{1, b, c}, {a, 1, c}, {a, b, 1}}

HTH

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