SAGE

A powerful open-source math software.

Created by Prof. William Stein.

http://sage.math.washington.edu/

http://www.sagemath.org/tour.html

SYMBOLIC EXPRESSIONS

You can use the "var()" command to create symbolic variables in Sage. 

{{{id=2| # This creates 3 variables x, y, and z var('x y z') /// (x, y, z) }}}

Now we can use them in a mathematical equation

{{{id=3| f = x^2/(2-x^2) + sin(y^3) + 2*z^3 /// }}}

We can print out "f" or use the method "show()" to print it out nicely!

{{{id=7| # ugly :( f /// 2*z^3 - x^2/(x^2 - 2) + sin(y^3) }}} {{{id=8| # Pretty! :) f.show() ///
2 \, z^{3} - \frac{x^{2}}{{(x^{2} - 2)}} + \sin\left(y^{3}\right)
}}}

How is this useful?

Let's make a really complicated equation

{{{id=10| g = (1-x^3)^3 - (y^4 -x*z)^4 + z^3 /// }}}

It automatically simplifies it a little bit

{{{id=12| g.show() new_g = g.expand() ///
-{(x^{3} - 1)}^{3} - {(y^{4} - x z)}^{4} + z^{3}
}}} {{{id=15| new_g = g.expand() /// }}} {{{id=16| new_g.show() ///
4 \, x y^{12} z - x^{9} + 3 \, x^{6} - 3 \, x^{3} - y^{16} - 6 \, x^{2} y^{8} z^{2} + 4 \, x^{3} y^{4} z^{3} - x^{4} z^{4} + z^{3} + 1
}}} {{{id=17| h = x^2 + 2*x + 1 /// }}} {{{id=72| def cool_function(num): print( num * 2) /// }}} {{{id=71| cool_function(45) /// 90 }}} {{{id=18| h.factor().show() ///
{(x + 1)}^{2}
}}}

So that's cool...

But is there any other useful things?

{{{id=19| f(x,y) = (1-x^3)^3 - (y^4 -x*y)^4 /// }}} {{{id=74| f(1, 3) /// -37015056 }}} {{{id=73| def new_function(x, y): return (1-x^3)^3 - (y^4 -x*y)^4 /// }}} {{{id=75| new_function(1,3) /// -37015056 }}} {{{id=87| # SAGE can do symbolic calculations too! var('A B') new_function(A, B) /// -(A^3 - 1)^3 - (B^4 - A*B)^4 }}} {{{id=21| for i in range(8): print(f(i, 2*i)) /// 1 -38416 -3782742359 -2667616642232 -272781427331263 -9801495008156624 -182329765194741191 -2155814220531729064 }}}

SAGE has a lot of built in functions that make math functions go very very fast.

{{{id=58| def noob_factorial(n): number = 1 for i in range(n): number *= n return number /// }}} {{{id=59| %time noob_factorial(100000) # ten thousand /// CPU time: 0.95 s, Wall time: 0.96 s }}} {{{id=60| %time noob_factorial(1000000) # one million /// ^CTraceback (most recent call last): File "", line 1, in File "_sage_input_22.py", line 7, in noob_factorial(_sage_const_1000000 ) File "_sage_input_20.py", line 8, in noob_factorial File "/sage/sage/local/lib/python2.6/site-packages/sage/interfaces/get_sigs.py", line 9, in my_sigint raise KeyboardInterrupt KeyboardInterrupt }}} {{{id=70| %time noob_factorial(200000) # two-hundred thousand /// CPU time: 11.41 s, Wall time: 13.23 s }}} {{{id=56| %time factorial(10000000) # ten million /// CPU time: 7.54 s, Wall time: 7.57 s }}}

GRAPHICAL REPRESENTATIONS

{{{id=22| f = x^3 + sin(x) /// }}} {{{id=76| /// }}} {{{id=23| plot(f, (-10,10)) /// }}} {{{id=24| show(f.differentiate()) ///
3 \, x^{2} + \cos\left(x\right)
}}} {{{id=61| /// }}} {{{id=26| f.integral().show() ///
\frac{1}{4} \, x^{4} - \cos\left(x\right)
}}}

So how can we plot two thing?

The Sage Notebook automatically prints out Sage objects. "plot()" is a Sage object so when we type "plot(f, (0,10))" it automatically prints it to the "console".

We can store the plot object a variable

{{{id=32| plot() /// Traceback (most recent call last): File "", line 1, in File "/home/sage/sagenb/sage_notebook-480/worksheets/nakajor/56/code/33.py", line 6, in exec compile(ur'plot()' + '\n', '', 'single') File "", line 1, in File "/home/sage/sage_install/sage/local/lib/python2.6/site-packages/sage/plot/misc.py", line 240, in wrapper return func(*args, **kwds) File "/home/sage/sage_install/sage/local/lib/python2.6/site-packages/sage/plot/misc.py", line 135, in wrapper return func(*args, **options) TypeError: plot() takes at least 1 non-keyword argument (0 given) }}} {{{id=27| a = plot(f, (-10,10)) b = plot(f.differentiate(x), (-10,10)) /// }}} {{{id=29| show(a+b) /// }}} {{{id=78| def hello(saying="howdy", num=1): for i in range(num): print saying /// }}} {{{id=77| hello(num = 4, saying = "hello") /// hello hello hello hello }}} {{{id=33| #change colors a = plot(f, (-10,10), rgbcolor='green') b = plot(f.differentiate(x), (-10,10)) show(a+b) /// }}} {{{id=30| @interact def yay(function = x^2, y = 4, hello = "hello"): a = plot(function, (-10,10), rgbcolor='red') b = plot(function.differentiate(x), (-10,10)) show(a+b) /// }}}

Finding out the different methods that an object has.

Create a variable for the object, put "." after, and press tab

{{{id=34| my_plot = plot(x^2, (-10,10)) /// }}} {{{id=54| /// }}} {{{id=35| f = x^2 f. /// }}} {{{id=38| plot3d(sin(x)*y^2, (x, -10,10), (y,-10,10)) /// }}} {{{id=40| %hide #auto Written by Tom Boothby, 2009 and released to public domain. index2block = [ [ 0,0,0, 1,1,1, 2,2,2], [ 0,0,0, 1,1,1, 2,2,2], [ 0,0,0, 1,1,1, 2,2,2], [ 3,3,3, 4,4,4, 5,5,5], [ 3,3,3, 4,4,4, 5,5,5], [ 3,3,3, 4,4,4, 5,5,5], [ 6,6,6, 7,7,7, 8,8,8], [ 6,6,6, 7,7,7, 8,8,8], [ 6,6,6, 7,7,7, 8,8,8] ] block2index = [ [ (0,0),(0,1),(0,2), (1,0),(1,1),(1,2), (2,0),(2,1),(2,2)], [ (0,3),(0,4),(0,5), (1,3),(1,4),(1,5), (2,3),(2,4),(2,5)], [ (0,6),(0,7),(0,8), (1,6),(1,7),(1,8), (2,6),(2,7),(2,8)], [ (3,0),(3,1),(3,2), (4,0),(4,1),(4,2), (5,0),(5,1),(5,2)], [ (3,3),(3,4),(3,5), (4,3),(4,4),(4,5), (5,3),(5,4),(5,5)], [ (3,6),(3,7),(3,8), (4,6),(4,7),(4,8), (5,6),(5,7),(5,8)], [ (6,0),(6,1),(6,2), (7,0),(7,1),(7,2), (8,0),(8,1),(8,2)], [ (6,3),(6,4),(6,5), (7,3),(7,4),(7,5), (8,3),(8,4),(8,5)], [ (6,6),(6,7),(6,8), (7,6),(7,7),(7,8), (8,6),(8,7),(8,8)], ] def dokurow(l): s = '' for i in [0,3,6]: s+= '|' for c in range(3): if l[c+i] == 0: s+= ' ' else: s+= '%s'%l[c+i] s+= '|' return s def printdoku(Puzzle): for i in [0,3,6]: print '+---+---+---+' for r in range(3): print dokurow(Puzzle[r+i]) print '+---+---+---+' def choices(Puzzle,row,col): b = block2index[index2block[row][col]] canhas = dict([(a,True) for a in [1..9]]) for x in range(9): canhas[Puzzle[row][x]] = False canhas[Puzzle[x][col]] = False canhas[Puzzle[b[x][0]][b[x][1]]] = False return [a for a in [1..9] if canhas[a]] def _backtrack(Puzzle,index,cellorder): if index >= len(cellorder): return True row,col = cellorder[index] if Puzzle[row][col] == 0: A = choices(Puzzle,row,col) for a in A: Puzzle[row][col] = a if _backtrack(Puzzle,index+1,cellorder): return True Puzzle[row][col] = 0 #reset to initial state return False else: return _backtrack(Puzzle,index+1,cellorder) def Backtrack(Puzzle): P = [[a for a in Row] for Row in Puzzle] roworder = [0..8] colorder = [0..8] cellorder = [(i,j) for i in colorder for j in roworder] if _backtrack(P,0,cellorder): return P else: return None /// }}} {{{id=41| %time Puzzle2 = [ [ 0,0,6, 2,0,0, 0,0,7 ], [ 0,4,5, 0,0,0, 0,6,0 ], [ 0,9,1, 4,0,0, 5,0,0 ], [ 0,0,0, 0,0,7, 9,0,0 ], [ 9,0,0, 0,8,0, 0,0,2 ], [ 0,0,8, 5,0,0, 0,0,0 ], [ 0,0,3, 0,0,5, 2,4,0 ], [ 0,8,0, 0,0,0, 6,7,0 ], [ 5,0,0, 0,0,9, 8,0,0 ] ] printdoku(Puzzle2) print printdoku(Backtrack(Puzzle2)) /// +---+---+---+ | 6|2 | 7| | 45| | 6 | | 91|4 |5 | +---+---+---+ | | 7|9 | |9 | 8 | 2| | 8|5 | | +---+---+---+ | 3| 5|24 | | 8 | |67 | |5 | 9|8 | +---+---+---+ +---+---+---+ |836|251|497| |245|978|163| |791|436|528| +---+---+---+ |354|127|986| |917|684|352| |628|593|714| +---+---+---+ |173|865|249| |489|312|675| |562|749|831| +---+---+---+ CPU time: 0.59 s, Wall time: 0.59 s }}}

RUBIX CUBE

Mathematically, it can be solved! Uses permutations and disjoint cycles

{{{id=42| C = RubiksCube('(1,9,35)(2,5,34,26)(3,22,27,16,33,41)(4,12,47,45,42,21,36,44,13)(6,19)(7,18)(8,17)(10,37,39,31,23,28,29,15,20)(11,25)(14,46,40)(24,48,43,38,30,32)') C.plot3d().show(viewer='jmol') /// }}} {{{id=43| C.solve() /// 'D F R U D2 B' }}} {{{id=45| C = C.scramble(20) /// }}} {{{id=46| %time C.solve() /// "U2 R2 U2 F' D2 B R2 B R U' F' L2 U' L F' R' F' R F' D'" CPU time: 0.05 s, Wall time: 3.81 s }}} {{{id=47| # Written by William Stein the_cube = RubiksCube() the_scramble = 0 @interact def _(move=selector([None,'F',"F'",'B',"B'",'L',"L'",'R',"R'",'U',"U'",'D',"D'"],buttons=True), scramble=(0..40)): global the_cube, the_scramble if the_scramble != scramble: the_cube = RubiksCube().scramble(scramble) the_scramble = scramble elif move: the_cube = the_cube.move(move) try: print "Solving..."; sys.stdout.flush() t = walltime() print the_cube.solve(), " (solve time = %s seconds)"%walltime(t) except: # since can't solve an unsolved cube print "(trivial solution)" the_cube.plot3d().show(viewer='tachyon') # change to 'jmol' if you have want to rotate cube /// }}}

Fincancial Time Series

{{{id=48| # Do this when the stock market is open for n in range(8): sleep(3) print finance.Stock('K') /// K (48.98) K (48.98) K (48.98) K (48.98) K (48.98) K (48.98) K (48.98) K (48.98) }}}

Reads in a file, and plots it!

Because it's a finance.TimeSeries object, there are a lot more methods that we can use with it. Use tab to find out more!

{{{id=50| T = finance.TimeSeries(open(DATA+'gas_prices_us_since_1990.txt').read().split()) T.plot().show(frame=True,figsize=[8,4]) /// }}} {{{id=51| T.min() /// 88.5 }}} {{{id=53| /// }}}

Mandelbrot Sets

{{{id=62| # Creates the complex number (2+3i) x = CC(2,3) show(x) ///
2.00000000000000 + 3.00000000000000i
}}} {{{id=64| x.abs() /// 3.60555127546399 }}} {{{id=80| n(x.abs()) /// 3.60555127546399 }}} {{{id=79| /// Traceback (most recent call last): File "", line 1, in File "_sage_input_5.py", line 4, in exec compile(ur'n(x.abs())' + '\n', '', 'single') File "", line 1, in File "/sage/sage/local/lib/python2.6/site-packages/sage/misc/functional.py", line 1015, in numerical_approx return x.numerical_approx(prec) File "expression.pyx", line 3339, in sage.symbolic.expression.Expression.n (sage/symbolic/expression.cpp:15863) File "expression.pyx", line 208, in sage.symbolic.expression.Expression.pyobject (sage/symbolic/expression.cpp:2701) TypeError: self must be a numeric expression }}} {{{id=66| x = CC(2, 2) y = CC(4,4) x+y /// 6.00000000000000 + 6.00000000000000*I }}} {{{id=67| x*y /// 16.0000000000000*I }}} {{{id=68| x^2 /// 8.00000000000000*I }}} {{{id=69| my_point = point((2,2)) my_point2 = point((.25,.25)) /// }}} {{{id=83| my_point = point((0,0)) for i in range(10): my_point += point((i,i)) /// }}} {{{id=84| /// }}} {{{id=81| my_point.plot() /// }}} {{{id=82| /// }}}