Ask Your Question

eric_g's profile - activity

2019-01-08 08:07:36 -0600 commented question Sage Notebook IOPub data rate exceeded error?

Jupyter has been updated to version 5.7.1 in SageMath 8.5, which has been released on 20 December. Maybe it is worth to try with SageMath 8.5 if you have not already done it.

2019-01-08 08:00:09 -0600 edited answer Calling Sage from Java

Hello CupOfCoffee,

First some caveats: I don't use windows and I've never tried to connect sage to java.

I think you should be able to run programs in Java with ProcessBuilder ( I expect you'd need to pass some parameters to sage to get it working correctly. Here are some relevant links:

Then I would think you would want to pass and read commands to Sage through input/output to the process. There is a StackOverflow question addressing this here:

I imagine if you want to just do what you are suggesting, this wouldn't be too bad, but if you want to do something more complex, dealing with the input/output could be a pain.

Best, Pat

PS. Sorry for mangling the links. The post wouldn't go through without it.

2019-01-08 07:56:18 -0600 answered a question Can't display figures

It looks like libtk8.6 is not installed on your system. Can you install the Ubuntu package libtk8.6 and try again?

2019-01-07 07:27:30 -0600 received badge  Good Answer (source)
2019-01-01 10:46:06 -0600 answered a question Sage Python Executable On Linux

I would say usr/bin/sage-ipython (probably this is equivalent to running sage -ipython in some terminal).

2018-12-17 19:23:26 -0600 commented answer Can I alias a sage command?

As explained by @nbruin, alias is not a proper Python command, so you cannot use it while defining a Python function with def. In your case, some correct syntax similar to what you wrote would be

def LP():
    lp = 'MixedIntegerLinearProgram'
    load(lp + '.sage')
2018-12-15 07:55:49 -0600 answered a question Wrong Pointer

Thanks for pointing this! The link has been corrected.

2018-12-15 07:54:36 -0600 commented question Velocity of time

The 3d plots display correctly for me. Which browser are you using?

2018-12-10 09:49:54 -0600 received badge  Nice Answer (source)
2018-12-04 06:32:22 -0600 answered a question Sagemanifold - Connection components from a tensor (not a metric)

If you consider a non-symmetric "metric", you should probably define a function with a code similar to that of the method LeviCivitaConnection.coef() (starting at line 388 of src/sage/manifolds/differentiable/ in SageMath 8.4), by replacing each instance of self._metric by your object.

2018-12-03 10:38:32 -0600 received badge  Good Answer (source)
2018-11-29 02:10:29 -0600 received badge  Good Answer (source)
2018-11-25 20:22:11 -0600 received badge  Great Answer (source)
2018-11-25 20:22:11 -0600 received badge  Guru (source)
2018-11-25 11:43:30 -0600 commented answer .canonicalize_radical() produces incorrect result

I would say it is pretty safe to use simplify_chain_real since it is used systematically in calculus on real manifolds and performs not too badly. Among all these examples, the only one where it was not sufficient is the hyperbolic plane example. It turned out necessary to add (cf. cell [4])

maxima_calculus.eval("domain: real;")

to have some sqrt simplification be done correctly.

2018-11-24 16:59:09 -0600 received badge  Good Answer (source)
2018-11-24 13:28:18 -0600 received badge  Nice Answer (source)
2018-11-24 11:22:32 -0600 answered a question .canonicalize_radical() produces incorrect result

A solution is to use simplify_sqrt_real, which assumes that x lies in the real domain:

sage: y = sqrt(sin(x)^2 + 4*sin(x) + 4) - sqrt(sin(x)^2 - 4*sin(x) + 4)
sage: from sage.manifolds.utilities import simplify_sqrt_real
sage: simplify_sqrt_real(y)

More generally, you can use simplify_chain_real, which applies a chain of simplifications (including simplify_sqrt_real) valid in the real domain:

sage: from sage.manifolds.utilities import simplify_chain_real
sage: simplify_chain_real(y)
2018-11-22 08:31:44 -0600 commented question Tensor density in sagemanifolds?

Btw, if you would like to implement tensor densities, please visit

2018-11-22 07:20:46 -0600 commented question Assignation of components of a differential form (or multivector field) in sagemanifold

Yes, please provide some example. Note that when you assign components to a tensor with symmetries, it suffices to assign the minimal number of independent components, the other components are then set by (anti)symmetry.

2018-11-22 07:16:29 -0600 commented question Tensor density in sagemanifolds?

Yes I confirm that tensor densities are not implemented yet. In particular, the method PseudoRiemannianMetric.volume_form() returns a tensor field (a $n$-form), not a tensor density.

2018-11-21 05:48:36 -0600 received badge  Nice Answer (source)
2018-11-19 19:07:11 -0600 received badge  Nice Answer (source)
2018-11-19 06:54:18 -0600 answered a question integration - StopIntegration support?

You can do it by setting the attribute terminal to True in the event function of solve_ivp:

from scipy.integrate import solve_ivp
def rhs(t, (r, v)):     # r'' = -1/r^2 rewritten as the system
    return (v, -1/r^2)  # r' = v, v' = -1/r^2
r_min = 200.
def small_radius(t, (r, v)): 
    return 0 if r < r_min else 1
small_radius.terminal = True
t0, r0, v0 = 0, 1000., 0  # initial conditions
t_max = 50000
sol = solve_ivp(rhs, (t0, t_max), (r0, v0), events=small_radius, rtol=1e-8)


'A termination event occurred.'

You can plot the obtained solution by

line(zip(sol.t, sol.y[0]), axes_labels=[r'$t$', r'$r(t)$'])

and check that it indeed stops at r=r_min: image description

For more details, see the documentation of solve_ivp.

2018-11-18 14:06:10 -0600 answered a question SageManifolds different frames

The result of t[e_uv,:] cannot be the same as t[:] with a mere substitution x=(u+v)/2 and y=(u-v)/2 because it gives the components of t in a different vector frame: the frame is e_uv for t[e_uv,:], whereas it is the default frame on M for t[:], which is e_xy. In other words, the components returned by t[e_uv,:] are those of the following expansion:

sage: t.display(e_uv)
t = (1/2*u + 1/2) d/du*du + (1/2*u - 1/2) d/du*dv + (1/2*v + 1/2) d/dv*du + (1/2*v - 1/2) d/dv*dv

whereas the components returned by t[:] are those of an expansion on a different basis:

sage: t.display()
t = x d/dx*dx + d/dx*dy + y d/dy*dx

Same thing for the vector w:

sage: w[:]
[-y, x]

agrees with

sage: w.display()
w = -y d/dx + x d/dy


sage: w[e_uv,:]
[v, -u]

agrees with

sage: w.display(e_uv)
w = v d/du - u d/dv
2018-11-08 10:48:17 -0600 answered a question Defining functions over symbolic domains

A solution is to use the symbolic function unit_step:

sage: L = var('L', domain = 'positive')
sage: f(x) = unit_step(L+x)*unit_step(L-x)*x
sage: diff(f(x), x)
-x*dirac_delta(L - x)*unit_step(L + x) + x*dirac_delta(L + x)*unit_step(L - x) + unit_step(L + x)*unit_step(L - x)
sage: integrate(f(x), (x, 0, 3*L))
sage: integrate(f(x), (x, -3*L, 3*L))
2018-11-08 10:40:41 -0600 commented answer Killing vectors

Not to my knowledge. It would be a nice addition to Sage to express the Killing equation as a system of linear 1st order PDEs and attempt to solve those.

2018-11-07 01:14:12 -0600 answered a question Restriction of domain of vector field

A solution is to transform the implicit graph into a curve and to use the method along of vector fields:

E.<x,y> = EuclideanSpace()
f(x,y) = 2*x*y^3
g(x,y) = x^2*3*y^2
v = E.vector_field([f(x,y)/sqrt(f(x,y)^2+g(x,y)^2),
t = var('t')
S = E.curve([t^3, t^(-2)], t)
vS = v.along(S)
graph = S.plot(prange=(-1.7, 1.7))
graph += vS.plot(E.cartesian_coordinates(), ranges={t: (-1.71, 1.7)},
show(graph, ymax=5)

image description

See this page for help/tutorials on vector fields with Sage.

2018-11-06 06:25:30 -0600 commented question how to get the transformation matrix for a transformation over 4x4 matrices?

The error message

TypeError: first argument must be a matrix or a vector space, not Full MatrixSpace of 4 by 4 dense matrices over Rational Field

is triggered by

sage: from sage.modules.module import is_VectorSpace
sage: is_VectorSpace(M)

which is quite weird, because M is indeed a vector space and moreover Sage recognizes it as such:

sage: M in VectorSpaces(QQ)
sage: dim(M)

So I would say it is a bug in linear_transformation...

2018-11-01 08:35:51 -0600 received badge  Nice Answer (source)
2018-10-31 04:59:52 -0600 answered a question Symbolic Taylor expansion

You can do

sage: f = function('f')
sage: var('h dt a1 a2 a3')
(h, dt, a1, a2, a3)
sage: dx = a1*h + a2*h^2 + a3*h^3
sage: s = taylor(f(x+dx), h, 0, 3); s
1/6*(a1^3*diff(f(x), x, x, x) + 6*a1*a2*diff(f(x), x, x) + 6*a3*diff(f(x), x))*h^3 + 1/2*(a1^2*diff(f(x), x, x) + 2*a2*diff(f(x), x))*h^2 + a1*h*diff(f(x), x) + f(x)
sage: sdt = s.subs({h: dt^(1/2)}); sdt
1/6*(a1^3*diff(f(x), x, x, x) + 6*a1*a2*diff(f(x), x, x) + 6*a3*diff(f(x), x))*dt^(3/2) + a1*sqrt(dt)*diff(f(x), x) + 1/2*(a1^2*diff(f(x), x, x) + 2*a2*diff(f(x), x))*dt + f(x)
sage: view(sdt)

$\frac{1}{6} {\left(a_{1}^{3} \frac{\partial^{3}}{(\partial x)^{3}}f\left(x\right) + 6 a_{1} a_{2} \frac{\partial^{2}}{(\partial x)^{2}}f\left(x\right) + 6 a_{3} \frac{\partial}{\partial x}f\left(x\right)\right)} \mathit{dt}^{\frac{3}{2}} + a_{1} \sqrt{\mathit{dt}} \frac{\partial}{\partial x}f\left(x\right) + \frac{1}{2} {\left(a_{1}^{2} \frac{\partial^{2}}{(\partial x)^{2}}f\left(x\right) + 2 a_{2} \frac{\partial}{\partial x}f\left(x\right)\right)} \mathit{dt} + f\left(x\right)$

2018-10-25 02:52:22 -0600 answered a question How to realize the composition of rational function mappings with Sagemath?

In addition to @slelievre 's answer, a possibility is to consider f and g as functions on the Euclidean plane:

sage: E.<x,y> = EuclideanSpace()
sage: f = E.diff_map(E, [(x^2+15*x-2) / (x+15), (x^2*y+x*y+y) / (x^2+30*x-1)])
sage: f.display()
E^2 --> E^2
   (x, y) |--> ((x^2 + 15*x - 2)/(x + 15), (x^2*y + x*y + y)/(x^2 + 30*x - 1))
sage: g = E.diff_map(E, [(x^2+20*x-18) / (x+20), (x^2*y+x*y+32*y) / (x^2-31*x+14)])
sage: g.display()
E^2 --> E^2
   (x, y) |--> ((x^2 + 20*x - 18)/(x + 20), (x^2*y + x*y + 32*y)/(x^2 - 31*x + 14))

Then you may compose f and g via the * operator:

sage: h = f * g
sage: h.display()
E^2 --> E^2
   (x, y) |--> ((x^4 + 55*x^3 + 962*x^2 + 4930*x - 5876)/(x^3 + 55*x^2 + 982*x + 5640), (x^6 + 42*x^5 + 478*x^4 + 1419*x^3 + 13026*x^2 - 9172*x + 11648)*y/(x^6 + 39*x^5 - 593*x^4 - 36773*x^3 - 320694*x^2 + 486956*x - 152264))

Note that the result is automatically simplified. You may access to the symbolic expressions composing the result via the method expr():

sage: h.expr()
((x^4 + 55*x^3 + 962*x^2 + 4930*x - 5876)/(x^3 + 55*x^2 + 982*x + 5640),
 (x^6 + 42*x^5 + 478*x^4 + 1419*x^3 + 13026*x^2 - 9172*x + 11648)*y/(x^6 + 39*x^5 - 593*x^4 - 36773*x^3 - 320694*x^2 + 486956*x - 152264))
sage: h.expr()[0]
(x^4 + 55*x^3 + 962*x^2 + 4930*x - 5876)/(x^3 + 55*x^2 + 982*x + 5640)
sage: h.expr()[1]
(x^6 + 42*x^5 + 478*x^4 + 1419*x^3 + 13026*x^2 - 9172*x + 11648)*y/(x^6 + 39*x^5 - 593*x^4 - 36773*x^3 - 320694*x^2 + 486956*x - 152264)
2018-10-23 13:35:17 -0600 commented answer Cannot get threejs rendering to work on MacOS

In console mode yes, but not in the Jupyter notebook: one gets TypeError: a bytes-like object is required, not 'str' from function displayhook (line 572 of src/sage/repl/rich_output/ This is with python3-built Sage 8.4.rc1 on Ubuntu 18.04 (I have not tried Sage 8.5.beta0 yet).

2018-10-22 17:55:06 -0600 commented answer Error while compiling sage 8.3 on Ubuntu 16.04

In the log, /usr/bin/ld: cannot find -lgfortran seems to indicate that gfortran is not installed in your system. So install the Ubuntu package gfortran (see the list of all required packages here) and run make again (no need to restart from scratch).

2018-10-22 13:55:18 -0600 received badge  Nice Answer (source)
2018-10-22 11:55:19 -0600 answered a question Cannot get threejs rendering to work on MacOS

It seems that you are using a version of SageMath built with Python3. Alas, Python3 SageMath is not fully ready yet (see this question for a status update, and in particular the ticket #26212). In particular, the threejs viewer is not Python3 ready.

2018-10-22 11:44:54 -0600 answered a question Error while compiling sage 8.3 on Ubuntu 16.04

From your logs, it seems that you are trying to compile Sage on an external media, with some permissions which are not suitable, like spkg-install should not be marked executable in the build/pkgs directory Can you try again on your internal hard disk?

Basically, after having checked that you have all these Ubuntu prerequisites, you should type, from e.g. your home directory

git clone 
cd sage
MAKE="make -j8" make

possibly adapting the 8 in -j8 to the number of cores in your CPU, for an efficient parallel build. NB: from the above procedure, you will get Sage 8.4, which is the latest stable version of Sage.

2018-10-12 17:44:18 -0600 received badge  Nice Answer (source)
2018-10-11 09:11:24 -0600 answered a question How to convert an integer to fixed length binary string in Sage?

Sage's preparser (*) turns ^ into the exponentiation symbol (**), in order to follow standard math notations:

sage: preparse("a=(a << 8) ^ (L[i])")
'a=(a << Integer(8)) ** (L[i])'

To enforce the original (Python) xor behavior, use ^^ instead of ^:

for i in range(3):
    a=(a << 8) ^^ (L[i])

(*) see the answer to this question.

2018-10-11 07:31:24 -0600 received badge  Nice Answer (source)
2018-10-11 06:46:24 -0600 answered a question How to edit pixel values of color image in SageMath?

This is because Sage's preparser transforms the 0 in px1[0,0] into Sage's integers, while the object pxl is expecting Python's integers. The solution is to force the conversion to Python's integers via int():


More generally to check what Sage's preparser is doing with things you type, use the preparse command:

sage: preparse("pxl[0,0]")

(Integer() is for Sage's integers, while int() is for Python's integers).

2018-10-07 07:34:57 -0600 answered a question error with importing sage into a python file

A solution is run Sage's Python (instead of your system's Python):

sage -python

Equivalently, you can run Python from Sage shell:

sage -sh

Then all Sage's environment variables are automatically set and from sage.all import var does not give any error.

2018-10-02 04:52:40 -0600 commented question Error: 'Object has no attribute 'intersection''

I cannot reproduce your issue with SageMath 8.3 since the step K.<p,d>=ExtensionField(K1,P2) leads to

NameError: name 'ExtensionField' is not defined

Which version of SageMath are you using?

2018-10-01 15:05:22 -0600 received badge  Nice Answer (source)
2018-10-01 08:09:01 -0600 answered a question show(LatexExpr(" \frac{x}{\cos(x)}")) need 2 \\

Yes, that's normal: the backslash character in a string is interpreted as an escape character for special commands; for instance \f in \frac stands for an ASCII formfeed. To avoid this and restore the standard LaTeX interpretation one has to type a double backslash, as you did. An alternative is to prefix the string with r (for "raw" string):