Ask Your Question
0

How do I filter only integer solutions in sagemath?

asked 2022-04-20 11:10:08 +0100

Periodic_1_6 gravatar image

updated 2022-04-20 11:19:34 +0100

How do I filter only integer solutions in sagemath?

import time
Start_Time = time.time()
var('x y N M F f G g L l Q q S s U u')

eq0 = N-1019 == 0 



eq3 = (32*((2*(3*3/4*M+1)-3*(-x)+1)/24)+3*(2*f-1)^2-3)/8-F==0
eq4 = ((2*(3*3/4*M+1)-3*(-x)+1)/24)+3/4*M- F == 0
eq5 = (32*((2*(3*F+1)-3*(f)+1)/24)+3*(2*g-1)^2-3)/8-G==0
eq6 = ((2*(3*F+1)-3*(f)+1)/24)+3/4*M- G == 0
eq7 = (32*((2*(3*G+1)-3*(g)+1)/24)+3*(2*l-1)^2-3)/8-L==0
eq8 = ((2*(3*G+1)-3*(g)+1)/24)+3/4*M- L == 0
eq9 = (32*((2*(3*L+1)-3*(l)+1)/24)+3*(2*q-1)^2-3)/8-Q==0
eq10 = ((2*(3*L+1)-3*(l)+1)/24)+3/4*M- Q == 0
eq11 = (32*((2*(3*Q+1)-3*(q)+1)/24)+3*(2*s-1)^2-3)/8-S==0
eq12 = ((2*(3*Q+1)-3*(q)+1)/24)+3/4*M- S == 0
eq13 = (32*((2*(3*S+1)-3*(s)+1)/24)+3*(2*u-1)^2-3)/8-U==0
eq14 = ((2*(3*S+1)-3*(s)+1)/24)+3/4*M- U == 0

eq15 =u+1==0


eq16 = (N-3)/8+y*(y-1)/2-M == 0

eq17 = 2*x*(x+1)-M == 0 

solutions = solve([eq0,eq3,eq4,eq5,eq6,eq7,eq8,eq9,eq10,eq11,eq12,eq13,eq14,eq15,eq16,eq17],x,y,N,M,F,f,G,g,L,l,Q,q,S,s,U,u)
sol = solutions  
Execution_Time = time.time() - Start_Time
print (Execution_Time)
print(sol)
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2022-04-20 13:00:17 +0100

Sébastien gravatar image

updated 2022-04-20 13:04:29 +0100

In Python, you can filter a list using list comprehension. Among the 128 solutions found, only 2 are integer solutions:

sage: len(sol)
128
sage: def is_integer_solution(solution):
....:     return all(eq.rhs() in ZZ for eq in solution)
....:     
sage: integer_solutions = [s for s in sol if is_integer_solution(s)]
sage: len(integer_solutions)
2
sage: integer_solutions
[[x == 127,
  y == -254,
  N == 1019,
  M == 32512,
  F == 30496,
  f == -63,
  G == 32016,
  g == -31,
  L == 32392,
  l == -15,
  Q == 32484,
  q == -7,
  S == 32506,
  s == -3,
  U == 32511,
  u == -1],
 [x == 127,
  y == 255,
  N == 1019,
  M == 32512,
  F == 30496,
  f == -63,
  G == 32016,
  g == -31,
  L == 32392,
  l == -15,
  Q == 32484,
  q == -7,
  S == 32506,
  s == -3,
  U == 32511,
  u == -1]]
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

1 follower

Stats

Asked: 2022-04-20 11:10:08 +0100

Seen: 351 times

Last updated: Apr 20 '22