Ask Your Question
1

Equation problem

asked 2020-04-18 16:01:45 +0100

Bert Henry gravatar image

updated 2020-04-18 18:50:38 +0100

FrédéricC gravatar image

I have the equation

x + y = 15

and I'm looking for solution only in the range x=1..9 and y=1..9, x and y both integer Is there a sage-command to do that?

I tried it with

var('x, y')
assume(x,"integer")
assume(x>0)
assume(y, "integer")
assume(y>0)
solve(x+y==15,x,y)

The result was

(t_0, -t_0 + 15)

obviously right, but not 6,9 7,8 8,7 and 9,6

Thanks in advance Bert Henry

edit retag flag offensive close merge delete

Comments

Why not this?

[(x,15-x) for x in [1..9] if 15-x in [1..9] ]
Juanjo gravatar imageJuanjo ( 2020-04-18 19:04:23 +0100 )edit
slelievre gravatar imageslelievre ( 2020-04-18 22:09:32 +0100 )edit

Thanks a lot for your answers!!! I will try them all. Perhaps with some additional conditions: x != y, z as a third variable and perhaps two equations.

Bert Henry gravatar imageBert Henry ( 2020-04-19 11:27:05 +0100 )edit

Dear sielievre, I posted it first in the Google-Group, thats true, but afterwards I recognised, that that group was for more technical questions.

Bert Henry gravatar imageBert Henry ( 2020-04-19 11:28:57 +0100 )edit

It's fine to ask at either place. Some prefer the mailing list format, others the Q&A website.

When a question is asked in multiple places it's nice if they link to each other so when someone later has a similar question they can see the answers at all places.

slelievre gravatar imageslelievre ( 2020-04-19 16:18:49 +0100 )edit

5 Answers

Sort by » oldest newest most voted
2

answered 2020-04-18 22:11:32 +0100

slelievre gravatar image

Expanding on hints by Matthias Köppe on sage-support and by FrédéricC here.

In RR^2, consider the set S of all (x, y) satisfying:

    x >= 1
    x <= 9
    y >= 1
    y <= 9
    x + y = 15

or if one prefers,

    -1 + x >= 0
    9 - x >= 0
    -1 + y >= 0
    9 - y >= 0
    -15 + x + y = 0

Since all the conditions used to define this set are of one of the following forms:

(linear form in x and y) = 0
(linear form in x and y) >= 0

the subset S is what is called a "polyhedron" in R^2.

The problem in your original post can now be rephrased as:

Find all integral points in the polyhedron S.

An introduction to polyhedra in Sage is at:

http://doc.sagemath.org/html/en/reference/discrete_geometry/sage/geometry/polyhedron/constructor.html

The polyhedron S can be input as

S = Polyhedron(ieqs=[[-1, 1, 0], [9, -1, 0], [-1, 0, 1], [9, 0, -1]], eqns=[[-15, 1, 1]])

Check that our input represents the correct polyhedron:

sage: print(S.Hrepresentation_str())
x0 + x1 ==  15
    -x0 >= -9
     x0 >=  6

Find all integral points:

sage: S.integral_points()
((6, 9), (7, 8), (8, 7), (9, 6))
edit flag offensive delete link more
1

answered 2020-04-18 19:42:46 +0100

This is a pure Python solution which also works in Sage:

sage: [(x,y) for x in range(1, 10) for y in range(1, 10) if x+y==15]
[(6, 9), (7, 8), (8, 7), (9, 6)]
edit flag offensive delete link more
1

answered 2020-04-19 10:34:55 +0100

vdelecroix gravatar image

More straightforward

sage: sum = 15
sage: xmin = 1
sage: xmax = 9
sage: ymin = 1
sage: ymax = 9
sage: for x in range(max(xmin, sum-ymax), min(xmax, sum-ymin)+1):
....:     print(x, sum-x)     
6 9
7 8
8 7
9 6
edit flag offensive delete link more
1

answered 2020-04-18 18:58:04 +0100

FrédéricC gravatar image

Using polyhedrons (here a simplified example) :

sage: P = Polyhedron(ieqs=[[0,1,0],[0,0,1]],eqns=[[-15,1,1]]) ; P
A 1-dimensional polyhedron in QQ^2 defined as the convex hull of 2 vertices
sage: P.integral_points()
((0, 15),
 (1, 14),
 (2, 13),
 (3, 12),
 (4, 11),
 (5, 10),
 (6, 9),
 (7, 8),
 (8, 7),
 (9, 6),
 (10, 5),
 (11, 4),
 (12, 3),
 (13, 2),
 (14, 1),
 (15, 0))
edit flag offensive delete link more
0

answered 2020-04-19 09:52:53 +0100

Cyrille gravatar image

In fact what you ask is the use of Fourier-Motzkin elimination. It can be obtain directly because Maxima implement it. But I have not been able to obtain all the solution in one program :

for [6,9] & [9, 6] you may use

var('x, y')
assume(x,"integer")
assume(y, "integer")
solve([x + y <= 15, x + y >= 15, x >=1, y>=1, x<=9, y<=9], x, y)

for [7,8] & [8, 7]you may use

var('x, y')
assume(x,"integer")
assume(y, "integer")
solve([x + y <= 15, x + y >= 15, x >=7, y>=7, x<=9, y<=9], x, y)

In fact I was persuaded that Maxima gives all the solution in one shot

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2020-04-18 16:01:45 +0100

Seen: 446 times

Last updated: Apr 19 '20