数学中线性方程组的非负整数解 [英] non-negative integer solutions to system of linear equations in mathematica

查看:32
本文介绍了数学中线性方程组的非负整数解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

非常感谢!

编辑

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

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.

推荐答案

Reduce 能够解决这些类型的问题.

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)

<小时>

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


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}}

这篇关于数学中线性方程组的非负整数解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆