Ask Your Question

Solve system of equations with additional conditions in sage

asked 2011-05-18 17:54:35 +0200

twk gravatar image

updated 2011-05-19 01:07:57 +0200

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

edit retag flag offensive 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?

Jason Bandlow gravatar imageJason Bandlow ( 2011-05-19 00:13:02 +0200 )edit

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

twk gravatar imagetwk ( 2011-05-19 01:07:31 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2011-05-20 10:36:30 +0200

Jason Bandlow gravatar image

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.

edit flag offensive delete link 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. :-)

Kelvin Li gravatar imageKelvin Li ( 2011-05-20 15:18:57 +0200 )edit

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

twk gravatar imagetwk ( 2011-05-20 17:07:26 +0200 )edit

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.

twk gravatar imagetwk ( 2011-05-20 17:12:23 +0200 )edit

answered 2011-05-18 20:17:50 +0200

Kelvin Li gravatar image

updated 2011-05-19 14:37:52 +0200

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.

edit flag offensive delete link 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?

twk gravatar imagetwk ( 2011-05-19 02:10:39 +0200 )edit

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

Kelvin Li gravatar imageKelvin Li ( 2011-05-19 14:41:45 +0200 )edit

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.

kcrisman gravatar imagekcrisman ( 2011-05-20 22:13:31 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2011-05-18 17:54:35 +0200

Seen: 4,031 times

Last updated: May 20 '11