[Edu-sig] Algebra + Python

Kirby Urner pdx4d@teleport.com
Fri, 27 Apr 2001 21:23:09 -0700

I like to imagine a fictional but possible world in which 
computer programming for everybody (CP4E) means smoother 
integration of math and computer science in the lower 

In the real world, there's a push to make the teaching of
algebra more widespread at the 8th grade level.  What I 
imagine is that this is the grade level where we might 
formally begin using some programming language (e.g. Python, 
Scheme) in the classroom -- in the sense of having students 
look at and write source code.  In 7th grade, they might 
watch the teacher project some command line stuff, but 
getting under the hood wouldn't happen quite as much.

A link between programming and algebra is in this concept
of variables.  A polynomial in the 2nd degree is typically
written as Ax^2 + Bx + C, which in Python is more like
A*x**2 + B*x + C.  The capital letters are called constant
coefficients and they "fix" a polynomial, give it its 
characteristic "call letters" (like in radio:  the is 
KQPD...).  Then x is what varies -- you might stipulate 
over some domain, e.g. from in [-10,10] (square brackets 
means inclusive).

At the command line, 8th graders would have one kind of 
function called a "polynomial factory" that turned out 
polynomials with a specific set of coefficients.  These
would then be floated as functions in their own right, 
ready to take in x domain values and spit out f(x) range

There may be a better way to write the factory function than
I've shown below.  I'd like to see other solutions:

 >>> def makepoly(A,B,C):	
        Build a polynomial function from coefficients
  	  return eval("lambda x: %s*x**2 + %s*x + %s" % (A,B,C))

 >>> f = makepoly(2,3,4)  # pass coefficients as arguments
 >>> f(10)                # f is now a function of x
 >>> 2*10**2 + 3*10 + 4   # check
 >>> f(-10)
 >>> [f(x) for x in range(-10,11)]  # remember, 2nd arg is non-inclusive
 [174, 139, 108, 81, 58, 39, 24, 13, 6, 3, 4, 9, 18, 31, 
  48, 69, 94, 123, 156, 193, 234]
 >>> g = makepoly(1,-2,-7)  # make a new polynomial
 >>> g(5)  
 >>> g(f(5))                # composition of functions
 >>> f(g(5))                # f(g(x)) is not equal to g(f(x))

The same technique might be applied to a sinewave function.  Here
the coefficients may appear is follows:

  f(x) = A sin(Bx + C) + D

You can imagine doing the factory function, based on the above
example, or using some (better?) strategy.

Below is a web resource that implements some of these same ideas,
also using a computer language, but to my eye it all looks less 
intuitive in the J language (what the text says is featured).
Is it just me?  Is this just because I've spent more time with
Python?  I'm sure that's partly it.


Moving beyond 8th grade, we want students to understand what's
meant be D(f(x)) at point x, i.e. dy/dx at x -- the derivative.
Again, Python makes this easy in that we can write a generic 
derivative taker:

  >>> def deriv(f,x):
        Return approximate value of dy/dx at f(x)
  	  h = .0001
	  return (f(x+h)-f(x))/h

  >>> deriv(f,2)       # f(x) = 2*x**2 + 3*x + 4
  >>> deriv(g,2)       # g(x) = x**2 - 2*x - 7

If you remember how to take the derivative of a polynomial, you'll
know that f'(x) = 4*x + 3 and g'(x) = 2*x - 2 -- so these are pretty
good approximations.

Somebody challenged us on math-teach to think of how we might teach
crypto and number theory to 7th graders.  My suggestions are in this
thread (Number Theory question, reply to Wayne Bishop, 25 Apr 2001
http://www.mathforum.com/epigone/math-teach/zilbangkan ).  I use 
the IDLE CLI quite a bit, along with my ciphers.py

Another post re Python + Intro Calculus, as per the above is at:

I think any of us who know some Python, and some math, can write
this kind of stuff.  It's not esoteric.  I'd just like to see 
more of it, is all, coming from a lot of different corners.  That's 
because I think a CP4E world would have some major advantages over 
the current one, including more students motivated to comprehend 
technology at deeper levels (I think adding more computer stuff 
to math will make it less of a turn off for many students, if 
done in a cool and interesting way (which doesn't have to mean
super glitzy at every turn -- a bare bones CLI ala IDLE can be 
part of it)).