SageMath 1: Using SageMath

Cheng-Hsin Hsu Naonal Tsing Hua University Department of Computer Science

CS3330 Scienfic Compung 1 What is SageMath

• Sage stands for System for and Geometry Experimentaon • Starts from a System (CAS) • Now a full spectrum of math tools, including – Algebra – Computaons – Numerical Math – hp://www..org – Visualizaon – Many other topics

CS3330 Scienfic Compung 2 How is SageMath Built

• Created in 2005 by William Stein at University of Washington • Goal: Offer an affordable math soware to solve various math problems • Approach – Leverage opensource (GPL-ed) soware, such as SciPy, , and GAP – Adopt the popular Python as the • Outcome: A huge set of math tools!

CS3330 Scienfic Compung 3 What SageMath Can Do

• Solving exact problems

• Solving numerical problems

• Plot cool figures – Get a sense of global behavior

CS3330 Scienfic Compung 4 Different Ways to Use SageMath

• Notebook: Web-based interface • SageMath Online: Cloud-based Web interface • Interacve command-line: Python-like shell prompt • Programs: Write and compile Sage programs • Scripts: Invoke Sage libraries from Python

CS3330 Scienfic Compung 5 Where Are Notebook and Command-line

CS3330 Scienfic Compung 6 SageMath Notebook

• Each notebook has several worksheets • Worksheets can be saved, opens, downloaded, shared, and etc. • SageMath Online: hp://cloud.sagemath.com/ provides similar interface

CS3330 Scienfic Compung 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 buon or Shi-Enter • You can always come back and edit your formulas/commands and re-evaluate them CS3330 Scienfic Compung 8 SageMath Commandline

• Command-line remembers states

CS3330 Scienfic Compung 9 Use SageMath as a Calculator

CS3330 Scienfic Compung 10 Square Root Funcon

SageMath loves exact answers

Numerical approximaon

CS3330 Scienfic Compung 11 Compared to Matlab

• Everything is double precision floang point by default • Through Symbolic toolbox, which is a trimmed down version of : hp://www.mapleso.com/products/Maple/

CS3330 Scienfic Compung 12 Special Constants

Simplest exact value

Decimals automacally lead to numerical approximaon

prec: (integer) default = 53: prec is the number of bits used to represent the manssa of a floang-point number.

CS3330 Scienfic Compung 13 More on Square Roots

List in Python/Sage, a sequence of comma-separated numbers

CS3330 Scienfic Compung 14 SageMath is Case Sensive

Mostly, with four excepons: true, false, i, n

CS3330 Scienfic Compung 15 How to Get

• Notebook: command? • Command-line: help(command) • Also, ulize the Tab-compleon

CS3330 Scienfic Compung 16 Trigonometry

CS3330 Scienfic Compung 17 Simple 2D Plots

CS3330 Scienfic Compung 18 More Complicated Funcon

CS3330 Scienfic Compung 19 Even More Complicated

What do they do?

CS3330 Scienfic Compung 20 Ranges of Axes

What do they do?

CS3330 Scienfic Compung 21 Superimposing of Mulple Graph

CS3330 Scienfic Compung 22 Polished Graph

CS3330 Scienfic Compung 23 Last Example on Graphs

CS3330 Scienfic Compung 24 Reduced Row Echelon Form (RREF)

• System of equaons: 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 Scienfic Compung 25 Geng RREF in SageMath

• Create a matrix ß what are the first two arguments of matrix(.)? • Invoke rref(.) funcon

CS3330 Scienfic Compung 26 Pracce, a Tricky Queson

• Try to solve the following equaon 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 Scienfic Compung 27 Inializing Matrices

• Two ways to create matrices

CS3330 Scienfic Compung 28 Exceponal 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 soluon?

CS3330 Scienfic Compung 29 Exceponal 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 soluons?

CS3330 Scienfic Compung 30 Define our Own Funcons

CS3330 Scienfic Compung 31 Funcon Composion

CS3330 Scienfic Compung 32 Factorizaon

CS3330 Scienfic Compung 33 Expanding a Funcon

CS3330 Scienfic Compung 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 Scienfic Compung 35 Single-Variable Formulas

Declare variable, noce that x is always a variable without explicit declaraon

CS3330 Scienfic Compung 36 Mulple-Variable Formulas

What does it do? Remember how to solve the same problem using RREF?

CS3330 Scienfic Compung 37 Non-Linear Equaon Systems

List of lists? How to make it easier to read?

CS3330 Scienfic Compung 38 Higher Order Equaons

BTW, general polynomial of degree 5 or higher have "no closed-form formula"

Try this to see what we get

CS3330 Scienfic Compung 39 Quan Polynomials and Numerical Solver

Solve x5 + x4 + x3 x2 + x 1=0

Why can’t we find a soluon?

CS3330 Scienfic Compung 40 More Numerical Solver Examples

Exercise: what are we calculang?

CS3330 Scienfic Compung 41 How to Take Derivaves

CS3330 Scienfic Compung 42 Higher Order Derivaves

CS3330 Scienfic Compung 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 Scienfic Compung 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 funcon for this!

CS3330 Scienfic Compung 45 Numerical Integraons

CS3330 Scienfic Compung 46 Summary

• We introduced SageMath, an opensource project based on Python • We go over of SageMath, including symbolic and numerical soluons, matrices, and simple plots • We will mostly use SageMath for symbolic soluons – SageMath was inially designed for this – Other tasks are done in Matlab/Octave • References: – hp://www.sagemath.org ß Official Web and resources – hp://www.gregorybard.com/SAGE.html ß Our textbook

CS3330 Scienfic Compung 47 SageMath #1 Homework (S1)

1. (1%) Find at least 5 real number soluons 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 equaon system (say variables are x, y, and z). Write down the equaon system (in Latex, please turn in the .tex and .pdf). Solve the equaon system using SageMath. Present the soluon 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 idenfy the key difference to get the point. x = ceil(random() * 888) print sqrt(x) print N(sqrt(x))

CS3330 Scienfic Compung 48 Preview of Midterm #1

CS3330 Scienfic Compung 49