[Edu-sig] What about CP4E in the inner city?
Kirby Urner
pdx4d@teleport.com
Sun, 14 May 2000 16:15:56 -0700
More ideas:
Function consists of (domain,range) pairs, usually with
some rule taking every domain input to its range output
(but a rule is not really required).
You never have the same domain value paired with two
different range values in a function, although you may
in a relation.
Quiz: Which if the following is not a function?
(a) [(a,b),(d,b),(e,c)]
(b) [(1,2),(2,4),(3,9)]
(c) [(1,a),(2,a),(1,b)]
(d) [(0,0),(0,0),(2,0)]
Answer: c (because (1,a) and (1,b) point the same
domain value to different range values.
Notice how the above question uses lists of tuples.
Functions are a subclass of Relation.
>>> domain = [1,2,3,4,5]
>>> def f(x): return x**2 # raise input to 2nd power
>>> range = map(f,domain)
>>> range
[1, 4, 9, 16, 25]
>>> def g(x): return x + 1 # another function, add 1 to input
>>> g(f(10)) # show f(g(x)) is not same as...
101
>>> f(g(10)) # g(f(x)) -- composition of functions
121
>>> domain = ['RE','DEA','HEA','FE'] # inputs needn't be numbers
>>> def addD(arg): return arg + 'D' # here's the "add 'D'" function
>>> range = map(addD,domain)
>>> range
['RED', 'DEAD', 'HEAD', 'FED']
Let's have a program that accepts a rule, a domain,
and returns the (domain, range) pairs. We'll call it
makepairs.
>>> def makepairs(rule,domain):
outlist = []
for x in domain:
outlist.append((x,rule(x)))
return outlist
>>> def f(x): return x**2
>>> makepairs(f,[1,2,3,4,5])
[(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
>>> makepairs(addD,['RE','DEA','HEA','FE'])
[('RE', 'RED'), ('DEA', 'DEAD'), ('HEA', 'HEAD'), ('FE', 'FED')]
Notice how we're passing a rule as a parameter. You
can define any number of rules, and makepairs will do
the same thing with all of them: apply it to each
domain element and append the resulting (domain,range)
tuple to a list.
Note that the rule might just be a random number,
with the domain having no roll in the computation.
Introduct the Python 'choice' function in the random
module. It simply picks randomly from a list. Lets
run it 10 times against ice cream flavors (could be 31):
>>> flavors = [['chocolate','vanilla','strawberry']
>>> for i in range(10): choice(flavors)
'chocolate'
'vanilla'
'strawberry'
'strawberry'
'chocolate'
'chocolate'
'chocolate'
'vanilla'
'chocolate'
'chocolate'
Make a rule based on choice, and make (input,output) pairs:
>>> def pickone(x): return choice(range(x))
>>> makepairs(pickone, [5,5,5,5,5])
[(5, 2), (5, 2), (5, 2), (5, 0), (5, 3)]
>>> makepairs(pickone, [5,5,5,5,5])
[(5, 3), (5, 0), (5, 4), (5, 0), (5, 3)]
>>> makepairs(pickone, [5,5,5,5,5])
[(5, 3), (5, 0), (5, 0), (5, 0), (5, 0)]
We're definitely getting relations here, not functions.
Introduce the concept of "inverse function".
If f(domain)->range and g(range)->domain, then f and g
are inverses.
Example:
>>> def f(x): return 3.0*x + 2
>>> def g(x): return (x-2)/3.0
>>> f(3) # f takes you to 11
11.0
>>> g(11) # g brings you back
3.0
>>> f(234309) # f(domain)->range
702929.0
>>> g(702929) # g(range)->domain
234309.0
Notice that a function that takes two domain values to
the same range value, will not have an inverse function,
only an inverse relation. Functions with inverse
functions are known as one-to-one, meaning every domain
value maps to a unique range value.
Quiz: Which if the following is a 1-to-1 function?
(a) [(a,b),(d,b),(e,c)]
(b) [(1,2),(2,4),(3,9)]
(c) [(1,a),(2,a),(1,b)]
(d) [(0,0),(0,0),(2,0)]
Answer: b
What is the inverse function?
Answer: [(2,1),(4,2),(9,3)]
Let's write a utility to switch pairs, called makeinverse:
>>> def makeinverse(inlist):
outlist = []
for pair in inlist:
newpair = (pair[1],pair[0])
outlist.append(newpair)
return outlist
>>> function = makepairs(addD,['RE','DEA','HEA','FE'])
>>> function
[('RE', 'RED'), ('DEA', 'DEAD'), ('HEA', 'HEAD'), ('FE', 'FED')]
>>> makeinverse(function)
[('RED', 'RE'), ('DEAD', 'DEA'), ('HEAD', 'HEA'), ('FED', 'FE')]
And so on, on and on...
Notice how we keep reusing the same rules and domains, over
and over, gradually building up concepts, of domain, range,
rule, function, relation, composition of functions, inverse
function -- all core concepts in mathematics, and all easily
implemented and sharable interactively in a Python-enabled
environment.
Kirby