Ask Your Question

eric_g's profile - activity

2019-04-15 06:23:45 -0500 commented question How to propose a major change of SageMath?

It would probably be nice to have MathGL as an (optional) graphical interface in Sage.

2019-04-15 04:09:43 -0500 commented question How to propose a major change of SageMath?

Why couldn't you access to the sage-devel Google group? This would be the good place to discuss this.

2019-04-14 11:49:00 -0500 answered a question return lists that do not share all of the same elements

Use sets, not lists:

sage: set([1, 2, 3, 4])
{1, 2, 3, 4}
sage: set([2, 3, 4, 1])
{1, 2, 3, 4}
sage: set([1, 2, 3, 4]) == set([2, 3, 4, 1])
2019-04-13 16:54:52 -0500 received badge  Good Answer (source)
2019-04-13 15:28:18 -0500 received badge  Nice Answer (source)
2019-04-13 11:47:08 -0500 answered a question Arrow length goes wild near poles in vector field plot

An alternative is to define f on the Euclidean plane minus a small disk around the origin:

E.<x,y> = EuclideanSpace()
U = E.open_subset('U', coord_def={E.cartesian_coordinates(): x^2+y^2>0.2})
f = U.scalar_field(1/sqrt(x^2+y^2))
grad = f.gradient()
grad.plot(max_range=1, scale=0.1)

gradient plot

2019-04-12 13:45:10 -0500 answered a question multiprocessing.Pool does not work

Unfortunately, this is a known issue: multiprocessing is broken on Mac OS...

2019-04-12 09:14:19 -0500 commented question Changing MatrixSpace Basis?

The method subspace_with_basis does not exist for matrix spaces in SageMath 8.7. Which version of SageMath are you using?

2019-04-11 21:12:18 -0500 received badge  Nice Answer (source)
2019-04-08 07:36:19 -0500 received badge  Nice Answer (source)
2019-04-08 06:24:42 -0500 answered a question Is SageManifolds adequate to work with homogeneous Riemannian manifolds?

In the current setting, all calculations on manifolds end as calculations on charts. Nothing specific to homogeneous manifolds has been implemented yet. Would you be interested in developing such a part? Note that the implementation of Lie groups has started, see here.

2019-04-07 08:37:40 -0500 answered a question Point -> Vector to a point

Like this:

E = EuclideanSpace(3)
A = E.point((4,3,2), name='A')
v = E.tangent_space(A)((1,2,1), name='v')  # a vector at A
A.plot(size=20, color='red', opacity=0.5) + v.plot(color='green')
  • Remark 1: the command A = E.point((4,3,2), name='A') can be shorten to A = E((4,3,2), name='A') (this notation reflects SageMath's parent/element framework, points being elements of the Euclidean space E)
  • Remark 2: the notation E.tangent_space(A)((1,2,1)) arises from differential geometry; it simply means the vector of components (1,2,1) whose origin is A.

For more details and examples see

2019-04-07 08:33:48 -0500 answered a question Line,Plane, Points 3D, Intersection,

You can try plotting points and vectors of Euclidean spaces: those have automatic labeling in 2D and 3D plots. For instance:

E.<x,y,z> = EuclideanSpace()
A = E.point((4,3,2), name='A')
v = E.tangent_space(A)((1,2,1), name='v')  # a vector at A
A.plot(size=20, color='red', opacity=0.5) + v.plot(color='green')
  • Remark 1: the command A = E.point((4,3,2), name='A') can be shorten to A = E((4,3,2), name='A') (this notation reflects SageMath's parent/element framework, points being elements of the Euclidean space E)
  • Remark 2: the notation E.tangent_space(A)((1,2,1)) arises from differential geometry; it simply means the vector at A whose components are (1,2,1)

For more details and examples see

2019-04-06 06:27:20 -0500 received badge  Good Answer (source)
2019-04-05 15:17:45 -0500 received badge  Nice Answer (source)
2019-04-01 16:55:23 -0500 commented answer grad() at glacial speed

I've edited my answer, indicating how to replace simplify_chain_real by the much more speedy simplify (but less efficient in simplifying).

2019-04-01 10:08:39 -0500 answered a question grad() at glacial speed

Using SageMath 8.7, I confirm that the command grad(Phi) runs for ever... Actually, this is not the computation of the gradient by itself that takes time but its simplification. The quantity phi_ges from which the scalar field Phi is formed is a quite lengthy symbolic expression. Sage evaluates

diff(phi_ges, z)

quite fast but then the command

diff(phi_ges, z).simplify_full()

runs for ages... It turns out that grad(Phi) is using a simplification sequence pretty similar to simplify_full() (the details are here) to simplify its output. Hence the observed behavior. Maybe this is an issue of the simplification of long symbolic expressions involving Heaviside functions in Sage...

EDIT: a possible workaround is to replace simplify_chain_real by the mere simplify function as the simplifying function associated to the default chart (x,z) of the Euclidean space S; to do this, it suffices to insert the line

S.default_chart()._calc_method._simplify_dict['SR'] = simplify

just after the definition S.<x,z> = EuclideanSpace(2).


  • S.default_chart() is the chart of Cartesian coordinates $(x,z)$ on the Euclidean plane $S$
  • _calc_method is the calculus method, i.e. the tool managing the symbolic backend that is used when perfoming calculations with the chart $(x,z)$: either the symbolic ring SR (SageMath's default) or SymPy
  • _simplify_dict is the dictionary of simplifying functions associated to each symbolic backend: by default, _simplify_dict['SR'] = simplify_chain_real and _simplify_dict['sympy'] = simplify_chain_real_sympy
  • the function simplify in the right-hand side of the suggested command is SageMath's minimal simplifying function: it is fast, but it does not do much. You can replace it by any function that maps a symbolic expression to another symbolic expression.

With the above command,

%time print((-grad(Phi)).display())


CPU times: user 498 ms, sys: 18.6 ms, total: 517 ms
Wall time: 396 ms

EDIT (3 April 2019): motivated by your question, I've opened the Trac ticket #27601 to allow for an easy modification of the simplifying function.

2019-03-26 08:35:35 -0500 commented question in command mode plot3d not displaying - only says "Graphics3d Object"


show(G, viewer='threejs')


2019-03-17 11:22:32 -0500 answered a question Upgrade sagemath to the latest stable version

For updating precompiled binaries, there is no other (simple) way than downloading a new archive. The git pull method would work only if you had built Sage from sources. This is feasible but you have to install some required Debian packages first (see this page). Here is the list of required Ubuntu packages; probably the list of Debian packages is close.

2019-03-13 11:32:37 -0500 received badge  Good Answer (source)
2019-03-13 07:01:32 -0500 received badge  Nice Answer (source)
2019-03-13 05:50:59 -0500 answered a question Coefficients of partial derivatives

coefficient() does work with partial derivative as argument. Actually the issue in your case is that you have used the same symbol chi to denote the scalar field and the symbolic function representing the scalar field in spherical coordinates. Indeed, when you write

chi = M.scalar_field(function('chi')(r,theta,phi), name='chi', latex_name=r'\chi')

function('chi') injects the name chi in the global namespace to denote the symbolic function $chi(r,\theta,\phi)$, but this name is then overwritten by chi = ... to denote instead the scalar field. Hence, when you enter diff(chi(r, theta, phi), r) you get an error. The solution is of course to use different names for the symbolic function and the scalar field, e.g.

chi_s = M.scalar_field(function('chi')(r,theta,phi), name='chi', latex_name=r'\chi')

Here is then the full code for collecting the coefficients of the partial derivatives in the Laplacian (I have used the Euclidean space as a manifold to shorten the code):

sage: M.<r,theta,phi> = EuclideanSpace(coordinates='spherical')
sage: chi_s = M.scalar_field(function('chi')(r,theta,phi), name='chi', latex_name=r'\chi')
sage: delChi = chi_s.laplacian()
sage: ddChi = delChi.expr()
sage: ddChi
((r^2*diff(chi(r, theta, phi), r, r) + 2*r*diff(chi(r, theta, phi), r) + diff(chi(r, theta, phi), theta, theta))*sin(theta)^2 + cos(theta)*sin(theta)*diff(chi(r, theta, phi), theta) + diff(chi(r, theta, phi), phi, phi))/(r^2*sin(theta)^2)
sage: for coord in [r, theta, phi]:
....:     for order in [1, 2]:
....:         der = diff(chi(r,theta,phi), coord, order)
....:         print("{}:  {}".format(der, ddChi.coefficient(der, 1)))
diff(chi(r, theta, phi), r):  2/r
diff(chi(r, theta, phi), r, r):  1
diff(chi(r, theta, phi), theta):  cos(theta)/(r^2*sin(theta))
diff(chi(r, theta, phi), theta, theta):  r^(-2)
diff(chi(r, theta, phi), phi):  0
diff(chi(r, theta, phi), phi, phi):  1/(r^2*sin(theta)^2)
2019-03-12 19:20:33 -0500 received badge  Nice Answer (source)
2019-03-12 18:08:00 -0500 edited answer Solved: Why does integrate(psi(y)*f(y),y) return an error but integrate(psi(t,y)*f(t,y),y) works?

This is definitely a bug, which seems to originate from the name psi for one of the two functions: it is confused with Maxima's special function psi, see

If you replace psi by another name, everything works:

sage: f = function('f')
sage: g = function('g')
sage: y = var('y')
sage: integrate(g(x-y)*f(y), y)
integrate(f(y)*g(x - y), y)

UPDATE (13 March 2019): the bug is now tracked via the ticket #27475.

2019-03-05 06:50:16 -0500 commented answer problems with sagecell

The -pip option has been in Sage for a while (at least since Sage 6.5?). Are you using the Ubuntu package sagemath? If yes, it may be that -pipis not available through it. I would recommend then to uninstall this package and to install the binary for Ubuntu 18.04 available at In addition, this will provide you with the latest stable release of Sage (version 8.6), while the Ubuntu package contains only Sage 8.1.

2019-03-04 06:50:48 -0500 commented question problems with sagecell

Maybe you can try

sage -pip install fabric

instead of pip install fabric

2019-02-16 11:05:31 -0500 received badge  Nice Answer (source)
2019-02-16 07:55:15 -0500 answered a question Wrong factorization of expressions containing exponentials ?

Thanks for the report. Looks like a terrible bug! It was introduced in SageMath 8.4: it is already there, while in SageMath 8.3, we get a mathematically correct result:

sage: factor(exp(-x)+2*exp(x))
(2*e^(2*x) + 1)*e^(-x)

I've opened the Trac ticket #27304 for it.

2019-02-14 15:18:51 -0500 answered a question "=" vs "==" - when to use what?

I agree that f.subs(a == 3) is bad syntax and should probably be deprecated. For substitutions, it is much clearer to use a Python dictionary:

sage: a, b = var('a b')
sage: f = a*x + b
sage: f.subs({a: 3})
b + 3*x
sage: f.subs({a: 3, b: -2})
3*x - 2
2019-02-13 02:41:56 -0500 answered a question Weired frontend behaviour? Weired User?

Replace the == by = in the lines

e_0 == 8.854 * 10^-12
e_s == e_0 * 3.5
e_m == e_0 * 81

and your code will work. Indeed == is the comparison operator, not the assignment one, which is .

Besides, you may redefine p as a callable symbolic variable:

p(e, Q, r) = 1/(4*pi*e)*Q/r

so that the definitions of p_1,...,p_4 become simpler:

p_1 = p(e_m, 1, x)
p_2 = p(e_m, -1, d_e-x)
p_3 = p(e_Ersatz, -1, sqrt((2*d_k)^2+x^2))
p_4 = p(e_Ersatz, 1, sqrt((2*d_k)^2+(d_e -x)^2))
2019-02-09 07:11:20 -0500 commented answer No output when documentation say it should be

I fully agree: the doctest should be written with a print() in the loop, or even better (more pythonic), using all() instead of a for loop.

2019-02-07 17:18:48 -0500 received badge  Nice Answer (source)
2019-02-07 06:31:19 -0500 answered a question jupyter tutorial

Regarding your question no. 2, there is

2019-02-05 08:27:47 -0500 commented question What does the [ File " " , Line 1, in < module > ] error mean?

If your computer is under Windows, why don't you try the Windows installer for SageMath 8.6 instead of the VM version ?

2019-02-03 14:59:20 -0500 answered a question jsmol problem in Sagemath/Jupyter notebooks

Regarding vector fields, you can use threejs via the method plot of vector fields on the Euclidean space:

E.<x,y,z> = EuclideanSpace()
v = E.vector_field((x*cos(z), -y*cos(z), sin(z)))
v.plot(max_range=pi/2, viewer='threejs')

See here for more details.

2019-01-29 04:07:56 -0500 commented answer can i display only two decimals of precision

Note that a predefined abbreviation of numerical_approx is n:

sage: n(pi, digits=2)
2019-01-20 03:32:00 -0500 received badge  Nice Answer (source)
2019-01-19 11:03:16 -0500 edited question periodicity of a word

I have the following code to create words.

sage: M.<x,y,z> = FreeMonoid(3)
sage: Word(x^3*y*x*z^2*x)
word: xxxyxzzx

I want find the periodicity of a given word.

For example ababab has periodicity 3 and abcdabcde has periodicity 1.

Kindly help me with this.

Thank you.