Using Sagemath

Using Sagemath

SageMath 1: Using SageMath Cheng-Hsin Hsu Na#onal Tsing Hua University Department of Computer Science CS3330 Scienfic Compung 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 Computaons – Numerical Math – Calculus – Number Theory hp://www.sagemath.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 soUware to solve various math problems • Approach – Leverage opensource (GPL-ed) soUware, such as SciPy, Maxima, and GAP – Adopt the popular Python as the programming language • 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 more global behavior CS3330 Scienfic Compung 4 Different Ways to Use SageMath • Notebook: Web-based interface • SageMath Online: Cloud-based Web interface • Interac3ve 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: hKp://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 ShiE-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 func3on 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 Maple: hKp://www.maplesoU.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 Sensi3ve Mostly, with four excep:ons: true, false, i, n CS3330 Scienfic Compung 15 How to Get Help • Notebook: command? • Command-line: help(command) • Also, u3lize the Tab-comple#on CS3330 Scienfic Compung 16 Trigonometry CS3330 Scienfic Compung 17 Simple 2D Plots CS3330 Scienfic Compung 18 More Complicated func3on 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 Mul3ple 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 Prac3ce, a Tricky Ques3on • 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 Ini3alizing Matrices • Two ways to create matrices CS3330 Scienfic Compung 28 Excep3onal 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 solu3on? CS3330 Scienfic Compung 29 Excep3onal 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 solu3ons? 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 func3on 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 Mul3ple-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 Quan3c 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 basics of SageMath, including symbolic and numerical solu3ons, matrices, and simple plots • We will mostly use SageMath for symbolic soluons – SageMath was ini3ally designed for this – Other tasks are done in Matlab/Octave • References: – hKp://www.sagemath.org ß Official Web and resources – hKp://www.gregorybard.com/SAGE.html ß Our textbook CS3330 Scienfic Compung 47 SageMath #1 Homework (S1) 1. (1%) find at least 5 real number solu3ons 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 solu3on 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 iden3fy 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 .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    49 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us