basic symbolic computation in perl

77
Symbolic Computation in Perl Mike Friedman (friedo) YAPC::NA 2015 Salt Lake City, UT @friedo

Upload: mike-friedman

Post on 04-Aug-2015

124 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Basic Symbolic Computation in Perl

Symbolic Computationin Perl

Mike Friedman (friedo)

YAPC::NA 2015 Salt Lake City, UT

@friedo

Page 2: Basic Symbolic Computation in Perl

Me:

Mike Friedman (friedo)

@friedo

Page 3: Basic Symbolic Computation in Perl

http://quire.com/

@quire

Page 4: Basic Symbolic Computation in Perl

The Problem:

Page 5: Basic Symbolic Computation in Perl

The Problem:We have computers.

Page 6: Basic Symbolic Computation in Perl

The Problem:We have computers.

But they don't really compute.

Page 7: Basic Symbolic Computation in Perl

The Problem:We have computers.

But they don't really compute.

Page 8: Basic Symbolic Computation in Perl

The Problem:We have computers.

But they don't really compute.

==

Page 9: Basic Symbolic Computation in Perl

The Problem:Native number types in most programming languages

(including Perl)

are ints and floats.

Page 10: Basic Symbolic Computation in Perl

The Problem:Native number types in most programming languages

(including Perl)

are ints and floats.

Because that's what the microprocessor can do.

Page 11: Basic Symbolic Computation in Perl

The Problem:What about rational numbers?

Page 12: Basic Symbolic Computation in Perl

The Problem:What about rational numbers?

Page 13: Basic Symbolic Computation in Perl

The Problem:What about rational numbers?

These can only be approximated by floating point.

Page 14: Basic Symbolic Computation in Perl

The Problem:What about irrational numbers?

Page 15: Basic Symbolic Computation in Perl

The Problem:What about irrational numbers?

Same problem.

Page 16: Basic Symbolic Computation in Perl

The Problem:What about imaginary and complex numbers?

Page 17: Basic Symbolic Computation in Perl

The Problem:What about imaginary and complex numbers?

Floats can't even approximate these.

Page 18: Basic Symbolic Computation in Perl

The Solution:

We do not want to calculate with numbers.

Page 19: Basic Symbolic Computation in Perl

The Solution:

We do not want to calculate with numbers.

We want to compute with symbols.

Page 20: Basic Symbolic Computation in Perl

The Solution:

We do not want to calculate with numbers.

We want to compute with symbols.

We do that in software.

Page 21: Basic Symbolic Computation in Perl

The Solution:

>

Page 22: Basic Symbolic Computation in Perl

Example 1Rational numbers

Page 23: Basic Symbolic Computation in Perl

Example 1Rational numbers

We start with integers.

Page 24: Basic Symbolic Computation in Perl

Example 1Rational numbers

We start with integers.

Page 25: Basic Symbolic Computation in Perl

Example 1For division, we must extend the integers.

Page 26: Basic Symbolic Computation in Perl

Example 1For division, we must extend the integers.

Page 27: Basic Symbolic Computation in Perl

Example 1We can think of them as ordered pairs.

Page 28: Basic Symbolic Computation in Perl

Example 1Rational object as an ordered pair:

Divide by greatest common factor $f to put in lowest terms.

Page 29: Basic Symbolic Computation in Perl

Example 1Convenience subs for a, b, c, d terms:

Page 30: Basic Symbolic Computation in Perl

Example 1Arithmetic operators

Page 31: Basic Symbolic Computation in Perl

Example 1Stringification operator

Page 32: Basic Symbolic Computation in Perl

Example 1Let's try it!

Page 33: Basic Symbolic Computation in Perl

Example 2Complex numbers

Page 34: Basic Symbolic Computation in Perl

Example 2Rational numbers

We start with the real numbers.

Page 35: Basic Symbolic Computation in Perl

Example 2Rational numbers

We start with the real numbers.

Page 36: Basic Symbolic Computation in Perl

Example 2Rational numbers

We start with the real numbers.

Page 37: Basic Symbolic Computation in Perl

Example 2To solve, we must extend the real numbers.

Page 38: Basic Symbolic Computation in Perl

Example 2We can think of them as ordered pairs.

Page 39: Basic Symbolic Computation in Perl

Example 2Complex object as an ordered pair:

Page 40: Basic Symbolic Computation in Perl

Example 2Convenience subs for a, b, c, d terms:

Remember these?

Page 41: Basic Symbolic Computation in Perl

Example 2Arithmetic operators

Page 42: Basic Symbolic Computation in Perl

Example 2Stringification operator

Page 43: Basic Symbolic Computation in Perl

Example 2Let's try it!

Page 44: Basic Symbolic Computation in Perl

Example 3Linear equations

Page 45: Basic Symbolic Computation in Perl

Example 3Linear equations

standard form:

Page 46: Basic Symbolic Computation in Perl

Example 3Linear equations

standard form:

solution:

Page 47: Basic Symbolic Computation in Perl

Example 3We can think of them as ordered pairs.

Page 48: Basic Symbolic Computation in Perl

Example 3Linear object as an ordered pair:

Page 49: Basic Symbolic Computation in Perl

Example 3Solving algorithm

That's it!

Page 50: Basic Symbolic Computation in Perl

Example 3Let's try it!

Page 51: Basic Symbolic Computation in Perl

Example 3What about Rationals?

Changed integer 0 to Rational 0/1. Overloading handles the rest.

Page 52: Basic Symbolic Computation in Perl

Example 3Let's try it!

Page 53: Basic Symbolic Computation in Perl

Example 4Quadratic equations

Page 54: Basic Symbolic Computation in Perl

Example 4Quadratic equations

standard form:

Page 55: Basic Symbolic Computation in Perl

Example 4Quadratic equations

standard form:

solution:

Page 56: Basic Symbolic Computation in Perl

Example 4We can think of them as ordered triplets.

Page 57: Basic Symbolic Computation in Perl

Example 4Quadratic object as an ordered triplet:

Page 58: Basic Symbolic Computation in Perl

Example 4Solving algorithm

Page 59: Basic Symbolic Computation in Perl

Example 4Let's try it!

Now make it work with Complex objects!

Page 60: Basic Symbolic Computation in Perl

Example 4Let's try it!

Now make it work with Complex objects!

(I'm too lazy.)

Page 61: Basic Symbolic Computation in Perl

Example 5Polynomials

Page 62: Basic Symbolic Computation in Perl

Example 5Polynomials

standard form:

Page 63: Basic Symbolic Computation in Perl

Example 5Polynomials

Page 64: Basic Symbolic Computation in Perl

Example 5We can think of them as ordered tuples of size n+1, where n is the degree of the

polynomial.

Page 65: Basic Symbolic Computation in Perl

Example 5Polynomial object as an ordered tuple:

Page 66: Basic Symbolic Computation in Perl

Example 5Addition operator:

Page 67: Basic Symbolic Computation in Perl

Example 5Let's try it!

Page 68: Basic Symbolic Computation in Perl

Example 5How about derivatives?

Page 69: Basic Symbolic Computation in Perl

Example 5Derivative operator:

Page 70: Basic Symbolic Computation in Perl

Example 5Let's try it!

Page 71: Basic Symbolic Computation in Perl

Conclusions

1. Figure out the rules for your thing.

Page 72: Basic Symbolic Computation in Perl

Conclusions

Complex numbers extend the reals.

They have self-consistent rules for arithmetic.

Page 73: Basic Symbolic Computation in Perl

Conclusions

2. Eliminate extraneous information.

Page 74: Basic Symbolic Computation in Perl

Conclusions

Rationals are just pairs of integers.Complex are just pairs of reals.

Fraction bars and i are just notation.

Page 75: Basic Symbolic Computation in Perl

Conclusions

3. Find a representation for your thing.

Page 76: Basic Symbolic Computation in Perl

Conclusions

Ordered pairs, triplets, and tuples.

For more interesting things, you'll use trees.

Page 77: Basic Symbolic Computation in Perl

Thanks!Symbolic Computation

in Perl

Mike Friedman(@friedo)

github.com/friedo/perl-symbolic