Maths with symbols
Computers usually calculate with numbers, and decimal numbers get rounded – try 0.1 + 0.2 in Ruby. SymPy calculates the way you do on paper: with fractions, square roots and letters like x. It multiplies out brackets, solves equations and finds derivatives, and every answer is exact.
SymPy is a Python library, so we use it through pycall, just like pandas in lesson 69: PyCall.import_module, a dot for everything in it. The first time, it loads about 5 MB.
require "pycall"
sp = PyCall.import_module("sympy")
puts 0.1 + 0.2
sp.Rational.new(1, 10) + sp.Rational.new(2, 10)The float is a tiny bit off; SymPy's Rational – a class, so .new, as with DataFrame – is exactly 3/10. (Ruby has exact fractions too: 1/10r + 2/10r. SymPy goes much further.) Square roots stay exact as well:
puts sp.sqrt(8)
puts sp.sqrt(2) * sp.sqrt(2)
sp.sqrt(2).evalf(50)sqrt(8) is simplified to 2*sqrt(2), and √2 · √2 is exactly 2 – not 2.0000000000000004. When you do want a decimal, evalf works one out, with as many digits as you like: here 50.
Now the letters. sp.symbols("x") makes a symbol: an x that stands for any number. You calculate with it like with a number, and you get a formula back:
x = sp.symbols("x")
term = (x + 1) ** 2
puts term
puts sp.expand(term)
sp.factor(x ** 2 - 9)expand multiplies out the brackets: (x + 1)² = x² + 2x + 1. factor goes the other way and finds the brackets. SymPy writes powers with **, like Ruby, and * for "times": 2*x is 2x.
A formula on one line is hard to read. sp.pretty draws it the way it looks in a book:
puts sp.pretty((x + 1) / (x - 1), use_unicode: false)
puts
puts sp.pretty(sp.sqrt(x ** 2 + 1) + 3 * x ** 3, use_unicode: false)use_unicode: false draws with plain characters – /, \ and - – which line up in any font. 3 * x ** 3 works even though the 3 is a Ruby number: Ruby asks the x what to do (coerce), and pycall hands the sum to Python.
Now solving. sp.solve(expression, x) finds every x for which the expression is 0. So for x² − 5x + 6 = 0 you write:
sp.solve(x ** 2 - 5 * x + 6, x)Two answers, 2 and 3. Let's check them: subs puts a number in for x. A Ruby block works as usual around it:
equation = x ** 2 - 5 * x + 6
[2, 3, 4].map { |n| equation.subs(x, n) }2 and 3 give 0, so they are solutions; 4 gives 2, so it isn't.
Last, a taste of calculus. The derivative tells you how steep a curve is at every point, and sp.diff works it out:
sp.diff(x ** 3 + 2 * x, x)The derivative of x³ + 2x is 3x² + 2. Where the derivative is 0, the curve is flat for a moment – at the top of a hill or the bottom of a valley. So diff and solve together find a curve's hills and valleys.
You need Python with SymPy (pip install sympy) and the gem: gem install pycall; the code of this lesson then runs unchanged. SymPy and mpmath, which it computes its decimals with, come under the BSD licence.
sp.diff, find where that is 0 with sp.solve, and store the answer in flat.require "pycall"
sp = PyCall.import_module("sympy")
x = sp.symbols("x")
curve = x ** 3 - 6 * x ** 2 + 9 * x
# flat = ... (the x where the curve is flat)