basic symbolic computation in perl
TRANSCRIPT
Symbolic Computationin 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 1Rational numbers
Example 1Rational numbers
We start with integers.
Example 1Rational numbers
We start with integers.
Example 1For division, we must extend the integers.
Example 1For division, we must extend the integers.
Example 1We can think of them as ordered pairs.
Example 1Rational object as an ordered pair:
Divide by greatest common factor $f to put in lowest terms.
Example 1Convenience subs for a, b, c, d terms:
Example 1Arithmetic operators
Example 1Stringification operator
Example 1Let's try it!
Example 2Complex numbers
Example 2Rational numbers
We start with the real numbers.
Example 2Rational numbers
We start with the real numbers.
Example 2Rational numbers
We start with the real numbers.
Example 2To solve, we must extend the real numbers.
Example 2We can think of them as ordered pairs.
Example 2Complex object as an ordered pair:
Example 2Convenience subs for a, b, c, d terms:
Remember these?
Example 2Arithmetic operators
Example 2Stringification operator
Example 2Let's try it!
Example 3Linear equations
Example 3Linear equations
standard form:
Example 3Linear equations
standard form:
solution:
Example 3We can think of them as ordered pairs.
Example 3Linear object as an ordered pair:
Example 3Solving algorithm
That's it!
Example 3Let's try it!
Example 3What about Rationals?
Changed integer 0 to Rational 0/1. Overloading handles the rest.
Example 3Let's try it!
Example 4Quadratic equations
Example 4Quadratic equations
standard form:
Example 4Quadratic equations
standard form:
solution:
Example 4We can think of them as ordered triplets.
Example 4Quadratic object as an ordered triplet:
Example 4Solving algorithm
Example 4Let's try it!
Now make it work with Complex objects!
Example 4Let's try it!
Now make it work with Complex objects!
(I'm too lazy.)
Example 5Polynomials
Example 5Polynomials
standard form:
Example 5Polynomials
Example 5We can think of them as ordered tuples of size n+1, where n is the degree of the
polynomial.
Example 5Polynomial object as an ordered tuple:
Example 5Addition operator:
Example 5Let's try it!
Example 5How about derivatives?
Example 5Derivative operator:
Example 5Let'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