Part 3 of my programming series about the Python package
swiftex
for writing math in Jupyter as fast as with pencil and paper. For the previous part, follow this link.
The plan for today: enable swiftex
to tag and retrieve subexpressions from complicated expressions like fractions and square roots! At the end of the session, we can do the following in Jupyter.
Fractions and Roots
First of all, let’s allow for defining fractions by giving the numerator and denominator as strings in the constructor:
Frac('a + b', 'c + d')
The corresponding test for setting this up reads
def test_simple_fraction_as_strings(self):
actual = Frac("a + b", "c + d")
self.assertEqual(r'\frac{a + b}{c + d}', actual)
As implementation, we define a new class inheriting from Symbol
. Probably it will turn out to be convenient to have access to the numerator and denominator as symbols, so I store them as Symbol
objects separately. Actually, I’m violating the TDD approach here when I implement something that’s not really needed yet, but hey, let’s not be dogmatic. ;-)
class Frac(Symbol):
def __init__(self, numer, denom):
self.numer = Symbol(numer)
self.denom =…