SymPy makes math fun again
wordsandbuttons.online
wordsandbuttons.online
I wrote a whole book around the idea that mathematics is understanding first and symbolic computation second. In Geometry for Programmers, SymPy does the grind for you in every chapter (except chapter 2). Initially, I was even going to omit all the math notation altogether and turn all the formulas into Python snippets. But several reviewers said that this is too much, so now there are formulas you can see and snippets you can run.
Even if the book is called Geometry for Programmers, I was trying really hard to make it self-sufficient. E. g. if you want to get deep into fascinating stuff like NURBS, you need to know a little calculus. So there is a chapter on calculus. It is shallow by itself but it opens the door for power series, polynomial interpolation, and then Bezier, rational Bezier, and finally NURBS.
Just like that, before introducing homogeneous coordinates and projective matrices, I put a chapter on linear equations. In any other geometry book it would have been completely redundant, but it explains so much about 4 point transformations, I just had to put it there.
TL&DR I sure hope so! This was my intention and if the book isn't appropriate for a beginner, I failed miserably as an author.
...and I learned all of that after the manuscript was done. That's on me, I should have been more inquisitive. Also, I should have asked about their style guide and technical requirements for the graphics. The book was supposed to be ready for printing sometime this month but now the launch is delayed till June since they have to redo 300+ graphics from scratch.
Self-publishing definitely gives you more freedom. But Manning also give you a development editor, a technical editor, a copy editor, a technical proofer, a copy proofer, plus a few dozen reviewers, and a whole production team. Writing for a publisher does feel like a deal with the devil, true, but it is not that bad of a deal.
If you want, write me a line (ok@wordsandbuttons.online) and we'll chat about publishing with Manning. Or without. My first book was published on Leanpub, and the second is available from wordsandbuttons.online directly.
There's latex display feature when in notebook. It looks good, can even have symbolical matrices and display them as you would've expected.
It's on par with Wolfram notebook, but you get to program in python instead of some propietary language.
If you got some really long formula, it's easier to see you got it correctly by displaying it symbolically in latex.
They have other things to smooth out the parts of python which are annoying in math, like exponentiation works with ^, and dividing integers returns a rational number instead of floating point or an integer.
A note to a bypassing reader, if you want to try Mathematica, and not sure if you would stay with it, don't go for a desktop license. Get a Raspberry Pi instead. Mathematica comes free for Raspberry!
for a_k in 1 to n:
for a_(k-1) in 1 to a_k:
....
I'm a heavy user of Mma but there are better examples. Automating the rewriting of formulas to make them more concise is a big one. (But also not as straightforward as it could be, tradeoffs between conciseness, ease of reading and speed of implementation need to be triaged, similar to Lisp/APL. Maybe sympy still has a chance to be more like math/TCS)As another example, I googled some project Euler solutions in Mathematica and (pure Python) and found this: https://www.nayuki.io/page/project-euler-solutions. Compared to the pure Python solutions, the Mathematica code is typically much smaller. Of course this is not Sympy, but a lot of the Python syntax carries over to SymPy as well.
Hardly surprising, is it? As someone with more experience in Python, it would likely be the opposite for me.
SageMath also has a multivariate inequality solver IIRC. `*` is repeated multiplication (exponentiation) in Python. If you require preprocessing to translate ^ to *, you don't have valid Python code that'll run with any other interpreter.
Is it easier to import SageMath from a plain Python script now; with conda repackaging?
Type promotion in Python:
from rational import Rational
assert Rational(1, 4) / 2 == Rational(1, 8)
Python 2 had a __future__ import to do floatdiv instead of floordiv: from __future__ import division
assert 3 / 2 == 1.5
assert 3 // 2 == 1
assert 4 / 2 == 2.0
Python 3 returns floats from ints or decimals IIRC: assert 3 / 2 == 1.5
assert 3.0 / 2 == 1.5
assert 3 // 2 == 1
assert 4 / 2 == 2.0Yes, it's easy. It was always possible, since fortunately none of the Sage library implementation uses the Sage preprocessor. You could always do "sage -python" and you get the Python interpreter that Sage uses, and can import Sage via "import sage.all". Somebody asked me exactly this question at a colloquium talk I gave on Sage yesterday, and here's my answer/demo:
https://youtu.be/dy792FEh1ns?t=1140
In the demo, I use a Jupyter kernel that runs the Python included in Sage-9.8.
Disclaimer: I created the Sage preparser long ago, in order to make Sage more palatable to mathematicians. In particular, it was inspired by audience feedback during a live demo I did at PyCon 2005.
I should add that this is an interesting chunk of Python code, and we've always planned to separate it out from Sage as a separate library than could be used with Sympy, etc.
https://github.com/sagemath/sage/blob/52a81cbd161ef4d5895325...
It does a surprising number of little clever things we realized would be a good idea over years, and it has some neat ideas from various Python PEP's like [1..10] that were rejected from Python, but are very useful when expressing mathematics. It's also annoying when you just want to format your code using prettier (say), and you can't because Sage isn't Python. :-(
Is there a %%sage IPython magic method that passes code through the preparse.py input transform?
I‘m not sure if that’s a good thing, considering how nice and well-suited to maths the Wolfram language is.
I get that you need to learn yet another language, but you would also need to learn the particularities of Sagemath and/or the libraries it builds on.
The tutorial is also available in notebook form: https://github.com/minireference/sympytut_notebooks/tree/mas... Binder link: https://mybinder.org/v2/gh/minireference/sympytut_notebooks/...
Here’s a blog post I made recently showing some fun SymPy calculations. https://www.swied.com/posts/make-a-ringtone-sound/make-a-rin...
I think the free-ness of Python is a really big deal, because free means you use it for everything, and you use it for things that you expect to share. I don't know the present license terms of those programs, but it used to be that unless you worked for a really enlightened organization, if you had a seat of Mathematica, it meant you had exactly one seat, probably on the computer in your office, possibly the only seat in your department. If you also had computers in the lab, at home, etc., those computers were running something else, such as Python.
If Python ever got good enough at something previously reserved for commercial software, even without being comprehensive at that thing, it triggered a decision on whether it was worth the headache of maintaining the commercial license and being chained down to a single computer.
I don't do large math projects, and have been satisfied with Maxima. Where I'm beginning to use SymPy is to contain the derivations for formulas within the same Python code that uses those formulas. Mainly it's a readability and documentation play.
I left matlab behind years ago, but I still use mathematica occasionally.
Though I haven't made a serious effort to try out Maxima, Reduce, or Sagemath.
I strongly encourage interested folks to pull it down and give it a spin. The printing/printer system, which the author touches on briefly with `jscode` is a brilliant way of converting sympy expressions into whatever you like. I found it a pretty smooth experience to write my own little printer.
Most other CASes seem to have a more "magic calculator" interface, where you plop down some crazy integral and it spits out a closed form. This is useful of course, but it has nothing really to do with doing math.
I'm sure both styles are possible in either one, that's just the spirit I feel like each one has.
Lately I’ve been using SymPy and Pluto.jl together, it’s been great for deriving results and making interactive visualizations for numerical code at the same time
I think I was looking into it at the start of this project, but it looks like it’s symbolic solver can still only handle linear equations, and I’m working with a mess of quadratic inequalities that I wanted to solve symbolically
Regarding his low exam performance , this is why classes are sometimes graded on a curve. Intro calc. is pretty easy, and the author def. seems smart enough to do well. Likely his class was unusually hard. Sometimes that happens..luck of the draw.
Of course, there is nothing wrong in doing symbolic computations by hand. But it's like walking vs. riding a bicycle. You're still getting from A to B but you save some time and energy with the machine.
In Mathematica, I have written calculations that involve dozens of variables. In sympy that will be very annoying.
In [1]: 22/7*foobar
Out[1]:
22⋅fo̅o
──────
7 In [1]: (foo + 1) ** 7
Out[1]:
7
(foo + 1)
In [2]: expand(_)
Out[2]:
7 6 5 4 3 2
foo + 7⋅foo + 21⋅foo + 35⋅foo + 35⋅foo + 21⋅foo + 7⋅foo + 1
In [3]: diff(_)
Out[3]:
6 5 4 3 2
7⋅foo + 42⋅foo + 105⋅foo + 140⋅foo + 105⋅foo + 42⋅foo + 7
In [4]: expand((cats+dogs)**5)
Out[4]:
5 4 3 2 2 3 4 5
cats + 5⋅cats ⋅dogs + 10⋅cats ⋅dogs + 10⋅cats ⋅dogs + 5⋅cats⋅dogs + dogs
In [5]: hip + hip + hooray
Out[5]: 2⋅hip + hooray a, b, c, d, f, g, h, i, j, k, l, m, n, p, q, r, s, t, u, v, w, x, y, z = var('a, b, c, d, f, g, h, i, j, k, l, m, n, p, q, r, s, t, u, v, w, x, y, z')
At the top of your Jupyter notebook, right after your import lines, and copy and paste it everywhere else. It's really not a big deal! You can always redefine these variables to other things later, if you need to.Mathematica costs $387 for the "home desktop" version, or $194/year for the online version. Pretty steep for anyone doing math as part of their hobby. Maybe it would be fine if math itself is your hobby, but even then I'd rather spend that on books.
Of course this is all moot if you're an academic and your school pays for all the licenses to any software you need.
Mathematica, just like, real mathematics, allows you to have variable names with subscripts/superscripts/greek-letters etc. E.g. a_g is distinct from a_f (which are both rendered in rich text)
My recommendation is not that everyone buy Mathematica, but that sympy figures out a way to allow every sequence of letters and numbers (that start with a letter) to automatically become a variable without declaration.
In your formulas you can have Greek letters in sympy. But you'll have to give the symbols valid python names.
(it puts things in the global namespace, but for the typical calculator use case that's probably fine)
(Sage seems to work like this too - it may use sympy under the hood)
(And I have dropped out the very same year then :-)
https://groups.google.com/g/sympy/c/jisKFe_xsd0
"From what I've seen, for a while, it's mostly just been people asking questions, but no one has been on there who is actually able to answer them."
https://live.sympy.org/ (recommend desktop browser, but works on mobile too)
Here's a completely separate Python in WebAssembly project I created, which also provides Sympy with "import sympy" being a little faster (1.7s v 1.16s on my laptop): https://cowasm.org/
I'd like to say that I use it all the time, but I'm too old to be bothered by learn new things at this point. However, I think the Python ecosystem is getting better all the time, and things like SymPy and Mathics give me a lot of hope for the future of technical computing with Python.