ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 20 May 2011 15:13:31 -0500Solve system of equations with additional conditions in sagehttps://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/Hi Sage users,
I've got a system of equations like the following example:
- **eq1 = a + b == n * (c + d)**
- **eq2 = b == k * d**
with n and k must be integers.
for the other variables, there are additional conditions like
- **a >= 80**
- **b >= 1000**
- **c >= 20**
- **d >= 40**
- **a + b <= 2000**
- **c + d <= 90**
I want to get all solutions of this system where n and k are integers.
Is there a way to find these with sage?
Would be great to get any possible hint to do this!
Thanks for your suggestions,
TobiWed, 18 May 2011 10:54:35 -0500https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/Comment by twk for <p>Hi Sage users,</p>
<p>I've got a system of equations like the following example:</p>
<ul>
<li><strong>eq1 = a + b == n * (c + d)</strong></li>
<li><strong>eq2 = b == k * d</strong></li>
</ul>
<p>with n and k must be integers.</p>
<p>for the other variables, there are additional conditions like</p>
<ul>
<li><strong>a >= 80</strong></li>
<li><strong>b >= 1000</strong></li>
<li><strong>c >= 20</strong> </li>
<li><strong>d >= 40</strong> </li>
<li><strong>a + b <= 2000</strong> </li>
<li><strong>c + d <= 90</strong></li>
</ul>
<p>I want to get all solutions of this system where n and k are integers.
Is there a way to find these with sage?</p>
<p>Would be great to get any possible hint to do this!</p>
<p>Thanks for your suggestions,
Tobi</p>
https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?comment=21715#post-id-21715Hi, sorry- my mistake - I wrote eq2 = b == k * e where eq2 = b == k * d should stand. Does it make more sense for you this way? Thanks, TobiWed, 18 May 2011 18:07:31 -0500https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?comment=21715#post-id-21715Comment by Jason Bandlow for <p>Hi Sage users,</p>
<p>I've got a system of equations like the following example:</p>
<ul>
<li><strong>eq1 = a + b == n * (c + d)</strong></li>
<li><strong>eq2 = b == k * d</strong></li>
</ul>
<p>with n and k must be integers.</p>
<p>for the other variables, there are additional conditions like</p>
<ul>
<li><strong>a >= 80</strong></li>
<li><strong>b >= 1000</strong></li>
<li><strong>c >= 20</strong> </li>
<li><strong>d >= 40</strong> </li>
<li><strong>a + b <= 2000</strong> </li>
<li><strong>c + d <= 90</strong></li>
</ul>
<p>I want to get all solutions of this system where n and k are integers.
Is there a way to find these with sage?</p>
<p>Would be great to get any possible hint to do this!</p>
<p>Thanks for your suggestions,
Tobi</p>
https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?comment=21717#post-id-21717For eqn1, there is an obvious upper and lower bound for n, and for each choice of n in this range, you will have 1 equation in four unknowns to determine a,b,c,d (in addition to the constraints). Once you decide on b, you can choose k freely, and this will determine e. What sort of answer are you hoping to get?Wed, 18 May 2011 17:13:02 -0500https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?comment=21717#post-id-21717Answer by Jason Bandlow for <p>Hi Sage users,</p>
<p>I've got a system of equations like the following example:</p>
<ul>
<li><strong>eq1 = a + b == n * (c + d)</strong></li>
<li><strong>eq2 = b == k * d</strong></li>
</ul>
<p>with n and k must be integers.</p>
<p>for the other variables, there are additional conditions like</p>
<ul>
<li><strong>a >= 80</strong></li>
<li><strong>b >= 1000</strong></li>
<li><strong>c >= 20</strong> </li>
<li><strong>d >= 40</strong> </li>
<li><strong>a + b <= 2000</strong> </li>
<li><strong>c + d <= 90</strong></li>
</ul>
<p>I want to get all solutions of this system where n and k are integers.
Is there a way to find these with sage?</p>
<p>Would be great to get any possible hint to do this!</p>
<p>Thanks for your suggestions,
Tobi</p>
https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?answer=12363#post-id-12363Since $n$ and $k$ are both bounded, you can try something like this:
sage: var('a b c d n k')
sage: nmin = ceil(1080/90); nmax = floor(2000/60)
sage: kmin = ceil(1000/70); kmax = floor(1920/40)
sage: constraints = [a >= 80, b >= 1000, c >= 20, d >= 40, a + b <= 2000, c + d <= 90]
sage: result = []
sage: for nn in [nmin..nmax]:
....: for kk in [kmin..kmax]:
....: result.append( solve(constraints + [n==nn, k==kk, a+b==n*(c+d), b==k*d], (a,b,c,d,n,k)))
I think this will take some time to run, but it should get you all the solutions you want.
Fri, 20 May 2011 03:36:30 -0500https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?answer=12363#post-id-12363Comment by twk for <p>Since $n$ and $k$ are both bounded, you can try something like this:</p>
<pre><code>sage: var('a b c d n k')
sage: nmin = ceil(1080/90); nmax = floor(2000/60)
sage: kmin = ceil(1000/70); kmax = floor(1920/40)
sage: constraints = [a >= 80, b >= 1000, c >= 20, d >= 40, a + b <= 2000, c + d <= 90]
sage: result = []
sage: for nn in [nmin..nmax]:
....: for kk in [kmin..kmax]:
....: result.append( solve(constraints + [n==nn, k==kk, a+b==n*(c+d), b==k*d], (a,b,c,d,n,k)))
</code></pre>
<p>I think this will take some time to run, but it should get you all the solutions you want.</p>
https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?comment=21705#post-id-21705Hi, thanks for this tricky approach - It calculates now - I'm curious about the results. Doesn't matter too much that it takes a while - I hope I will get some results by tomorrow.. Fri, 20 May 2011 10:07:26 -0500https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?comment=21705#post-id-21705Comment by twk for <p>Since $n$ and $k$ are both bounded, you can try something like this:</p>
<pre><code>sage: var('a b c d n k')
sage: nmin = ceil(1080/90); nmax = floor(2000/60)
sage: kmin = ceil(1000/70); kmax = floor(1920/40)
sage: constraints = [a >= 80, b >= 1000, c >= 20, d >= 40, a + b <= 2000, c + d <= 90]
sage: result = []
sage: for nn in [nmin..nmax]:
....: for kk in [kmin..kmax]:
....: result.append( solve(constraints + [n==nn, k==kk, a+b==n*(c+d), b==k*d], (a,b,c,d,n,k)))
</code></pre>
<p>I think this will take some time to run, but it should get you all the solutions you want.</p>
https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?comment=21704#post-id-21704Would there be a more fast and simple solution if I could make all variables to integers? Just noticed, that this wouldn't be a big problem for my application.Fri, 20 May 2011 10:12:23 -0500https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?comment=21704#post-id-21704Comment by Kelvin Li for <p>Since $n$ and $k$ are both bounded, you can try something like this:</p>
<pre><code>sage: var('a b c d n k')
sage: nmin = ceil(1080/90); nmax = floor(2000/60)
sage: kmin = ceil(1000/70); kmax = floor(1920/40)
sage: constraints = [a >= 80, b >= 1000, c >= 20, d >= 40, a + b <= 2000, c + d <= 90]
sage: result = []
sage: for nn in [nmin..nmax]:
....: for kk in [kmin..kmax]:
....: result.append( solve(constraints + [n==nn, k==kk, a+b==n*(c+d), b==k*d], (a,b,c,d,n,k)))
</code></pre>
<p>I think this will take some time to run, but it should get you all the solutions you want.</p>
https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?comment=21706#post-id-21706Good observation about the bounds on n and k. Trying your computation caused my VM to run out of memory... but I only gave it 256 MB. :-)Fri, 20 May 2011 08:18:57 -0500https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?comment=21706#post-id-21706Answer by Kelvin Li for <p>Hi Sage users,</p>
<p>I've got a system of equations like the following example:</p>
<ul>
<li><strong>eq1 = a + b == n * (c + d)</strong></li>
<li><strong>eq2 = b == k * d</strong></li>
</ul>
<p>with n and k must be integers.</p>
<p>for the other variables, there are additional conditions like</p>
<ul>
<li><strong>a >= 80</strong></li>
<li><strong>b >= 1000</strong></li>
<li><strong>c >= 20</strong> </li>
<li><strong>d >= 40</strong> </li>
<li><strong>a + b <= 2000</strong> </li>
<li><strong>c + d <= 90</strong></li>
</ul>
<p>I want to get all solutions of this system where n and k are integers.
Is there a way to find these with sage?</p>
<p>Would be great to get any possible hint to do this!</p>
<p>Thanks for your suggestions,
Tobi</p>
https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?answer=12361#post-id-12361Here is some progress, but not very much:
sage: var('a b c d n k')
(a, b, c, d, n, k)
sage: eqs = (
....: a + b == n*(c + d),
....: b == k*d,
....: a >= 80,
....: b >= 1000,
....: c >= 20,
....: d >= 40,
....: a + b <= 2000,
....: c + d <= 90
....: )
....:
sage: result = solve(eqs, (a,b,c,d,n,k)) # takes a while; be patient
sage: for r in result:
....: print "--"
....: for t in r:
....: print " ", t
# ... huge amount of output ...
I tried constraining `n` and `k` to take on integer values via:
sage: assume(n, "integer")
sage: assume(k, "integer")
... before calling `solve`, but it does not help.Wed, 18 May 2011 13:17:50 -0500https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?answer=12361#post-id-12361Comment by Kelvin Li for <p>Here is some progress, but not very much:</p>
<pre><code>sage: var('a b c d n k')
(a, b, c, d, n, k)
sage: eqs = (
....: a + b == n*(c + d),
....: b == k*d,
....: a >= 80,
....: b >= 1000,
....: c >= 20,
....: d >= 40,
....: a + b <= 2000,
....: c + d <= 90
....: )
....:
sage: result = solve(eqs, (a,b,c,d,n,k)) # takes a while; be patient
sage: for r in result:
....: print "--"
....: for t in r:
....: print " ", t
# ... huge amount of output ...
</code></pre>
<p>I tried constraining <code>n</code> and <code>k</code> to take on integer values via:</p>
<pre><code>sage: assume(n, "integer")
sage: assume(k, "integer")
</code></pre>
<p>... before calling <code>solve</code>, but it does not help.</p>
https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?comment=21709#post-id-21709`solve` is currently done via Maxima, and the `assume`/`assumptions`/`forget` framework also interfaces with Maxima. Thus, as far as Maxima goes, this is what you would do to declare variables as integers. But it seems that Maxima's assumption framework does not perform some simplifications... a symbolics expert should chime in here. :-)Thu, 19 May 2011 07:41:45 -0500https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?comment=21709#post-id-21709Comment by twk for <p>Here is some progress, but not very much:</p>
<pre><code>sage: var('a b c d n k')
(a, b, c, d, n, k)
sage: eqs = (
....: a + b == n*(c + d),
....: b == k*d,
....: a >= 80,
....: b >= 1000,
....: c >= 20,
....: d >= 40,
....: a + b <= 2000,
....: c + d <= 90
....: )
....:
sage: result = solve(eqs, (a,b,c,d,n,k)) # takes a while; be patient
sage: for r in result:
....: print "--"
....: for t in r:
....: print " ", t
# ... huge amount of output ...
</code></pre>
<p>I tried constraining <code>n</code> and <code>k</code> to take on integer values via:</p>
<pre><code>sage: assume(n, "integer")
sage: assume(k, "integer")
</code></pre>
<p>... before calling <code>solve</code>, but it does not help.</p>
https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?comment=21714#post-id-21714Hi, with changing e to d (had been my fault), I get some results that are useful for me. But still, solve does ignore the 'integer' asumption. Is there maybe an option to declare that all variables must be integers?Wed, 18 May 2011 19:10:39 -0500https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?comment=21714#post-id-21714Comment by kcrisman for <p>Here is some progress, but not very much:</p>
<pre><code>sage: var('a b c d n k')
(a, b, c, d, n, k)
sage: eqs = (
....: a + b == n*(c + d),
....: b == k*d,
....: a >= 80,
....: b >= 1000,
....: c >= 20,
....: d >= 40,
....: a + b <= 2000,
....: c + d <= 90
....: )
....:
sage: result = solve(eqs, (a,b,c,d,n,k)) # takes a while; be patient
sage: for r in result:
....: print "--"
....: for t in r:
....: print " ", t
# ... huge amount of output ...
</code></pre>
<p>I tried constraining <code>n</code> and <code>k</code> to take on integer values via:</p>
<pre><code>sage: assume(n, "integer")
sage: assume(k, "integer")
</code></pre>
<p>... before calling <code>solve</code>, but it does not help.</p>
https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?comment=21702#post-id-21702Maxima's assumption framework is "weak", according to them (obviously, stronger than our non-existent one otherwise), and intentionally does not interface with 'solve', where the variables are considered to be sort of dummy variables.Fri, 20 May 2011 15:13:31 -0500https://ask.sagemath.org/question/8120/solve-system-of-equations-with-additional-conditions-in-sage/?comment=21702#post-id-21702