On Sun, Feb 3, 2013 at 6:31 PM, kirby urner <kirby.ur...@gmail.com> wrote:

<< SNIP >>


> Your idea to do fractions, to build them from the inside out, is a
> good idea.  You could start with Guido's record-simple GCD function,
> for Euclid's Algorithm, then gradually build out with __add__, __mul__
> etc. using these inside a class (you can call out to them).
>
> def gcd(a, b):
>     while b:
>         a, b = b, a % b
>     return a
>
> def lcm(a, b):
>     return ( a * b ) // gcd(a, b)
>
> The thing about Euclid's Algorithm is it's barely taught at all in
> high schools, and if you ask why, then you've again jumped into a
> rather deep discussion, deeper than just computers.
>
>
As I was mentioning to Dan off-list, it's the Closure property of Rational
numbers (rats) under both addition-subtraction and multiplication-division,
that makes them such pleasing mathematical objects.

Yes, the whole numbers and naturals (N) are closed under addition too, but
then there're no inverses except for zero.

With Z we get both inverses and closure (operation addition).
Associativity.

With Q, we get that for two operations (__add__ and __mul__), and
distributivity besides.

Our first field maybe.

But to really show that, you want to be able to give birth to Fractions
with fractions as inputs:

>>> p = Rat(1,2)
>>> q = Rat(4,3)
>>> r = Rat(p, q)

This shows allowing both int type and rat type as inputs.

I think that's a fine place to draw the line, in terms of introducing basic
type checking.  I've seen blogs about how "isinstance is evil in a duck
pond" but I'm fine with type checking.  Python is dynamically typed, not
weakly typed.

So that puts the burden on __init__ to be sophisticated enough to birth new
Rationals from any two Rationals.  ints are immediately recognized as
Rat(n, 1).  The set int (Z) is a subset of Q.

That, by the way, is how to make this seem more like math teaching, not
just computer science teaching.  Remind students about N < Z < Q < R < C
(which is not the whole story).

Operator overriding is what makes for a sophisticated turn of the spiral
around the 8th or 9th grade reading level.

You realize that __add__ and __mul__ may be defined for many types, and yet
there's a family resemblance that emerges:  the property of having an
identity element; the property of having and inverse element for each
element, such that self + ~self == Identity element.

In addition to the Rats, I like to introduce numbers that multiply modulo
N, could be called the Modulo Numbers.  That's just repackaging well known
content, using the OO approach of "math objects".

Take the totatives of a number N and make them multiply module N:  you've
got closure, associativity, identity and inverse elements, every time.
That's a group.  Welcome to abstract algebra, courtesy of __mul__.

def totatives(n):
    return [ r for r in range(1, n) if gcd(r, n) == 1]  #  gcd defined above

def totient(n):
    return len(totatives(n))

>>> totatives(12)
[1, 5, 7, 11]
>>> totatives(100)
[1, 3, 7, 9, 11, 13, 17, 19, 21, 23, 27, 29, 31, 33, 37, 39, 41, 43, 47,
49, 51, 53, 57, 59, 61, 63, 67, 69, 71, 73, 77, 79, 81, 83, 87, 89, 91, 93,
97, 99]


I've got a web page called Vegetable Group Soup with more about all this
but I won't give the URL, because there's a nasty noise, like a machine at
a factory, and you might be wearing headphones.

Kirby



> The Litvins books starts to build a Fraction but mostly eschews
> defining classes.
>
> A "classes early" approach is also doable -- many different approaches
> to this airport, pick your vector.
>
> I show how a class looks something like a snake:
>
> class Head:
>     # rib cage
>     def __rib__(self):
>         pass  # air between ribs
>     def __rib__(self):
>         pass
>     def __rib__(self):
>         pass
>     def __rib__(self):
>         pass
>     def __rib__(self):
>         pass
>
> ... and in general use a lot of biological metaphors / analogies.
>
>
> Kirby
>
> Web resources:
>
> http://www.4dsolutions.net/ocn/numeracy0.html
> http://www.4dsolutions.net/ocn/cp4e.html
>
_______________________________________________
Edu-sig mailing list
Edu-sig@python.org
http://mail.python.org/mailman/listinfo/edu-sig

Reply via email to