
SageMath – Open-Source Mathematical Software System - gnramires
https://www.sagemath.org/
======
bollu
I'm a huge fan of SAGE! I keep using it to discover new math. I generally play
with the associate Sage module of any algebraic structure to get a real feel
for what is going on. Some examples of:

1\. Grobner basis: [http://bollu.github.io/#computing-equivalent-gate-sets-
using...](http://bollu.github.io/#computing-equivalent-gate-sets-using-
grobner-bases)

2\. Commutators of subgroups: [http://bollu.github.io/#5age-code-to-play-
around-with-commut...](http://bollu.github.io/#5age-code-to-play-around-with-
commutators-of-)

3\. Graph/group theory:
[https://github.com/bollu/bollu.github.io/blob/2f88c28efc54fb...](https://github.com/bollu/bollu.github.io/blob/2f88c28efc54fb16463e2d9e0ac63f28620e223b/content/blog/math/intro_to_sage.md)

~~~
abdullahkhalids
Completely off topic question. Can Grobner basis techniques be used to solve a
set of quadratic inequalities?

~~~
thechao
Not ... really? I usually compare the Gröbner basis to generalized matrix
decomposition in the sense of solving systems of linear equations. If you’re
solving systems of quadratic equations, you should look into interior barrier
solving methods, which generalize the simplex method.

~~~
blt
Really? I thought the simplex method was kind of the opposite of an interior
point method.

------
noir_lord
Saved my bacon about 5-6 years ago, the company we where going to depend on to
do a whole bunch of complex calculations related to solar insolation
discontinued their API but didn't tell anyone, I found out when I rang up to
get a price.

So suddenly I was in the situation of dealing with maths that was harder than
anything I'd done at school years and years ago.

In the end I transliterated the equations into sage, checked the results
carefully and then wrote the PHP to do the same calculations using a large set
of tests against the official stuff, stressful two weeks but sage was useful
as hell.

~~~
fho
Same for me ... my MsC thesis relied heavily on the result of a single complex
three-dimensional integral ... I spend weeks trying to solve it by hand.
SageMath wasn't able to solve it either, but it helped me towards the
solution.

------
abdullahkhalids
I used Mathematica a lot during my PhD. Now, using Sage and I am disconcerted
by it. The great things about the Wolfram Language is that it is functional,
which is a very nice way to think about maths, and how I am now used to. Sage
is not functional. You create mathematical objects and those objects have
properties that have to extract using Python class functions.

My other gripe, more objective than the above, with Sage is the lack of good
documentation. The documentation is certainly complete, but it is painful to
read. Mathematica documentation is a treat to read. Every function has its own
page with plenty of examples to show capabilities, and show relations to other
functions.

With Sage I constantly have to search the web to how to do things, while in
Mathematica, I rarely had to leave the inbuilt help.

~~~
williamstein
The programming language of Sage is indeed imperative (Python) rather than
functional. Python's approach is much better than Mathematica's for some kinds
of mathematics, and much worse for others. This is more generally true of
imperative versus functional programming -- each is good for some classes of
problems and terrible for others.

Regarding examples, currently there are 50,283 functions defined in the Sage
library, and 96.8% of these functions have documentation that is illustrated
with working tested examples. Also, you can read the source code of 100% of
these functions (unlike with Mathematica) in case something isn't clear. Much
of the Sage reference manual (find it here:
[https://doc.sagemath.org/](https://doc.sagemath.org/)) is autogenerated from
the sagemath library source code; that's why there are 1,287,666 source lines
of comments in sage, versus 636,899 lines of non-comment code.

~~~
smabie
What stuff is functional programming bad at? You can just introduce loops and
mutation with tail recursion and get all the "benefits" of imperative code.

~~~
carlob
Not OP.

First of all I wanna say that Mathematica/Wolfram Language is not _just_
functional and certainly not purely functional.

Thus said, I feel trying to be purely functional has its drawbacks whenever
you have to keep track of a lot of stuff that requires state: for example if
you have to declare that some of your constants are real you end up having to
use Assuming or Assumptions a lot and that can get verbose quickly. Setting
the $Assumptions global makes for terser code.

If you wanna dig more into the differences of the two approaches in the same
language you can look at the xAct package vs. the built-in symbolic tensors:
xAct has many more features but feels less idiomatic, because you have to set
they type of your symbols beforehand and expressions are not really self-
contained.

------
heavyset_go
I highly suggest using this package if you're in school or otherwise learning
higher level math. Being able to programmatically interact and manipulate
abstractions over the mathematical concepts you're learning is an enriching
experience, and it can help you check your answers to your homework. It's also
a Python module so you can learn a skill that transfers well to other domains
compared to, say, Mathematica.

~~~
fizixer
Could you refer to a tutorial or a bit more description about what you mean by
'manipulate abstraction over the mathematical concepts', especially when doing
higher math?

Simple arithmetic, or even computer-algebra simplifications are too
simplistic. They help with high-school stuff but are useless for higher math
where the bread and butter are theorem-proof-theorem-proof based
learning/understanding/research.

Geometric plotting/vis, again, good for high-school and freshman stuff mainly.
(Plotting is useful for research, data science, numerics, what not, but that's
a whole other story. And computer graphics is a rabbit hole).

Same with Analysis. Good to have an automatic integrator for Calculus, useless
for Real Analysis.

Numerical calculations are too laborious and you have to have a good
understanding of a wide body of numerical literature, plus have experience.
(again a rabbit hole)

That leaves us with proof-assistants, model-checkers, and small-scale
automatic theorem-provers. And experience research mathematicians are still
learning those.

Am I missing something?

~~~
gnramires
Not a mathematician. Used Sage for engineering purposes.

But I think one example would be: you're investigating something and get a
large linear system, a polynomial, etc. Sage can solve equations with symbols.
It saves a lot of manual labor. Various expressions can be manipulated,
simplified, etc. It can readily verify equality of huge expressions (which can
be error prone by hand), and so on. More specific fields have more specific
tools that could be used to assist, so it would depend on the field I guess.

See here:

[https://ask.sagemath.org/question/24866/how-to-simplify-
with...](https://ask.sagemath.org/question/24866/how-to-simplify-with-sage/)

Edit: I also think it should be fairly common to need to perform an actual
numerical calculation in mathematical work? I think it's quite better than a
simple calculator for that too, since it gives exact e.g. rational results,
and approximations would be unfit for a proof (unless extensive precautions
are made).

I remember I got an expression for an eigenvalue of a matrix (as a function of
some parameters) that would be very laborious to get by hand.

------
gnramires
"It builds on top of many existing open-source packages: NumPy, SciPy,
matplotlib, Sympy, Maxima, GAP, FLINT, R and many more. Access their combined
power through a common, Python-based language or directly via interfaces or
wrappers."

------
haberman
As someone who needs math software somewhat infrequently, I had assumed that
SageMath was a sort of de facto standard for people using Python with math. I
was surprised to see that Google Colab doesn't support Sage in its notebooks.

I asked a coworker about it, someone who had previously worked on Sage. It
sounds like some parts of the community are now favoring using lighter-weight
libraries directly (like SymPy for symbolic math).

I discovered that Sage is not pure Python: Sage is translated to Python before
running, which is how it can support syntax like "f(x) = x + 1" to define a
function (whereas in pure Python you would need to use "def".

~~~
watwatinthewat
Graduate educational background in math here and working in software
engineering and machine learning. Like your coworker said, I do my work in
Python and libraries like Sympy/pandas/numpy, not Sage. Sage never made more
sense than Python since Python covers everything I need to do, including what
Sage does that I need.

I've never met anyone who uses Sage and work at large research center. If your
college background is math, you used Matlab in school. If your background is
in computer science, you go with Python or another programming language. Sage
seems like the middle ground between those two, but who is that?

The cases posted here seem to be like yours where there is occasional need for
workers or students whose employer or school doesn't or won't supply a Matlab
license. Matlab is in most college math departments and offers keys directly
to students for like $50-100, so there are few of those cases. An employer
with math researchers isn't going blink at the $2k for a perpetual license, so
anyone doing the work regularly gets Matlab who wants it.

I do think Sage is good software and it is important to have a free
alternative to Matlab. The audience is pretty limited, though.

~~~
boothby
> An employer with math researchers isn't going blink at the $2k for a
> perpetual license, so anyone doing the work regularly gets Matlab who wants
> it.

Not all employers can splash for matlab. You might be right about a simple
license, but in my experience most matlab developers rely on toolkits which
ratchet that price up through the roof.

For one example, somebody I worked with rewrote the parfor functionality as a
c extension, because we couldn't justify the expense of that addon for every
seat. Not a _great_ use of that math researcher's time, but it got the job
done, I guess.

I and a couple others at my company showed that Python / Cython could be meet
and beat matlab's performance, and we built up an open source stack that our
customers love and contribute to -- good luck doing that with matlab.

Our physicists found that the only feature they needed of matlab was its
plotting functionality -- so they rewrote that in their native lisp and moved
on with their lives.

Today, we only need a few seats for some researchers with seniority. I can't
say how much we're saving, obviously, but we eliminated a significant cost in
two departments' budgets and our finance folks are quite pleased.

My regret in this is that I used to be a Sage developer, but I've only got so
much sway at the company and GPL'd code is a no-go.

~~~
xvilka
With GNU Octave[1] you can run your existing MATLAB code with minimal changes.
And since 5.x versions it's now way faster than ever.

[1]
[https://www.gnu.org/software/octave/](https://www.gnu.org/software/octave/)

------
lvh
Sage is wonderful. It has a huge number of uses but I mostly use it for
cryptography. Sage has the best (certainly open source) support I know of for
a myriad of things like group theory and elliptic curves. Here's a short
example for how easy it is to play around with a tiny elliptic curve:

    
    
        sage: p = 19; p.is_prime()
        True
        sage: K = GF(p); K
        Finite Field of size 19
        sage: E = EllipticCurve(K, [5, 9]); E
        Elliptic Curve defined by y^2 = x^3 + 5*x + 9 over Finite Field of size 19
        sage: E.count_points(), E.order(), E.gens()
        (19, 19, [(4 : 6 : 1)])
    

There are far too many things to name that sage can do, but the CLI has a
great autocomplete. Here's an example: let's say you have ECDH with point
compression, and you specify only an x coordinate. Point compression limits
the effectiveness of invalid curve attacks, where an attacker gives you a
maliciously picked Diffie-Hellman value that isn't actually on the curve
you're supposed to be on. However, if the x coordinate doesn't map to a point
on the curve, it's necessarily on its "nontrivial quadratic twist". Sage makes
this easy to play with because sage makes pretty much everything easy to play
with:

    
    
       sage: E.lift_x(6, all=True)
       []
       sage: E.quadratic_twist()
       Elliptic Curve defined by y^2 = x^3 + 6*x + 13 over Finite Field of size 19
       sage: E.quadratic_twist().lift_x(6, all=True)
       [(6 : 6 : 1), (6 : 13 : 1)]
    

If you want to do a full-on invalid curve attack, the easiest way to do that
is with Sage. You look up how the explicit formulas work in the EFD[efd], you
write a ladder, you figure out how to create other elliptic curves for which
the short Weierstrass doubling formulas still work (which parameter doesn't
appear in the formula?), and then just let sage generate every possible curve
and see which ones have the poor cryptographic properties you're after.

There's a reason the introduction to Cryptopals Set 8[set8] sends with the
words:

> By the time you're done, you will have written an ad hoc, informally-
> specified, bug-ridden, slow implementation of one percent of SageMath.

As much as I'm a Clojure fanboy none of the Java libraries come even close.
Unfortunately, sage is mildly annoying to use/install as a Python library
(that would help interop significantly).

[efd]: [https://www.hyperelliptic.org/EFD/g1p/auto-
shortw.html](https://www.hyperelliptic.org/EFD/g1p/auto-shortw.html)

[set8]: [https://cryptopals.com/sets/8](https://cryptopals.com/sets/8)

~~~
oefrha
I haven’t used CAS for nontrivial algebra and number theory in a while (think
half a decade), but IIRC Magma is a fair bit more powerful than Sage on these
topics and more consistent as well. But alas, it’s not open source and not
free. There’s a free calculator though subject to limits.
[http://magma.maths.usyd.edu.au/calc/](http://magma.maths.usyd.edu.au/calc/)

------
jwilber
Never used sage but William Stein is a dope skater and I’m always very stoked
to have other skateboarders involved in tech.

------
29athrowaway
If you are interested in trying sagemath, read the PREP tutorials.

[https://doc.sagemath.org/html/en/prep/quickstart.html](https://doc.sagemath.org/html/en/prep/quickstart.html)

These contain have examples for linear algebra, calculus, diff equations,
statistics, etc.

I use sagemath with Jupyter lab and it works pretty well. You can evaluate an
expression and ask sage to output the LaTeX code for it, so you can have
everything nicely formatted.

------
amir734jj
I used SageMath for my master's thesis in cryptography and group theory. It
was awesome! I highly recommended it and its unfortunately very underrated.

~~~
seamyb88
Using it extensively for my crypto PhD. Several well-known cryptographers
contribute to it.

It's an amazing bit of kit. But I'm not without criticisms. I feel it could do
with the kind of fresh-start overhaul that really isn't practical for an open-
source code base of this nature. E.g. move python 2.6 to python3. Also, you
have excellent multi-precision support, but many of the algorithms from other
modules don't support precision beyond machine lengths.

But really really good. Love it.

~~~
nenadst
fyi from the website :

Since version 9.0 released in January 2020, SageMath is using Python 3.

------
chaoxu
I've contributed some code to matroid optimization. The people working on it
are very knowledgable, often professors at universities donating their time. I
definitely learned a lot through implementing some algorithms from the 70s.

------
st1ck
Would anyone suggest a way to make use of Sage for non-mathematical work? Is
it better for plotting things, compared to plain matplotlib for example?

I once used Sympy to parse formulas written in infix notation and convert them
to S-expressions. But that's just less than 1% of what Sympy can do. I'd like
to find more uses for it.

~~~
boothby
Plotting is a killer feature of Sage, IMO. It's a wrapper over matplotlib, and
eliminates _reams_ of boilerplate. In my experience, it's not just easier to
use, but also easier to navigate the Sage docs than figure out the right way
to do something in matplotlib.

I'm a weensy bit biased; I probably took part in conversations around the
plotting interface as it was taking shape, and got familiar with that before
learning matplotlib in earnest.

~~~
williamstein
> It's a wrapper over matplotlib, and eliminates reams of boilerplate.

What happened is that in 2006 I hired Alex Clemesha to implement a
"Mathematica-like API on top of matplotlib for Sage", and he spent a year
doing so. Then Robert Bradshaw (lead dev of Cython) and Carl Witty wrote some
really sophisticated just in time compiler code to make it more efficient to
evaluate symbolic expression at many floating point values, which helps in
plotting (especially for 3d plots). Over many years after that other people
filled in little missing pieces of functionality and exposed more of
Matplotlib's functionality, and also just added a bunch of new functionality
(e.g., for drawing gridlines, 3d implicit plots, etc.) that wasn't available
in the Python ecosystem.

And that's Sage's plotting functionality...

------
mathgeek
Previous discussions for perusal:

[https://news.ycombinator.com/item?id=266040](https://news.ycombinator.com/item?id=266040)
(2008)

[https://news.ycombinator.com/item?id=17995031](https://news.ycombinator.com/item?id=17995031)
(2018)

~~~
dang
See also:

[https://news.ycombinator.com/item?id=19682126](https://news.ycombinator.com/item?id=19682126)
\- "Should I Resign from My Full Professor Job to Work Fulltime on Cocalc?" \-
2019

[https://news.ycombinator.com/item?id=19485166](https://news.ycombinator.com/item?id=19485166)
\- "Computational Mathematics with SageMath" \- 2019

[https://news.ycombinator.com/item?id=11883987](https://news.ycombinator.com/item?id=11883987)
\- "The Origins of SageMath" \- 2016

[https://news.ycombinator.com/item?id=11078220](https://news.ycombinator.com/item?id=11078220)
\- "Open source is now ready to compete with Mathematica for use in the
classroom" \- 2016

[https://news.ycombinator.com/item?id=10175563](https://news.ycombinator.com/item?id=10175563)
\- "The Simons Foundation and Open Source Software" \- 2015

[https://news.ycombinator.com/item?id=10147401](https://news.ycombinator.com/item?id=10147401)
\- "React, Flux, RethinkDB and SageMathCloud – Summer 2015 Update" \- 2015

[https://news.ycombinator.com/item?id=2088871](https://news.ycombinator.com/item?id=2088871)
\- "SageMath: Python-based Mathematics. Rock it." \- 2011 - a little bit of
editorializing there...

[https://news.ycombinator.com/item?id=983423](https://news.ycombinator.com/item?id=983423)
\- "Conception, birth and developement of a Free math software (Sage)" \-
ditto...

[https://news.ycombinator.com/item?id=179416](https://news.ycombinator.com/item?id=179416)
\- "Sage: Can There be a Viable Free Open Source Alternative to Magma, Maple,
Mathematica, and Matlab?" \- 2008

Any others?

------
ktm5j
I remember way back when this project was getting started (around 2005), I
hosted a public use Sage instance that was linked to on their website! I was
in high school at the time

------
fish45
I love sage and I used it for multivar calc this year but unfortunately it's
not as good in some aspects (mainly symbolic integration) as Mathematica. One
of my primary motivations for learning math is being able to contribute

------
xvilka
I hope these systems will be more modern in terms of tooling. For example,
having the LSP server[1] will help a lot for those who don't want to leave
their favorite editor. Or a better Unicode support[2].

[1]
[https://savannah.gnu.org/bugs/?57106](https://savannah.gnu.org/bugs/?57106)

[2]
[https://savannah.gnu.org/bugs/?57103](https://savannah.gnu.org/bugs/?57103)

~~~
BeetleB
Sagemath is probably the best you'll get in the free software ecosystem. I
don't think anything comes close.

Yes, better tooling would be nice. But I think better math support would be
better. I encourage everyone to read William Stein's posts on the challenges
they face on that front (incredibly hard to get funding, etc). Their resources
are very limited, and they have to prioritize heavily.

~~~
williamstein
Regarding LSP support, SageMath is a Python library and Python has LSP
support, as does JupyterLab...

------
noobermin
I used Sage years ago. Since I've felt that jupyter and such have taken its
place. Does sage have a place outside of use for mathematicians?

I'm a physicist, and the most I'd need a CAS for is essentially mathematica's
"Simplify". DESolve is great too but back then (2010 or so) Sage's diffeq
solvers felt limited.

------
skeeks
We used SageMath in cryptography lessons at university. Very useful stuff and
easy to get!

------
vsskanth
I really wish I started using Sage instead of maxima directly. The
documentation for maxima is pretty difficult to understand for a noob just
looking to solve some non linear equations

------
grizzles
Is there an easy way to embed a sage notebook viewer / editor inside a webpage
without installing a sagecell server?

~~~
williamstein
You can upload it to cocalc.com, then share it publicly, then click the embed
link to get a version that is suitable for embedding in an iframe...

~~~
grizzles
It might be a good strategic move to publish a React component to do this for
the non iframe solution enthusiasts out there. It would be good to see people
start using SageMath notebooks where they otherwise might be using Katex or
Mathjax today. It could always have an "Open/Run on CoCalc" link as part of
it.

~~~
williamstein
I probably should, since the underlying renderer mentioned above is actually
implemented using React (it takes as input JSON describing a notebook and
renders it, entirely on the frontend). Also, in case it isn't clear, these
days "Sage notebook" just means "Jupyter notebook with the SageMath kernel"...
since "the Sage notebook" (=the SageMath project's predecessory to Jupyter,
but written in 2007) is basically deprecated now in favor of the official
Jupyter notebook project.

------
amw-zero
How does it compare to Mathematica?

~~~
BeetleB
Nothing compares to Mathematica, unfortunately. Mathematica really is the
best, if you can afford it.

~~~
williamstein
More realistically. Mathematica is the best at some things, but is hopelessly
behind in others (e.g. algebraic number theory and group theory).
Computational mathematics is deep and vast.

~~~
BeetleB
Well, I can't argue with the Father of Sage :-)

A bit surprised to hear that, though. Do you mean Sage is better in algebraic
number theory as a whole, or is it more of some aspects are better with Sage
and others with Mathematica?

Any one done a writeup comparing the strengths of Sage w.r.t
Mathematica/Maple/etc? (In detail, not just big picture).

------
esrh
How good is it for machine learning? I'm currently using julia which I also
really enjoy.

~~~
yza
Sage is not something that you would want to use for ML per say. It can,
however, help you with understanding of the underlaying math and algorithms.
It has excellent support for symbolic math, statistics (distributions etc.)
and just about everything else you are going to need to study ML. I recently
switched from Mathematica and could not be happier.

~~~
noobermin
How are sage's differential equation solvers now? How does it compare to
Mathematica's? I'd love to get back into sage if it worked better with that
sort of thing now.

------
shuringai
why is the existence of sagemath have any news relevance on HN?

~~~
johnsonjo
> why is the existence of sagemath have any news relevance on HN?

Ideas from STEM (Science Technology Engineering and Mathematics) are pretty
large and pervasive throughout the tech industry. I think there are also a
large area of tech startups that could come from any area of STEM. Also one of
the goals of info on hacker news is to be intriguing to hackers themselves and
to fulfill some sense of intellectual curiosity. For me personally SAGEMath
opens doors for me as a programmer to better understand abstract math concepts
that might or might not have relevance to my daily work as a programmer and as
a cofounder. In that sense I think SAGEMath helps fulfill that intellectual
curiosity. Heck it may even help me complete that next projecteuler problem.
(Which also is only done for the fun and experience of it.)

Also people who come up with really creative company ideas or that build free
software that really do something quite amazing are often at the intersection
of tech and some other field. This is not to say that tech solves all other
fields’ problems. It’s that tech can benefit other fields when it takes the
approach of how can I make this field’s work easier which often comes from a
good amount of knowledge of how that field actually works otherwise it’s just
too superficial. SAGEMath is a great example of tech actually helping research
mathematicians and most of it is made by mathematicians themselves (who
clearly have some programming prowess.)

By the way I’m sorry you were downvoted. Looking at your profile I see you’ve
only been on here for 82 days. I see your comment as very genuine but I also
understand a little of why you might have been downvoted but for that I’ll
just point you to the sites guidelines:

[https://news.ycombinator.com/newsguidelines.html](https://news.ycombinator.com/newsguidelines.html)

The original post very clearly meets the guidelines though it is not your
typical news, it is news to hackers. Happy Hacking. :)

