Ask Your Question

slelievre's profile - activity

2019-05-23 22:52:38 -0500 commented question RE: This following Question

What operating system? How was Sage installed?

2019-05-21 05:04:01 -0500 commented question Maximum algebraic connectivity from a given collection of graphs

@rburing -- I had missed that. Indeed the answer there adapts to the question here.

2019-05-21 04:58:38 -0500 commented question Help with this?:Tried to use Sage's Python which was not yet installed.

Would you add an answer to say what solution (or workaround) you used? It might help anyone with the same problem.

2019-05-21 04:57:30 -0500 edited question Help with this?:Tried to use Sage's Python which was not yet installed.

My computer is a MacBook Pro (Late 2013) running macOS High Sierra. I've downloaded and tried to run it. I got the following error message in the sage.log file.

Unfortunately the advice in the error message seems hard to use: the directories it refers to cannot be found. I have Python 3.4 installed.

Any suggestions would be greatly appreciated. My expertise level is "beginner".

Error setting environment variables by sourcing '/Applications/SageMath-8.5';
possibly contact sage-devel (see "link removed because karma not high enough to post it here").
/Applications/ line 311: [: /Applications/SageMath-8.5: binary operator expected
Error: Tried to use Sage's Python which was not yet installed.
If this was called from an spkg-install script for another 
package you should add $(PYTHON) as a dependency in 
2019-05-21 04:42:05 -0500 commented answer Maximum algebraic connectivity from a given collection of graphs

@rburing -- I thought that should be possible but missed that part of the documentation when I read it to answer the question... Thanks for coming to the rescue; I edited my answer accordingly.

@Kuldeep -- see revised answer, no longer assuming prior knowledge of the top algebraic connectivity.

2019-05-19 20:04:21 -0500 commented question SageMath Gap Kernel Compatibility

What operating system? How was Sage installed?

2019-05-19 19:04:01 -0500 commented answer Maximum algebraic connectivity from a given collection of graphs

Sorry, sorted now!

2019-05-19 17:23:51 -0500 commented answer How well print a list of matrices ?

@Daniel Krenn - try this instead

sage: l = [identity_matrix(QQ, 2) for _ in range(3)]
sage: from sage.repl.display.formatter import SagePlainTextFormatter
sage: f = SagePlainTextFormatter()
sage: f(l)
'[\n[1 0]  [1 0]  [1 0]\n[0 1], [0 1], [0 1]\n]'
sage: print(f(l))
[1 0]  [1 0]  [1 0]
[0 1], [0 1], [0 1]
2019-05-19 16:25:35 -0500 edited question Is there any way to plot3d latex package on sagemath?

I want to draw a triangle on hyperboloid and put the name of angles and vertices but for example when i want use alpha or beta it doesn't show up? NB i use sagemath.

Here is an example of angles drawn on a surface, to illustrate what I want to achieve:

2019-05-19 16:21:16 -0500 edited question Derivative of a recurrence equation


var('β α γ t R')
c = function('c')
g = function('g')
f = function('f')
λ = function('λ')

suppose I define a function as

def U(l):
    eq = 0
    for i in range(l):
        eq += (β^i)*(((c(t+i)+α*g(t+i))^(1-R))/(1-R))
    return eq

How do I take the first order derivative w.r.t. C_t C_t+1 and so forth?

I tried:

n = 2
L = U(n)

without any luck.

2019-05-19 11:34:58 -0500 answered a question Maximum algebraic connectivity from a given collection of graphs

[Edited to take comments into account.]

Bicyclic graphs with top algebraic connectivity

One can construct the list using Nauty's generator and filtering by the desired conditions.

Note that Nauty's generator can already filter by minimum and maximum number of edges using mine:maxe (for "minimum number of edges", "maximum number of edges") -- thanks to @rburing for pointing this out; see note below on ways to browse the documentation.

There are 236 bicyclic graphs on 8 vertices:

sage: bicy8 = [g for g in graphs.nauty_geng("8 9:9 -c")]
sage: len(bicy8)

For a given graph g, its algebraic connectivity is the second smallest laplacian eigenvalue, so it can be obtained as sorted(g.laplacian_matrix().eigenvalues())[1].

Compute their algebraic connectivity, and the top algebraic connectivity:

sage: ac = [sorted(g.laplacian_matrix().eigenvalues())[1] for g in bicy8]
sage: tac = max(ac)

Extract graphs with top algebraic connectivity:

sage: gg = [g for i, g in enumerate(bicy8) if ac[i] == tac]

There are three:

sage: len(gg)

Their edges:

sage: for g in gg:
....:     print(g.edges(labels=None))
[(0, 6), (0, 7), (1, 6), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7)]
[(0, 5), (0, 7), (1, 6), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7)]
[(0, 3), (0, 6), (1, 4), (1, 6), (2, 5), (2, 6), (3, 7), (4, 7), (5, 7)]

Show them:

sage: for g in gg:

Documentation of Nauty's graph generator

The documentation is available via any of the following solutions, which respectively give the documentation in text mode, the full source code in text mode, and the documentation in html mode:

$ graphs.nauty_geng?
$ graphs.nauty_geng??
$ browse_sage_doc(graphs.nauty_geng)

The html documentation can also be browsed locally (at least if you installed from source or from binaries from the Sage download pages) by pointing a browser to SAGE_ROOT/local/share/doc (where SAGE_ROOT denotes the root directory of your local Sage installation).

Or see the online SageMath documentation (html or pdf).

To avoid storing a long list graphs

To avoid storing all bicyclic graphs on 8 vertices in a list, one could run through them, keeping and updating a current value of the top algebraic connectivity and a current list of graphs achieving it.

sage: tac = 0
sage: gtac = []
sage: for g in graphs.nauty_geng("8 9:9 -c"):
....:     ac = sorted(g.laplacian_matrix().eigenvalues())[1]
....:     if ac == tac:
....:         gtac.append(g)
....:     elif ac > tac:
....:         tac, gtac = ac, [g]

In the end we get the top algebraic connectivity:

sage: tac

and know how many graphs achieve it:

sage: len(gtac)

and we can show their edges:

sage: [g.edges(labels=None) for g in gtac]
[[(0, 6), (0, 7), (1, 6), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7)],
 [(0, 5), (0, 7), (1, 6), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7)],
 [(0, 3), (0, 6), (1, 4), (1, 6), (2, 5), (2, 6), (3, 7), (4, 7), (5, 7)]]

or visualize them:

sage: for g in gtac:

Note there are other options for visualization, including:


Defining a function

The code above can be turned into a function of the number of vertices:

def top_alg_conn(n):
    m = n + 1
    tac = 0
    gtac = []
    for g in graphs.nauty_geng("{} {}:{} -c".format(n, m, m)):
        ac = sorted(g.laplacian_matrix().eigenvalues())[1]
        if ac == tac:
        elif ac > tac:
            tac, gtac = ac, [g]
    return tac, gtac

For 9 vertices this might take something like 10 seconds:

sage: tac, gtac = top_alg_conn(9)
sage: tac, len(gtac)
(1, 2)
sage: for g in gtac:
....:     print(g.edges(labels=False))
[(0, 7), (0, 8), (1, 7), (1, 8), (2, 8), (3, 8), (4, 8), (5, 8), (6, 8), (7, 8)]
[(0, 6), (0, 8), (1, 7), (1, 8), (2, 8), (3, 8), (4, 8), (5, 8), (6, 8), (7, 8)]

For 10 vertices this might take on the order of 30 seconds:

sage: tac, gtac = top_alg_conn(10)
sage: tac, len(gtac)
(1, 2)
sage: for g in gtac:
....:     print(g.edges(labels=False))
[(0, 8), (0, 9), (1, 8), (1, 9), (2, 9), (3, 9), (4, 9), (5, 9), (6, 9), (7, 9), (8, 9)]
[(0, 7), (0, 9), (1, 8), (1, 9), (2, 9), (3, 9), (4, 9), (5, 9), (6, 9), (7, 9), (8, 9)]

For 11 vertices this might take on the order of 2 minutes:

sage: %time tac, gtac = top_alg_conn(11)
CPU times: user 2min 5s, sys: 960 ms, total: 2min 6s
Wall time: 2min 9s

sage: tac, len(gtac)
(1, 2)
sage: for g in gtac:
....:     print(g.edges(labels=False))
[(0, 9), (0, 10), (1, 9), (1, 10), (2, 10), (3, 10), (4, 10), (5, 10), (6, 10), (7, 10), (8, 10), (9, 10)]
[(0, 8), (0, 10), (1, 9), (1, 10), (2, 10), (3, 10), (4, 10), (5, 10), (6, 10), (7, 10), (8, 10), (9, 10)]

And so on...

2019-05-19 10:35:23 -0500 edited question Maximum algebraic connectivity from a given collection of graphs

A connected graph on $n \ge 1$ vertices is cyclic (or unicyclic) if it has $n$ edges, bicyclic if it has $n + 1$ edges.

The following code runs through all connected graphs on 8 vertices with 9 edges, and shows each graph as well as the sorted list of its laplacian eigenvalues.

for g in graphs.nauty_geng("8 -c"):
    if g.size() == 9:
        h = g.laplacian_matrix().eigenvalues()

How could one get the list of graphs whose algebraic connectivity (i.e. second smallest laplacian eigenvalue) is the maximum among those (the maximum here being $1$)?

2019-05-15 16:01:54 -0500 commented question plotting complicated function

In that branch of mathematics, it's usual to study functions that are defined only almost everywhere. The set of measure zero where they are not defined is just disregarded and "does not matter". Of course, when experimenting with a computer, it can turn out to matter.

2019-05-15 15:52:22 -0500 edited question Solving an ODE and simplifying the result

I'm interested in solving the differential equation $$3 h' + 3 h^2 = c_1,$$ where $c_1$ is a positive real number.

var('c1', latex_name=r'c_1')
h = function('h')(t)
eq = -3*h^2 + c1 - 3*diff(h, t)
eq_sol = desolve(eq, h, ivar=t, contrib_ode=True)

The above code works, but it's not solved explicitly for $h$, so

h_sol = solve(eq_sol, h)
h_sol = h_sol[0]

This gives something like $$h\left(t\right) = \frac{\sqrt{3} \sqrt{c_{1}} {\left(e^{\left(\frac{2}{3} \, \sqrt{3} C \sqrt{c_{1}} + \frac{2}{3} \, \sqrt{3} \sqrt{c_{1}} t\right)} + 1\right)}}{3 \, {\left(e^{\left(\frac{2}{3} \, \sqrt{3} C \sqrt{c_{1}} + \frac{2}{3} \, \sqrt{3} \sqrt{c_{1}} t\right)} - 1\right)}},$$

in sage notation (non-LaTeX) it starts like

h(t) == 1/3*sqrt(3)*sqrt(c1)* ...

Question 1: Is there a way to allocate to the solution (i.e. h_sol) the RHS of the above? without the h(t) == part.

I had to set by hand (it is ease, but it would be nice to automatize the allocation)

var('C')    # the integration constant introduced above
h_sol = 1/3*sqrt(3)*sqrt(c1)* ...

Then, by simply looking at the solution it is clear that it can be simplified. I tried things like

h_sol = h_sol.canonicalize_radical()
h_sol = h_sol.collect_common_factors()
h_sol = h_sol.simplify_rectform(complexity_measure = None)

but none of them returns the expected result, which could be obtained from Mathematica's kernel

mathematica("DSolve[3*h'[t] + 3*h[t]^2 == C[1], h[t], t]//FullSimplify")

$$ \sqrt{\frac{c_1}{3}} \tanh\left( \sqrt{\frac{c_1}{3}} (t - 3 c_2) \right) $$

Question 2: How could the expression h_sol be manipulated to obtain the hyperbolic tangent?

2019-05-14 20:13:51 -0500 edited question Checking conjugacy of two matrices
  1. I want to check whether or not two matrices are conjugate. I need to check conjugacy for two matrices in $SL(2,\mathbb{Z})$. How to do it ? How to do it for any general ring or field ? (Notice that I just want to verify whether two given matrices are conjugate or not, I don't need the actual matrices; meaning that say $X$ and $Y$ are conjugates and $AXA^{-1}=Y$. I just want to know if or not $X$ and $Y$ are conjugates, I don't need the matrix $A$)

  2. Extending the previous question, say $AXA^{-1}=Y$. Then we can solve the system of linear equations $AX=YA$ to find out $A$. But is there any command which can directly find $A$ ?

2019-05-14 17:21:35 -0500 edited question plotting complicated function

I would like to approximate the sum $$h(a,x) = \frac{-2}{n} \sum_{n=0}^{n-1} log|T_a^n(x)| $$ where $n$ is large like $n= 1000 - 5000$ and for a fixed $a$ $$T_a(x) = \Big|\frac{1}{x}\Big| - \Big\lfloor{\Big|\frac{1}{x}\Big| - 1 +a}\Big\rfloor$$ where $x \in (0,1).$

By fixing $x$ to be a value $x_0 \in (0,1)$, e.g. $x_0 = 1/\pi$, $$h(a, x_0) = h(a, 1/\pi)$$ a function of one variable, and I want to plot a 2D graph of point $(a, h(a, 1/\pi))$, by fixing $n = 2000$, for $a \in [0,1].$

I figure how to calculate the value at one given $a$ using SageMath, for example, when $a = 1$,

T(x) = 1/x - floor(1/x)
s = 0
for k in xrange(0, 1000):
     a = 0
     a = nest(T, k, 0.79)
     b = abs(a)
     c = log(b)
     s = s + c

Then $\frac{-2}{1000}s$ give the approximation for the sum when $x = 0.79$, $n = 1000$, $a = 1$.

But for plotting, I think I need to define the function $h(a, x)$ which is a summation over composition of functions. I tried to use sum and symbolic_sum but failed.

Any help how to achieve this please?

2019-05-14 17:18:21 -0500 commented question plotting complicated function

@Masacroso: it means the function $T_a$ is applied $n$ times. For example, $T_a^2(x) = T_a(T_a(x))$.

2019-05-14 17:11:57 -0500 answered a question How to combine 3d and 2d plots?

If I understand correctly, the goal is to produce a 3d plot of a function of two variables, with some partial functions plotted on top of it.

The partial plots, or plots of the partial functions, are supposed to be inserted vertically at the relevant x or y values; in other words we want to visualize the intersections of the 3d plot with some vertical planes corresponding to a few particular values of x, and of y.

Here is one way this can be achieved.

Pick a function f of two variables to plot:

sage: f = lambda x, y: x^2 + sqrt(y)

Pick the x-range and the y-range:

sage: x_xx = (-3.1, 3.1)
sage: y_yy = (0, 6)

Define the 3d-plot with partial opacity.

sage: P = plot3d(f, x_xx, y_yy, color='red', alpha=0.25)

To plot partial functions of f, we will use:

sage: Px = lambda x: parametric_plot((lambda t: x, lambda t: t, lambda t: f(x, t)), y_yy)
sage: Py = lambda y: parametric_plot((lambda t: t, lambda t: y, lambda t: f(t, y)), x_xx)

Define a new graphics object G that initially is P and add the desired partial plots to it:

sage: G = P
sage: for x in (-3 .. 3):
....:     G += Px(x)
sage: for y in (0 .. 6):
....:     G += Py(y)

View the result (choices for the viewer include 'jmol', 'tachyon', 'threejs'):

2019-05-14 16:12:49 -0500 commented answer Plot points in loglog
2019-05-13 10:14:37 -0500 received badge  Nice Answer (source)
2019-05-12 06:05:36 -0500 commented question Chevie on Windows

Also if you want to keep working on Windows but can upgrade to Windows 10, then you can use the "Windows Subsystem for Linux" which lets you run Linux "inside Windows".

2019-05-12 05:42:28 -0500 commented question How to implement pairings on MNT curves?

A quick websearch reveals a number of introductory texts.

Maybe it can help clarify the background and the question.

2019-05-11 15:53:10 -0500 answered a question Infinite polynomial ring with several index

One workaround would be to use an infinite polynomial ring in $x$ and $y$, with $x_i y_j$ standing for $x_{i, j}$.

sage: R.<x, y> = InfinitePolynomialRing(ZZ)
sage: x
sage: y
sage: x[3]*y[4]
sage: y[6]*x[1]
2019-05-11 15:34:11 -0500 commented question Plot boxes have jumpy xy ranges in log scale
2019-05-11 15:32:52 -0500 answered a question Plot points in loglog

It would seem plots in log scale use a sampling done in the non-log scale...

Since plots only 200 plot points by default, the sampling in non-log scale means that for a log-scale plot, most plot points are near the end, and they are very scarce in the first 3/4 of the plot.

A workaround, as suggested in @Iguananaut's answer, is to use more plot points.

Using 800 plot points already gives a much smoother curve here:

sage: tau = RDF.pi()*2
sage: out2 = lambda r, c: lambda f: ~(1 + (tau*f*r*c)^2)
sage: plot(out2(15, 2.7e-9), (10, 500e6), scale='loglog', plot_points=800)
Launched png viewer for Graphics object consisting of 1 graphics primitive

There was a problem I thought looked similar: the x-range and y-range for a plot are chosen slightly wider than the actual range of x-values and y-values that occur in the plot, and sometimes in log scale it looks like this "slightly wider" is chosen in non-log scale, giving a wrong aspect. See

2019-05-09 09:40:23 -0500 received badge  Nice Answer (source)
2019-05-09 02:44:36 -0500 commented question Chevie on Windows

@Erratis -- installing Linux is always a good idea. Note that you can try it out with Sage Debian Live which ships a Debian installation (bootable from a usb drive) with lots of free software including a very complete Sage installation.

2019-05-08 07:06:57 -0500 edited answer What does the 'a' and x^254 in code means?

When defining a finite field of non-prime order, it is useful to give a name to the generator.

Likewise, when defining a polynomial ring, it is useful to give a name to the polynomial variable.

In the example, GF(2^8, 'a') returns the finite field with $2^8$ elements, with a as the display name of its generator.

And R.<x> = K[] simultaneously defines R as a polynomial ring over the field K, with a polynomial variable that displays as 'x', and defines x as its generator, i.e., the polynomial variable.

So x^254 is the monic monomial of degree 254 in this polynomial ring.

For more, read the documentation or/and the source code for GF:

sage: GF?
sage: GF??

and for PolynomialRing:

sage: PolynomialRing?
sage: PolynomialRing??

Note that R.<x> = K[] is transformed by the Sage preparser into:

sage: preparse("R.<x> = K[]")
"R = K['x']; (x,) = R._first_ngens(1)"
2019-05-06 16:58:54 -0500 commented question How to print the numeric part of a symbolic expression with arbitrary precision?

It would be interesting to get some Sage commands that allow to create this matrix.

If not the original commands, then at least one way to get hold of this matrix.

To share the matrix but not the original way it was produced, you could run

sage: sage_input(Hf)

and if that works you could copy and paste what it produces, which will give a way for others to define the same matrix and try things out on it.

2019-05-06 16:55:37 -0500 answered a question How to print the numeric part of a symbolic expression with arbitrary precision?

It seems your matrix was defined with entries in Sage's symbolic ring.

Elements in the symbolic ring are symbolic expressions, which don't get automatically simplified.

One way to make the matrix more readable would be to think of it as a matrix whose entries are rational functions in E with coefficients in the real algebraic numbers.

Try this:

sage: Hf.change_ring(AA['E'].fraction_field())

If that does not work, then maybe rational functions in the (floating-point) "reals".

sage: Hf.change_ring(RR['E'].fraction_field())

Maybe the matrix was the following?

sage: Hf = matrix([

in which case rational functions over RR give:

sage: Hf.change_ring(RR['E'].fraction_field())
[                        6.16558207374834e-6/E (1000.00000000000*E - 0.000156202516960468)/E                        0.000294594522326585/E]
[                       0.000127914742222187/E                       -0.000277894374679915/E                        0.000309720283903677/E]
[                       0.000138909104469688/E                       -0.000288946039356407/E                        0.000294594522326585/E]

and rational functions over RDF give:

sage: Hf.change_ring(RDF['E'].fraction_field())
[              6.165582073748336e-06/E (1000.0*E - 0.00015620251696046755)/E               0.0002945945223265847/E]
[             0.00012791474222218708/E              -0.0002778943746799149/E              0.00030972028390367713/E]
[              0.0001389091044696876/E             -0.00028894603935640675/E              0.00029459452232658467/E]

Using a version of floating-point numbers with less bits (only 20 instead of the usual 53):

sage: Hf.change_ring(RealField(20)['E'].fraction_field())
[              6.1656e-6/E (1000.0*E - 0.00015620)/E              0.00029459/E]
[             0.00012791/E             -0.00027790/E              0.00030972/E]
[             0.00013891/E             -0.00028895/E              0.00029459/E]

Going down to 12 bits gives roughly three decimal digits as required in the question:

sage: Hf.change_ring(RealField(12)['E'].fraction_field())
[             6.17e-6/E (1000.*E - 0.000156)/E             0.000295/E]
[            0.000128/E            -0.000278/E             0.000310/E]
[            0.000139/E            -0.000289/E             0.000295/E]
2019-05-06 16:45:59 -0500 edited question How to print the numeric part of a symbolic expression with arbitrary precision?

How to print the numeric part of a symbolic expression with arbitrary precision?

I have a matrix Hf, it is a 3 by 3 matrix. It's a symbolic matrix, with just one variable 'E'. But on printing it, I get the as shown below. Basically what I want is to approximate the numerical part of the expression to arbitrary precision (let's say 3 decimal points), so that I can get a much cleaner expression.

sage: Hf.str()
2019-05-06 02:10:27 -0500 received badge  Good Answer (source)
2019-05-06 02:09:31 -0500 received badge  Nice Answer (source)
2019-05-06 01:52:24 -0500 received badge  Nice Answer (source)
2019-05-06 01:51:41 -0500 received badge  Necromancer (source)
2019-05-05 18:19:24 -0500 edited question How does one install Sage in windows?

Can someone help me with the process?

I downloaded a .ova and a .vmdk file from the mirror

Now what?

2019-05-05 18:19:08 -0500 edited answer How does one install Sage in windows?


A SageMath installer for 64-bit Windows is in the works. It is now is in alpha testing [...]. It should work with Windows 7 and up. [...]

Update (2019-05-06). The SageMath installer for Windows has been stable for some time now, and can be downloaded from the SageMath download page or from

2019-05-05 17:43:51 -0500 commented question Constructing graphs using permutation or symmetric groups

Please provide some code to let others reproduce the problem easily.

This dramatically increases the chances of an answer, the speed of getting an answer, and the accuracy with which the answers target the problem.