ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 10 Aug 2019 16:31:56 -0500Obtain a particular solution for a system of inequalities whose variables can only take certain valueshttp://ask.sagemath.org/question/47447/obtain-a-particular-solution-for-a-system-of-inequalities-whose-variables-can-only-take-certain-values/I want to get a particular solution (assuming it exists) of a system of inequalities, with 4 variables, having that those variables can take only certain values. What I have so far is:
SRC = 64
a, b, c, d = var('a', 'b', 'c', 'd')
assume(a >= 1 and a <= 8)
assume(b >=4 and b <= 512)
assume(c >= 2 and c <= 128)
assume(d, 'integer')
eq1 = (SRC / a) * b <= 418
eq2 = (((SRC / a) * b) / c) / d <= 200
eq3 = ((SRC / a) * b) / c <= 480
res = solve([eq1, eq2, eq3], a, b, c, d=1)
for i in res:
print(i)
The first result I obtain is:
[a < 0, 0 < c, -d > 0, 25*a*c*d - 8*b > 0, -15*a*c + 2*b > 0]
As you can see, `a < 0`, but I have stated that `a` must be greater than `1`. Why this happens?
How can I obtain a particular solution (if it exists)?Fri, 09 Aug 2019 05:07:47 -0500http://ask.sagemath.org/question/47447/obtain-a-particular-solution-for-a-system-of-inequalities-whose-variables-can-only-take-certain-values/Comment by DanDan for <p>I want to get a particular solution (assuming it exists) of a system of inequalities, with 4 variables, having that those variables can take only certain values. What I have so far is:</p>
<pre><code>SRC = 64
a, b, c, d = var('a', 'b', 'c', 'd')
assume(a >= 1 and a <= 8)
assume(b >=4 and b <= 512)
assume(c >= 2 and c <= 128)
assume(d, 'integer')
eq1 = (SRC / a) * b <= 418
eq2 = (((SRC / a) * b) / c) / d <= 200
eq3 = ((SRC / a) * b) / c <= 480
res = solve([eq1, eq2, eq3], a, b, c, d=1)
for i in res:
print(i)
</code></pre>
<p>The first result I obtain is:</p>
<pre><code>[a < 0, 0 < c, -d > 0, 25*a*c*d - 8*b > 0, -15*a*c + 2*b > 0]
</code></pre>
<p>As you can see, <code>a < 0</code>, but I have stated that <code>a</code> must be greater than <code>1</code>. Why this happens?</p>
<p>How can I obtain a particular solution (if it exists)?</p>
http://ask.sagemath.org/question/47447/obtain-a-particular-solution-for-a-system-of-inequalities-whose-variables-can-only-take-certain-values/?comment=47458#post-id-47458Sorry, it is a constant. Edited for fixing it.Sat, 10 Aug 2019 08:16:44 -0500http://ask.sagemath.org/question/47447/obtain-a-particular-solution-for-a-system-of-inequalities-whose-variables-can-only-take-certain-values/?comment=47458#post-id-47458Comment by slelievre for <p>I want to get a particular solution (assuming it exists) of a system of inequalities, with 4 variables, having that those variables can take only certain values. What I have so far is:</p>
<pre><code>SRC = 64
a, b, c, d = var('a', 'b', 'c', 'd')
assume(a >= 1 and a <= 8)
assume(b >=4 and b <= 512)
assume(c >= 2 and c <= 128)
assume(d, 'integer')
eq1 = (SRC / a) * b <= 418
eq2 = (((SRC / a) * b) / c) / d <= 200
eq3 = ((SRC / a) * b) / c <= 480
res = solve([eq1, eq2, eq3], a, b, c, d=1)
for i in res:
print(i)
</code></pre>
<p>The first result I obtain is:</p>
<pre><code>[a < 0, 0 < c, -d > 0, 25*a*c*d - 8*b > 0, -15*a*c + 2*b > 0]
</code></pre>
<p>As you can see, <code>a < 0</code>, but I have stated that <code>a</code> must be greater than <code>1</code>. Why this happens?</p>
<p>How can I obtain a particular solution (if it exists)?</p>
http://ask.sagemath.org/question/47447/obtain-a-particular-solution-for-a-system-of-inequalities-whose-variables-can-only-take-certain-values/?comment=47456#post-id-47456What is `SRC`?Fri, 09 Aug 2019 17:10:35 -0500http://ask.sagemath.org/question/47447/obtain-a-particular-solution-for-a-system-of-inequalities-whose-variables-can-only-take-certain-values/?comment=47456#post-id-47456Answer by Emmanuel Charpentier for <p>I want to get a particular solution (assuming it exists) of a system of inequalities, with 4 variables, having that those variables can take only certain values. What I have so far is:</p>
<pre><code>SRC = 64
a, b, c, d = var('a', 'b', 'c', 'd')
assume(a >= 1 and a <= 8)
assume(b >=4 and b <= 512)
assume(c >= 2 and c <= 128)
assume(d, 'integer')
eq1 = (SRC / a) * b <= 418
eq2 = (((SRC / a) * b) / c) / d <= 200
eq3 = ((SRC / a) * b) / c <= 480
res = solve([eq1, eq2, eq3], a, b, c, d=1)
for i in res:
print(i)
</code></pre>
<p>The first result I obtain is:</p>
<pre><code>[a < 0, 0 < c, -d > 0, 25*a*c*d - 8*b > 0, -15*a*c + 2*b > 0]
</code></pre>
<p>As you can see, <code>a < 0</code>, but I have stated that <code>a</code> must be greater than <code>1</code>. Why this happens?</p>
<p>How can I obtain a particular solution (if it exists)?</p>
http://ask.sagemath.org/question/47447/obtain-a-particular-solution-for-a-system-of-inequalities-whose-variables-can-only-take-certain-values/?answer=47460#post-id-47460Well, Sage doesn't checks its solutions against the assumptions. So you have to add them to the system of inequations you're trying to solve:
SRC=64
a, b, c, d = var('a', 'b', 'c', 'd')
Cns=[a >= 1, a <= 8, b >=4, b <= 512, c >= 2, c <= 128]
eq1 = (SRC / a) * b <= 418
eq2 = (((SRC / a) * b) / c) / d <= 200
eq3 = ((SRC / a) * b) / c <= 480
Since we note that `eq3` is redundant against `eq2`:
sage: eq3.subs(d==1)
64*b/(a*c) <= 480
We can build our set of inequations quite easily:
Sys=copy(Cns)
Sys.append(eq1)
Sys.append(eq2.subs(d==1))
which is easily, if a bit slowly) solved:
sage: %time Sol=solve(Sys,[a,b,c])
CPU times: user 19.1 s, sys: 47.9 ms, total: 19.2 s
Wall time: 16.4 s
The solution seems a bit intricate:
sage: len(Sol)
54
but it could be considerably simplified with a bit of rewriting:
sage: for s in Sol:
....: print(s)
....:
[max(1, 32/209*b) < a, a < 8, 4 < b, b < (209/4), 2 < c, c < 128, 25*a*c - 8*b == 0]
[max(1, 32/209*b) < a, a < 8, 4 < b, b < (209/4), 2 < c, c < 128, 25*a*c - 8*b > 0]
[a == 1, 4 < b, b < (209/32), 2 < c, c < 128, -8*b + 25*c == 0]
[a == 1, 4 < b, b < (209/32), 2 < c, c < 128, -8*b + 25*c > 0]
[a == 1, b == 4, 2 < c, c < 128, 25*c - 32 == 0]
[a == 1, b == 4, 2 < c, c < 128, 25*c - 32 > 0]
[a == 1, b == 4, c == 2, 18 == 0]
[a == 1, b == 4, c == 2, 18 > 0]
[a == 1, b == 4, c == 128, 3168 == 0]
[a == 1, b == 4, c == 128, 3168 > 0]
[a == 1, b == (209/32), 2 < c, c < 128, 25*c - 209/4 == 0]
[a == 1, b == (209/32), 2 < c, c < 128, 25*c - 209/4 > 0]
[a == 1, b == (209/32), c == 2, (-9/4) == 0]
[a == 1, b == (209/32), c == 2, (-9/4) > 0]
[a == 1, b == (209/32), c == 128, (12591/4) == 0]
[a == 1, b == (209/32), c == 128, (12591/4) > 0]
[a == 1, c == 2, 4 < b, b < (209/32), -8*b + 50 == 0]
[a == 1, c == 2, 4 < b, b < (209/32), -8*b + 50 > 0]
[a == 1, c == 128, 4 < b, b < (209/32), -8*b + 3200 == 0]
[a == 1, c == 128, 4 < b, b < (209/32), -8*b + 3200 > 0]
[a == 8, 4 < b, b < (209/4), 2 < c, c < 128, -8*b + 200*c == 0]
[a == 8, 4 < b, b < (209/4), 2 < c, c < 128, -8*b + 200*c > 0]
[a == 8, b == 4, 2 < c, c < 128, 200*c - 32 == 0]
[a == 8, b == 4, 2 < c, c < 128, 200*c - 32 > 0]
[a == 8, b == 4, c == 2, 368 == 0]
[a == 8, b == 4, c == 2, 368 > 0]
[a == 8, b == 4, c == 128, 25568 == 0]
[a == 8, b == 4, c == 128, 25568 > 0]
[a == 8, b == (209/4), 2 < c, c < 128, 200*c - 418 == 0]
[a == 8, b == (209/4), 2 < c, c < 128, 200*c - 418 > 0]
[a == 8, b == (209/4), c == 2, -18 == 0]
[a == 8, b == (209/4), c == 2, -18 > 0]
[a == 8, b == (209/4), c == 128, 25182 == 0]
[a == 8, b == (209/4), c == 128, 25182 > 0]
[a == 8, c == 2, 4 < b, b < (209/4), -8*b + 400 == 0]
[a == 8, c == 2, 4 < b, b < (209/4), -8*b + 400 > 0]
[a == 8, c == 128, 4 < b, b < (209/4), -8*b + 25600 == 0]
[a == 8, c == 128, 4 < b, b < (209/4), -8*b + 25600 > 0]
[a == 32/209*b, (209/32) < b, b < (209/4), 2 < c, c < 128, 800/209*b*c - 8*b == 0]
[a == 32/209*b, (209/32) < b, b < (209/4), 2 < c, c < 128, 800/209*b*c - 8*b > 0]
[b == 4, 1 < a, a < 8, 2 < c, c < 128, 25*a*c - 32 == 0]
[b == 4, 1 < a, a < 8, 2 < c, c < 128, 25*a*c - 32 > 0]
[b == 4, c == 2, 1 < a, a < 8, 50*a - 32 == 0]
[b == 4, c == 2, 1 < a, a < 8, 50*a - 32 > 0]
[b == 4, c == 128, 1 < a, a < 8, 3200*a - 32 == 0]
[b == 4, c == 128, 1 < a, a < 8, 3200*a - 32 > 0]
[b == 209/32*a, c == 2, 1 < a, a < 8, -9/4*a == 0]
[b == 209/32*a, c == 2, 1 < a, a < 8, -9/4*a > 0]
[b == 209/32*a, c == 128, 1 < a, a < 8, 12591/4*a == 0]
[b == 209/32*a, c == 128, 1 < a, a < 8, 12591/4*a > 0]
[c == 2, max(1, 32/209*b) < a, a < 8, 4 < b, b < (209/4), 50*a - 8*b == 0]
[c == 2, max(1, 32/209*b) < a, a < 8, 4 < b, b < (209/4), 50*a - 8*b > 0]
[c == 128, max(1, 32/209*b) < a, a < 8, 4 < b, b < (209/4), 3200*a - 8*b == 0]
[c == 128, max(1, 32/209*b) < a, a < 8, 4 < b, b < (209/4), 3200*a - 8*b > 0]
Supposing `u<v`, Sage insist to write `[[u==t,...],[u<t, t<v, ...], [t==v, ...]]` in place of `[u<=t, t<=v, ...]`. It is certainly possible to write something for automatically rewrite this, but I'm not currently aware of how...
Cheating a bit :
sage: %time MSol=mathematica.Reduce(Sys,[a,b,c])
CPU times: user 6.76 ms, sys: 0 ns, total: 6.76 ms
Wall time: 31.3 ms
sage: MSol
Inequality[1, LessEqual, a, LessEqual, 8] &&
((Inequality[4, LessEqual, b, LessEqual, (25*a)/4] &&
Inequality[2, LessEqual, c, LessEqual, 128]) ||
(Inequality[(25*a)/4, Less, b, LessEqual, (209*a)/32] &&
Inequality[(8*b)/(25*a), LessEqual, c, LessEqual, 128]))
faster and considerably terset (but not as explicit) as Sage's solution...
HTH,Sat, 10 Aug 2019 16:31:56 -0500http://ask.sagemath.org/question/47447/obtain-a-particular-solution-for-a-system-of-inequalities-whose-variables-can-only-take-certain-values/?answer=47460#post-id-47460