# 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!

edit retag close merge delete

For 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?

Hi, 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, Tobi

Sort by » oldest newest most voted

Since $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.

more

Good 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. :-)

Hi, 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..

Would 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.

Here 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.

more

Hi, 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?

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. :-)

Maxima'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.