ASKSAGE: Sage Q&A Forum - Latest question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 19 Oct 2020 23:12:53 -0500Interactivelinearprogramming gives the wrong answerhttps://ask.sagemath.org/question/53982/interactivelinearprogramming-gives-the-wrong-answer/Here are two versions of the same linear programming problem.
The first one is simply the second one after eliminating the variable $z$.
- $\min$ { $-1.8 x - y + 600 \; | \; x \leq 30; \; y \leq 32 ; \; 2 x + 3 y \leq 90; \; 10 x + 6 y \leq 230$ }
- $\min$ { $4.2 x + 5 y + 6 z \; | \; x \leq 30; \; y \leq 32; \; 2 x + 3 y \leq 90; \; 10 x + 6 y \leq 230; \; x + y + z = 100$ }
Here is some SageMath code to solve the first one
%display latex
A = matrix([[1, 0], [0, 1], [2, 3], [10, 6]])
b = vector([30, 32, 90, 230])
c = vector([-1.8, -12])
# La forme du `problem_type` est obligatoire pour `InteractiveLPProblemStandardForm`
inc = InteractiveLPProblem(A, b, c, ["I_A", "I_B"],
problem_type="min",
objective_constant_term=600)
show(inc)
inc.plot()
inc = inc.standard_form()
inc = inc.run_simplex_method()
show(inc)
But the answer is obviously false. I have used the same type of code to solve the second one.
%display latex
A = matrix([[1, 0, 0], [0, 1, 0], [2, 3, 0], [10, 6, 0], [1, 1, 1]])
b = vector([30, 32, 90, 230, 100])
c = vector([4.2, 5, 6])
inc = InteractiveLPProblem(A, b, c, ["x", "y", "z"],
problem_type="min",
constraint_type=['<=', '<=', '<=', '<=', '=='],
objective_constant_term=600)
show(inc)
inc = inc.standard_form()
inc = inc.run_simplex_method()
show(inc)
Here the answer is infeasibility.
But with the following code I reach the good answer (verified by Lips <- Sourceforge,
which gives the same answer for both problems):
n = 5 # nombre de contraintes
m = 3 # nombre de variables
A = matrix(n, m, [1, 0, 0, 0, 1, 0, 2, 3, 0, 10, 6, 0, 1, 1, 1]) # les coefficients
bmin = [0, 0, 0, 0, 100] # bornes inférieures pour les contraintes
bmax = [30, 32, 90, 230, 100] # bornes supérieures pour les contraintes (oo = infini)
show(LatexExpr("A="), A)
show(LatexExpr("bmin="), bmin)
show(LatexExpr("bmax="), bmax)
sol = MixedIntegerLinearProgram(maximization=False, solver="GLPK") # on crée le programme
x = sol.new_variable(integer=False, indices=[0 .. m - 1]) # les nouvelles variables sont x_0 à x_11
B = A*x # la fonction linéaire pour les contraintes
sol.set_objective(4.2*x[0]+5*x[1]+6*x[2]+600) # fixe l'objectif
# on construit les contraintes avec leurs bornes
for i in range(n):
sol.add_constraint(B[i], min=bmin[i], max=bmax[i])
sol.show()
sol.solve()
xx = sol.get_values(x)
show(LatexExpr(r"I_A^\star ="),
xx[0], LatexExpr(r", I_B^\star ="),
xx[1], LatexExpr(r", E^\star ="),
xx[2], LatexExpr(r" \text{ et } C^\star ="),
sol.get_objective_value())
So I wonder if I made a mistake or if there is an error in the code.
I am perfectly aware that the `interactivelinearprogramming` package
is built for pedagogical reasons. But some things are not very obvious.
For instance it is sensitive to the order of the options.CyrilleMon, 19 Oct 2020 23:12:53 -0500https://ask.sagemath.org/question/53982/One step linear programming pivothttps://ask.sagemath.org/question/52769/one-step-linear-programming-pivot/The following question is not because I do not appreciate the numerous linear programming tools disponible from Sage or Python, but because I want to force my student to have a reflexion on how the Dantzig algorithm works by themselves
Here is some data
A=matrix([[1,2,3,4],[4,3,2,1],[2,4,3,1]])
b=vector([10,20,30])
c=vector([1,-2,3,-1])
With the help of this function
def tableau_(mat, cont, obj):
Id = identity_matrix(mat.nrows())
z = matrix(zero_vector(mat.ncols()-1))
return block_matrix([[A,II,matrix(b).transpose()],[matrix(c),matrix(z),zero_matrix(1,1)]],subdivide=False)
I can construct the following table
mm=matrix(tableau_(A, b, c))
show(mm)
now I want a simple step a the Dantzig pivot algorithm applied to mm throug a calling function like
`one_step_pivot(mm, mm[2][2])` where `mm[2][2]` is the pivot. I wonder if there is a simple tools in Sage or numpy or any other package that do the trick or if I would be oblige to write the code by myself nwhich will tazkes me a lot of time. By advance thanks for the help.CyrilleFri, 31 Jul 2020 01:29:40 -0500https://ask.sagemath.org/question/52769/Linear programming displaying Latex formated equationshttps://ask.sagemath.org/question/52724/linear-programming-displaying-latex-formated-equations/The following works fine:
A = ([1,0], [0, -1], [6,10], [-6,-10])
B = (18, -12,70, -70)
C = (-4.10, -8)
P = InteractiveLPProblem(A, B, C, ["x_1", "x_2"], variable_type=">=")
P
P.plot()
But when I run:
P.run_simplex_method()
SageMath says that the original problem is infeasible and should stop there.
But some LaTeX formatted equations appear after.
This could be troublesome for student so I would be glad to know
how to stop displaying anything after the diagnosis of infeasibility.
Is this possible? My students need to be confronted to infeasibility.CyrilleTue, 28 Jul 2020 09:55:19 -0500https://ask.sagemath.org/question/52724/Warning against a deprecated functionhttps://ask.sagemath.org/question/51576/warning-against-a-deprecated-function/ When programming the following code
def varelmat(M) :
return sum((vector(M)).apply_map((x-(sum(vector(M)))/len(vector(M)))^2))/len(vector(M))
and applying it
B= random_matrix(QQ, 7,7)
varelmat(B)
I end with the following warning
`/opt/sagemath-9.0/local/lib/python3.7/site-packages/sage/repl/ipython_kernel/__main__.py:3: DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)
See http://trac.sagemath.org/5930 for details.
IPKernelApp.launch_instance(kernel_class=SageKernel'
Could some one explain me how to keep my code save ?CyrilleWed, 27 May 2020 04:33:48 -0500https://ask.sagemath.org/question/51576/Some code gives error in sagemath 9 but OK in 8.9https://ask.sagemath.org/question/50204/some-code-gives-error-in-sagemath-9-but-ok-in-89/After I upgraded to sagemath 9.0 some of the code that used to work now gives error. The code was used to obtain an estimate of size of expression.
>sage --version
SageMath version 9.0, Release Date: 2020-01-01
>which sage
/bin/sage
It looks like sagemath 9 now uses python 3.0 while 8.9 used python 2 (since I had to change all my print statements to use () to make them work.
Here is an example of function that now gives an error. This is in file, say `bug_sage.py`
#!/usr/bin/env sage
from sage.all import *
def tree(expr):
if expr.operator() is None:
return expr
else:
return [expr.operator()]+map(tree, expr.operands())
var('x')
tree(x*e**((x*log(x) + 1)/log(x)))
Now from command line at Linux, I type `sage ./bug_sage.py` and it gives the error
Traceback (most recent call last):
File "./bug_sage.py", line 12, in <module>
print (tree(x*e**((x*log(x) + 1)/log(x))))
File "./bug_sage.py", line 9, in tree
return [expr.operator()]+map(tree, expr.operands())
TypeError: can only concatenate list (not "map") to list
In 8.9, no error is generated.
It looks like this is due to change in Python itself? This function is meant to generate list of all operands in expression in order to estimate the size of the expression. It is later used as follows
len(flatten(tree(anti)))
Any idea how to fix it? Or do you suggest better way to obtain size of expression (called leaf count in other CAS systems).
NasserMon, 09 Mar 2020 15:52:26 -0500https://ask.sagemath.org/question/50204/Should I avoid using unicode in names of variables?https://ask.sagemath.org/question/49370/should-i-avoid-using-unicode-in-names-of-variables/ Hello, Sage community!
Since Sage now supports Python 3, the possibility of using tildes and other unicode letters in names of variables is real. For example, I could define a constant `Gauß` instead of `Gauss` (with a "ß" instead of "ss"); or I could define a function called `Müller` (with two dots over the "u"). In Spanish and other languages, this is a more pressing matter. If I defined a function `design(howMany)`, in Spanish it would be `diseño(cuántos)`.
However, it seems quite strange to have tildes, betas, etc. in variable names. Is it a good programming behavior to avoid this kind of names?, or, should I use them sparely?
Thanks in advance!dsejasSat, 04 Jan 2020 19:23:51 -0600https://ask.sagemath.org/question/49370/Running Python code from Jupyter and transforming it in a Sage functionhttps://ask.sagemath.org/question/48995/running-python-code-from-jupyter-and-transforming-it-in-a-sage-function/ I would like to know if it is possible to run a python code directly within a notebook and transform it in a sagemath function. The problem is that I am nearly sure that is possible, but I do not know how to install it --- I am under windows an a poor programmer.
For instance if some one has to kindness to take as an exemple https://github.com/fordc5/PrisonersDilemmaCSWorkshop, it will be great.CyrilleWed, 11 Dec 2019 03:01:06 -0600https://ask.sagemath.org/question/48995/SEND+MORE=MONEY (one more time)https://ask.sagemath.org/question/48931/sendmoremoney-one-more-time/I have some heavy problems of formulation with Sagemath. It's certainly a formulation problem --- I will add the theoretrical aspect of the model at the end of the question :
I have corrected my code. May be some errors could stay. But even if I am wrong I need some information on how to write my program. You can find the source in the free paper https://pubsonline.informs.org/doi/pdf/10.1287/ited.2016.0163. There is also an Excel code on the internet. Here is my code
p = MixedIntegerLinearProgram(maximization=True,solver='PPL')
v = p.new_variable(nonnegative=True)# permet de définir v[i, j] aussi bien que v[i]
c = p.new_variable(nonnegative=True)# permet de définir v[i, j] aussi bien que v[i]
p.set_objective(v[0,0])
for i in range(7) : # nombre de lettres (8)
for j in range(9) : #nombre de chiffres
p.set_binary(v[i,j])
#S=sum(j*v[0,j] for j in range(9))
#E=sum(j*v[1,j] for j in range(9))
#N=sum(j*v[2,j] for j in range(9))
#D=sum(j*v[3,j] for j in range(9))
#M=sum(j*v[4,j] for j in range(9))
#O=sum(j*v[5,j] for j in range(9))
#R=sum(j*v[6,j] for j in range(9))
#Y=sum(j*v[7,j] for j in range(9))
for k in range(1,4) :
p.set_binary(c[k])
# Une lettre n'est représentée que par un chiffre
for j in range(8) :
p.add_constraint(0<= sum(v[i,j] for i in range(7)) <= 1)
# Un chiffre n'est associé qu'à une seule lettre
for i in range(7) :
p.add_constraint(sum(v[i,j] for j in range(9)) == 1)
# D + E = Y + 10 C_1
p.add_constraint(sum(j*v[3,j] for j in range(7))+sum(j*v[1,j] for j in range(9))==
sum(j*v[7,j] for j in range(7))+10*c[1])#
# C_1 + R + N = E + 10 C_2
p.add_constraint(c[1]+sum(j*v[6,j] for j in range(7))+sum(j*v[2,j] for j in range(9))==
sum(j*v[1,j] for j in range(9))+10*c[2])#
# C_2 + E + O = N + 10 C_3
p.add_constraint(c[2]+sum(j*v[5,j] for j in range(8))+sum(j*v[1,j] for j in range(9))==
sum(j*v[2,j] for j in range(9))+10*c[3])#
# C_3 + S + M = O + 10 C_4
p.add_constraint(c[3]+sum(j*v[0,j] for j in range(9)) + sum(j*v[4,j] for j in range(9))==
sum(j*v[5,j] for j in range(9))+10*c[4])#
# C_4 = M
p.add_constraint(c[4]==sum(j*v[4,j] for j in range(9)))
p.solve()
p.show()
xx=p.get_values(v)
show(xx)
for j in range(9) :
if xx[0,j]== 1 : print('S='+ `j`)
for j in range(9) :
if xx[1,j]== 1 : print('E='+ `j`)
for j in range(9) :
if xx[2,j]== 1 : print('N='+ `j`)
for j in range(9) :
if xx[3,j]== 1 : print('D='+ `j`)
for j in range(9) :
if xx[4,j]== 1 : print('M='+ `j`)
for j in range(9) :
if xx[5,j]== 1 : print('O='+ `j`)
for j in range(9) :
if xx[6,j]== 1 : print('R='+ `j`)
for j in range(9) :
if xx[7,j]== 1 : print('Y='+ `j`)
I have 8x10 = 80, v[i,j] variables v[i, j] and 4 c[j] variables. I have asked all to be binary. But when I look the printed model I see only 73 variables and from $x_{66}$ to $x_{73}$ they are not binary. More than that the (7,6) variables takes a value of 3/6. It's not particularly simple to associate the variables internal variables to the internal ones.
I need help! (Thanks by advance). The documentation is of no help for me.
MODELIZATION CLUES
In this so called "cryptarythm" one use 8 letters $S_{(1)}E_{(2)}N_{(3)}D_{(4)}M_{(5)}O_{(6)}R_{(7)}Y_{(8)}$ so we define the binary variables
$x_{ij} =
\begin{cases}
1 & \text{if letter } i (i= 1, \ldots 8) \text{ is associated to th digit } j (j = 0,\ldots, 9)\\\\
0 & \text{sinon}
\end{cases}$
We need also 4 variables $C_k$ for the retention.
From this one can write $S = \sum_{1}^9 j x_{1j }$, $E = \sum_{1}^9 j x_{2j }$ and so on. For the constraints one have 17 general constraints
--- each letter is associated to a unic digit $\sum_{j=0}^9 x_{ij} = 1, \text{ for }
i = 1, \ldots 8$
--- each digit is associated to a only one letter $\sum_{i=0}^8 x_{ij} \leq 1, \text{ for }
j = 1, \ldots 9$
Then one hase the pure addition constraints :
$D + E = Y + 10 C_1 \Longleftrightarrow \sum_{j=0}^9 j x_{2j} +\sum_{j=0}^9 j x_{4j}= \sum_{j=0}^9 j x_{8j} + 10 C_1$
$C_1 + R + N = Y + 10 C_2 \Longleftrightarrow C_1+\sum_{j=0}^9 j x_{3j} +\sum_{j=0}^9 j x_{4j}= \sum_{j=0}^9 j x_{2j} + 10 C_2$
$C_2 + E + O = N + 10 C_3 \Longleftrightarrow C_2+\sum_{j=0}^9 j x_{2j} +\sum_{j=0}^9 j x_{6j}= \sum_{j=0}^9 j x_{3j} + 10 C_3$
$C_3 + S + M = O + 10 C_4 \Longleftrightarrow C_3+\sum_{j=0}^9 j x_{1j} +\sum_{j=0}^9 j x_{5j}= \sum_{j=0}^9 j x_{6j} + 10 C_4$
$C_4 = M \Longleftrightarrow C_4 = \sum_{j=0}^9 j x_{5j}$
Here is the solution
$(S=9). (E=5). (N=6). (D=7).$
$+. (M=1). (O=0).(R=8). (E=5).$
$= (M=1). (O=0). (N=6). (E=5). (y=2)$CyrilleTue, 03 Dec 2019 09:01:23 -0600https://ask.sagemath.org/question/48931/The annulus problem in linear programminghttps://ask.sagemath.org/question/48924/the-annulus-problem-in-linear-programming/ I do not know if the following question is a question about Sagemath or a question about my incapacity to use `MixedIntegerLinearProgram`
The problem is to find the smallest annulus for a set of points. Here is a reference `https://www.ti.inf.ethz.ch/ew/courses/CG12/lecture/Chapter%2011%20and%2012.pdf`
# list of point
lis=[(4,1),(5,6),(2,2),(3,4),(6,3.2),(2.6,2.5),(5.8, 2.4),(2,3.4),(4.2,4),(5.6,1)]
from sage.plot.circle import Circle
# Evaluation of the norm of each point
d = [sqrt(lis[i][0]^2+lis[i][1]^2) for i in range(len(lis)-1)]
anmin = MixedIntegerLinearProgram(maximization=True, solver = "GLPK")# création du programme
nvar=len(lis)-1 # nombre de variables
x = anmin.new_variable(integer=False, indices=[0..nvar-1],nonnegative=True) # les nouvelles variables sont x[0] ... x[3]
anmin.set_objective(x[0]-x[1])# fixe l’objectif
# Constraints
for i in range(nvar) :
anmin.add_constraint(x[0]+ 2*(lis[i][0]*x[2]+lis[i][1]*x[3]), min=0, max=n(d[i]^2, digits = 2))
for i in range(nvar) :
anmin.add_constraint(x[1]+ 2*(lis[i][0]*x[2]+lis[i][1]*x[3]), min=n(d[i]^2, digits = 2), max=oo)
anmin.show()
anmin.solve()
xx=anmin.get_values(x)
show(xx)
The result is obviously false. It would be great if somebody helps me find the good answer.
CyrilleSun, 01 Dec 2019 09:58:04 -0600https://ask.sagemath.org/question/48924/SEND+MORE=MONEY (Milp programming)https://ask.sagemath.org/question/48795/sendmoremoney-milp-programming/ Her is the code (unhappily wrong) for the famous verbal arithmetic : SEND+MORE=MONEY coded by Simon (Puzzle—Verbal Arithmetic and Mastermind --- accessible in line)
p = MixedIntegerLinearProgram(maximization=True,solver='GLPK')
v = p.new_variable(nonnegative=True)
p.set_objective(v[0,0])
for i in range(9) :
for j in range(8) :
p.set_binary(v[i,j])
#S=sum(j*v[0,j] for i in range(0,7))
#E=sum(j*v[1,j] for i in range(0,7))
#N=sum(j*v[2,j] for i in range(0,7))
#D=sum(j*v[3,j] for i in range(0,7))
#M=sum(j*v[4,j] for i in range(0,7))
#O=sum(j*v[5,j] for i in range(0,7))
#R=sum(j*v[6,j] for i in range(0,7))
#Y=sum(j*v[7,j] for i in range(0,7))
# Une lettre n'est représentée que par un chiffre
for j in range(8) :
p.add_constraint(sum(v[i,j] for i in range(0,7)) <= 1)
# Un chiffre n'est associé qu'à une seule lettre
for i in range(7) :
p.add_constraint(sum(v[i,j] for j in range(0,8)) == 1)
# le modèle génère 80 variables, les retenues seront notées au delà
# D + E = Y + 10 C_1
p.add_constraint(sum(j*v[3,j] for i in range(0,7))+sum(j*v[1,j] for i in range(0,7))==sum(j*v[7,j] for i in range(0,7))+10*v[8,8])
# C_1 + R + N = E + 10 C_2
p.add_constraint(v[8,8]+sum(j*v[6,j] for i in range(0,7))+sum(j*v[2,j] for i in range(0,7))==sum(j*v[1,j] for i in range(0,7))+10*v[9,8])
# C_2 + E + O = N + 10 C_3
p.add_constraint(v[9,8]+sum(j*v[5,j] for i in range(0,7))+sum(j*v[1,j] for i in range(0,7))==sum(j*v[2,j] for i in range(0,7))+10*v[10,8])
# C_3 + S + M = O + 10 C_4
p.add_constraint(v[10,8]+sum(j*v[0,j] for i in range(0,7)) + sum(j*v[2,j] for i in range(0,7))==sum(j*v[7,j] for i in range(0,7))+10*v[11,8])
# C_4 = M
p.add_constraint(v[11,8]==sum(j*v[4,j] for i in range(0,7)))
p.show()
p.solve()
xx=p.get_values(v)
show(xx)
Obviously there is a mistake in my code that appears clearly with `p.show()`. The $j*V_{i,j}$ are evaluated outside of the context. Secondly, the code would be more readable if I could replace the sums by the letters. I think the evaluation must be postponed but I dont know how.
CyrilleMon, 18 Nov 2019 08:08:50 -0600https://ask.sagemath.org/question/48795/Mixed integer programming constraint definitionhttps://ask.sagemath.org/question/48228/mixed-integer-programming-constraint-definition/Sorry to ask so much questions but I am in hurry. I would like to know why this procedure doesn't work
> A=
> matrix(8,4,(1,1,1,-14,0,1,2,-8,-1,1,1,0,0,0,0,-1,1,0,0,0,0,1,0,0,0,0,0,0,0,1))
> b= matrix(8,1,(0,0,0,-1,0,0,0,0))
> sign=list('==' '==' '>=' '>=' '>='> '>=' '>=' '>=')
> x = vector(var('x',n=4, latex_name='x'))
>B=A*x
>p = MixedIntegerLinearProgram(maximization=False, solver = "GLPK")
> x = p.new_variable(integer=True)
> p.add_constraint(B[0] 'sign[0]' b[0])
for the last line I have tryed
> p.add_constraint(B[0] sign[0] b[0])
> p.add_constraint(B[0] == b[0])
without any success.CyrilleMon, 07 Oct 2019 14:23:21 -0500https://ask.sagemath.org/question/48228/On Linear Programming with a double sumhttps://ask.sagemath.org/question/48761/on-linear-programming-with-a-double-sum/With the help of **nbruin**, this is a perfect way to resolve a double sum problem.
c = matrix(3,3,(-1,2,-3,3,2,-1,2,1,3))
d = matrix(3,3,(1,2,3,3,2,1,2,1,3))
show(c)
show(d)
p = MixedIntegerLinearProgram(solver='GLPK')
v = p.new_variable(nonnegative=True)
for j in range(3):
p.set_objective(sum(c[i,j]*v[i,j] for i in range(0,3) for j in range(0,3)))
p.add_constraint(sum(d[i]*v[i,j] for i in range(0,3)) <= 1)
p.show()
p.solve()
vv=p.get_values(v)
show(vv)
w=v.items()
w
But 1) w is not sorted 2) I would like to construct a table with $x_{(a, b)}$ in one column and the optimal value of $x_c$ which corresponds to the $x_{(a, b)}$ . CyrilleFri, 15 Nov 2019 09:39:39 -0600https://ask.sagemath.org/question/48761/Linear programming sum and double sumhttps://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/ I wonder why those syntax are invalid
p = MixedIntegerLinearProgram(solver='GLPK')
v = p.new_variable(nonnegative=True)
p.add_constraint(sum([v[i,1] for i in [0,3]) <= 1)
show(p)
or
p = MixedIntegerLinearProgram(solver='GLPK')
v = p.new_variable(nonnegative=True)
p.add_constraint(sum(sum([v[i,j] for i in [0,3]),for j in [1, 5]) <= 1)
show(p)CyrilleThu, 14 Nov 2019 00:24:32 -0600https://ask.sagemath.org/question/48722/Functions in GAPhttps://ask.sagemath.org/question/48711/functions-in-gap/ How can i write special numbers(Mersenne, Fibonacci etc.) or the average or the GCD's two different algorithm in GAP(Group, Algebra and Programming)?
hayyambeyTue, 12 Nov 2019 14:30:04 -0600https://ask.sagemath.org/question/48711/How do I write and implement a program in Sage for Windows?https://ask.sagemath.org/question/48422/how-do-i-write-and-implement-a-program-in-sage-for-windows/ I downloaded Sage 8.9 for Windows and want to use it to do some matrix computations. I do not have any prior experience in programming. So far I've been able to use the SageMath 8.9 Console to declare variables, define matrices, and multiply them together. But I want to write a program which does these computations successively and have SAGE implement it, rather than me typing in the commands one at a time.
I haven't been able to find how to do this in any tutorials. I don't understand where exactly I should be writing the code, how to save the file, how to get SAGE to read the program I wrote and implement it. D_SFri, 18 Oct 2019 22:29:30 -0500https://ask.sagemath.org/question/48422/Adapt the nauty_directg functionhttps://ask.sagemath.org/question/48275/adapt-the-nauty_directg-function/Dear reader,
For a research interest, I would like to generate the collection of non-isomorphic digraphs on a given graph, with a given subset of the edges fixed. Naturally, simply generating the full collection using digraphs.nauty_directg() and removing the undesirable members of the obtained collection would be functional, but it's quite the waste of computing time to take this approach. Hence, I was wondering whether I'd be able to get under the hood of the nauty_directg function to create my own adapted version, which simply skips over the orientation of the edges listed in some input argument. Would anyone be able to help me get started with this endeavor?
Best,
PepijnPepijnWissingFri, 11 Oct 2019 06:01:38 -0500https://ask.sagemath.org/question/48275/MixedIntegerLinearProgram strange behaviorhttps://ask.sagemath.org/question/48107/mixedintegerlinearprogram-strange-behavior/This is a reedition of my initial question
I find missleading the following comportment (I have add name="w") according the suggestion of dsejas
> %display latex p =
>
> MixedIntegerLinearProgram(maximization=False,
>
> solver = "GLPK")
>
> w = p.new_variable(integer=True,
>
> nonnegative=True,name="w")
>
> p.add_constraint(w[0] + w[1] + w[2] -
> 14*w[3] == 1)
>
> p.add_constraint(w[1] + 2*w[2] -
> 8*w[3] == 0)
>
> p.add_constraint(2*w[2] - 3*w[3] == 0)
>
> p.add_constraint(w[0] - w[1] - w[2] >=
> 0)
>
> p.add_constraint(w[3] >= 1)
>
> p.set_objective(w[2]+ 5*w[3])
>
> p.show()
but look at what is shown. The $x_i$'s are in the display. Then I have typpeset :
> b = p.get_backend()
>
> b.solver_parameter("simplex_or_intopt", "simplex_only")
>
> b.solver_parameter("verbosity_simplex", "GLP_MSG_ALL")
>
> ans = p.solve()
>
>ans
followed by
> val=p.get_values(w)
>
> val
and finally
> val0=p.get_values(w[2])
>
> val0
Look at w[2]'s value. It is not an integer value.CyrilleMon, 30 Sep 2019 15:13:18 -0500https://ask.sagemath.org/question/48107/Interactive linear programminghttps://ask.sagemath.org/question/48092/interactive-linear-programming/1) According to the documentation of "interactive simplex method" the following code seems legitimate
> %display latex A = ([1, 1, 3], [3, 1,
> 2], [2, 0, 2]) b = (1000, 1500, 1100)
> c = (10, 5, 2) P =
> InteractiveLPProblemStandardForm(A, b,
> c,["x_1", "x_2",
> "x_3"],slack_variables=["e_4", "e_5",
> "e_6"],
> problem_type="min",constraint_type=["<=",
> ">=","<="]) show(P)
seems correct. But I receive an error message
TypeError: __init__() got an unexpected keyword argument 'constraint_type'.
2) mai I write "="
3) If I add
> variable_type=[">=", ">="]
what must I enter if there is no restriction on a variable ?CyrilleSun, 29 Sep 2019 02:07:27 -0500https://ask.sagemath.org/question/48092/How to create a "sage program" with command line inputhttps://ask.sagemath.org/question/44708/how-to-create-a-sage-program-with-command-line-input/I am completely new to Sage and Pyhton, so this is probably a very trivial question, but I am not able to find any answer. I have a file script.sage
N=10
sage_command1
sage_command2
sage_command3
print(result)
I use this script via `sage script.sage` and it does what I want. How can I modify this so that I can pass the variable `N` in the command line? Something like `sage script.sage 10` and then it prints the result.
Thank you!RiccardoTue, 18 Dec 2018 16:35:35 -0600https://ask.sagemath.org/question/44708/Using multiple lines of pari/gp code in a Sage notebookhttps://ask.sagemath.org/question/41758/using-multiple-lines-of-parigp-code-in-a-sage-notebook/I am struggling to get multiple lines of pari/gp code working in a Jupyter Sage notebook.
When I enter:
%%gp
for(x=1,10,print(x))
it all works fine, however when I for instance write:
%%gp
for(x=1,10,{
print(x);
})
the system just 'hangs' and doesn't return any output. The same issue occurs in the Cocalc Sage cloud environment. I also tried 'pari/gp in your browser' and there it works fine.
Am I doing something wrong or isn't the multi-line pari/gp option supported in Sage?RuudHSun, 25 Mar 2018 15:58:41 -0500https://ask.sagemath.org/question/41758/Monkey-patching PARI callshttps://ask.sagemath.org/question/39807/monkey-patching-pari-calls/I'm currently trying to implement some number field computations on a very particular number field (specifically, $K = \mathbb{Q}(\zeta_{n-1},n^{1/(n-1)})$). The code I've written so far has some bottlenecks that profiling via %prun has identified, namely:
1. `{method '_nf_rnfeq' of 'sage.libs.cypari2.gen.gen' objects}`, which seems to compute the minimal polynomial of $K$ (currently I'm defining $K$ via `F.<u> = CyclotomicField(n-1)`, and then `K.<a> = F.extension(x^(n-1) - n)`, so $K$ is a relative extension. `_nf_rnfeq` seems to compute the absolute minimal polynomial of a given relative extension (via PARI).
2. `{method 'discriminant' of 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint' objects}`, which appears to just be the polynomial discriminant computation.
Assume I've done analysis by hand gives me a much faster computation of the absolute minimal polynomial for this particular number field. I'd then want a version of `_nf_rnfeq` that:
1. Checks if it's being called on my special case, where better algorithms exist. If so, run that.
2. Otherwise, run the traditional algorithm.
Of course, I'd prefer to make this modification without modifying the source code, as that seems to be an especially ugly solution.
One way to implement this is to change is by modifying the relevant method at run-time, via a technique known as "Monkey-Patching" (see [here](https://stackoverflow.com/questions/47503061/replacing-specific-function-calls/47503146#47503146)).
The issue I'm having now is that attempting to monkey-patch the PARI call gives me the error:
`TypeError: can't set attributes of built-in/extension type 'sage.libs.cypari2.gen.gen'`
The code I'm using (in the first cell of my IPython notebook) is below:
import sage.libs.cypari2.gen
orig_nf_rnfeq = sage.libs.cypari2.gen.gen._nf_rnfeq
def _nf_rnfeq(*args, **kwargs):
print("Rnfeq works!")
return orig_nf_rnfeq(*args, **kwargs)
sage.libs.cypari2.gen.gen._nf_rnfeq = _nf_rnfeqorangejakeSun, 26 Nov 2017 17:37:25 -0600https://ask.sagemath.org/question/39807/Can you help me with digits of pi?https://ask.sagemath.org/question/39517/can-you-help-me-with-digits-of-pi/ I want to put the first n digits of pi into a list. I have tried numerical approximation but I was not able to figure out if it could help me .Neither did I find a function for it.veritasMon, 13 Nov 2017 13:22:11 -0600https://ask.sagemath.org/question/39517/Can you help me with the digits of pi?https://ask.sagemath.org/question/39518/can-you-help-me-with-the-digits-of-pi/ I want to put the first n digits of pi into a list. I tried numerical approximation but it didn't give me solutions. Neither did I find function which shows the first n digits not the rounded version of piveritasMon, 13 Nov 2017 13:29:21 -0600https://ask.sagemath.org/question/39518/using sage library in C or C++?https://ask.sagemath.org/question/39196/using-sage-library-in-c-or-c/ Is there a way to run sage library in C/C++ or include the sage in C/C++? For example, I want to calculate minkowski sum, difference or decomposition in C or C++? studentboyTue, 17 Oct 2017 10:57:44 -0500https://ask.sagemath.org/question/39196/how to use sage in PyCharm on CoCalchttps://ask.sagemath.org/question/39262/how-to-use-sage-in-pycharm-on-cocalc/How can I use Sage library in PyCharm IDE like on CoCalc? studentboyTue, 24 Oct 2017 04:15:47 -0500https://ask.sagemath.org/question/39262/programming of looping to print selected value of mhttps://ask.sagemath.org/question/35264/programming-of-looping-to-print-selected-value-of-m/I have the following code for m=1:
Qp=pAdicField(13)
E=EllipticCurve(Qp,[0,0,0,-3267,45630]); E
P=E([-21,324]);P
p=13;p #because I am working 13-adically
R=12104*1*P;R #m=1
S=-2*P;S
Q=R+S;Q
(12 + 8*13 + 5*13^2 + 10*13^3 + 10*13^4 + O(13^5) : 9 + 3*13 + 6*13^2 + 13^3 + 5*13^4 + 2*13^5 + O(13^5) : 1 + O(13^5))
x=Q[0];x
12 + 8*13 + 5*13^2 + 10*13^3 + 10*13^4 + O(13^5)
y=Q[1];y
9 + 3*13 + 6*13^2 + 13^3 + 5*13^4 + O(13^5)
W=(81*x^16 + 40662*x^15 + 14353281*x^14 - 460241028*x^13 - 644722959186*x^12 + 39379675354740*x^11 + 5212980804862026*x^10 - 415546630058854656*x^9 - 8202010485984353739*x^8 + 1396767997483732402758*x^7 - 27550698906220673513787*x^6 - 1044392234943529703379852*x^5 + 60770398462922893831446348*x^4 - 1284453663719469166478575296*x^3 + 14183844641879715988450074288*x^2 - 81800517874945025246941522368*x + 196162341839727571433321441856)- (3240*x^14 + 456840*x^13 + 188268624*x^12 - 45834271200*x^11 - 2435651997264*x^10 + 682353767281968*x^9 - 7053953405575680*x^8 - 2553415737499629216*x^7 + 98906717445152189544*x^6 + 1348117411901578667784*x^5 - 162666175355778441465360*x^4 + 4276857451171442758058304*x^3 - 54456600108308451946891776*x^2 + 350065581968511893813480064*x - 918312303919436410092339456)*y;W
2 + 4*13 + 3*13^2 + 11*13^3 + 11*13^4 + O(13^5)
D=W[0];D
2
T=kronecker(D,p);T
-1
So in this example I reject m=1 because T=-1. So basically I want to do a loop for m=1,2,...,10, and everytime T=-1 I will reject that value of m and only leave set of numbers of m=[2,..10] that gives T=1. I tried doing the coding to work but it keeps giving error regarding the "m" value. I did something like this
Qp=pAdicField(13)
E=EllipticCurve(Qp,[0,0,0,-3267,45630]); E
P=E([-21,324]);P
p=13;p #because I am working 13-adically
R=12104*m*P;R #I use a general m which I will define later that m=1,2,...,10
S=-2*P;S
Q=R+S;Q
x=Q[0];x
y=Q[1];y
W=(81*x^16 + 40662*x^15 + 14353281*x^14 - 460241028*x^13 - 644722959186*x^12 + 39379675354740*x^11 + 5212980804862026*x^10 - 415546630058854656*x^9 - 8202010485984353739*x^8 + 1396767997483732402758*x^7 - 27550698906220673513787*x^6 - 1044392234943529703379852*x^5 + 60770398462922893831446348*x^4 - 1284453663719469166478575296*x^3 + 14183844641879715988450074288*x^2 - 81800517874945025246941522368*x + 196162341839727571433321441856)- (3240*x^14 + 456840*x^13 + 188268624*x^12 - 45834271200*x^11 - 2435651997264*x^10 + 682353767281968*x^9 - 7053953405575680*x^8 - 2553415737499629216*x^7 + 98906717445152189544*x^6 + 1348117411901578667784*x^5 - 162666175355778441465360*x^4 + 4276857451171442758058304*x^3 - 54456600108308451946891776*x^2 + 350065581968511893813480064*x - 918312303919436410092339456)*y;W
D=W[0];D
T=kronecker(D,p);T
for m in range(10) :
if T == 1:
print(m)
Unfortunately, the programming is not working, keeps giving error for `m`.
By the way, I got the following code working on PARI, but I prefer to use SAGE. Is there a way to get the coding to work on SAGE too.
for(m=1,10,R=12104*m*P;p=13;x=R[1];y=R[2]; W=(81*x^16 + 40662*x^15 + 14353281*x^14 - 460241028*x^13 - 644722959186*x^12 + 39379675354740*x^11 + 5212980804862026*x^10 - 415546630058854656*x^9 - 8202010485984353739*x^8 + 1396767997483732402758*x^7 - 27550698906220673513787*x^6 - 1044392234943529703379852*x^5 + 60770398462922893831446348*x^4 - 1284453663719469166478575296*x^3 + 14183844641879715988450074288*x^2 - 81800517874945025246941522368*x + 196162341839727571433321441856)- (3240*x^14 + 456840*x^13 + 188268624*x^12 - 45834271200*x^11 - 2435651997264*x^10 + 682353767281968*x^9 - 7053953405575680*x^8 - 2553415737499629216*x^7 + 98906717445152189544*x^6 + 1348117411901578667784*x^5 - 162666175355778441465360*x^4 + 4276857451171442758058304*x^3 - 54456600108308451946891776*x^2 + 350065581968511893813480064*x - 918312303919436410092339456)*y;q=lift(W);a=q*(denominator(q))^2;if(kronecker(a,p)<0, print1(m "\t")))ShaThu, 27 Oct 2016 05:10:22 -0500https://ask.sagemath.org/question/35264/How to define Sage function with Optional arguments?https://ask.sagemath.org/question/35192/how-to-define-sage-function-with-optional-arguments/I need to define a Sage function with optional arguments.
The number of mandatory arguments is 2 and there are up to 3 optional arguments.
How do I do this is Sage?VovaWed, 19 Oct 2016 13:56:35 -0500https://ask.sagemath.org/question/35192/SAGE multiplication in 2^8https://ask.sagemath.org/question/34841/sage-multiplication-in-28/Hi guys, I have a questions:
If I want to do the multiplication 0x09 * x where x is another number hex.
or another example, 0x0d * x, how can I do this in sage?
Because for example when I multiply by 2, I do:
x=2*x
if x>=256:
x=(x-256)^^0x1b
the multplication is in 2⁸
Thank you very muchmanu4912Fri, 16 Sep 2016 03:45:08 -0500https://ask.sagemath.org/question/34841/If , else, case?https://ask.sagemath.org/question/34830/if-else-case/Hallo!
Is there a function to help me out to test a condition without a 'cascade' of 'else(s)'? Like a "**case**"? I'm trying hard to find out a solution using the build-in help/manual and could not find a "case" statement. *Thank you in advance.*
r = randint(1,3)
if r == 1:
print 'One.'
if r == 2:
print 'Two.'
if r == 3:
print 'Three.'LRMThu, 15 Sep 2016 17:24:08 -0500https://ask.sagemath.org/question/34830/Recursive Algorithm for Graph Coloringhttps://ask.sagemath.org/question/34051/recursive-algorithm-for-graph-coloring/In a 2014 article by Exoo, Ismailescu, and Lim ("On the Chromatic Number of R^4"), a recursive algorithm is described that verifies the absence of a proper $k$-coloring of a graph $G$. The authors include only the following description of the algorithm.
"[The program is] based on the following recursive procedure that does an exhaustive search for a $K$-coloring of a graph of order $N$. It employs a global variable *color*, an array of order $N$, which records the color of each vertex $v$ for $1 \leq v \leq N$. The search is initiated with the call DFS(1)."
procedure DFS(v):
Local variable: FCSet - the set of feasible colors for vertex v.
if v > N:
All vertices have been colored, report G is K-colorable.
Exit.
end if
FCSet = {1 ... K}
for u = 1 to v-1:
if u is adjacent to v:
FCSet = FCSet - color(u)
end if
end for
for c in FCSet:
color(v) = c
DFS(v+1)
end for
end procedure
I am having difficulty implementing this algorithm in Sage. Given the nature of the program, I thought Java would be a more natural programming language to use for this algorithm, but I'm afraid I am not familiar with Java syntax.
Any help with implementing this algorithm would be greatly appreciated! JEAThu, 07 Jul 2016 12:53:25 -0500https://ask.sagemath.org/question/34051/