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.Thu, 20 Jan 2022 21:29:53 +0100Finding a unique integer solution to a set of inequalitieshttps://ask.sagemath.org/question/60699/finding-a-unique-integer-solution-to-a-set-of-inequalities/I am trying to find the unique integer solution to a set of equalities and inequalities. The equations I have are `0<=k0<p-1 and k=k0+j(p-1)`. I know that for p a prime >=5 and k an integer, there is is a unique set of integers k0 and j such that these equations are satisfied. However, I can't seem to implement this.
What I have is
k0,j = var('k0 j')
solve([0<=k0<p-1,k==k0+j(p-1),k0 in ZZ,j in ZZ],k0,j)
Where I added the "in ZZ" parts later to try and force it to give me an answer, but no matter what I plug in for p and k, the output is always just
(k0, j)
What exactly am I doing wrong?Sun, 16 Jan 2022 20:14:40 +0100https://ask.sagemath.org/question/60699/finding-a-unique-integer-solution-to-a-set-of-inequalities/Answer by rburing for <p>I am trying to find the unique integer solution to a set of equalities and inequalities. The equations I have are <code>0<=k0<p-1 and k=k0+j(p-1)</code>. I know that for p a prime >=5 and k an integer, there is is a unique set of integers k0 and j such that these equations are satisfied. However, I can't seem to implement this.</p>
<p>What I have is</p>
<pre><code>k0,j = var('k0 j')
solve([0<=k0<p-1,k==k0+j(p-1),k0 in ZZ,j in ZZ],k0,j)
</code></pre>
<p>Where I added the "in ZZ" parts later to try and force it to give me an answer, but no matter what I plug in for p and k, the output is always just</p>
<pre><code>(k0, j)
</code></pre>
<p>What exactly am I doing wrong?</p>
https://ask.sagemath.org/question/60699/finding-a-unique-integer-solution-to-a-set-of-inequalities/?answer=60724#post-id-60724Sage doesn't work that way, e.g. `k0 in ZZ` and `j in ZZ` evaluate to `False` because they are symbolic variables. You meant `assume(k0, 'integer')` and `assume(j, 'integer')`. Still, `solve` doesn't seem very good at your problem. Instead, you can define your solution set as a [Polyhedron](https://doc.sagemath.org/html/en/reference/discrete_geometry/sage/geometry/polyhedron/constructor.html#sage.geometry.polyhedron.constructor.Polyhedron) and ask for its integral points:
sage: p = 5
sage: k = 1
sage: Polyhedron(ieqs=[[0,1,0], [p-2,-1,0]], eqns=[[-k, 1,p-1]]).integral_points()
((1, 0),)Wed, 19 Jan 2022 13:42:50 +0100https://ask.sagemath.org/question/60699/finding-a-unique-integer-solution-to-a-set-of-inequalities/?answer=60724#post-id-60724Comment by Rune for <p>Sage doesn't work that way, e.g. <code>k0 in ZZ</code> and <code>j in ZZ</code> evaluate to <code>False</code> because they are symbolic variables. You meant <code>assume(k0, 'integer')</code> and <code>assume(j, 'integer')</code>. Still, <code>solve</code> doesn't seem very good at your problem. Instead, you can define your solution set as a <a href="https://doc.sagemath.org/html/en/reference/discrete_geometry/sage/geometry/polyhedron/constructor.html#sage.geometry.polyhedron.constructor.Polyhedron">Polyhedron</a> and ask for its integral points:</p>
<pre><code>sage: p = 5
sage: k = 1
sage: Polyhedron(ieqs=[[0,1,0], [p-2,-1,0]], eqns=[[-k, 1,p-1]]).integral_points()
((1, 0),)
</code></pre>
https://ask.sagemath.org/question/60699/finding-a-unique-integer-solution-to-a-set-of-inequalities/?comment=60741#post-id-60741Thanks, this works. I'm still new to Sage, so I'm still learning how it works.Thu, 20 Jan 2022 21:29:12 +0100https://ask.sagemath.org/question/60699/finding-a-unique-integer-solution-to-a-set-of-inequalities/?comment=60741#post-id-60741Answer by Max Alekseyev for <p>I am trying to find the unique integer solution to a set of equalities and inequalities. The equations I have are <code>0<=k0<p-1 and k=k0+j(p-1)</code>. I know that for p a prime >=5 and k an integer, there is is a unique set of integers k0 and j such that these equations are satisfied. However, I can't seem to implement this.</p>
<p>What I have is</p>
<pre><code>k0,j = var('k0 j')
solve([0<=k0<p-1,k==k0+j(p-1),k0 in ZZ,j in ZZ],k0,j)
</code></pre>
<p>Where I added the "in ZZ" parts later to try and force it to give me an answer, but no matter what I plug in for p and k, the output is always just</p>
<pre><code>(k0, j)
</code></pre>
<p>What exactly am I doing wrong?</p>
https://ask.sagemath.org/question/60699/finding-a-unique-integer-solution-to-a-set-of-inequalities/?answer=60725#post-id-60725The given conditions imply that `k0` and `j` are simply the remainder and quotient of division of `k` by `p-1`. Correspondingly, they can be computed as:
j, k0 = k.quo_rem(p-1)Wed, 19 Jan 2022 20:29:34 +0100https://ask.sagemath.org/question/60699/finding-a-unique-integer-solution-to-a-set-of-inequalities/?answer=60725#post-id-60725Comment by Rune for <p>The given conditions imply that <code>k0</code> and <code>j</code> are simply the remainder and quotient of division of <code>k</code> by <code>p-1</code>. Correspondingly, they can be computed as:</p>
<pre><code>j, k0 = k.quo_rem(p-1)
</code></pre>
https://ask.sagemath.org/question/60699/finding-a-unique-integer-solution-to-a-set-of-inequalities/?comment=60742#post-id-60742This works as well, but the other answer gave more info on how Sage works.Thu, 20 Jan 2022 21:29:53 +0100https://ask.sagemath.org/question/60699/finding-a-unique-integer-solution-to-a-set-of-inequalities/?comment=60742#post-id-60742