1062 (lecture 6 - ACTUAL LECTURE): python (part 3) - 20120123

114 days ago by William_Stein

Announcements:

  • First homework due tonight at midnight (you can ask additional questions after class today if you need to).
  • Due to the chaos, next homework will be DUE Friday, February 3
  • Office hours will always be Thursday 11-2.
  • Sage seminar: Thursday 2:00 - 3:00.

Today: Functions and Classes

 
       

Functions

Use def

def f(a, b, c=10): if not isinstance(a, int): raise TypeError print "a=", a, "b=", b, "c=",c return a+b+c 
       
f(10, 11, 12) 
       
Traceback (click to the left of this block for traceback)
...
TypeError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_56.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("ZigxMCwgMTEsIDEyKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpOKfMK9/___code___.py", line 3, in <module>
    exec compile(u'f(_sage_const_10 , _sage_const_11 , _sage_const_12 )
  File "", line 1, in <module>
    
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpMSsiWK/___code___.py", line 5, in f
    raise TypeError
TypeError
f(int(10), 11, 12) 
       
a= 10 b= 11 c= 12
33
a= 10 b= 11 c= 12
33
def f(a, b, c=10): print "a=", a, "b=", b, "c=",c return a+b+c 
       
def f(a, b, c=10): print "a=", a, "b=", b, "c=",c return a+b+c 
       
f(1,2,5) 
       
a= 1 b= 2 c= 5
8
a= 1 b= 2 c= 5
8
f(1,2) 
       
a= 1 b= 2 c= 10
13
a= 1 b= 2 c= 10
13
f('nikhil', 'william', 'jon') 
       
a= nikhil b= william c= jon
'nikhilwilliamjon'
a= nikhil b= william c= jon
'nikhilwilliamjon'
f([1,2], [3,4], [5,6]) 
       
a= [1, 2] b= [3, 4] c= [5, 6]
[1, 2, 3, 4, 5, 6]
a= [1, 2] b= [3, 4] c= [5, 6]
[1, 2, 3, 4, 5, 6]
f(True, False, True) 
       
a= True b= False c= True
2
a= True b= False c= True
2
'chris'*7 
       
'chrischrischrischrischrischrischris'
'chrischrischrischrischrischrischris'
7*'chris' 
       
'chrischrischrischrischrischrischris'
'chrischrischrischrischrischrischris'
%python f('chris', 'chris', 2008) 
       
a= chris b= chris c= 2008
Traceback (click to the left of this block for traceback)
...
TypeError: cannot concatenate 'str' and 'int' objects
a= chris b= chris c= 2008
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_51.py", line 10, in <module>
    exec compile(u'print _support_.syseval(python, u"f(\'chris\', \'chris\', 2008)", __SAGE_TMP_DIR__)
  File "", line 1, in <module>
    
  File "/Users/wstein/sage/install/sage-5.0.beta1/devel/sagenb/sagenb/misc/support.py", line 481, in syseval
    return system.eval(cmd, sage_globals, locals = sage_globals)
  File "/Users/wstein/sage/install/sage-5.0.beta1/local/lib/python2.7/site-packages/sage/misc/python.py", line 56, in eval
    eval(z, globals)
  File "", line 1, in <module>
    
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpPplOUx/___code___.py", line 5, in f
    return a+b+c
TypeError: cannot concatenate 'str' and 'int' objects
f(a=1, c=3, b=2) 
       
a= 1 b= 2 c= 3
6
a= 1 b= 2 c= 3
6

Variable Scope

c = 1; d = 1 def bar(a, b): global d # this is a rare beast. c = a; d = b print c, d def foo(x, y): c = 'fun'; d = 'stuff' print c, d foo(c,d) print c,d 
       
bar(5,10) 
       
5 10
fun stuff
5 10
5 10
fun stuff
5 10
c,d 
       
(1, 10)
(1, 10)

Python "functions" are not functions in the sense of mathematics.  They depend on more than their input, and can have side effects.

def f(x): import time if int(time.time()) % 2 == 0: return x^2 else: return x^3 
       
import time 
       
time.time() 
       
1327355654.310404
1327355654.310404
help(time) 
       
f(2) 
       
4
4
f(2) 
       
8
8
f(2) 
       
8
8
plot(f, figsize=[8,2]) 
       
plot(f, figsize=[8,2]) 
       
 
       

Arguments of functions are just new references to an object (just like assignments):

def f(v): print "v -->", id(v) v[0] = 10 print v 
       
v = [1,2,7] print id(v) f(v) 
       
4484384888
v --> 4484384888
[10, 2, 7]
4484384888
v --> 4484384888
[10, 2, 7]
       
[10, 2, 7]
[10, 2, 7]
v = [1,2,7] print id(v) f(copy(v)) 
       
108833536
v --> 90694144
[10, 2, 7]
108833536
v --> 90694144
[10, 2, 7]

Standard mistake that you may never make:  a default list as input

def f(a, L=[]): print "L -->", id(L) L.append(a) print L 
       
f(1) 
       
L --> 4485333720
[1]
L --> 4485333720
[1]
f(2) 
       
L --> 4485333720
[1, 2]
L --> 4485333720
[1, 2]
f(3) 
       
L --> 4485333720
[1, 2, 3]
L --> 4485333720
[1, 2, 3]
       
Traceback (click to the left of this block for traceback)
...
NameError: name 'L' is not defined
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_96.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("TA=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmptu5Cnp/___code___.py", line 2, in <module>
    exec compile(u'L
  File "", line 1, in <module>
    
NameError: name 'L' is not defined
def f(a, L=389): print L, "L -->", id(L) L = a print L 
       
f(1) 
       
389 L --> 4459132896
1
389 L --> 4459132896
1
f(2) 
       
389 L --> 4459132896
2
389 L --> 4459132896
2
def f(a, L=None): if L is None: L = [] print "L -->", id(L) L.append(a) print L 
       
class DumbClass(object): # dunder = double underscore def __cmp__(self, right): print 'hi' sleep(5) return cmp(type(self), type(right)) def append(self, a): print "no, i don't want to!" 
       
DumbClass 
       
<class '__main__.DumbClass'>
<class '__main__.DumbClass'>
id(DumbClass) 
       
140512116046256
140512116046256
d = DumbClass() 
       
d.append(7) 
       
no, i don't want to!
no, i don't want to!
d.append 
       
f(5, d) 
       
L --> 4467228176
no, i don't want to!
<__main__.DumbClass object at 0x10a447e10>
L --> 4467228176
no, i don't want to!
<__main__.DumbClass object at 0x10a447e10>
d == 'sdlfkjsad' 
       
hi
False
hi
False
f(1) 
       
L --> 4485232616
[1]
L --> 4485232616
[1]
f(2) 
       
L --> 4481575680
[2]
L --> 4481575680
[2]
f(3) 
       
L --> 4481753528
[3]
L --> 4481753528
[3]
f(5, [1,2,3]) 
       
L --> 4484369296
[1, 2, 3, 5]
L --> 4484369296
[1, 2, 3, 5]

Recursion

It works, but by default it is limited.

def fac(n): # assume input >= 1 return n if n==1 else n*fac(n-1) 
       
fac(5) 
       
120
120
fac(1100) 
       
WARNING: Output truncated!  
full_output.txt



Traceback (click to the left of this block for traceback)
...
RuntimeError: maximum recursion depth exceeded in cmp
WARNING: Output truncated!  
full_output.txt



Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_133.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("ZmFjKDExMDAp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpOVkxqM/___code___.py", line 3, in <module>
    exec compile(u'fac(_sage_const_1100 )
  File "", line 1, in <module>
    
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac

...

    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpCi60wv/___code___.py", line 4, in fac
    return n if n==_sage_const_1  else n*fac(n-_sage_const_1 )
RuntimeError: maximum recursion depth exceeded in cmp
import sys sys.getrecursionlimit() 
       
1000
1000
sys.setrecursionlimit(1000000) 
       
fac(1100) 
       
534370848809263770342421558229505611830801307688668954088274588180366180\
507230142593359743515667883274154675144889870336145824766456944642617389\
104733808858036231887137145451981246007462271428980768726544820445627938\
521384696779273305727495769481668074212168774982084587690054053663052207\
974765307461854174761407828535465327192407514146840446037688836558176964\
615337214194379121252115480979065230041337053263382381903878504838477803\
724588229236052052802737146250787678631254934255034752507195831922837186\
030239875807777800355948130376137959023725202218865036777462816124054696\
067849623033545571878068013262711078162177228752949470718358225524298555\
062659304726235529400439683750995457229825772063311815720595326782817708\
920420936288168056236465773117812572473654112151753182299528872746983872\
340629193606959156392223691790438526372745935906675613488166812498098047\
697386214801382532597457501623884675983655617265690103925964794592333024\
323536810983017987978944342677864199822342261966341544427566312125083062\
324656190016824389531140071376929493673248560712179251586301228003899683\
281401139943312168491213607465195878608574071289179201422918723640246484\
582489779977603613604718849834315506364003926574273569421105427234464838\
229595036372921748183592701478838442454491279935703996801454263268591217\
411293039868708405683574547193689070618929850174592801197517541507435597\
289982955567323602505100388178442747542851341998969076383964477727378271\
071547961192019215362150550579304549119593219720589371606769113577207430\
589945046720705494848261132473585053881617998656177083673499543703808532\
192432883186291945808989805781924389370357686568396818659379312091885082\
282670831506561018936080655136970279976803222392921356379936817819124988\
714512823673236915860472014331087898518943799844045922160923039163416282\
747378951005309376545654483238563097799645410120160626688816981494456970\
335134673327992503771447719262504033078194051236272349251363303333031943\
725505672666478934559485065930513420665737267456485791075859859536589310\
456140360381555965691258129225797502605868499261246665965245515699452002\
476606332869836526626211811389591512469040731053588029798552054188230707\
892213626619862738697277632967887268727487919402518596797342957419685013\
036891174178271574199813848317234052091159529637811519976390901131118896\
757105267934486097267301995911743575358800329297051491721051468122364760\
559105886494816369278793601900859787526572270771248827682569134013497568\
169550577062064755085844944156336082567893030480431935935274066793521165\
881294443099150565491295279276352340940609556759543421730501252476349532\
078080000000000000000000000000000000000000000000000000000000000000000000\
000000000000000000000000000000000000000000000000000000000000000000000000\
000000000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000
53437084880926377034242155822950561183080130768866895408827458818036618050723014259335974351566788327415467514488987033614582476645694464261738910473380885803623188713714545198124600746227142898076872654482044562793852138469677927330572749576948166807421216877498208458769005405366305220797476530746185417476140782853546532719240751414684044603768883655817696461533721419437912125211548097906523004133705326338238190387850483847780372458822923605205280273714625078767863125493425503475250719583192283718603023987580777780035594813037613795902372520221886503677746281612405469606784962303354557187806801326271107816217722875294947071835822552429855506265930472623552940043968375099545722982577206331181572059532678281770892042093628816805623646577311781257247365411215175318229952887274698387234062919360695915639222369179043852637274593590667561348816681249809804769738621480138253259745750162388467598365561726569010392596479459233302432353681098301798797894434267786419982234226196634154442756631212508306232465619001682438953114007137692949367324856071217925158630122800389968328140113994331216849121360746519587860857407128917920142291872364024648458248977997760361360471884983431550636400392657427356942110542723446483822959503637292174818359270147883844245449127993570399680145426326859121741129303986870840568357454719368907061892985017459280119751754150743559728998295556732360250510038817844274754285134199896907638396447772737827107154796119201921536215055057930454911959321972058937160676911357720743058994504672070549484826113247358505388161799865617708367349954370380853219243288318629194580898980578192438937035768656839681865937931209188508228267083150656101893608065513697027997680322239292135637993681781912498871451282367323691586047201433108789851894379984404592216092303916341628274737895100530937654565448323856309779964541012016062668881698149445697033513467332799250377144771926250403307819405123627234925136330333303194372550567266647893455948506593051342066573726745648579107585985953658931045614036038155596569125812922579750260586849926124666596524551569945200247660633286983652662621181138959151246904073105358802979855205418823070789221362661986273869727763296788726872748791940251859679734295741968501303689117417827157419981384831723405209115952963781151997639090113111889675710526793448609726730199591174357535880032929705149172105146812236476055910588649481636927879360190085978752657227077124882768256913401349756816955057706206475508584494415633608256789303048043193593527406679352116588129444309915056549129527927635234094060955675954342173050125247634953207808000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
 
       

By the way, Sage's builtin factorial function (which is part of the MPIR library) is pretty damn fast (far faster to compute the answer than convert it to decimal):

time n = factorial(10^6) 
       
Time: CPU 0.94 s, Wall: 0.94 s
Time: CPU 0.94 s, Wall: 0.94 s
time len(str(n)) 
       
5565709
Time: CPU 5.50 s, Wall: 5.47 s
5565709
Time: CPU 5.50 s, Wall: 5.47 s
 
       

Symbolic Functions

These are used for Calculus.  They are also called "functions" but have little to do with Python functions.

f(x) = sin(x)*log(x) f 
       
x |--> log(x)*sin(x)
x |--> log(x)*sin(x)
type(f) 
       
<type 'sage.symbolic.expression.Expression'>
<type 'sage.symbolic.expression.Expression'>
f(pi/2) 
       
log(1/2*pi)
log(1/2*pi)
f.integrate(x) 
       
x |--> -log(x)*cos(x) + 1/2*Ei(-I*x) + 1/2*Ei(I*x)
x |--> -log(x)*cos(x) + 1/2*Ei(-I*x) + 1/2*Ei(I*x)
f.plot((x,0,10), figsize=[8,2]) 
       
 
       

Classes (easily create your own "types"!)

Classes allow you to make new types of objects.

class CoolThing(object): def foo(self, xyz): print self, xyz 
       
z = CoolThing() z.foo('abc') 
       
<__main__.CoolThing object at 0x10a447b50> abc
<__main__.CoolThing object at 0x10a447b50> abc
type(z) 
       
<class '__main__.CoolThing'>
<class '__main__.CoolThing'>
class MyRational(object): def __init__(self, n, d): # called to initialize object self._n = Integer(n); self._d = Integer(d) def reduced_form(self): # call it explicitly """Return the reduced form of this rational number.""" a = self._n / self._d return MyRational(a.numerator(), a.denominator()) def __repr__(self): # called when printing return '%s/%s'%(self._n, self._d) def __add__(self, right): # called to implement self + right return MyRational(self._n*right._d + self._d*right._n, self._d*right._d) def __mul__(self, right): # implements self * right return MyRational(self._n*right._n, self._d*right._d) 
       
a = MyRational(2,6); b = MyRational(2, 3) print a, b 
       
2/6 2/3
2/6 2/3
a.reduced_form() 
       
1/3
1/3
c = a + b; c 
       
18/18
18/18
c.reduced_form() 
       
1/1
1/1

How do you think we should get the following to work?

a - b 
       
Traceback (click to the left of this block for traceback)
...
TypeError: unsupported operand type(s) for -: 'MyRational' and
'MyRational'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_142.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("YSAtIGI="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpUBX_CC/___code___.py", line 2, in <module>
    exec compile(u'a - b
  File "", line 1, in <module>
    
TypeError: unsupported operand type(s) for -: 'MyRational' and 'MyRational'
class MyRational2(MyRational): # inheritence (multiple also fully supported) def __sub__(self, right): return MyRational2(self._n*right._d - self._d*right._n, self._d*right._d) 
       
MyRational(2,6) - MyRational(2, 3) 
       
Traceback (click to the left of this block for traceback)
...
TypeError: unsupported operand type(s) for -: 'MyRational' and
'MyRational'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_29.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("TXlSYXRpb25hbCgyLDYpIC0gTXlSYXRpb25hbCgyLCAzKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmp5EJwuz/___code___.py", line 3, in <module>
    exec compile(u'MyRational(_sage_const_2 ,_sage_const_6 ) - MyRational(_sage_const_2 , _sage_const_3 )
  File "", line 1, in <module>
    
TypeError: unsupported operand type(s) for -: 'MyRational' and 'MyRational'
a = MyRational2(2,6); b = MyRational2(2, 3) print a, b 
       
2/6 2/3
2/6 2/3
a + b 
       
18/18
18/18
a - b 
       
-6/18
-6/18
type(a-b) 
       
<class '__main__.MyRational2'>
<class '__main__.MyRational2'>
 
       
type(a+b) 
       
<class '__main__.MyRational'>
<class '__main__.MyRational'>
class MyRational3(object): def __init__(self, n, d): # called to initialize object self._n = Integer(n); self._d = Integer(d) def __add__(self, right): # called to implement self + right return self.__class__(self._n*right._d + self._d*right._n, self._d*right._d) class MyRational4(MyRational3): def __sub__(self, right): # called to implement self + right return self.__class__(self._n*right._d - self._d*right._n, self._d*right._d) a = MyRational4(2,6); b = MyRational4(2, 3) type(a-b), type(a+b) 
       
(<class '__main__.MyRational4'>, <class
'__main__.MyRational4'>)
(<class '__main__.MyRational4'>, <class '__main__.MyRational4'>)
 
       
 
       
 
       
 
       

This week:

  • Wednesday -- Data types such as lists, tuples, strings, etc. 
  • Friday -- Exception handling and decorators

Next week: Cython

Week after: Sage development

Then: 2d and 3d Graphics

Rest of course: Applications of Sage in Calculus, Linear Algebra, etc.