how to solve for all non-negative integer xi's in mathematica

给你一囗甜甜゛ 提交于 2019-12-11 08:16:00

问题


I have a problem similar to IntegerPartitions function, in that I want to list all non-negative integer xi's such that, for a given list of integers {c1,c2,...,cn} and an integer n:

x1*c1+x2*c2+...+xn*cn=n

Please share your thoughts. Many thanks.


回答1:


The built-in function FrobeniusSolve solves the case where the c1, c2, ..., cn are positive integers (and the right hand side is not n):

In[1]:= FrobeniusSolve[{2, 3, 5, 6}, 13]

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

Is this the case you need, or do you need negative c1, c2, ..., cn also?




回答2:


Construct your list of ci's and coefficients using

n = 10;
cList = RandomInteger[{1, 20}, n]
xList = Table[Symbol["x" <> ToString[i]], {i, n}]

Then, if there's a set of solutions for non-negative xi's, it will be found by

Reduce[cList.xList == n && And@@Thread[xList >= 0], xList, Integers]


来源:https://stackoverflow.com/questions/4987972/how-to-solve-for-all-non-negative-integer-xis-in-mathematica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!