python calculus

Jack Callaway Sanders
Jan 8 · 4 min read

(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)

We’re going to use the free python library SymPy (click on SymPy to check it out). which uses “symbolic computation”, which you can read about here

Okay, Lets “cut the cake” so to speak…

Installing SymPy

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”…

about SymPy

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:

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:

now

this will activate your virtual environment, which will look like:

See that little (venv) on the left? That means your virtual environment is running

If you ever want to exit the virtual environment, just type:

So, inside your virtual environment we are gonna install the dependencies...

and I THINK, SymPy needs mpmath, a “Python library for arbitrary-precision floating-point arithmetic”… so to be on the safe side:

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:

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:

You have to print now

now from the shell, run the file:

Outstanding… it works.

Join me in part 2 here!!!!

(where we actually do some calc with this!!)

The Startup

Get smarter at building your thing. Join The Startup’s +724K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store