Latest / Talk Python To Me / #364: Symbolic Math with Python using SymPy
Transcript
- 0:00We're all familiar with the data science tools like NumPy, Pandas, and others.
- 0:03These are numerical tools that work with floating point numbers, often to represent real-world systems.
- 0:09But what if you could exactly specify the equations, symbolically,
- 0:14like many of us did back in calculus or differential equations courses?
- 0:17With SymPy, you can do exactly that.
- 0:20Create equations, integrate, differentiate, and solve them.
- 0:24Then you can convert those solutions into Python or even C++ and Fortran code.
- 0:29We're here with two of the core maintainers, Andrei Sertik and Aaron Moyer, to learn about SymPy.
- 0:34This is Talk Python To Me, episode 364, recorded May 6th, 2022.
- 0:40Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:56This is your host, Michael Kennedy.
- 0:58Follow me on Twitter, where I'm @mkennedy.
- 1:00And keep up with the show and listen to past episodes at talkpython.fm.
- 1:04And follow the show on Twitter via at Talk Python.
- 1:07We've started streaming most of our episodes live on YouTube.
- 1:11Subscribe to our YouTube channel over at talkpython.fm/youtube to get notified about upcoming shows and be part of that episode.
- 1:18This episode is sponsored by Microsoft for startups.
- 1:26Founders Hub to get early support for your startup.
- 1:29And it's brought to you by Sentry.
- 1:31Don't let those errors go unnoticed.
- 1:33Use Sentry.
- 1:34Get started at talkpython.fm/sentry.
- 1:37Transcripts for this and all of our episodes are brought to you by Assembly AI.
- 1:42Do you need a great automatic speech-to-text API?
- 1:45Get human-level accuracy in just a few lines of code.
- 1:47Visit talkpython.fm/assemblyai.
- 1:52Andre and Aaron, welcome to Talk Python To Me.
- 1:54Thanks for having us.
- 1:56Yeah.
- 1:56It's great.
- 1:58Yeah, it's fantastic to have you both here.
- 2:00You know, what a cool topic.
- 2:01Symbolic math with Python.
- 2:03I think I'd heard of SymPy before, but I didn't fully appreciate, you know, how neat and how advanced it really was.
- 2:11So people are going to enjoy diving into this and we can talk some internals and whatnot.
- 2:18And if you're doing anything scientific or computational, this is certainly a project worth checking out.
- 2:23But before we get to that, let's just hear your story.
- 2:27How did you get into programming in Python?
- 2:28Aaron, you want to go first?
- 2:29I've always kind of been into computers ever since I was a little kid.
- 2:34I actually started messing around with programming and AppleScript in my family's Macintosh back when I was, like, a really little kid.
- 2:46Nice.
- 2:47But didn't really do any serious programming until college when I started taking, like, computer science courses.
- 2:54And actually, for Python, my college had a free course that they were offering to teach this little language called Python.
- 3:04So, you know, the CS course that I was taking was in, like, Java and C.
- 3:10And those languages are – I wasn't a huge fan of those languages.
- 3:14But I learned about this language called Python, and, yeah, I thought it was great.
- 3:18Yeah, I was immediately hooked with Python just because it's, you know, it's so easy to use.
- 3:25It's so easy to just write a program, and you don't have to worry about compilers or all this boilerplate.
- 3:34Yeah, I'd always been a little suspicious of languages that described themselves that way, which is ironic given how much I do with Python.
- 3:42But still often when you hear that, it's like, oh, it doesn't need all the symbols, and it's super easy to get started.
- 3:47That means, like, oh, it's really easy to do easy stuff, but then you can't do interesting things.
- 3:52And I think one of the things that's special about Python is, like, you can keep going.
- 3:56You know, you don't have to stop once you need advanced ideas.
- 3:58That's pretty awesome.
- 4:00Yeah, it really is.
- 4:01You're right.
- 4:02A lot of languages kind of stop.
- 4:04But Python, I mean, it's got a lot of advanced stuff as well, and it's got a huge ecosystem of libraries you can do just pretty much anything you want in it.
- 4:15Yeah, for sure.
- 4:17The big paradox for me is there's a joke T-shirt that I've seen that says, I learned Python.
- 4:23It was a great weekend, which I think is kind of true.
- 4:25You can do that.
- 4:26But at the same time, for many, many years now, I've been studying Python, and there's still just so much more to learn.
- 4:32One, about the language, and also just, like, as you said, all the different libraries, like, oh, I want to learn Pydantic now, or this other thing, or so on.
- 4:40Very cool.
- 4:41All right, how about now?
- 4:42What are you working on day-to-day?
- 4:43Yeah, so I work at a company called Quansight.
- 4:48We do consulting, mostly around open source Python, open source data science stack.
- 4:56I work about 50% of my time on SimPy as part of a grant, which we might talk about later, I think.
- 5:04And I also work on different consulting projects.
- 5:10Actually, right now, I'm part of a project called the Data APIs Consortium, which is trying to standardize the array API libraries for different Python array libraries, the APIs for those.
- 5:30So, you know, like, NumPy and X-Array and those types of things?
- 5:32NumPy, PyTorch, Jax, QPy.
- 5:36So, the idea is that all these libraries have very slightly different APIs, and it makes it hard to write code against that just works against all of them.
- 5:47So, what we're doing is creating a standard API that all of these libraries will target.
- 5:52And so, you can write code that will just work with NumPy, but you can also just, instead of importing NumPy, you can import QPy, and that same code will run on the GPU, for example.
- 6:04Oh, that's fantastic.
- 6:05Yeah, that's a really good, ambitious goal.
- 6:08It sounds so easy, and I'm sure it's pretty challenging.
- 6:12Yeah, well, I think we're being successful.
- 6:15We just released the first version of the specification, and we've got several libraries that are implementing it already, including NumPy, QPy, several others.
- 6:25So, it's actually, I think it's being a successful project and should really help push the Python data science ecosystem forward.
- 6:35Yeah, it sounds like a definitely good step.
- 6:37Andrei, how about you?
- 6:38How did you get into programming in Python?
- 6:40You know, probably in high school, I was trying to figure it out when I got into it.
- 6:44I think at high school.
- 6:45I don't know.
- 6:45I don't remember how I found it, but it was early 2000s, I'm guessing.
- 6:51It was before NumPy.
- 6:53It was, I think, it was NumArray, I think.
- 6:55It was the array library in Python.
- 6:57And during my undergrad, I was studying physics.
- 7:01I wanted to play with the mathematical formulas in Python.
- 7:06So, that's when I started SymPy.
- 7:07And then I used Python, you know, pretty much, probably close to 20 years now, or over 20 years.
- 7:12And I cannot say I'm an expert.
- 7:15You know, it wasn't a good weekend.
- 7:20I don't know.
- 7:20A lot of the stuff I'm not that great at, you know, but the basics, I would say I know pretty well.
- 7:26Yeah, sure.
- 7:27After my PhD in physics, I went to work at Los Alamos National Lab for about eight years.
- 7:32Computational physicist.
- 7:34I use Python.
- 7:36I use a lot of Fortran, a lot of computational codes, and a lot of C++.
- 7:39That sounds fun.
- 7:40There's a lot of Python stuff happening there at Los Alamos.
- 7:42Yeah, it is.
- 7:43The NetworkX, I think, library was started here.
- 7:48And then, very recently, half a year ago, I changed jobs.
- 7:53I now work at a company called GSI Technology, which is a hardware vendor, as a compiler developer.
- 8:00And so, at Los Alamos, I started this compiler for Fortran called L-Fortran.
- 8:07It's a compiler for Fortran, but it also allows you to use it interactively, just like you would Python.
- 8:11It runs in a Jupyter Notebook, as well, as a kernel.
- 8:14And at this new company, we are also, in addition to L-Fortran, creating a new frontend called L-Python,
- 8:22which takes Python, but compiles it through the same pipelines, through the same intermediate representation
- 8:28and all the backends.
- 8:29All the code is the same.
- 8:31So, effectively, it treats Python like Fortran.
- 8:33So, the same speed, and so forth.
- 8:37Oh, wow.
- 8:37That sounds awesome.
- 8:38It is awesome.
- 8:39Yes.
- 8:39Very, very cool.
- 8:41All right.
- 8:42Well, super fun to be talking about SymPy with you all today, symbolic math.
- 8:48I want to start this off, though, with a write-up from Aaron that is maybe not what people would expect
- 8:56as the first topic to introduce SymPy.
- 8:58SymPy has been in the news a little bit for the wrong reasons recently, right?
- 9:02Right, Aaron?
- 9:04So, it was even on the front page of Hacker News because this company, HackerRank, reached
- 9:12out to GitHub, sounds like through automated ways, and said, we seem to see some math that
- 9:20looks like questions we might ask or something like that.
- 9:23So, we need to completely ban SymPy because they're stealing our interview questions or something
- 9:29like that, right?
- 9:30Give us the story.
- 9:31What kind of madness was this?
- 9:33Yeah.
- 9:33So, we sort of just got this notice from GitHub that a DMCA takedown had been issued against
- 9:40one of the pages on our documentation.
- 9:44And the way the DMCA works and the way GitHub's DMCA policy works is when somebody issues a notice,
- 9:51they basically just have to take it down.
- 9:55And GitHub basically took down the entire documentation site for about 12 hours.
- 10:03And so, I've got a timeline of everything that happened here in this blog post.
- 10:10But basically, yeah, they had some company that they were working with to try to find their
- 10:20solutions on GitHub and issued DMCA takedown notices against them.
- 10:25And somehow, this SWIRTH IT solutions company decided that our docs was one of these, and they
- 10:32just issued the notice.
- 10:33And so, yeah, it ended up taking down our documentation, which immediately, like you said, it made it
- 10:42to the top of Hacker News and kind of tension.
- 10:46Because it's too outrageous.
- 10:46Yeah.
- 10:47So, the notice itself, it was completely ridiculous.
- 10:53Like, the stuff that they were claiming was their copyright is probably not even copyrightable.
- 11:02Like, if you look at the examples on that documentation page that they took down, they're just like
- 11:08simple math examples.
- 11:09Yeah.
- 11:09It was like X squared minus two solution is, you know, negative plus or minus radical two.
- 11:16Right?
- 11:16Like, that you shouldn't be able to copyright.
- 11:19I think that actually predates HackerRank's foundation.
- 11:23I think it predates HackerRank as a company.
- 11:26Yeah.
- 11:27Yeah.
- 11:27So, the CEO of HackerRank, I guess, noticed the buzz that was being made about it and retracted
- 11:36the notice.
- 11:36So, the docs ended up going back online about 12 hours later.
- 11:41If they hadn't done that, we would have had to issue a counter notice, which would have kept
- 11:47the docs offline for about two weeks before that counter notice would have taken effect.
- 11:53And that's just because of the way the DMCA law works.
- 11:55I also explained how the DMCA law works in this blog post.
- 12:00If anyone's wondering, like, why would GitHub even do this?
- 12:04Like, well, they're kind of required to do this kind of stuff by law.
- 12:07There are some ways I think they could have improved what they did here, which I go over
- 12:12as well.
- 12:12But this DMCA law is really...
- 12:14It's probably like pointing those out.
- 12:15Yeah.
- 12:15So, I mean, the DMCA provides what's called safe harbor for places that host user-generated
- 12:22content, basically.
- 12:22YouTube, the social media places.
- 12:25GitHub clearly has a ton of user-generated content that's public.
- 12:29If you got the source code of Windows or something and you posted it up there, right?
- 12:35Theoretically, Microsoft might say, you know, we don't really want you hosting this.
- 12:38It's not supposed to be open source.
- 12:40Please take it down, right?
- 12:41So that's the idea.
- 12:43The way it works, though, is basically once there's a request to take it down, they just
- 12:49have to...
- 12:50Here's the part that I think that got missed.
- 12:52Decide that it's a valid request and then just take it down.
- 12:55Like, they don't really try to negotiate or determine the right...
- 13:00The correctness of that assertion, right?
- 13:03Yeah.
- 13:03So they sort of have to stay, like, as a disinterested third party, I guess.
- 13:11And the reason is that in order for them to have the safe harbor, the safe harbor status
- 13:17sort of frees them from the liability on either side if there is a copyright claim or if there's
- 13:24a lawsuit or something.
- 13:25They can just say, okay, well, we're just hosting the content.
- 13:29The law sort of allows them to do that.
- 13:31But in order to do that, they sort of have to just...
- 13:34If they get a DMCA claim, they have to just take it down.
- 13:36And then if there's a counter notice issued, then they can put it back up.
- 13:40And the DMCA has these...
- 13:42The DMCA sort of tells them that if they get a claim, they have to sort of take it down expeditiously.
- 13:49And if there's a counter notice, they have to put it back up in 10 to 14 days.
- 13:54So it's...
- 13:55There's a lot of parts of the law that they sort of have to do.
- 13:59There's some things I think that they could have done better.
- 14:02And there's also some things that they already do, which they don't have to do, which are nice,
- 14:06including they have a repo where they actually post every single DMCA notice that they've received,
- 14:12going all the way back to 2011, actually.
- 14:15Interesting.
- 14:16So you can actually see all the DMCA notices that have been issued against GitHub repositories on that repo.
- 14:22It's github.com/github slash DMCA.
- 14:28This portion of Talk Python To Me is brought to you by Microsoft for Startups Founders Hub.
- 14:32Starting a business is hard.
- 14:35By some estimates, over 90% of startups will go out of business in just their first year.
- 14:39With that in mind, Microsoft for Startups set out to understand what startups need to be successful
- 14:45and to create a digital platform to help them overcome those challenges.
- 14:49Microsoft for Startups Founders Hub was born.
- 14:52Founders Hub provides all founders at any stage with free resources to solve their startup challenges.
- 14:58The platform provides technology benefits, access to expert guidance and skilled resources,
- 15:04mentorship and networking connections, and much more.
- 15:07Unlike others in the industry, Microsoft for Startups Founders Hub doesn't require startups to be
- 15:13investor-backed or third-party validated to participate.
- 15:16Founders Hub is truly open to all.
- 15:19So what do you get if you join them?
- 15:21You speed up your development with free access to GitHub and Microsoft Cloud computing resources
- 15:25and the ability to unlock more credits over time.
- 15:29To help your startup innovate, Founders Hub is partnering with innovative companies like OpenAI,
- 15:34a global leader in AI research and development, to provide exclusive benefits and discounts.
- 15:39Through Microsoft for Startups Founders Hub, becoming a founder is no longer about who you know.
- 15:44You'll have access to their mentorship network, giving you a pool of hundreds of mentors across a range of
- 15:49disciplines and areas like idea validation, fundraising, management and coaching, sales and marketing,
- 15:55as well as specific technical stress points.
- 15:58You'll be able to book a one-on-one meeting with the mentors, many of whom are former founders themselves.
- 16:03Make your idea a reality today with the critical support you'll get from Founders Hub.
- 16:08To join the program, just visit talkpython.fm/founders hub, all one word.
- 16:13No links in your show notes.
- 16:14Thank you to Microsoft for supporting the show.
- 16:19I want to preface this by saying I'm not a lawyer, so I'm only speculating here.
- 16:24But one of the things that seems very, very much like it fell through the cracks.
- 16:28And this part, I think, is both maliciousness and laziness on the worth IT company, as well as,
- 16:36you know, sort of either laziness or disinterest, I guess, from GitHub.
- 16:42I don't really know how to characterize it.
- 16:43But this company came and said, on your documentations, there is content that is copyright.
- 16:50And it comes from our website.
- 16:52And it just linked just to the homepage of that other website.
- 16:56You got to take it down.
- 16:57That seems like insufficient information to GitHub to go, yeah, well, they said there's a website.
- 17:04And here's a website.
- 17:05That website doesn't like this one.
- 17:06So it should go away.
- 17:07It should say, do you see these lines of code here?
- 17:11That was stolen from us.
- 17:13And here's where it originates.
- 17:15Or, you know, our thing is not public.
- 17:16But these are the lines.
- 17:17They didn't even say what was wrong, right?
- 17:19It just said, we believe this code is going to destroy them.
- 17:22It was insufficient even for us if it was an accurate claim.
- 17:25Because if you look at this page that they put the claim on, it's a huge page.
- 17:30Because it's got, like, about a dozen different functions on it.
- 17:35And it just said that they copied our examples.
- 17:38It didn't say which part of the page was copied or anything.
- 17:41So, yeah, I mean, I still don't know, like, how they even decided that this was a thing.
- 17:49That somehow their automation got flagged incorrectly or something.
- 17:54I don't know.
- 17:55Like, I don't know if we're ever going to know that.
- 17:57Yep.
- 17:58All right.
- 17:59Well, I don't want to spend the whole time on this.
- 18:01But let me wrap it up with a quick thought.
- 18:04The CEO of HackerRank, Vivek, posted a message that said, this was not our intended consequence.
- 18:13Yes.
- 18:14But if you send bots running around the internet looking for algebraic equations and saying that those are, you know, like, this is your problem, even if it was not your intent to do these things.
- 18:30And so they fixed it.
- 18:34I don't know what they're doing now.
- 18:35But they said, we're going to stop the whole process for now and figure out what we can do better.
- 18:38But on the other hand, we should give them props for donating $25,000 to the Senpai project as a, we're sorry.
- 18:45That's actually kind of cool.
- 18:47They didn't have to do that.
- 18:49No, they did not have to do that.
- 18:50And they did send the money, I think, last week.
- 18:53And so just...
- 18:55You guys are all on vacation?
- 18:56What is this, Hawaii?
- 18:57I'm just teasing.
- 18:58Or if the finances are online, you can find them up.
- 19:03I think it's an open collective and it pretty much almost doubles our budget.
- 19:07And so it will allow us to really fund all kinds of cool development.
- 19:11One thing on this note I wanted to say is that I was thinking, so when it all started, I felt like a little guy completely being crushed by some huge corporation or just this.
- 19:23You know, they just take down the documentation and GitHub just says, you know, it's not enough to put a commit to fix it.
- 19:29You have to excise the commit from the Git history.
- 19:32So we would have to figure out exactly what line was copied.
- 19:36And so I went to the Git history and tried to look up, well, maybe some of our contributors maybe copied some stuff from HackerRank.
- 19:42So I went to the history, but they didn't see anything.
- 19:45They did not see any mention of HackerRank.
- 19:47You know, oh, I copied this from HackerRank.
- 19:49They didn't see anything like that.
- 19:50They didn't see anything suspicious.
- 19:51But the consequence was dire.
- 19:54And also the documentation is generated from the Simpi source code.
- 19:57So really, the offending lines are coming from the Simpi repository itself, not just the documentation.
- 20:03So luckily they missed it.
- 20:05We were really, I was like, well, this might be, this is terrible.
- 20:09And then they, oh, and also we thought maybe, maybe this looks like a spam, you know, maybe it's just a spam.
- 20:16So we kind of ignored it a little bit, not ignored it, but, you know, we didn't know.
- 20:20And so I emailed GitHub right away, is this real or is this just a spam?
- 20:24And they got back to us on Monday evening and said, no, this isn't real.
- 20:27And then the next morning they took down the whole documentation.
- 20:29So I felt really just, you know, I spent, Aaron and others spent so many hours just trying to figure out what we should do and what the options are.
- 20:38And I was, it was, Google Summer of course was, Redline was on that Tuesday, I think as well.
- 20:44So I was just super busy with that.
- 20:46I just felt, you know, this is, and I'm doing this for free for Simpa.
- 20:48I don't think I've ever paid anybody to work on Simpa.
- 20:52I just felt, you know, this is, this is not right.
- 20:55And then when it got to Hacker News, I thought, you know, I thought about how can the company, what can they do to like fix it?
- 21:02And I thought, you know, there's not much they can do, the time was wasted.
- 21:04But then Drag Race got an idea, Oliphant got an idea, well, why don't they donate some money?
- 21:10And I thought, you know, this would fix it.
- 21:12And so I didn't think they would do that, but I'm very happy that they did.
- 21:15As far as I'm concerned, personally, I don't, I think we are even, they, they give us the money and we can use it to really progress the development forward.
- 21:25So I'm very happy that how it all ended for Simpa.
- 21:28Sure.
- 21:29You could pay to hire somebody to work on some important part, or you could pay to have sprints, like bring the people together to work together or something like that.
- 21:37Right?
- 21:37Nice.
- 21:38Yeah.
- 21:38We're still, we're still talking about what we're going to do.
- 21:40Cool.
- 21:42Well, I'm glad it came out well, but what a wild story.
- 21:46I've had, I've had run-ins like this before, and they are incredibly frustrating.
- 21:51Even when you're on the right side of it, it's just like, you know, you're, you're up against lawyers from.
- 21:58Yeah.
- 21:59You're on, you're on YouTube.
- 22:00So I've heard that DMCA is going to be pretty bad on YouTube.
- 22:03I don't know if that you've ever been hit by anything like that there.
- 22:05I, you know, what's so funny is only once as it happened on YouTube, it's happened for my courses.
- 22:12It's not exactly, it's happened sort of almost in reverse for my courses.
- 22:16I put one of my free courses on Udemy as a way to try to reach out to people who didn't already know about my content.
- 22:24Like, I'll put it up there for free.
- 22:25People can experience it there, and then maybe they'll go look for other courses for me, like, you know, something like that.
- 22:31Someone stole my free course, started selling it on Udemy, and it took me two weeks to get them to, to even take it down.
- 22:39Even though at the beginning, it's a picture of me saying, hi, I'm Michael.
- 22:43And I'm like, you gotta, we don't believe this is yours.
- 22:46Like, what do you mean?
- 22:47Have you watched it?
- 22:49The person who is the account it's under is absolutely clearly not Michael.
- 22:55Anyway, on YouTube, what has happened to me is I was doing a webcast way back in the day when people would call in, sometimes on phones,
- 23:04and somebody interrupted someone else on, during the webcast, and they put it on hold.
- 23:11And it was really frustrating because their hold music started playing back into the webcast.
- 23:16And for some reason, the way it was set up by the company I was working with to do it, they couldn't stop it.
- 23:21So there was just like five minutes of hold music going along with the presentation.
- 23:26That hold music got a DMCA takedown, and it took down my webcast.
- 23:30So what are you going to do?
- 23:33I didn't really mind.
- 23:35It was kind of a crappy thing anyway, but, you know, it was up there, and it got taken down.
- 23:38All right, let's talk about Senpai because I think this is a really neat project.
- 23:43Like I said, when I think about the data science stack and the scientific computing stack of Python,
- 23:49traditionally what I've thought of is NumPy, Pandas, Matplotlib, the stuff that does numerical processing,
- 23:59and then maybe visualizes that or does machine learning off of it or something like that.
- 24:03But Senpai, I think, occupies a slightly different but really important space in this whole computational Python stuff.
- 24:11Tell us about it.
- 24:11Yeah, so Senpai, like you said, it sort of stands in contrast to these other libraries in that it's not,
- 24:18it is a mathematics library, but it's not numerical, it's symbolic.
- 24:22So the focus is on computing with mathematical expressions exactly.
- 24:29So, you know, if you tell Senpai to, like you said earlier, solve the equation x squared minus 2 equals 0,
- 24:37you'll get the exact answer, square root of 2 minus square root of 2.
- 24:41You won't get a numerical answer like, you know, 0.7, whatever square root of 2 is.
- 24:48Yeah.
- 24:48But you'll get that exactly, square root of 2.
- 24:52And so there's all sorts of things that you can do with symbolic mathematics.
- 24:57People may be familiar with other similar tools called, it's basically, it's called the computer algebra system.
- 25:07There's proprietary tools like Maple and Mathematica.
- 25:10There are some other open source systems that people may have used as well, like Sage and Maxima.
- 25:18So just, if you've ever heard of any of those, Senpai sort of occupies a similar space.
- 25:23But Senpai, I guess, sort of stands apart from those in that it's open source and it's also, it's a Python library.
- 25:30So if you're familiar at all with Python, it's really easy to start using Senpai because it's just Python.
- 25:37Yeah.
- 25:38I think people who have not seen this kind of stuff in action should definitely go poke around and check it out.
- 25:45And we'll talk about some ways in which you can play with it just online with super low effort.
- 25:49I remember the first time I had been previously using MATLAB or something like that.
- 25:56And I saw Maple, which is one of the ones you called out, which is a symbolic sort of solving thing.
- 26:02You can say, oh, here, I want to do the integral from negative infinity to positive infinity of this thing.
- 26:07And it looks like it would come out of the textbook and it will sometimes even show you the steps.
- 26:12It's unbelievable.
- 26:13And that was 20 years ago when I saw it.
- 26:15I'm like, that's unbelievable that this computer is doing this.
- 26:18And Senpai is like that, but for Python, which is fantastic.
- 26:22Yeah.
- 26:22So it's, you know, it's basically, you know, it's the math that you would learn in a classroom.
- 26:28Except it, you know, it can also get very advanced.
- 26:31Like you said, we can do integrals.
- 26:34We have lots of very advanced mathematical features.
- 26:37We can do, we have a module for doing various areas of physics, for example.
- 26:44Nice.
- 26:45And it's also the foundation of many other things as well.
- 26:49Just as a, like setting the stage, a preview is you've got algebra, you've got calculus, you've got differential equations, you've got linear algebra and matrices.
- 27:00It's there's a lot of, a lot of good stuff in here.
- 27:01Andrei, do you want to maybe give us a little run through of like sort of the selling points of Senpai?
- 27:08Yeah, that's very easy.
- 27:10I was an undergrad student and, you know, studied physics or any kind of engineering a lot of times, even math.
- 27:16You have to do a lot of symbolic, you know, I would say derivations on paper.
- 27:21And I remember sitting in a physics class and I thought, you know, this, I would like to do this from Python, interactively.
- 27:29And so that's the idea.
- 27:30And then of course I sat down and I thought, well, let's get started.
- 27:34And my friends were telling me, you know, what, you know, can it do Gaussian integrals?
- 27:38You know, it's this integral of exponential, some polynomial, you know, X squared, for example.
- 27:42And I said, you know, it cannot because, but it will in five years, just not now.
- 27:48That's the hard problem.
- 27:51Oh, and I said, well, then use Mathematica.
- 27:53It could do it now.
- 27:54But, but they asked me, well, why don't you use Mathematica?
- 27:58You know, and I said, you know, Mathematica is great.
- 28:00Maple as well.
- 28:02It's absolutely, as you said, Michael, absolutely amazing that, you know, 20 years ago, and it could do all these integrals and, and serious expansion of these functions.
- 28:10Yeah, I was using Windows NT when I did that.
- 28:13I like how old that was.
- 28:14I did too.
- 28:14I did too, actually.
- 28:15And so, yeah, and, but, but the motivation is,
- 28:21to be able to do this from Python interactively allows you to kind of almost program with the symbolics using a language.
- 28:27That's a pretty decent language that you might already use for something else.
- 28:32And so what I wanted to do is assign the expression to a variable, then use the variable interactively and write some scripts to kind of handle my, my calculation and maybe save it to a file after that.
- 28:43And, and so on.
- 28:44And so that's, once you want to program with that, Mathematica, it allows you to program, but you have to learn.
- 28:50Yeah.
- 28:51And so that's why I wanted to have that in Python.
- 28:53Yeah.
- 28:53And so that's why I wanted to have that in Python.
- 28:57Well, the other thing that from this perspective, sitting on the Python or the comp size side of things and looking back is obvious.
- 29:07I don't know that it is as obvious coming from the other perspective of like, oh, I'm doing a MATLAB.m file or whatever.
- 29:13But in that world, if you work in MATLAB or Mathematica, you can do what MATLAB or Mathematica does.
- 29:20Right.
- 29:21That's a, it's really amazing in its specialty, but it's very specialized and limited.
- 29:26Right.
- 29:26You can't build like, you know, FastAPI and database driven apps that are doing some of this kind of computation along the way.
- 29:33right, if you move it to Python, like this is now, this capability is now accessible for all the applications, not just within, you know, the MATLAB or Maple environment.
- 29:46Yeah.
- 29:47And I guess it's an argument, you know, what's the advantage of Python, let's say for numerical computing, let's use it by or scientific, let's say for scientific computing, but the advantage of Python over, let's say Mathematica.
- 29:58And I think it's not a clear good answer.
- 30:00I think Mathematica is great.
- 30:01And they've done actually, it's quite amazing how good job they've done because they designed the language, I believe in the early eighties and they did not have to really modify the language too much.
- 30:10it was same spirit.
- 30:11So that's kind of amazing.
- 30:12It's much older than Python.
- 30:14So it's quite amazing how far, and then it still works pretty well and it's a decent language.
- 30:19But I think what I like about Python for scientific computing, you know, is that it's open source and it's the language just seems more kind of robust and kind of easier, more imperative.
- 30:29Don't have to really learn too much.
- 30:30It seems that anybody can learn it quickly and doesn't seem to have too many kind of things that would surprise you.
- 30:37Yeah.
- 30:38Very cool.
- 30:39So how long ago did you start on Senpai?
- 30:42I think I started in 2005 or six.
- 30:45Nice.
- 30:46It's been going for a while.
- 30:47Yes.
- 30:48So that really predates a lot of the scientific computing frameworks that are well known today, right?
- 30:54So you must have felt good about picking the right horse as you saw, you know, Jupyter or IPython at the time come along and those libraries like that.
- 31:04Way before Jupyter, IPython was there.
- 31:07Somebody told me about it and I tested it out and I thought, you know, this is pretty cool.
- 31:11It looks like Mathmerica.
- 31:12Then I later, when I talked to Fernando Perez who wrote it, he said, yeah, it's Mathmerica.
- 31:16That's what I wanted to have in Python.
- 31:17That's what I wanted to have in Python.
- 31:18So kind of the same motivation almost.
- 31:20Just, you know, so he kind of provided the foundation for the interactive part, you know, with the prompt and the numbers.
- 31:28This portion of Talk Python To Me is brought to you by Sentry.
- 31:32How would you like to remove a little stress from your life?
- 31:34Do you worry that users may be encountering errors, slowdowns or crashes with your app right now?
- 31:40Would you even know it until they sent you that support email?
- 31:43How much better would it be to have the error or performance details immediately sent to you, including the call stack and values of local variables and the active user recorded in the report?
- 31:54With Sentry, this is not only possible, it's simple.
- 31:57In fact, we use Sentry on all the Talk Python web properties.
- 32:00We've actually fixed a bug triggered by a user and had the upgrade ready to roll out as we got the support email.
- 32:07That was a great email to write back.
- 32:09Hey, we already saw your error and have already rolled out the fix.
- 32:12Imagine their surprise.
- 32:14Surprise and delight your users.
- 32:16Create your Sentry account at talkpython.fm/sentry.
- 32:20And if you sign up with the code talkpython, all one word, it's good for two free months of Sentry's business.
- 32:26This is a great idea of Sentry's business plan, which will give you up to 20 times as many monthly events as well as other features.
- 32:32Create better software, delight your users and support the podcast.
- 32:36Visit talkpython.fm/sentry and use the coupon code talkpython.
- 32:41When you work with Senpai, we'll see some examples and talk about them in a minute.
- 32:47You can put it into this pretty printing mode where it attempts with as much as it can within the terminal output to have more formal mathematical representation.
- 32:59So instead of like E^x, it might put a little smaller x up high and then an E down slightly to the lower left of it like you would try to write it.
- 33:09It seems that if it's in a notebook, it should be able to do even better, like maybe a LaTeX rendering or something really shiny.
- 33:16Yeah, it does.
- 33:17So in the notebook, it'll use the MathJax.
- 33:19So it'll, it'll, yeah, it'll, it'll just use, it'll look, it'll look great basically.
- 33:24Cause it, the MathJax looks just like, it's the same as a Logitech document.
- 33:28Yeah.
- 33:29Yeah.
- 33:30Oh man.
- 33:31Yeah.
- 33:32And in the terminal we use, we use, we use all these different Unicode characters to, to sort of render it as 2D text in the terminal so that it looks, it looks pretty decent in the terminal considering the constraints of a terminal.
- 33:46Nice.
- 33:47And I personally use it.
- 33:49I mainly use it in the terminal.
- 33:51I saw it.
- 33:52Yeah.
- 33:53So let's go and maybe, maybe we could walk through a little bit on one of the tutorials here.
- 33:57I mean, obviously we don't want to talk too much code or whatever, but I do think maybe just touching on this, will give people a sense of what it's like to work with this because.
- 34:09Yeah.
- 34:10So I think.
- 34:11Yeah.
- 34:12Yeah.
- 34:13Give it, give us a sense of what.
- 34:14In the tutorial, there's a, there's actually a section somewhere.
- 34:16where is that section?
- 34:19This one perhaps.
- 34:20But didn't get deleted.
- 34:22That's, it's in the introduction page.
- 34:25Yeah, it's on that page.
- 34:26But, if you maybe, scroll down, to this, the power of symbolic computation is the name of the header.
- 34:33There you go.
- 34:34So this is just, this is just sort of a, a bucket list of, just sort of a, a bunch of different examples of, of things that can do.
- 34:41So just to give you an idea and actually you can see there.
- 34:45also you can also see the pretty printing there as well, that Unicode pretty printing, that it does.
- 34:51that law tag is what we would see in the notebook.
- 34:54So you have to run underscore printing using Unicode equals true, right?
- 34:57It, it doesn't do it by default.
- 34:59it, it, yeah.
- 35:00So that in, in the notebook, it does actually do it by default.
- 35:03Sure.
- 35:04But not in the terminal or the rebel.
- 35:05Yeah.
- 35:06In the terminal, you need to, you need to run this.
- 35:08Otherwise it'll just print out like a one dimensional string that, that, you would get.
- 35:13it would, it looks sort of like what you would input.
- 35:16but yeah, you can see here, we can, it can take derivatives, take integrals.
- 35:21if you keep scrolling down, you can see, I think limits and, differential equations.
- 35:28you can solve.
- 35:30Yeah.
- 35:31So let me, let me talk people through just a couple of things, because I think.
- 35:34it's a little hard to understand how this might work.
- 35:37And the trick, I think really Andre, you tell me your thoughts on this is the ability
- 35:45for it to, define these symbols and these functions, here.
- 35:50Yeah.
- 35:51So the trick able to represent the expression symbolically.
- 35:54So you type, X plus Y, and plus some function sine X.
- 35:59And in, in memory, it represents it as a tree and it, and it, of course, the expression in symbolic
- 36:04form.
- 36:05And then it allows, it has all kinds of functionality that allows you to manipulate the expression,
- 36:09print it, you know, differentiate it.
- 36:11So many differentiate, or let's say an expression that takes the original expression and creates
- 36:15a new expression, the derivative.
- 36:17Right.
- 36:18With normal math, like pen and paper math, you don't usually define variables every now and
- 36:24then you say, you know, let X be an element of like the real L, real numbers such that,
- 36:28whatever.
- 36:29Normally you just say F of X or F of T or something like that, or Y.
- 36:34And it just works.
- 36:35You needed some way in Python to allow you to write expressions like X plus two Y without Python
- 36:44going, I have no idea what that means.
- 36:45Right.
- 36:46Yes, that's right.
- 36:47So, and yes, you first have to declare what X and Y is.
- 36:50You declare it as a symbol and then you can use it.
- 36:52We override the Python operation like plus, minus, and then you can construct the expression.
- 36:58And so what's nice about Python is that when I started with simple, I mean, I was kind of
- 37:02investigating the idea.
- 37:03What's nice that Python pretty much allows you to override all these operations.
- 37:07And so you can write it almost like math, you, in exponential, two stars.
- 37:12Yeah.
- 37:13Only caveat is that if you write, let's see one divided by two, and we started in Python
- 37:18two, that gives you a zero, I believe.
- 37:20And that's very confusing from Synthy's perspective, because you would like it, you would like it
- 37:24to give, the expression one over two, not, not zero.
- 37:28And because, and so I don't think there is any way still to, to, to fix that, to override
- 37:34this division.
- 37:35Yeah.
- 37:36At least in Python three, you get the right, at least you can load.
- 37:38I, what you get, I float double precision.
- 37:41Yeah.
- 37:42Yeah.
- 37:43You don't want that.
- 37:44You want one divided by two as simple expression.
- 37:46So, so when you have something like that, you have to wrap the number one into a teacher
- 37:51S.
- 37:52Yeah.
- 37:53Or, define some kind of symbol with that value and then use it like, like you have
- 37:57to go over B from, you know, quadratic equation.
- 38:01Right.
- 38:02You could like that kind of thing.
- 38:03but yeah, so you create these symbols and then you can write simple math that looks
- 38:09like it's just straight Python.
- 38:10So you could say X and Y are symbols.
- 38:13And then you could say something like X plus two times Y plus one.
- 38:18And that, that isn't not evaluated in any way.
- 38:22It's just becomes this, like, as you said, as tree, this expression that then you can operate
- 38:26on, like integrate it, take the derivative of it, find the limit of it, all those kinds of
- 38:31things.
- 38:32Right.
- 38:32So, yeah, that's the basic idea.
- 38:33Yeah.
- 38:34Yeah.
- 38:35That's the basic idea.
- 38:36and, and you can use it to do any, basically any sort of math that you would normally do,
- 38:42uh, with, with so that sort of expression, except you're doing it on the computer.
- 38:46So cool paper.
- 38:47You can do, you know, just like a real simple example might be, rational polynomials
- 38:52or something like that.
- 38:53Like if you had X squared minus one is one equation and then X plus one as another equation,
- 39:00you could say the first equation divided by a second and get, geez, which did I say minus
- 39:05that you get X minus one as simplifications.
- 39:08Another one where you can take something and it'll, it can figure out how to simplify it to
- 39:13an equivalent form.
- 39:14That's like, like dividing out factors and things like that.
- 39:18Yeah.
- 39:19Okay.
- 39:20These are, are super neat.
- 39:22you can do factors.
- 39:23So like you say, you could say like, polynomial factors, you know, one of the things I don't
- 39:28know if I should point this out, or maybe this should have been a warning to the whole show.
- 39:33If you're in high school or early college, this might be a terrible cheat sheet for homework
- 39:41and other types of is fantastic, right?
- 39:44If you're in an algebra course, you can do, do your work and then you could type this in and
- 39:49type this in and go, did I get it right?
- 39:51Wham.
- 39:52And out it comes, especially in a notebook.
- 39:53Right.
- 39:54Yep.
- 39:55Yeah.
- 39:56I mean, I think that's kind of unavoidable these days.
- 39:58you know, there's cause I mean, there's, there's also, you know, just graphing calculators
- 40:05that do this as well from alpha.
- 40:07I think simple is a great, a great way to do that if you're going to do it because now
- 40:11you're also learning Python.
- 40:12Right.
- 40:13Which I think is, would be great for someone who's like a high school student or an undergraduate
- 40:19because that, that's an actual useful skill as well.
- 40:22It might be even more, utilitarian in the long run than the calculus they're learning.
- 40:29so yeah, you can do like, derivatives and integrals of these equations.
- 40:35So you could say integrate E to the X times sine of X plus E to the X times cosine of X.
- 40:41And it will give you the result, which apparently is E to the X sine of X.
- 40:45I used to be able to do that.
- 40:46I can't anymore.
- 40:47Well, you can see that the line above it, it's differentiating that.
- 40:51So it's just undoing the line above it.
- 40:53Oh, I see.
- 40:53Well, that, that does, that is a bit of a cheat, isn't it?
- 40:56Yeah.
- 40:57But yeah, I mean that that's, yeah, you don't, that it, the integration is nice.
- 41:01Cause you don't have to remember all the, these, how to do all this integration by parts
- 41:05or whatever you would do.
- 41:06Yeah, exactly.
- 41:07So another thing it can do is, like concrete.
- 41:10I forgot.
- 41:11There's like a better term for that, but definitely integration over a range or, or so.
- 41:16So you could say integrate from like negative 10 to 20 of sine of X squared, or here's the
- 41:22example of negative infinity to positive infinity.
- 41:25And it says that square root of two, you know, radical two, radical pi over two as the answer,
- 41:31not like point.
- 41:32What is that point to something, something, something, something, something, I don't know,
- 41:37something like that.
- 41:38right.
- 41:39It gives you the actual theoretical answer, the exact mathematical answer in symbols, which
- 41:44is amazing.
- 41:45It is, it's amazing.
- 41:46And it's also not easy to.
- 41:48So when I started in SIFI, I knew this is not easy to program.
- 41:51Like how, how do you program such an integrator?
- 41:53It's not easy.
- 41:54But I figured, you know, if I can at least figure, at least get the basics, you know, like
- 41:58be able to represent these equations at least, that would still be a huge help.
- 42:02Cause sometimes the expression can be long and be able to manipulate it.
- 42:05And then as we were joined with, you know, so many, excellent people, they, they contributed
- 42:10all the, all the features students, mostly actually it was developed by Google summer of
- 42:14cold students.
- 42:15And okay.
- 42:16Yeah.
- 42:17Cool.
- 42:18It's, it, it, it, I would not be able to do that.
- 42:21Let's just put it that way.
- 42:22It's a, it's a lot of work that's behind it.
- 42:24Yeah.
- 42:25I'm looking at this.
- 42:27I know these concepts just on pure math are hard and then to automate it in general, it's
- 42:32got to be next level.
- 42:33so other things you can do is like, you can define a function.
- 42:36So here you say, here's a function called Y, and then you can say, well, what if you had
- 42:41the differential equation that was, you know, the second derivative of Y minus Y equals E
- 42:46to the T, right?
- 42:48Solve that.
- 42:49And then sure enough, outcomes, the solution.
- 42:53So that's a function for that.
- 42:55what the function is, which is pretty amazing.
- 42:58Yeah.
- 42:59So does things like, eigen values from, linear algebra and all kinds of stuff.
- 43:06yeah.
- 43:07Great.
- 43:08And then we have in Python, we have stir and repper.
- 43:12You guys also have late tech, which is pretty cool.
- 43:16Right.
- 43:17I just call latex, latex on a function and out it comes with like the crazy escape code.
- 43:22Latex that you would need for a paper or something.
- 43:25Right.
- 43:26Yep.
- 43:27Yeah.
- 43:28And this, this actually, I mean, this is only even scratching the surface of, of the, what
- 43:32Simpa can do.
- 43:33So it's actually quite a large library.
- 43:37Yeah.
- 43:38I don't want to focus too much on just the pure math, but maybe give us a sense of like some
- 43:42of the things that amaze you.
- 43:43What do you have in mind when you're saying that kind of stuff?
- 43:45that amaze me.
- 43:46Yeah.
- 43:47I mean, I'm, I think the whole thing's amazing still, even though I know how it works
- 43:53and I've been working on it for awhile, but yeah, just the fact that the computer can do,
- 43:57uh, the same sort of mathematics that you would do, on a piece of paper is, is incredible.
- 44:03some of the algorithms that to do like integration and stuff are also, pretty interesting
- 44:09because, they, they sort of, they go beyond what you would learn in a calculus class.
- 44:14there, there are algorithms that, that can compute integrals that you would, you would
- 44:18never be able to know how to compute them just using what you'd learn in a, in a calc one
- 44:23or calc two.
- 44:24Sure.
- 44:25but those, those are something that you can actually implement in something like Simpi
- 44:29and then, Simpi will be able to do those integrals for you.
- 44:33some of the features that, I mean, if you go on the website, there's a, there's a
- 44:39huge list of features.
- 44:40I think, one feature that we didn't mention that's, that's maybe worth mentioning,
- 44:46um, is, code generation.
- 44:49So, code generation, means that you take one of these expressions and you turn it
- 44:56into basically code for another language, whether that's C or Fortran or, pretty much any,
- 45:05any language that you would want to, translate it into.
- 45:09And then you can then take that expression and, and, basically convert it into, something
- 45:15that you could numerically evaluate, very fast in, in whatever language you prefer
- 45:19to use.
- 45:20Wow.
- 45:21Okay.
- 45:22So you've got your notebook written in Python or something and you've come up with some expressions
- 45:25and you're like, I would like to just do that in Fortran or C and I, and I don't trust myself
- 45:31to get it right.
- 45:32myself to get it right.
- 45:33So just print out the syntax basically.
- 45:36Yeah.
- 45:37Yeah.
- 45:38Oh, go ahead, Andre.
- 45:39Yeah.
- 45:40Well, let's say you want to approximate some function, you know, with a polynomial.
- 45:43So you, you can use him by to do that.
- 45:45And then at the end, you just want the polynomial in the form that you can put in your production
- 45:49code, whether for Tron or C.
- 45:50So it's still simple to do it.
- 45:52Then you can copy and paste it.
- 45:53You know, it's correct.
- 45:54Assuming there is no back in simple.
- 45:55Yeah, sure.
- 45:56Yeah.
- 45:57But I suspect it's probably pretty straightforward.
- 46:01Once you've got the expression to break it down into, you know, this is a multiply.
- 46:06Here's where the parentheses that group it in the expression tree.
- 46:09But yeah, there's, I mean, there's, there's a few places where it's like, okay, we need
- 46:14to make sure we get the semantics.
- 46:16The semantics might be a little different between like C and, and Python or something like that.
- 46:22Yeah.
- 46:23So we need to make sure we sort of translate that correctly.
- 46:25But yeah, it's, I mean, in principle, it's not that hard, but I mean, it's not that hard.
- 46:30I mean, it's also not something you want to do by hand either because.
- 46:34Yeah.
- 46:35It's super error prone if you were to try to do it by hand.
- 46:37Yeah.
- 46:38Yeah.
- 46:39It sounds like a great feature.
- 46:40That's, that's quite neat.
- 46:42Maybe we could talk just a little bit internally, Andre, about how you accomplish this.
- 46:48Yeah.
- 46:49Like how I started it.
- 46:50Well, just like, how does it work?
- 46:51If I give it a, if I give it a, an expression of, you know, e to the x cosine x, and I say integrate
- 46:59that, like, where do you even start?
- 47:01Yeah, exactly.
- 47:02Well, the integration, I guess, I don't know, those more details, there's like a couple different
- 47:06algorithms, but I'll tell you kind of the basic idea.
- 47:09When I did, you know, the first thing you do in your search, how do you do symbolic integration?
- 47:14And then there is this famous algorithm called the RISH algorithm.
- 47:18So you kind of start studying and then you realize, oh, that's like multiple years of
- 47:22worth of work to implement this.
- 47:24And it's kind of fragile and doesn't always work.
- 47:27And Mathematica, they, as the company, and Perkis Wolfram, they, they managed to do that.
- 47:33That's a lot of effort, a lot of work.
- 47:35And they had to invest in a large teams of people, test it carefully and so on.
- 47:38But in practice, there are other approaches.
- 47:43And it turns out that you can integrate a lot, you know, wide classes of functions just
- 47:49by implementing an algorithm that works.
- 47:51For example, for a polynomial, it's easy.
- 47:53Anybody, anybody can do it, right?
- 47:55And then you can add some trigonometric functions.
- 47:57So you kind of implement algorithm for that.
- 47:59Then you kind of built your way up.
- 48:01It turns out when you do that, you can make it very useful in practice.
- 48:05Maybe it's not as powerful as Mathematica is, but it's very helpful.
- 48:10So that's how we started.
- 48:11And then once you have the rules, I mean, first, second, third year of calculus, there's
- 48:19in math, I, there is a ton of creativity in the calculus classes.
- 48:23It's a lot of like, I just today we're doing integration by parts today.
- 48:27We're doing like integration of stuff that's divided or what, you know, like there's a,
- 48:31there's set algorithms that they tell you to follow.
- 48:33And once you get the basics, it seems like, I guess you could compose them.
- 48:36Yeah.
- 48:37So that was kind of my initial idea.
- 48:38Let's just kind of do stuff that we know how to do.
- 48:41And it's kind of straightforward.
- 48:43But, but then kind of the engineering part comes to it as well, because you don't want,
- 48:49you want things to always work quickly.
- 48:51And if it cannot be done, you would like to know the answer.
- 48:53This cannot be done quickly, not to wait a couple hours.
- 48:56until it tries all the functions.
- 48:58Yeah.
- 48:59And so then it just becomes a lot of, I would say engineering decisions, how to make this
- 49:04all work.
- 49:05And I think we are still not done yet.
- 49:07I think there are a couple of, I would say promising approaches.
- 49:11one approach is a certain professor, I think, or a professor, I think in Canada, he has,
- 49:20he figured out how to take all these rules, how to integrate functions and compose them into
- 49:23just a decision tree.
- 49:24So you give it an expression and then it just figures out exactly as you said, is it the polynomial?
- 49:29Is it like dividing?
- 49:30Is it like dividing?
- 49:31And so on.
- 49:32And then the decision tree tells it exactly how to go about it to give you the final formula,
- 49:37the integral.
- 49:38Okay.
- 49:39And here's the small formula you apply to it or whatever.
- 49:41So you take out these 5,000 formulas and then he takes them and actually he uses Mathematica
- 49:47and he composes those into a decision tree and then it prints the decision tree as for example,
- 49:51Python code and so on.
- 49:52And we still haven't, well actually, so we have a module for that.
- 49:56And it's something that, what I like about this approach is that it's quick.
- 49:59In principle, it's quick.
- 50:00You just go through the decision tree and it gives you a formula or it says it cannot be there.
- 50:04And I would like, but you know, there are all kinds of practical issues.
- 50:09The tree is pretty quick.
- 50:10And so when you encode it in Python, it takes forever to import and so on.
- 50:14So we are still kind of struggling with these issues, but it just gives you an idea what kind of issues are,
- 50:18engineering, I would say issues are involved in.
- 50:20Yeah, that's pretty wild.
- 50:22I mean, have you thought of or are you using things like Cython or these other types of speed up type things?
- 50:29Yes.
- 50:30Yeah, I spent, so maybe 10 years ago, I spent a lot of time investigating how to make Simpy faster.
- 50:35Simpy is great, it's pure Python, it's easy to contribute to, and for many things it's amazing, just works and that's all you need.
- 50:42But sometimes you need either larger calculation or a lot of calculations and it's not fast enough.
- 50:48So how to speed it up?
- 50:49So we investigated just pure C, Cython, all kinds of tricks in Python itself to speed it up.
- 50:56Eventually, that kind of led to the SimEngine project.
- 50:59I decided, let's just use C++.
- 51:01It's the only tool that I know that actually can deliver the performance and kind of maintaining a bigger project.
- 51:08So that's SimEngine and then we have Python wrappers and then Simpy can use it.
- 51:14So there are modules in Simpy such as the dynamic, kind of dynamic, what do I say?
- 51:20The classical, mechanical, I guess, module that allows you to simulate a solid body, you know, physics.
- 51:26It's able to use SimEngine out of the hood so it gets much, much faster.
- 51:31Okay, interesting.
- 51:32Yeah, so this is a C++ project, but it has Python wrappers for easy Python integration with Simpy and other things.
- 51:40Yeah, that's cool.
- 51:41You know, I think that kind of summarizes a lot of scientific Python is there's a really great API.
- 51:49And when you ask it to do things, it takes the data and hands it off to some C layer that actually does it for Rust or something like that, right?
- 51:56Yes.
- 51:57And it's not just writing it in C++.
- 52:00There are other libraries that also in C++, but to make it really fast, one has to spend the time and figure out how to represent the expression in memory,
- 52:09how to allocate the memory, how to, so we have to make, you know, reference content pointers, for example.
- 52:16So that's what, that's the design I chose.
- 52:18How to make the reference content pointer as fast as possible, things like that.
- 52:23How to make sure that you can actually maintain it so that it gives you, if you make a mistake, it doesn't give you a segfold, but some kind of a, in debug mode, some kind of ways to debug it, right?
- 52:32So there's all kinds of engineering, you know, decision behind the scenes.
- 52:37I asked it to solve this and it went poof and went away.
- 52:39I guess that means something.
- 52:40I don't know.
- 52:41Yep.
- 52:42So we don't want that.
- 52:43But anyway, but yes, so the symmetry works.
- 52:45It's, it, it contains kind of like the core level of the core of Simpy.
- 52:50Is this what I would, if I go pip install Simpy, do I get this or do I have to do something special to like swap out the computation engine with this?
- 52:59You have to install it as a separate library.
- 53:01So if you do just pip install Simpy by default, it only installs Simpy, but, but you can do pip, I think you can do pip install Sim engine.
- 53:08I use Coda.
- 53:09So you can do Coda install Sim engine, that install Sim engine as a, as a library.
- 53:13And in Python, you do import Sim engine, and then you use it kind of like Simpy.
- 53:17And Simpy can also optionally use it.
- 53:20Got one to build.
- 53:21Okay.
- 53:22Yeah, this looks really neat.
- 53:24one of the things I wanted to touch on here, and at the top right of the page, if I go just to the Simpy page, it says try it with Simpy gamma.
- 53:34And so maybe we could sort of close out the conversation a bit with just poking around with this and then maybe highlighting some projects that use it.
- 53:42So for example, it says, oh, you could just try some random example.
- 53:45And it's got like algebra, algebraic things I could do.
- 53:49So it says, you know, polynomial divisor, I'm coming in and copy this.
- 53:53And if I go, there's a little calculator like thing at the top where I go hit equals, and it will go and run that in,
- 54:01The polynomial division in this case, it looks like.
- 54:03Yeah, yeah.
- 54:04Like what are the divisors of, of this thing, right?
- 54:06Something like that.
- 54:07Yeah.
- 54:08So this is sort of our, our little version of, of Wolfram alpha, but using Simpy under the hood.
- 54:13it's not quite as powerful as Wolfram alpha.
- 54:16We don't support like, natural language input.
- 54:20that sort of thing.
- 54:21But, yeah, you can, you can kind of get an idea of sorts of things Simpy can do here.
- 54:27maybe one of the interesting ones.
- 54:28If you scroll down a little bit, and click on one of the, integral, the one that says get steps for integrals.
- 54:35Yeah.
- 54:36How about this?
- 54:37I, I'm a big fan of, E.
- 54:39There's a whole book called E the story of a number.
- 54:41I have you guys read it.
- 54:42no, I haven't heard of it.
- 54:43You haven't E, E the story of a number is a great book.
- 54:45It's about like Euler and all that stuff.
- 54:47Okay.
- 54:48Yeah.
- 54:49So if this was just integrate E to the X over one plus E to the two X, right?
- 54:54Yeah.
- 54:55So if you scroll down here, hopefully, should see something that says, see steps.
- 55:03yeah.
- 55:04I see under the derivative, for example.
- 55:06Yeah.
- 55:07That was pretty interesting.
- 55:08I don't know if this is the one that has steps, but I saw it in other places.
- 55:11yeah, maybe that's, maybe this example is not working right now.
- 55:15Yeah.
- 55:16Yeah.
- 55:17There's some, here, let's do, probably the differentiation of something might be, I bet it's got some steps for that, but that was part of what I was talking about.
- 55:23I don't know what I was talking about with the cheat and not, and I don't mean that in a negative way, but like, it will show you the steps as if it was written out in a textbook, of how like this one right here, like, look at this.
- 55:36You're going to apply the quotient rule, which is the X of F of X over G of X equals this.
- 55:41And then you're going to apply this.
- 55:42And I mean, this is amazing.
- 55:45Yeah.
- 55:46Well, I mean, I think, I think it's, you know, it's instructive.
- 55:50It's not, it's not just cheating.
- 55:52It's not just cheating.
- 55:53No, of course it's not.
- 55:54And I do think it's really instructive because it's one thing to just go take a function or an expression and say, take the derivative of this.
- 56:03And it says, great.
- 56:04It's this crazy thing.
- 56:06Like, how was I supposed to know?
- 56:07It was like the tan of the cotangent of X or like something like that.
- 56:11Right.
- 56:12but if it says, you know, like, look, here's how we got there.
- 56:15That's yeah.
- 56:16That's very educational.
- 56:17Yeah.
- 56:18So it's not, this, this, but many parts of Senpai don't work like this because, sort of the underlying algorithms don't, don't really work the same way that you would do it by hand.
- 56:29But there are some that basically work the same way you would do it by hand, like differentiation as an example.
- 56:35So we can, we can sort of extract the steps and show them to you.
- 56:39Yeah, this is great.
- 56:40I'll put this, this explanation example into the show notes so people can, come back and check it out.
- 56:46But gamma.senpai.org and then just choose some of the examples is really how to do it.
- 56:50And it's how it gets there.
- 56:51So very nice.
- 56:53So let's, let's wrap this up by going to senpai.org.
- 56:59And then on senpai.org, if you go just down a little bit, there's projects using Senpai that are, they all sound super interesting to me.
- 57:08You've got like chempy, a package for chemistry in Python, Einstein pi for symbolic numeric, for general relativity.
- 57:16Are there any of these that you think are particularly neat or stand out to you?
- 57:19You want to give a shout out to either of you?
- 57:21Oh yeah.
- 57:22I, all of them, but I like the pi guy, for example, it's a, it's a very nice package for, you can, can.
- 57:27Pi dies and dynamics.
- 57:29Yeah.
- 57:30So you can, it allows you to simulate, you get kind of like a robot or multiple, you know,
- 57:37a pendulum, like double pendulum.
- 57:38You just kind of tell it exactly the relations of the body and then it allows you to write.
- 57:43And then pi die can derive equations of motion.
- 57:45And those equations get extremely complicated, real quick.
- 57:49for triple pendulum, which just gets very complicated.
- 57:52Pi die can derive the equations for you, correctly.
- 57:56And so, it's, it's a, it's a certainly not neat project.
- 57:59Yeah.
- 58:00That one's really cool.
- 58:01So you can see the chem pie kind of stands out for me.
- 58:03Cause I've, I've always loved chemistry.
- 58:05Yeah.
- 58:06Any others that you want to give a shout out to?
- 58:08well, I guess one, one that I'll mention here, actually it's listed as a sage.
- 58:13So sage, sage is something that a lot of people might know as sort of an alternative, computer algebra system.
- 58:20Yeah.
- 58:21I've actually had a William Stein on the show to talk about it as well from a sage math and,
- 58:27yeah.
- 58:28So sage, sage sort of integrates a lot of libraries, including Senpai itself.
- 58:31You can see Senpai is listed there on the, on the front page of their website.
- 58:34Yep.
- 58:35so, if you use sage, there's, there's a chance that you're actually just using Senpai under the hood.
- 58:42so that, yeah, it's, that's, that's one that I like.
- 58:48a lot of these are, are, are making use of that, the code generation, concept that I talked about where they're, they're basically sort of modeling whatever chemistry or dynamics and in Senpai.
- 59:00And then, then taking that expression and turning it into something that they can numerically evaluate.
- 59:07Yeah.
- 59:08It looks like maybe some quantum computing stuff here.
- 59:11All right, great.
- 59:12Now these are all cool.
- 59:13And I think probably seeing some uses of it, like give people ideas for their own projects.
- 59:19Let's wrap this up, this, conversation up just, to talk about maybe bridging the gap or bridging the world of computational stuff, symbolic stuff, and then, this numerical side, right?
- 59:34So I've gone through and I've said, here's some complex function and maybe here's another one.
- 59:39And I'm going to combine them through whatever in some, some way, and then now generate me a second derivative or whatever it is I need or integrate it.
- 59:48I've now got a new expression.
- 59:50Can I somehow take that and turn it into something I can call as a Python function?
- 59:56Like here's a number of what's the answer effectively, or here's a numpy array.
- 1:00:00Give me the evaluation across the numpy array.
- 1:00:02Yeah.
- 1:00:03So there's, there's a function called lambda phi.
- 1:00:05it's a L A M B D A F Y, which basically does exactly what you said.
- 1:00:12It takes an expression and it turns it into a function that you can evaluate with numpy.
- 1:00:19so, yeah, that, that workflow is basically exactly what you describe.
- 1:00:24You, you, you, you simply derive whatever it is you're doing using the mathematics of,
- 1:00:30of your problem.
- 1:00:31And then you, you put that inside of lambda phi, and now you have something that you can actually
- 1:00:35pass, your data to with numpy, and, evaluate it numerically.
- 1:00:42Nice.
- 1:00:43That's awesome.
- 1:00:44Does it effectively use that, that code generation thing you were talking about?
- 1:00:48Yeah.
- 1:00:49So under the hood, it's, it's using, it's basically generating code for a language equals.
- 1:00:55Okay.
- 1:00:56Yeah.
- 1:00:57Yeah.
- 1:00:58Numpy itself is sort of a language.
- 1:01:00It's got its own set of functions.
- 1:01:02and right, right.
- 1:01:03Instead of looping, you'll, you do sort of vector operations and things like that, which you'd
- 1:01:09want it to do that.
- 1:01:10Yeah.
- 1:01:11Yeah.
- 1:01:12there are other functions, which you can do to, to, instead of calling numpy, if
- 1:01:16you want to like call it through C as well, there, there's options for that as well.
- 1:01:20but yeah, that, that lambda phi is sort of the big one that, that you need to know
- 1:01:25if you want to do that.
- 1:01:26Okay.
- 1:01:27Cause to me, that seems like a really important bridge.
- 1:01:30You know, I've got this idea.
- 1:01:32I here's my theoretical equations, but at some point I want to evaluate it and make, make the
- 1:01:38computer, you know, generate a graph based on these numbers or something.
- 1:01:43Right.
- 1:01:44Yeah, definitely.
- 1:01:45Cool.
- 1:01:46Good work on this project.
- 1:01:47You guys, it, it's pretty amazing.
- 1:01:49And, and the explanation, thing, Aaron, you had me pull out.
- 1:01:55That's really cool.
- 1:01:57How much it will say, these are the algorithms we applied and, and here's the little lesson
- 1:02:01to take from, like why this makes sense here and so on that.
- 1:02:05That's a way better than just some sort of numerical integral.
- 1:02:09Like you gave me this function and it's 7.2 is the answer.
- 1:02:13Like, well, great.
- 1:02:14But what do I do with that?
- 1:02:15Right.
- 1:02:16Yeah.
- 1:02:17Yeah.
- 1:02:18So maybe people, people could use it as a teaching tool.
- 1:02:20Assuming.
- 1:02:21Yeah.
- 1:02:22There are people who use it in the classroom.
- 1:02:23Definitely.
- 1:02:24Sure.
- 1:02:25Yeah.
- 1:02:26Yeah.
- 1:02:27Well, before we're done, let me ask you both the final two questions briefly.
- 1:02:31If you've got some kind of notable package, maybe it's not the most popular thing, but you,
- 1:02:37you know, some Python package that's awesome.
- 1:02:39That needs a shout out.
- 1:02:40There probably are a lot on this page already and you've already given some, but anything you
- 1:02:45want to just, call out real quick.
- 1:02:47well, so perhaps unrelated to Senpai, a little bit, but,
- 1:02:54It can be unrelated, anything.
- 1:02:55Yeah.
- 1:02:56well, I, I, I'd have to say one of my favorite packages that I use, is this
- 1:03:04testing library called hypothesis.
- 1:03:05Yeah.
- 1:03:06I'd actually like to, to make use of it in Senpai.
- 1:03:09We aren't using it right now, but, I'd like to, but, if you haven't used it before
- 1:03:15the, the idea, it's sort of, it sort of flips the idea of, of how you do tests on its
- 1:03:20head a little bit.
- 1:03:21instead of, instead of sort of coming up with a bunch of examples and sort of testing
- 1:03:25that they, your function, you know, does the right thing on, on the, on different inputs,
- 1:03:30you just sort of tell it what your function should do.
- 1:03:32And then it takes care of the job of, of generating examples.
- 1:03:36And the end result is that you, you end up testing a lot more of your code than you would
- 1:03:41with a normal test.
- 1:03:43And you get, you end up with much more robust code as a result.
- 1:03:47So I recommend checking it out.
- 1:03:48It's called hypothesis.
- 1:03:49I don't know if you've had hypothesis on this.
- 1:03:51I have, it's been quite a while.
- 1:03:54It's been three or four years at least, but yeah, it's, it's a very neat project.
- 1:03:58And I'm glad to see it's still going strong.
- 1:04:00I hear a lot about it.
- 1:04:01Andre.
- 1:04:02Oh, I was going to say, do you want a Python package or any package?
- 1:04:06Python.
- 1:04:07If you got one, if not, we can fall back to any.
- 1:04:09yeah, I was going to actually have both of these also.
- 1:04:12I also discovered it kind of actually relatively recently.
- 1:04:15yeah.
- 1:04:17Yeah.
- 1:04:18I recommend people to check it out.
- 1:04:19Which one?
- 1:04:20Sorry.
- 1:04:21Oh, the hypothesis.
- 1:04:22Oh yeah.
- 1:04:23Uh huh.
- 1:04:24Sure.
- 1:04:25And I did talk about it way back in the day.
- 1:04:27This is 2016 had a David McIver on to talk about it.
- 1:04:32So that was cool.
- 1:04:33All right.
- 1:04:34And then final question.
- 1:04:35If you're going to write some code, some Python code, what editor do you use?
- 1:04:40I use Emacs.
- 1:04:42Emacs right on.
- 1:04:43And I use Wim.
- 1:04:44Oh, and you guys still get along.
- 1:04:46Yeah.
- 1:04:47Of course.
- 1:04:48No, just teasing.
- 1:04:49That's awesome.
- 1:04:50All right.
- 1:04:51Well, thanks so much for being here.
- 1:04:53You know, final call to action.
- 1:04:54People are excited to get started with Senpai.
- 1:04:56Like what do they do?
- 1:04:57How do they get started?
- 1:04:58Where do they go?
- 1:04:59well, so the easiest way is, if you go to Senpai.org, like you said, like,
- 1:05:06uh, you said, you can try it online.
- 1:05:08there's also a link there to, to download it.
- 1:05:12yeah.
- 1:05:13Otherwise, you know, I would say, if you're going to go to Senpai.
- 1:05:15you know, you can, if you really want to get dive in, you can join our mailing list,
- 1:05:20uh, or, you can follow us on Twitter.
- 1:05:23We're at Senpai and, yeah, it, the, basically the easiest way to get it.
- 1:05:29Is it just pip install Senpai or condo install Senpai and you'll have it.
- 1:05:33Nice.
- 1:05:34Andre.
- 1:05:35Yeah.
- 1:05:36That's a,
- 1:05:37or simengine.org.
- 1:05:38You can like, you like the plus plus version.
- 1:05:40Nice.
- 1:05:41And you have the tutorials that I highlighted.
- 1:05:42I'll link in the show notes so that you can go through on Senpai.org as well.
- 1:05:46Fantastic.
- 1:05:47Really nice work.
- 1:05:48I love to see you bringing this to all the developers and data scientists and stuff.
- 1:05:53So thanks for being here and sharing Senpai.
- 1:05:56No, thank you for having us.
- 1:05:57You bet.
- 1:05:58Yeah.
- 1:05:58Thank you so much.
- 1:05:59Thank you.
- 1:06:00This has been another episode of Talk Python To Me.
- 1:06:03Thank you to our sponsors.
- 1:06:05Be sure to check out what they're offering.
- 1:06:06It really helps support the show.
- 1:06:08Starting a business is hard.
- 1:06:10Microsoft for startups founders hub provides all founders at any stage with free resources
- 1:06:16and connections to solve startup challenges.
- 1:06:19Apply for free today at talk python.fm/foundershub.
- 1:06:23Take some stress out of your life.
- 1:06:26Get notified immediately about errors and performance issues in your web or mobile applications with
- 1:06:31Sentry.
- 1:06:32Just visit talkpython.fm/sentry and get started for free.
- 1:06:37And be sure to use the promo code talkpython, all one word.
- 1:06:40Want to level up your Python?
- 1:06:42We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:06:46Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:06:51And best of all, there's not a subscription in sight.
- 1:06:54Check it out for yourself at training.talkpython.fm.
- 1:06:57Be sure to subscribe to the show.
- 1:06:59Open your favorite podcast app and search for Python.
- 1:07:02We should be right at the top.
- 1:07:03You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the direct
- 1:07:09RSS feed at /rss on talkpython.fm.
- 1:07:13We're live streaming most of our recordings these days.
- 1:07:16If you want to be part of the show and have your comments featured on the air, be sure
- 1:07:19to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:07:23This is your host, Michael Kennedy.
- 1:07:26Thanks so much for listening.
- 1:07:27I really appreciate it.
- 1:07:28Now get out there and write some Python code.
- 1:07:30We'll see you next time.
- 1:07:31Bye.
- 1:07:31Bye.
- 1:07:31Bye.
- 1:07:32Bye.
- 1:07:32Bye.
- 1:07:33Bye.
- 1:07:33Bye.
- 1:07:34Bye.
- 1:07:35Bye.
- 1:07:36Bye.
- 1:07:37Bye.
- 1:07:38Bye.
- 1:07:39Bye.
- 1:07:40Bye.
- 1:07:41Bye.
- 1:07:42Bye.
- 1:07:43Bye.
- 1:07:44Bye.
- 1:07:45Bye.
- 1:07:45Bye.
- 1:07:46Bye.
- 1:07:47you
- 1:07:47you
- 1:07:48you
- 1:07:48Thank you.
- 1:07:50Thank you.