I'm wondering if anyone can let me know advantages to installing/setting up Sage on my computer for doing computational math (work in groups, finite fields, and combinatorics, along with some search algorithms).

Currently, I do a lot of my work with Magma; this is great and fast for implementing permutation groups and doing linear algebra over finite fields. I have also been doing some searching for combinatorial objects by setting up my matrices in Magma (geometric constructs), saving them to a file, then using Gurobi to search by setting up an integer programming problem. Gurobi uses a Python interface, so I have a Python script modify my Magma output and feed it into Gurobi and then initialize my problem.

As I understand Sage, it is based on Python, and can interface with a bunch of other things, including Magma and Gurobi. But the syntax seems really weird for calling Magma through the Sage interface. Is there some advantage to be gained? Mostly in terms of using Sage to interface between the two other programs? Am I better off just using Python?

Update, since this question was closed at some point: To make this less opinion based, I am (was) really interested in knowing if it is possible to attain the functionality of Sage in normal Python by loading packages, or if not, what does Sage provide that can not be easily reproduced in Python?

  • 1
    $\begingroup$ At least there's an advantage that you don't have to glue Magma output with Gurobi input using some intermediate file and Python script. As far as I understand with SAGE you can do this seamlessly within one Python script (since SAGE has bindings to both of packages). Of course, syntax might confuse you at the beginning but that's what regularly happens when you have more than 1-2 languages to use... $\endgroup$
    – Evgeny
    Commented Jul 28, 2015 at 17:15
  • $\begingroup$ This is kind of what I am hoping, but it is not clear to me from the documentation how to convert Magma objects over. It seems Sage can call a Magma function, which will return a Magma object, and then you can call further Magma functions related to that object. But I don't see a good way to convert over an object, for example, a matrix from Magma, to a format to put in to Gurobi without pulling out each entry manually. And I shudder to think at the overhead involved in having Sage as a middleman for that. Syntax I'm not too worried about, as long as I can convince myself there's a reason. $\endgroup$
    – xxxxxxxxx
    Commented Jul 30, 2015 at 21:15

2 Answers 2


This answer is something of a counterpoint to Samuel's enthusiastic answer. In short (and to directly answer your question), I would say that you are much better off just using Python (or other constituent programs) directly. Exactly which of these you might use depends very much on what you want to do.

While Sage relies on Python, it is not a simple Python library. It is a monolithic amalgam of many open source, mathematical tools. The tools are supposedly integrated via a single language but, in reality, the parts don't generally fit together very well. Here's a simple example that you can try on CoCalc or a SageCellServer:

n, t = var('n,t')
integrate(t^n, t)

Any reasonable computer algebra system should be able to deal with this problem. Sage simply returns a long string of errors. To be fair, there is a workaround: simply execute the command assume(n!=-1) before executing the integrate command. (Or, I guess, one could execute assume(n == -1) first; it's hard to say.) But this step is not necessary in maxima, the (very good) computer algebra system that Sage punts to for integrate and many other algebraic tasks. Thus, this example illustrates an important point: The various tools incorporated into Sage are tied together very poorly. As a result, we might say that Sage is much less than the sum of it's parts. Thus one wonders, why not just use the parts that one needs?

Again, you ask specifically about the use of Python and it sounds like you've already got a Python program to do exactly the kind of thing that it's good for. So, why mess with Sage? You're not going to use Sage's Python to execute your program - you'll probably use your system Python or some other (more manageable) Python distribution. Furthermore, there's a huge scientific ecosystem surrounding Python that's developed a large number of great mathematical libraries. Use what you need.

In fairness, there is quite a bit of mathematical code that's been developed specifically for Sage, particularly in number theory. If you happen to need exactly that stuff, then go for it. Also, the Sage group has been an important force in the open source movement for mathematical software. I'm fairly certain their server based notebook inspired the notebook we see in Jupyter now, for example. Having said that and admitting that I love to tinker with open source software, I really think that the advantage that open source software supposedly has in mathematics over closed source software is quite overblown.

Whatever you do, first and foremost - have fun with the math and the code!

  • 1
    $\begingroup$ Thank you, I think this is more my style. I have just made Magma output my matrices to a file as a list of the row vectors, and made a Python function to strip away unnecessary characters and read in the values. The real discouragement (for me) against using Sage is just how many things it tries to incorporate and glue together. Plus, even if I used Sage to build my Gurobi model, I would likely want to call Gurobi to optimize in a very stripped down environment without any extraneous drains on my system. $\endgroup$
    – xxxxxxxxx
    Commented Aug 1, 2015 at 7:08
  • $\begingroup$ Just as a comment here, Maxima for this example requires manual input without an assumption - (%i1) integrate(t^n,t); Is n equal to - 1? If you are always at the terminal ready to answer this, that's fine. $\endgroup$
    – kcrisman
    Commented Aug 14, 2015 at 12:55
  • $\begingroup$ @kcrisman Well, that's rather the point isn't it? Maxima is incorporated into Sage with a clunky pexpect interface (or, at least that's how it used to be) and Sage is ill prepared to deal with this type of interaction. That's not the only example, though. Some years ago, I was working Cayley graphs - i.e. graphs associated with groups. I figured that Sage would be perfect, as it contained Gap and NetworkX. The group computations and the graph computations went both individually fine, but the interaction between the two was very difficult. $\endgroup$ Commented Aug 14, 2015 at 13:10
  • $\begingroup$ Or one could say that Maxima has an antiquated interface... but I don't really care. I'm surprised you had trouble with the Cayley graphs, though; I've had good success combining Sage's graph and group interfaces. Unless you were having trouble with infinite groups or something? Feel free to post something on ask.sagemath! $\endgroup$
    – kcrisman
    Commented Aug 14, 2015 at 19:01
  • $\begingroup$ @kcrisman Thanks - it's been some time and I've moved on, though. It was an infinite group, incidentally - the Baumslag-Solitar group. $\endgroup$ Commented Aug 15, 2015 at 0:23

Here are some advantages of Sage (also known as SageMath) relevant to your question.

Sage supports all fields of mathematics, including those you mentioned (groups, finite fields, combinatorics, permutation groups, linear algebra over finite fields, linear programming, integer programming).

Sage relies on Python.

  • it is a widespread, well-designed programming language;
  • learning it is useful beyond Sage;
  • you get all the strength of Python;
  • you can easily install and use lots of Python libraries.

Sage also lets you use Cython to speed up Python code.

Sage packages and uses a lot of free open-source packages and libraries.

Sage also has interfaces to non-free software, so that you can interact with Magma for instance, if you already have lots of Magma code.

In the long run using Sage directly and becoming independent of Magma could be a greater gain for you.

One big advantage of Sage for mathematics is its being open-source, so that

  • everything it does can be tracked and checked;
  • any bug you encounter can be documented and fixed;
  • you can share any code you write with others;
  • people you share your code with are likely to actually use it because they can freely install Sage;
  • you can contribute code to Sage.

Standards for proofs in mathematics are that one should be able to understand every step of the proof. For the computational parts, this is only possible with open-source software.

Sage has a friendly community of users and developers. User support and development discussion happens openly on the ask-sage forum and on public mailing lists such as sage-support and sage-devel. You can ask there when you encounter a difficulty, or if you find that Sage is slower than other software for certain computations.

Sage does linear programming and integer programming, with support for several libraries, including CBC, CPLEX, CVXOPT, GLPK, GUROBI, PPL. See linear programming documentation, integer programming documentation, this page by Nathann Cohen, and you can find more example code by searching the web.

  • $\begingroup$ I don't have a problem with the fact that Magma is not open-source. It is unlikely to be able to understand every step of a computation (bugs can involve buffer overruns, typos, unexpected variable values, etc. in addition to problems with the algorithm itself), regardless of whether the code is open-source or not. You give some good reasons Sage could be useful, but the question I still have is: Am I better off just using Python? Can the interactions between Sage and Magma be loaded as a package into vanilla Python while ignoring Sage itself? $\endgroup$
    – xxxxxxxxx
    Commented Jul 29, 2015 at 16:20
  • $\begingroup$ Hi Sam - thanks for fixing the links in my post! I reviewed your edit and made sure that got in there. I was unclear on why you edited the code, though. Changing var('n,t') to SR.var('n t') seems a minor matter of preference. Is there some deeper reason for that change that I'm unaware of? (I'm particularly unsure why you used the SR object.) $\endgroup$ Commented May 1, 2018 at 15:23
  • $\begingroup$ I think var puts things in the global namespace while SR.var does not, but it's not something most users would care about. $\endgroup$ Commented May 1, 2018 at 18:55

Not the answer you're looking for? Browse other questions tagged .