ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 02 Aug 2014 12:31:20 +0200Solving system of linear Diophantine equationshttps://ask.sagemath.org/question/23666/solving-system-of-linear-diophantine-equations/I have an $m \times n$ integer matrix $A$ with $m>n$, and a set $S \subset \mathbb{Z}$, for example $S=\\{-1,0,1\\}$.
I want to enumerate all possible $X \in S^n$ such that $AX=0$.
I tried using smith_form(), but then i could not figure out how to force the solution to belong to elements of $S$.
----------
EDIT. Thank you Dima for the solution. It works well. I am adding an example here to illustrate your solution.
lb = -1
ub = 1
A = matrix(ZZ, [(8,2,10,0,12,2,0), (4,6,9,1,14,5,2), (2,0,1,1,0,1,0), (2,1,3,0,4,0,1)])
eq1 = [(0,8,2,10,0,12,2,0), (0,4,6,9,1,14,5,2), (0,2,0,1,1,0,1,0), (0,2,1,3,0,4,0,1)]
ieq1 = [(-lb,1,0,0,0,0,0,0), (ub,-1,0,0,0,0,0,0),
(-lb,0,1,0,0,0,0,0), (ub,0,-1,0,0,0,0,0),
(-lb,0,0,1,0,0,0,0), (ub,0,0,-1,0,0,0,0),
(-lb,0,0,0,1,0,0,0), (ub,0,0,0,-1,0,0,0),
(-lb,0,0,0,0,1,0,0), (ub,0,0,0,0,-1,0,0),
(-lb,0,0,0,0,0,1,0), (ub,0,0,0,0,0,-1,0),
(-lb,0,0,0,0,0,0,1), (ub,0,0,0,0,0,0,-1)]
p = Polyhedron(eqns=eq1, ieqs=ieq1, base_ring=QQ)
p.integral_points()
returns the answer
((-1, -1, 1, 1, 0, 0, 0),
(0, -1, -1, 1, 1, 0, 0),
(0, -1, 0, -1, 0, 1,1),
(0, 0, 0, 0, 0, 0, 0),
(0, 1, 0, 1, 0, -1, -1),
(0, 1, 1, -1, -1, 0,0),
(1, 1, -1, -1, 0, 0, 0))
which is exactly what i needed. Thanks again.Sat, 02 Aug 2014 12:17:47 +0200https://ask.sagemath.org/question/23666/solving-system-of-linear-diophantine-equations/Answer by Dima for <p>I have an $m \times n$ integer matrix $A$ with $m>n$, and a set $S \subset \mathbb{Z}$, for example $S=\{-1,0,1\}$.</p>
<p>I want to enumerate all possible $X \in S^n$ such that $AX=0$.</p>
<p>I tried using smith_form(), but then i could not figure out how to force the solution to belong to elements of $S$.</p>
<hr/>
<p>EDIT. Thank you Dima for the solution. It works well. I am adding an example here to illustrate your solution.</p>
<pre><code>lb = -1
ub = 1
A = matrix(ZZ, [(8,2,10,0,12,2,0), (4,6,9,1,14,5,2), (2,0,1,1,0,1,0), (2,1,3,0,4,0,1)])
eq1 = [(0,8,2,10,0,12,2,0), (0,4,6,9,1,14,5,2), (0,2,0,1,1,0,1,0), (0,2,1,3,0,4,0,1)]
ieq1 = [(-lb,1,0,0,0,0,0,0), (ub,-1,0,0,0,0,0,0),
(-lb,0,1,0,0,0,0,0), (ub,0,-1,0,0,0,0,0),
(-lb,0,0,1,0,0,0,0), (ub,0,0,-1,0,0,0,0),
(-lb,0,0,0,1,0,0,0), (ub,0,0,0,-1,0,0,0),
(-lb,0,0,0,0,1,0,0), (ub,0,0,0,0,-1,0,0),
(-lb,0,0,0,0,0,1,0), (ub,0,0,0,0,0,-1,0),
(-lb,0,0,0,0,0,0,1), (ub,0,0,0,0,0,0,-1)]
p = Polyhedron(eqns=eq1, ieqs=ieq1, base_ring=QQ)
p.integral_points()
</code></pre>
<p>returns the answer </p>
<pre><code>((-1, -1, 1, 1, 0, 0, 0),
(0, -1, -1, 1, 1, 0, 0),
(0, -1, 0, -1, 0, 1,1),
(0, 0, 0, 0, 0, 0, 0),
(0, 1, 0, 1, 0, -1, -1),
(0, 1, 1, -1, -1, 0,0),
(1, 1, -1, -1, 0, 0, 0))
</code></pre>
<p>which is exactly what i needed. Thanks again.</p>
https://ask.sagemath.org/question/23666/solving-system-of-linear-diophantine-equations/?answer=23667#post-id-23667You can enumerate integer points in a polytope using Sage.
Create the polytope $P$ using $A$ and inequalities specifying $S$, i.e. $-1\leq x_i\leq 1$ for all $i$.
(look up docs on Polyhedron).
Then call P.integral_points()
Sat, 02 Aug 2014 12:31:20 +0200https://ask.sagemath.org/question/23666/solving-system-of-linear-diophantine-equations/?answer=23667#post-id-23667