(part 1) “…hello SymPy”
This week I decided to challenge myself… to really dig deep. I wanted to write a blog post, but had no inclination towards any specific topic. So, in order to brain storm I did a “technic of ideation”, in which I write down things that I love and lo, the words “python calculus” were there on the page. “Is this even possible?” I asked myself, “sigh, maybe not today… I can’t handle a rabbit hole right now, I have things to do”. But my morbid curiosity grew and grew, and the topic called to me more and more. After some research, a few hours of deep thought and a few beers, what I am currently looking at on my computer screen is making the former calculus instructor in me (he’s always there) want to weep with joy.
…so (deep breath) let’s do it, together… let’s do calculus with python.
*note*, Here in part 1 we are going to set up our environment in order to do this… If you want to see me actually do some calc check out part 2 (link at the bottom)
Okay, Lets “cut the cake” so to speak…
This is the trickiest part, its always the trickiest part… many math people work with SymPy in some thing called jupyter, a “python notebook”, but we are programmers so we are doing it in the terminal because we “know how to computer”…
Very briefly, SymPy depreciates around python 2 ish and caps out at python 3.7… which is annoying as I have python 3.9. So I’m just gonna use the python that comes free on all macs (3.9 I installed, but 2.7 comes for free). If you don’t have a mac I’m pretty sure you can just pip install python 2.7. Anyway, here Is what I am working with:
The way I got this to work was by running SymPy in a virtual environment (which you can read about here). So, make the folder where you want this to happen and:
$ virtualenv --python=python2 venv
This will set up the “environment to run a virtual environment”. In your folder there will be a venv folder… which has a bunch of stuff in it (but thats another blog post, go read about it if you’re curious) but we just need to activate it. Touch a python file or two (mine are called calc.py and test.py, this is where you’ll write your calculus code). So far I have:
$ source venv/bin/activate
this will activate your virtual environment, which will look like:
If you ever want to exit the virtual environment, just type:
So, inside your virtual environment we are gonna install the dependencies...
$ pip install sympy
and I THINK, SymPy needs mpmath, a “Python library for arbitrary-precision floating-point arithmetic”… so to be on the safe side:
$ pip install mpmath
after this, you SHOULD be good to go… If any of this doesn’t work check out the full tutorial here.
Now lets test it, run python in you terminal:
You should now be able to:
>>> from sympy import *
>>> x = Symbol('x')
>>> limit(sin(x)/x, x, 0)
You’ve imported SymPy, defined a variable called x and took the limit of sin(x)/x as x approaches 0… the answer is 1.
If you can do this than you are perfect:
Now to get it going in my test.py, fire up vim and write what you just wrote in you whatever file you made:
now from the shell, run the file:
Outstanding… it works.
Join me in part 2 here!!!!
(where we actually do some calc with this!!)