SageMath 1: Using SageMath
Cheng-Hsin Hsu Na onal Tsing Hua University Department of Computer Science
CS3330 Scien fic Compu ng 1 What is SageMath
• Sage stands for System for Algebra and Geometry Experimenta on • Starts from a Computer Algebra System (CAS) • Now a full spectrum of math tools, including – Algebra – Combinatorics – Matrix Computa ons – Numerical Math – Calculus – Number Theory h p://www.sagemath.org – Visualiza on – Many other topics
CS3330 Scien fic Compu ng 2 How is SageMath Built
• Created in 2005 by William Stein at University of Washington • Goal: Offer an affordable math so ware to solve various math problems • Approach – Leverage opensource (GPL-ed) so ware, such as SciPy, Maxima, and GAP – Adopt the popular Python as the programming language • Outcome: A huge set of math tools!
CS3330 Scien fic Compu ng 3 What SageMath Can Do
• Solving exact problems
• Solving numerical problems
• Plot cool figures – Get a sense of more global behavior
CS3330 Scien fic Compu ng 4 Different Ways to Use SageMath
• Notebook: Web-based interface • SageMath Online: Cloud-based Web interface • Interac ve command-line: Python-like shell prompt • Programs: Write and compile Sage programs • Scripts: Invoke Sage libraries from Python
CS3330 Scien fic Compu ng 5 Where Are Notebook and Command-line
CS3330 Scien fic Compu ng 6 SageMath Notebook
• Each notebook has several worksheets • Worksheets can be saved, opens, downloaded, shared, and etc. • SageMath Online: h p://cloud.sagemath.com/ provides similar interface
CS3330 Scien fic Compu ng 7 SageMath Worksheet
• Each worksheet contains several (independent, stateless) cells • Each cell is a box where you can type in formulas and evaluate them ß by pressing the evaluate bu on or Shi -Enter • You can always come back and edit your formulas/commands and re-evaluate them CS3330 Scien fic Compu ng 8 SageMath Commandline
• Command-line remembers states
CS3330 Scien fic Compu ng 9 Use SageMath as a Calculator
CS3330 Scien fic Compu ng 10 Square Root Func on
SageMath loves exact answers
Numerical approxima on
CS3330 Scien fic Compu ng 11 Compared to Matlab
• Everything is double precision floa ng point by default • Through Symbolic toolbox, which is a trimmed down version of Maple: h p://www.mapleso .com/products/Maple/
CS3330 Scien fic Compu ng 12 Special Constants
Simplest exact value
Decimals automa cally lead to numerical approxima on
prec: (integer) default = 53: prec is the number of bits used to represent the man ssa of a floa ng-point number.
CS3330 Scien fic Compu ng 13 More on Square Roots
List in Python/Sage, a sequence of comma-separated numbers
CS3330 Scien fic Compu ng 14 SageMath is Case Sensi ve
Mostly, with four excep ons: true, false, i, n
CS3330 Scien fic Compu ng 15 How to Get Help
• Notebook: command? • Command-line: help(command) • Also, u lize the Tab-comple on
CS3330 Scien fic Compu ng 16 Trigonometry
CS3330 Scien fic Compu ng 17 Simple 2D Plots
CS3330 Scien fic Compu ng 18 More Complicated Func on
CS3330 Scien fic Compu ng 19 Even More Complicated
What do they do?
CS3330 Scien fic Compu ng 20 Ranges of Axes
What do they do?
CS3330 Scien fic Compu ng 21 Superimposing of Mul ple Graph
CS3330 Scien fic Compu ng 22 Polished Graph
CS3330 Scien fic Compu ng 23 Last Example on Graphs
CS3330 Scien fic Compu ng 24 Reduced Row Echelon Form (RREF)
• System of equa ons: 3x 4y +5z = 14 3 45 14 x + y 8z = 5 A = 11 8 5 2 3 2x + y + z =7 21 1 7 6 7 4 5 • RREF: 1003 A0 = 2 0100 3 x =3,y =0,z =1 0011 6 7 4 5
CS3330 Scien fic Compu ng 25 Ge ng RREF in SageMath
• Create a matrix ß what are the first two arguments of matrix(.)? • Invoke rref(.) func on
CS3330 Scien fic Compu ng 26 Prac ce, a Tricky Ques on
• Try to solve the following equa on system using SageMath 2x 5z + y =6+w 5+z y =0 w + 3(x + y)=z 1+2x y = w 3x • Answer: 107 12 54 19 w = ,x= ,y = ,z = 7 7 7 7
CS3330 Scien fic Compu ng 27 Ini alizing Matrices
• Two ways to create matrices
CS3330 Scien fic Compu ng 28 Excep onal RREF
x +2y +3z =7 123 7 4x +5y +6z =16 C1=2 45616 3 78924 7x +8y +9z =24 6 7 4 5
• What is the solu on?
CS3330 Scien fic Compu ng 29 Excep onal RREF (cont.)
x +2y +3z =7 123 7 4x +5y +6z =16 C2=2 45616 3 7x +8y +9z =25 78925 6 7 4 5
• What are the solu ons?
CS3330 Scien fic Compu ng 30 Define our Own Func ons
CS3330 Scien fic Compu ng 31 Func on Composi on
CS3330 Scien fic Compu ng 32 Factoriza on
CS3330 Scien fic Compu ng 33 Expanding a Func on
CS3330 Scien fic Compu ng 34 Use Sage to Solve Problem Symbolically
x2 • Solving x 2=0 2
– Symbolically, we get We will start from here 1 p5 ± – Numerically, we get
3.23606797749979 and 1.23606797749979
CS3330 Scien fic Compu ng 35 Single-Variable Formulas
Declare variable, no ce that x is always a variable without explicit declara on
CS3330 Scien fic Compu ng 36 Mul ple-Variable Formulas
What does it do? Remember how to solve the same problem using RREF?
CS3330 Scien fic Compu ng 37 Non-Linear Equa on Systems
List of lists? How to make it easier to read?
CS3330 Scien fic Compu ng 38 Higher Order Equa ons
BTW, general polynomial of degree 5 or higher have "no closed-form formula"
Try this to see what we get
CS3330 Scien fic Compu ng 39 Quan c Polynomials and Numerical Solver
Solve x5 + x4 + x3 x2 + x 1=0
Why can’t we find a solu on?
CS3330 Scien fic Compu ng 40 More Numerical Solver Examples
Exercise: what are we calcula ng?
CS3330 Scien fic Compu ng 41 How to Take Deriva ves
CS3330 Scien fic Compu ng 42 Higher Order Deriva ves
CS3330 Scien fic Compu ng 43 Indefinite Integral
1 x cos(x2)dx = sin(x2) 2 ? Z
x 1 dx = log(x2 + 2) x2 +2 2 ? Z
Don’t forget the constant!
CS3330 Scien fic Compu ng 44 Definite and Impossible Integrals
3 x cos(x2)dx 2 Z 2 x dx x2 +2 Z1 y 2 x2 e p⇡ Z0 We even define a special func on for this!
CS3330 Scien fic Compu ng 45 Numerical Integra ons
CS3330 Scien fic Compu ng 46 Summary
• We introduced SageMath, an opensource project based on Python • We go over basics of SageMath, including symbolic and numerical solu ons, matrices, and simple plots • We will mostly use SageMath for symbolic solu ons – SageMath was ini ally designed for this – Other tasks are done in Matlab/Octave • References: – h p://www.sagemath.org ß Official Web and resources – h p://www.gregorybard.com/SAGE.html ß Our textbook
CS3330 Scien fic Compu ng 47 SageMath #1 Homework (S1)
1. (1%) Find at least 5 real number solu ons of x2 in e sin(16x)=0 [-1, 1] using SageMath – Hint: Plot the curve first! 2. (1%) Run A = random_matrix(ZZ, 3, 4) in SageMath. Map the matrix into an equa on system (say variables are x, y, and z). Write down the equa on system (in Latex, please turn in the .tex and .pdf). Solve the equa on system using SageMath. Present the solu on in terms of x, y, and z. 3. (1%) Execute the following code in SageMath. Explain why the two prints give different answers? You need to iden fy the key difference to get the point. x = ceil(random() * 888) print sqrt(x) print N(sqrt(x))
CS3330 Scien fic Compu ng 48 Preview of Midterm #1
CS3330 Scien fic Compu ng 49