Ask Your Question

Eugene's profile - activity

2020-07-14 01:55:19 -0500 received badge  Famous Question (source)
2020-07-14 01:55:19 -0500 received badge  Popular Question (source)
2020-07-14 01:55:19 -0500 received badge  Notable Question (source)
2020-04-30 01:54:13 -0500 received badge  Famous Question (source)
2020-01-29 00:50:58 -0500 received badge  Famous Question (source)
2019-11-25 11:04:20 -0500 received badge  Famous Question (source)
2019-11-01 04:27:59 -0500 received badge  Popular Question (source)
2019-09-05 07:40:28 -0500 received badge  Notable Question (source)
2019-09-05 07:40:28 -0500 received badge  Famous Question (source)
2019-09-05 07:40:28 -0500 received badge  Popular Question (source)
2019-06-15 23:06:56 -0500 received badge  Famous Question (source)
2019-05-22 04:15:32 -0500 marked best answer init.sage doesn't work for the notebook server?


I have the same problem as in Is there a way to prefix all sage code in order to include a custom module?, but the init.sage appears not to work with notebook's worksheet. Here is what I have:

$ cat ~/.sage/init.sage 
import numpy as np

then inside Sage I have:

$ ./sage 
sage: np
<module 'numpy' from ....>

But then I run notebook() and create a new worksheet, inside that worksheet session:

NameError: name 'np' is not defined

So I assume that the script was not executed for the worksheet.

Is there a way to create sage.init but for the worksheets?

2019-05-22 04:08:04 -0500 received badge  Popular Question (source)
2018-12-23 12:21:10 -0500 received badge  Famous Question (source)
2018-10-07 17:41:21 -0500 received badge  Famous Question (source)
2018-09-20 05:22:49 -0500 received badge  Famous Question (source)
2018-08-28 21:58:27 -0500 received badge  Notable Question (source)
2018-06-05 10:57:29 -0500 received badge  Famous Question (source)
2018-03-25 06:44:53 -0500 received badge  Famous Question (source)
2018-03-25 06:44:53 -0500 received badge  Notable Question (source)
2018-02-12 07:46:14 -0500 received badge  Popular Question (source)
2018-01-02 06:50:51 -0500 received badge  Famous Question (source)
2017-10-15 13:58:41 -0500 marked best answer latex.table() function?

Can Sage construct latex code of table? Something like html.table, but with tex-output instead of html-output.

2017-10-15 13:58:41 -0500 received badge  Good Answer (source)
2017-10-15 13:58:41 -0500 received badge  Enlightened (source)
2017-08-05 03:48:08 -0500 received badge  Popular Question (source)
2017-08-01 16:21:24 -0500 received badge  Notable Question (source)
2017-08-01 16:21:24 -0500 received badge  Popular Question (source)
2017-07-14 04:09:12 -0500 received badge  Notable Question (source)
2017-05-29 14:37:23 -0500 commented question VirtualBox - Error

Something is wrong with the configuration of the VirtualBox itself? Have you moved any directories manually? Try removing C:\Users\vmuser\VirtualBox VMs\Sage-7.6\ and import again.

2017-04-22 02:43:01 -0500 commented question UnicodeDecodeError in Notebook Server if Worksheet is set to 'python' instead of 'sage'

Learned about Jupyter for real last evening, bot that problem and some other problems are not present in Jupyter (and Jupiter seems prettier, not sure about the Jupyter's markdown vs Sage's rich text boxes and Jupyter's versioning vs Sage's versioning thought). I think because of Jupyter this question is outdated, and the related github issues may also be closed in order not to draw developers attention and save time. Still thanks :)

2017-04-21 14:24:54 -0500 asked a question UnicodeDecodeError in matplotlib if 'python' set instead of 'sage' in Notebook


I am trying to make publication ready images for LaTeX in Sage with matplotlib (due to the complex nature of the plot) in Notebook.

I have a lot of numerical calculations (not symbolic) so I switch to 'python-mode' using drop-down list on top of the Worksheet (so it looks like 'File...', 'Action...', 'Data...', 'python') so that my constants would read as native python's data types not Sage's symbolic objects.

Now having the switch on top of the page in 'python' mode the following code:

from matplotlib import rc

rc('text', usetex=True)
rc('text.latex', unicode=True)
rc('text.latex', preamble='\usepackage[utf8]{inputenc}')
rc('text.latex', preamble='\usepackage[russian]{babel}')

font = {'family': 'serif',
        'serif': ['Computer Modern Unicode']}

rc('font', **font)

import matplotlib.pyplot as plt
plt.plot([1], [1])

yields ether UnicodeDecodeError or the image with the text corrupted: example of the corrupted output

But if I switch to 'sage' on top of the page, it works as expected:

example of the valid output

I have no idea how that switch on top of the page affects matplotlib's output, but would really like to be able to use Cyrillic (utf8) letters and 'python' mode at the same time.

P.S. That magical switch on top of the page is really painful since I also can not save Worksheets if they contain utf-8 characters: as in this still unresolved issue :-( :-(

UPDATE: Tested the MWE above in Jupiter - seems to work properly! And due to inevitable migration to Jupiter, the problem seems not to be obsolete.

2017-04-12 08:55:38 -0500 received badge  Notable Question (source)
2017-03-15 14:22:53 -0500 asked a question Sage Math future (notebook vs cloud vs jupiter)?


For some years I have used Sage but only on my local PC and via notebook. Now I am noticing that Sage Cloud becomes more popular, it has jupiter and the interface differs a lot.

So, I have a some general questions about the future of Sage Notebook, Sage Cloud and jupiter:

  1. Is using Sage Notebook is the 'recommended' way of using sage offline (or I should google about jupiter)?
  2. Will something like Sage Cloud Docker replace the use of Sage's binary and Sage's Notebook in the future?
2017-02-15 06:37:51 -0500 received badge  Popular Question (source)
2017-02-04 15:23:06 -0500 received badge  Self-Learner (source)
2017-02-04 14:14:26 -0500 answered a question How to efficiently calculate a sum of arrays with numpy and @parallel decorator?

Workaround: use sharedmemory and multiprocessing packages (sharedmemory is available via pip). I tried the following code within a separate .py file:

import sharedmem
import multiprocessing as mp
import numpy as np

N = 1024 * 2
K = 256

_lock = mp.Lock()

def f(_shared_buffer):
    partial_result = np.ones((N, N), dtype=np.complex128)
    _shared_buffer += partial_result

def test():
    shared_buffer = sharedmem.empty((N, N), dtype=np.complex128)
    pool = mp.Pool(4), [shared_buffer for _ in range(K)])
    return shared_buffer

Advantages of this workaround:

  • Executes as fast as the original code with no parallelism (about 5 seconds on my PC)


  • Requires usage of the additional package sharedmem (although this package is available via pip)
  • I was not able to pass _lock object to f() from within Sage's Notebook and have to put the code in a separate file, than import and only then execute
  • The buffer from np.ndarray becomes sharedmem.sharedmem.anonymousmemmap (not sure if that is a disadvantage, I guess one can convert it back to numpy's type pretty fast)
2017-02-04 14:03:46 -0500 commented question How to efficiently calculate a sum of arrays with numpy and @parallel decorator?

Thanks for the note about shared memory! I think that is the case here, I found a workaround with sharedmem and multiprocessing modules, gonna post it as an answer.

2017-02-04 05:33:32 -0500 asked a question Multiple tables in Notebook's output?


Trying to print more than one table in the output of Sage's Notebook cell, with fancy-printing:


but only the last one is shown (checked in Firefox and Chrome in Linux). Currently found workaround is:


but it gives a deprecation warning, or:

html(table([[0]])._html_() + table([[0]])._html_())

which seems to work fine.

2017-02-03 13:36:48 -0500 received badge  Notable Question (source)
2017-02-02 03:27:41 -0500 asked a question How to efficiently calculate a sum of arrays with numpy and @parallel decorator?


I have an algorithm to process a huge array by chunks. Each processing operation results in a matrix of size N*N, I need to calculate a sum of these matrices. For simplicity assume processing function does almost nothing and requires no input - just returns zeros. In that case working example looks like this:

import datetime
import numpy as np
import time

N = 1024 * 2
K = 256

def f():
    return np.ones((N, N),  dtype=np.complex128)

buffer = np.zeros((N, N),  dtype=np.complex128)

start_time =

for i in range(K):
    buffer += f()

print 'Elapsed time:', ( - start_time)

Execution takes about 5 seconds on my PC. Now, as function f becomes more complex, I would like to run in parallel, so I modify code as follows:

import datetime
import numpy as np

N = 1024 * 2
K = 256

def f(_):
    return np.ones((N, N),  dtype=np.complex128)

start_time =

for o in f(range(K)):
    buffer += o[1]

print 'Elapsed time:', ( - start_time)

And now it takes about 26 seconds to calculate! What am I doing wrong? Or what causes such a huge overhead? (it looks silly for if the cost of collecting the result of f() across parallel processes is more than calculating one iteration of f() itself, I better run f() without parallelism at all)

2017-01-31 11:14:18 -0500 received badge  Popular Question (source)
2017-01-31 11:14:18 -0500 received badge  Notable Question (source)
2016-11-29 12:49:03 -0500 received badge  Notable Question (source)
2016-11-27 10:24:15 -0500 asked a question How to put legend outside of the graph at its bottom?


I would like to have legend located outside of the of the graph at the bottom. In the matplotlib legend guide there is an example how to locate it on the top:

# Place a legend above this subplot, expanding itself to
# fully use the given bounding box.
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
           ncol=2, mode="expand", borderaxespad=0.)

Base on that example I write in Sage Note the following code:

p = line([(0, 0), (1, 1)], legend_label='test', axes_labels=('X LABEL', 'Y LABEL'))
p.set_legend_options(bbox_to_anchor=(0., -.14, 1., .102), loc=3, mode="expand", borderaxespad=0.), axes=False)

bbox_to_anchor parameter is (left, bottom, width, height) , the issue is that I have to manually set bottom (-.14 in the example) to compensate for the X label (it may be present, may be missing, may have a bigger font etc.)

Is there a way to automatically put legend on the bottom just below the x-axes label?