Symbolic Computation
in Perl
Mike Friedman
(friedo)
YAPC::NA 2015
Salt Lake City, UT
@friedo
Me:
Mike Friedman
(friedo)
@friedo
http://quire.com/
@quire
The Problem:
The Problem:
We have computers.
The Problem:
We have computers.
But they don't really compute.
The Problem:
We have computers.
But they don't really compute.
The Problem:
We have computers.
But they don't really compute.
==
The Problem:
Native number types in most programming languages
(including Perl)
are ints and floats.
The Problem:
Native number types in most programming languages
(including Perl)
are ints and floats.
Because that's what the microprocessor can do.
The Problem:
What about rational numbers?
The Problem:
What about rational numbers?
The Problem:
What about rational numbers?
These can only be approximated
by floating point.
The Problem:
What about irrational numbers?
The Problem:
What about irrational numbers?
Same problem.
The Problem:
What about imaginary and complex numbers?
The Problem:
What about imaginary and complex numbers?
Floats can't even approximate these.
The Solution:
We do not want to calculate with numbers.
The Solution:
We do not want to calculate with numbers.
We want to compute with symbols.
The Solution:
We do not want to calculate with numbers.
We want to compute with symbols.
We do that in software.
The Solution:
>
Example 1
Rational numbers
Example 1
Rational numbers
We start with integers.
Example 1
Rational numbers
We start with integers.
Example 1
For division, we must extend the integers.
Example 1
For division, we must extend the integers.
Example 1
We can think of them as ordered pairs.
Example 1
Rational object as an ordered pair:
Divide by greatest common factor $f to put
in lowest terms.
Example 1
Convenience subs for a, b, c, d terms:
Example 1
Arithmetic operators
Example 1
Stringification operator
Example 1
Let's try it!
Example 2
Complex numbers
Example 2
Rational numbers
We start with the real numbers.
Example 2
Rational numbers
We start with the real numbers.
Example 2
Rational numbers
We start with the real numbers.
Example 2
To solve, we must extend the real numbers.
Example 2
We can think of them as ordered pairs.
Example 2
Complex object as an ordered pair:
Example 2
Convenience subs for a, b, c, d terms:
Remember these?
Example 2
Arithmetic operators
Example 2
Stringification operator
Example 2
Let's try it!
Example 3
Linear equations
Example 3
Linear equations
standard form:
Example 3
Linear equations
standard form:
solution:
Example 3
We can think of them as ordered pairs.
Example 3
Linear object as an ordered pair:
Example 3
Solving algorithm
That's it!
Example 3
Let's try it!
Example 3
What about Rationals?
Changed integer 0 to Rational 0/1.
Overloading handles the rest.
Example 3
Let's try it!
Example 4
Quadratic equations
Example 4
Quadratic equations
standard form:
Example 4
Quadratic equations
standard form:
solution:
Example 4
We can think of them as ordered triplets.
Example 4
Quadratic object as an ordered triplet:
Example 4
Solving algorithm
Example 4
Let's try it!
Now make it work with Complex objects!
Example 4
Let's try it!
Now make it work with Complex objects!
(I'm too lazy.)
Example 5
Polynomials
Example 5
Polynomials
standard form:
Example 5
Polynomials
Example 5
We can think of them as ordered tuples
of size n+1, where n is the degree of the
polynomial.
Example 5
Polynomial object as an ordered tuple:
Example 5
Addition operator:
Example 5
Let's try it!
Example 5
How about derivatives?
Example 5
Derivative operator:
Example 5
Let's try it!
Conclusions
1. Figure out the rules for your thing.
Conclusions
Complex numbers extend the reals.
They have self-consistent rules for
arithmetic.
Conclusions
2. Eliminate extraneous information.
Conclusions
Rationals are just pairs of integers.
Complex are just pairs of reals.
Fraction bars and i are just notation.
Conclusions
3. Find a representation for your thing.
Conclusions
Ordered pairs, triplets, and tuples.
For more interesting things, you'll use
trees.
Thanks!
Symbolic Computation
in Perl
Mike Friedman
(@friedo)
github.com/friedo/perl-symbolic

Basic Symbolic Computation in Perl