Ask Your Question

jsrn's profile - activity

2020-06-29 02:07:25 -0500 received badge  Famous Question (source)
2020-06-29 02:07:25 -0500 received badge  Notable Question (source)
2019-07-31 12:20:34 -0500 received badge  Notable Question (source)
2018-05-07 03:17:23 -0500 received badge  Popular Question (source)
2017-10-11 06:51:43 -0500 received badge  Notable Question (source)
2017-04-17 18:50:22 -0500 received badge  Famous Question (source)
2017-04-11 09:29:35 -0500 received badge  Famous Question (source)
2015-07-17 04:38:39 -0500 received badge  Popular Question (source)
2014-06-28 20:14:32 -0500 marked best answer How do I find if/where a specific algorithm has been implemented


Since beginning using Sage, I have often been in the position that I need to find a specific algorithm for solving some problem. I might or might not already know that Sage has _some_ algorithm for doing it, but I want this one (for some reason). Is there an index or an easy way to search for this? The times I have tried using the search bar of, it has mostly been irrelevant posts, while a search on google like "" can give a bit as it also searches through the comments of the source code; but this is not very user-friendly, and it can take some time from finding some comment in a file, to know how to actually invoke specifically that code.

An example of this is the Berlekamp-Zassenhaus algorithm for factorising polynomials over integers.

2014-02-08 22:34:50 -0500 received badge  Notable Question (source)
2013-12-12 01:34:08 -0500 received badge  Notable Question (source)
2013-11-30 08:23:07 -0500 received badge  Nice Question (source)
2013-04-19 01:28:09 -0500 received badge  Popular Question (source)
2012-06-15 10:01:49 -0500 received badge  Popular Question (source)
2012-05-03 22:02:37 -0500 commented answer How do I share a library of Sage functions

On my local machine, I have a make-script calling sage -preparse on all the .sage files which have canged.On remote server's notebooks, I'm currently downloading the .sage files to a temporary directory, then calling preparse_file_named on them, and then import <module>. As the temp dir will be wiped every once in a while, I do each time the server starts. I don't really see how to do it any other way when I don't have control over the sage server.

2012-05-03 21:59:15 -0500 commented answer How do I share a library of Sage functions

Wrt. contributing, then most of my stuff is definitely not mature enough (or as you say, too idiosyncratic), while some of it might be contributable now or in the near future. But even if I contribute all of this, I still have this problem with the remaining :)

2012-05-03 00:24:47 -0500 asked a question How do I share a library of Sage functions

My question concerns what other people usually do in this -- seemingly common -- scenario, which I have needed to think on quite a lot to accomplish. I would like to know whether my way of handling this is the envisioned one. Sorry for the question being long ;)

I am using the notebook for my day-to-day math exploration, but in order to share code between worksheets (and for overview of the individual worksheets), I place often-used code in .sage-files on a folder which I have added to the SAGE_PATH. I have a make-script to precompile these into .py-files which are then loaded at the top of each worksheet using

from sagelib import *

which works by creating an file in the directory. The individual .sage files of course also need each other (in a non-cyclic way, naturally), so they also have lines like "from util import *" in the top.

The reason I'm using .sage files and not programming directly in .py should be obvious: I like the sage short-hands. Also, if I did that, I could not even use these short-hands inside the notebook, as I would have to refactor the code if I later wanted to move it to the library (which happens all the time).

The reason I'm then using Python's "import" and not Sage's "load" is for two reasons: One is the module system, so I can avoid stating every single library file in the top of each of my worksheets. The other is for efficiency: Let's say I have two files A.sage and B.sage, and B.sage uses A. If I load all first A and then B, the loading of B would cause A to be reloaded again. By this process, A can be reloaded an exponential number of times. I hope I am wrong, but it seems that this is not really handled in Sage's load. Lastly, I also like being able to use the module system once in a while, to not pollute the namespace overly.

Ok, so my Python recompiling-system works on my local machine, but then I would sometimes like to share code or run on some remote server. I can then put my .py or .sage files on a public server, like bitbucket, and in the top of the remote server's notebook worksheet, I wish to load this url. However, having used the Python module system as I mentioned above, I again cannot use Sage's "load" command, as B would have as it's first line "from A import *", but using Sage's load, module A does not exist. This can then be circumvented using code for dynamically loading modules and more manually (calling sage.misc.remote_file) retrieving the file and making sure it's imported properly (possibly precompiling it if it's a .sage file).

As I mentioned to begin with, this seems to actually work and be somewhat manageable. But the custom code I had to write for ... (more)

2012-03-23 01:21:51 -0500 received badge  Popular Question (source)
2011-06-21 03:45:56 -0500 received badge  Taxonomist
2010-10-14 16:11:12 -0500 received badge  Teacher (source)
2010-10-14 16:11:11 -0500 received badge  Student (source)
2010-10-14 16:11:10 -0500 received badge  Scholar (source)
2010-10-14 16:11:10 -0500 received badge  Supporter
2010-10-14 16:11:10 -0500 received badge  Editor (source)
2010-09-23 02:22:06 -0500 marked best answer Calling Unix shell command as an expression

I often use os.popen to solve exactly this problem:

sage: os.popen('pwd').read()

Big Caveat: Note that according to the Python docs, os.popen is deprecated and one is supposed to use subprocess.Popen as explained here. However, if you look at the examples using subprocess.Popen to do the same as what you can do using os.popen, you see that using subprocess.Popen makes the code often twice as long and complicated. This sucks. I think it would be good to add a new command to Sage that works much like'...').read(), and is implemented using subprocess. I don't think this currently exists.

2010-09-23 02:21:37 -0500 marked best answer What is the standard pickle jar for and how do I update it

The pickle jar contains old pickles of objects that we want to ensure work in the future. For example, if you made the changes at 9907 as the are, then possibly many pickles out in the wild would break. Instead of trying to update the pickle jar, the appropriate thing to do would be to add the following to sage/plot/

#For backward compatibility -- see #9907.
from sage.misc.decorators import options, suboptions, rename_keyword

That way, the old pickles will still work as they will still be able to find the decorators in sage.plot.misc.

2010-09-23 02:20:25 -0500 marked best answer Why do so many tests fail on a clean Sage installation

This may depend on your machine. I don't think this "answers" your question, but these things are really dependent on the situation. In general, a fairly modern (i.e. fast) machine and normal distribution should not have any doctest failures (other than optional ones) and only a couple timeouts. (I get lots of timeouts on some of my machines, but they are also >5 years old, and setting SAGE_TIMEOUT or SAGE_TIMEOUT_LONG usually fixes that.)

So I guess what I'm saying is that it's better to first make sure your patch is removed, that no extra spkgs are included, run the doctests again and then report them immediately either to sage-release or sage-devel. Even if you have a non-standard setup, our goal is to fix any bugs we can find. Please let us know!

If you are using a prerelease version, on the other hand, we usually expect a certain number of errors to arise as people test them on various platforms.

2010-09-20 21:14:44 -0500 answered a question When is 0^0 NaN in Sage?

Here is another case:

sage: GF(7)(0)^0
ArithmeticError                           Traceback (most recent call last)

/home/jsrn/local/sage/<ipython console> in <module>()

/home/jsrn/local/sage/sage-4.5.3/local/lib/python2.6/site-packages/sage/rings/finite_rings/ in sage.rings.finite_rings.integer_mod.IntegerMod_int.__pow__ (sage/rings/finite_rings/integer_mod.c:16942)()

ArithmeticError: 0^0 is undefined.

The same goes for polynomial rings over fields: GF(7)[x](0)^0.

I think that the primary thing is for Sage to be consistent; as you hinted at, I would think that the following invariants should always hold:

a^x == a**x == pow(a,x)


a^x mod n == pow(a,x,n) == power_mod(a,x,n)

For all rings in which they make sense. The 0^0 case is sometimes convenient to define to 1, sometimes 0 and sometimes NaN, so I would think that always giving an error is sensible. It is kind of annoying to often have to work around in general formulas, but on the other hand, in each of these cases, Sage will force you to consider the behaviour that makes mathematical sense for you; otherwise, you might miss rare cases of errors. As an alternative, some sort of global setting (or ring-specific setting) might be added, so one could set the value.

The behaviour of simplify is another discussion, I guess. There, it might prove _very_ annoying to not simplify 0^some_expr, but then again, I do like consistency.

Maybe this should be taken to sage_devel; maybe it has already been there? Sorry for the discussion-like quality of the answer.

2010-09-18 00:34:27 -0500 commented answer Why do so many tests fail on a clean Sage installation

Ok, now I downloaded the full source, compiled it myself and ran all the test, and this looks much better: only two errors (I'll post on sage.devel). I think my old version was the pre-compiled 4.5 for Ubuntu 64 bit...

2010-09-17 09:51:27 -0500 commented answer What is the standard pickle jar for and how do I update it

Ok, that would be great. So in an actual patch, I would also post a whole new piclee_jar.tar.bz2 file with the patch? Or I would leave a note with the patch for reviewers and the release manager that this picle_jar.tar.bz2 should be remade? Probably the latter...

2010-09-17 03:14:37 -0500 answered a question how to remove duplicate elements in a list

If you're lazy and don't care about ordering in L:

sage: list(set(L))

Again, all of this is pure Python, so you might benefit from reading a tutorial on writing Python code.

2010-09-17 03:11:19 -0500 commented answer What is the standard pickle jar for and how do I update it

Thanks for the reference Niles, but yes, I have looked at that example, but I didn't really understand it; first off, I don't know where the standard pickle jar is :)

2010-09-17 01:29:42 -0500 commented question sublist of a given list of objects

Please be more specific and clear about what you ask for, if you expect people to provide a correct, clear and understandable answer.

2010-09-16 23:32:42 -0500 answered a question sub list like subsets

I'm not completely sure I understand your question, but what you're asking for sounds simply (at least almost simply) like cartesian product, which is implemented in Python in itertools.product. In the basic case, you could then just do:

sage: L = [ [1,2,3,4],[3,5,6,7],[7,8,9,10] ]  # note that this is a list, while you did a tuple
sage: from itertools import product
sage: list(product(L, L))
[([1, 2, 3, 4], [1, 2, 3, 4]), ([1, 2, 3, 4], [3, 5, 6, 7]), ...

(note the "list" around product, as product returns a generator)

The above can easily be generalised to having 3,4,...,n elements in each sublist (i.e. L^n) by modifying the last line to

list(product(repeat(L, n)))

However, this would also give those sublists where the same element might be present more than once in each list. If you don't want this, you can write a simple list-comprehension to handle the 2-case:

sage: [ [a,b] for a in L for b in L if a != b ]
[[[1, 2, 3, 4], [3, 5, 6, 7]], [[1, 2, 3, 4], [7, 8, 9, 10]], [[3, 5, 6, 7], [1, 2, 3, 4]], [[3, 5, 6, 7], [7, 8, 9, 10]], [[7, 8, 9, 10], [1, 2, 3, 4]], [[7, 8, 9, 10], [3, 5, 6, 7]]]

Manually, the above can also be extended to 3, 4, ... but not easily to any number n given as a parameter. Also, this has the drawback of creating the entire list in memory instead of being a light generator as the product returns. You would have to write a longer piece of code to get a generator with the above behaviour.

Note that this is all Python-stuff and not specific to Sage at all.

Cheers, Johan

2010-09-16 22:32:18 -0500 asked a question What is the standard pickle jar for and how do I update it

I started Trac #9907, which basically consists of a relocation of some general decorators into sage.misc.decorators. However, after applying the patch, the doctest for sage.structure.sage_object.unpickle_all fails. It seems to be because there is a standard pickle jar which still contains the information that the decorators are located in sage.plot.misc.

Is it correct that there is such a pickle jar? In that case, what for, and how do I get about updating it and fixing my patch?

Cheers, Johan

2010-09-16 20:37:48 -0500 commented answer Why do so many tests fail on a clean Sage installation

I have a brand new thinkpad t410 with quadcore i7 processor, running an up-to-date Ubuntu, so that should be ok. I'll try to make sure I have a "stable" Sage and redo the tests.

2010-09-16 04:46:39 -0500 asked a question Why do so many tests fail on a clean Sage installation

With a completely clean Sage installation, performing ./sage -testall as suggested (required) in the "Walking Through the Development Process", many doctests fail with various errors (including a few segfaults). Is this normal? If so, I'm sure that it is not intended, so is there someone with a grand overview on whether these errors are all in the process of being handled?

These errors mess up the output and makes it very difficult to sort out which are errors caused by the patch you're working on, so you might miss them. It's like dirty dishes in a shared kitchen: if there are _any_, these will quickly multiply ;-) Is there a way to suppress those errors known (assuming the errors are not due to something I did wrong), so I can get a clean overview of new errors?

Cheers, Johan

2010-08-24 20:42:47 -0500 marked best answer How do I find if/where a specific algorithm has been implemented

Is there an index

There is no such index of algorithms implemented in Sage.

or an easy way to search for this? An example of this is the Berlekamp-Zassenhaus algorithm for factorising polynomials over integers.

You could use the search functions search_def, search_doc, or search_src to search through the Sage source tree. When someone implements an algorithm or interfaces to an algorithm from a Sage package, the name of the algorithm should be mentioned in the docstring of the relevant module, class, method, or function. For the Berlekamp-Zassenhaus algorithm, I got this

sage: search_src("Berlekamp-Zassenhau")
rings/polynomial/polynomial_element.pyx:2656:        ## NTL uses the Berlekamp-Zassenhaus method with van Hoeij's improvements.

That should tell you where to start looking, specifically in the file sage/rings/polynomial/polynomial_element.pyx around line 2656.

2010-08-24 20:31:48 -0500 asked a question Calling Unix shell command as an expression

Can I call a Unix shell command from within Sage as an expression? For example, I might want to do something like


Unfortunately, the !-operator does _very_ weird things when used as an expression (bug?), so the above doesn't work. The os.system() does not work either (it returns the return code instead of the output). Of course, Python has options, like handling pipes or subprocesses, but for many applications this is a bit tedious.

2010-08-19 03:21:58 -0500 answered a question What are the 10 most often used functions in Sage?

help and ?? ;-)