mathematica 线性方程组的非负整数解

发布于 2024-12-17 01:14:40 字数 421 浏览 3 评论 0原文

我之前的问题相关,只是想知道如何求解具有非负积分解的线性方程组,例如:

c11*x+c12*y+c13*z=d1
c21*x+c22*y+c23*z=d2

非常感谢!

编辑

我的意思是高效。例如,我可以使用 FrobeniusSolve 来获取两个解列表并尝试找到交集。但有时,单个解决方案列表可能非常大。或者尝试验证一个 FrobeniusSolve 返回的每个单独的解,看看它们是否满足所有剩余的方程,但这也有同样的缺点。

Related to my previous question, just wonder how to solve a system of linear equations with non-negative integral solutions, for example:

c11*x+c12*y+c13*z=d1
c21*x+c22*y+c23*z=d2

Thanks a lot!

Edit

I meant efficiently. For example, I could have used FrobeniusSolve to get two solution lists and try to find the intersection. But sometimes, the individual solution list is probably hugely large. Or try to verify each individual solution returned by one FrobeniusSolve to see whether they satisfy all the remaining equations, but that suffers from the same drawback.

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

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

发布评论

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

评论(1

空城旧梦 2024-12-24 01:14:40

Reduce 能够解决这类问题。

要回答上面评论中的具体情况:

In[1]:= solns =  Reduce[x1 + 2 x2 + 5 x3 + 7 x4 == 40 &&
                        x1 + x2 + 2 x3 + x4 == 20 &&
                        x1 > 0 && x2 > 0 && x3 > 0 && x4 > 0, 
                       {x1, x2, x3, x4}, Integers]

Out[1]= (x1 == 6 && x2 == 11 && x3 == 1 && x4 == 1) ||
        (x1 == 7 && x2 == 8 && x3 == 2 && x4 == 1) ||
        (x1 == 8 && x2 == 5 && x3 == 3 && x4 == 1) ||
        (x1 == 9 && x2 == 2 && x3 == 4 && x4 == 1) ||
        (x1 == 11 && x2 == 5 && x3 == 1 && x4 == 2) ||
        (x1 == 12 && x2 == 2 && x3 == 2 && x4 == 2)

编辑:

您可以通过分别求解两个方程并取其解的交集来检查这是否与您获得的解相同:

In[2]:= a = Reduce[x1 + 2 x2 + 5 x3 + 7 x4 == 40 && 
                   x1 > 0 && x2 > 0 && x3 > 0 && x4 > 0, 
                  {x1, x2, x3, x4}, Integers];

        b = Reduce[x1 + x2 + 2 x3 + x4 == 20 && 
                   x1 > 0 && x2 > 0 && x3 > 0 && x4 > 0, 
                  {x1, x2, x3, x4}, Integers];

In[4]:= solns == Intersection[a, b]

Out[4]= True

并且您可以通过以下方式提取解:
将解决方案转化为替换规则列表
并应用于变量:

In[5]:= {x1, x2, x3, x4} /. {ToRules[solns]}

Out[5]= {{6, 11, 1, 1}, {7, 8, 2, 1}, {8, 5, 3, 1}, 
         {9, 2, 4, 1}, {11, 5, 1, 2}, {12, 2, 2, 2}}

Reduce is able to solve these types of problems.

To answer the specific case in your comment above:

In[1]:= solns =  Reduce[x1 + 2 x2 + 5 x3 + 7 x4 == 40 &&
                        x1 + x2 + 2 x3 + x4 == 20 &&
                        x1 > 0 && x2 > 0 && x3 > 0 && x4 > 0, 
                       {x1, x2, x3, x4}, Integers]

Out[1]= (x1 == 6 && x2 == 11 && x3 == 1 && x4 == 1) ||
        (x1 == 7 && x2 == 8 && x3 == 2 && x4 == 1) ||
        (x1 == 8 && x2 == 5 && x3 == 3 && x4 == 1) ||
        (x1 == 9 && x2 == 2 && x3 == 4 && x4 == 1) ||
        (x1 == 11 && x2 == 5 && x3 == 1 && x4 == 2) ||
        (x1 == 12 && x2 == 2 && x3 == 2 && x4 == 2)

Edit:

You can check that this is the same solution you get by solving the two equations separately and taking the intersection of their solutions:

In[2]:= a = Reduce[x1 + 2 x2 + 5 x3 + 7 x4 == 40 && 
                   x1 > 0 && x2 > 0 && x3 > 0 && x4 > 0, 
                  {x1, x2, x3, x4}, Integers];

        b = Reduce[x1 + x2 + 2 x3 + x4 == 20 && 
                   x1 > 0 && x2 > 0 && x3 > 0 && x4 > 0, 
                  {x1, x2, x3, x4}, Integers];

In[4]:= solns == Intersection[a, b]

Out[4]= True

And you can extract the solutions by, e.g.,
turning the solutions into a list of replacement rules
and applying to the variables:

In[5]:= {x1, x2, x3, x4} /. {ToRules[solns]}

Out[5]= {{6, 11, 1, 1}, {7, 8, 2, 1}, {8, 5, 3, 1}, 
         {9, 2, 4, 1}, {11, 5, 1, 2}, {12, 2, 2, 2}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文