010-"SAGEbyStein"-expanded&modified for Math010-v1

694 days ago by dan@hacclanc


SAGE for HACC-Math 010

Software for Algebra and Geometry Experimentation

A practical alternative to
Maple, Magma, Mathematica, and Matlab

Sage is 100% Free and Open Source Software

                                                 while Mathematica, Matlab, and Maple are very expensive.

Main website: sagemath.org

 


History

  • William Stein, Associate Professor, University of Washington  started Sage at Harvard in January 2005
  • He realized that "No mathematical software (free or commercial) good enough."
  • Sage-1.0 released February 2006 at Sage Days 1 (San Diego).
  • Sage Days 1, 2, ..., 11, at UCLA, UW, Cambridge, Bristol, Austin, France, San Diego, Seattle, etc.
  • Funding from UW, NSF, DoD, Microsoft, Google, Sun, private donors, etc.
                                                    SAGE = Python + Local Web Interface + Tons of Work

 

What is Sage? (according to W. Stein)

  • Well over 300,000 lines of new Python/Cython code
  • A Distribution of mathematical software (over 60 third-party packages); builds from source without dependency (over 5 million lines of code)
  • Exact and numerical linear algebra, optimization (numpy, scipy, R, and gsl all included)
  • Group theory, number theory, combinatorics, graph theory
  • Symbolic calculus
  • Coding theory, cryptography and cryptanalysis
  • 2d and 3d plotting
  • Statistics (Sage includes R)
  • Overall range of functionality rivals that of Maple, Matlab, and Mathematica
  • Sage is amazingly huge

 

A GOOD CALCULATOR - but a lot better than others

We can do simple arithmetic with SAGE:

Use:  * for multiplication,  + for addition, - for subtraction, / divide, ^ or ** for exponentiation

Place mouse on the cell below. Then press "evaluate" that appears below this cell. Or press Shift-Enter. (Note: don't put the equals (=) sign! )


3+17 
       
3-17 
       
3*17 
       
3/17 
       
(16-3*2+8)/(3-5) 
       
#DECIMALS #Note: SAGE will not answer in decimal unless instructed to do so. Just like in 3/17 above. #We give a decimal example below. SAGE now knows you have decimals. The decimal point is the key. 
       
3./17 
       
8.25 + 3.2745 - 0.00567*2.4 
       
#Below we shall learn how to limit the number of decimal places (hence, digits also) that will be displayed 
       

At the very least, it can do what any calculator can do.  SAGE will try to perform everything according to the standard ORDER of operations.

 

FRACTIONS

It can handle fractions symbolically (manipulate algebraically as if all are symbols) and numerically. More on the difference later.

We will use the pound sign "#" to place a comment within the cell without affecting what you're calculating. SAGE-python knows this.

3/15 + 2/32 - 11/64 #This is a comment: everything after the pound "#" sign is considered a comment and is not processed 
       
(3+2/5)+(1+7/9) #Mixed fractions: "3 and 2/5" can be written here as (3+2/5) 
       

Now we can further process that last result using the following method:

233//45 #two forward slashes: gives the WHOLE number part of the quotient 233/45 or how many times 45 goes into 233 
       
233%45 #"percent" symbol is used differently: gives the REMAINDER part of the quotient 233/45 
       
#233/45 is also 5 and 8/45 in mixed form. To experiment with mixed fractions, you would write (5+8/45). Remember to use the parenthesis to group the whole and fractional parts together! 
       
#check by adding 5 and 8/45 to left of this "#" sign. Evaluate. 
       

Handling Exponents using the caret ^ symbol or double asterisk  **.

2^34 #Raising a number to a power is done using the ^ symbol (it's on the "6" of your keyboard) 
       
2**34 #Since SAGE uses PYTHON, ** also means exponentiation. Should yield the same result as above. 
       
5*2^3 -1 #By default SAGE uses the standard order of operations. This can be useful in checking exercises in your text! 
       

SQUARE ROOTS

Using the square root function  sqrt( )

sqrt(4) #The square root of 4. 4 happens to have an exact square root. You can plug-in any other number, say, 121 
       
sqrt(5) #Get the exact square root of 5. Notice it can't because it's irrational #-- it's non-repeating and non-terminating decimal. SAGE knows this 
       
#What is the square root of 45,679? Don't use the comma! Type answer on left of "#" sign and evaluate. 
       

Notice that if it can't get the exact value of the square root of 5, it will handle it just symbolically. And it would seem like it didn't do anything. You see it simply wants to try to maintain an exact value.  To force it to perform and provide at least an estimate of the square root, just add a decimal point so that SAGE knows your input is a decimal and you want it to give an answer in decimal form.  Then in the next cell we introduce the number "n" operator to control the number of digits of our final answer/estimate.

sqrt(5.) #add a decimal point 
       

Then in the next cell we introduce the number "n" operator to control the number of digits of our final answer/estimate. This is even better.

n(sqrt(5),digits=5) #The number "n" instruction lets you write the square root of 5 IN DECIMAL rounded up to 5digits #Rounding to 5 digits doesn't mean 5 decimal places. The 5 includes all digits in the number. #Try other numbers but restore it back to 5 and save. 
       
n(233/45, digits=10) #Convert the fraction to decimal and write only the first 10 digits 
       
n(8.25 + 3.2745 - 0.00567*2.4, digits=5) #you can specify the number of digits (called precision) 
       

Using the UNDERSCORE "_ "  and the SHOW( ) functions or commands:

We use the underscore "_" symbol to mean "use the last result"  OR more precisely, the result of the last cell that was evaluated.  This is a really great time-saver as it aids in better manipulation w/o retyping longer and complicated expressions if they happen to be one.

4+(-9) 
       
_ + 2 #Use last result (from the cell last evaluated, normally the previous cell above) and add 2. 
       
#Translate: two times the square root of cube of x. #Hint: sqrt(cube of x). #Write answer on the cell below. Evaluate it. 
       
 
       
_ + x^2 + 1 
       
show(_) #In this case, we want to show(the last result)and type it in a PRETTIER format 
       
(_)^4/(x^2) #we raise the last result to 4th power and divide by "x squared" 
       
show(_) 
       
pi #SAGE knows what "pi" is! 
       
show(_) #Use standard "prettier" symbol for pi. 
       

PI and the SEMICOLON

SAGE can be used do display \pi's  approximate value up to a certain number of digits.  Note: we use a semicolon to separate the instructions or commands we want SAGE to do for us. This means we can do multiple instructions within one cell.

n(pi,digits=3); n(pi,digits=100) #Write pi up to 3 digits then up to 100 digits including the whole number part. 
       

BASIC ALGEBRA:  VARIABLES, EXPRESSIONS, AND EQUATIONS

In SAGE, x is considered a variable. To use other letters as variables, one must "declare" them using the var command: 

var('y') #This is how we DECLARE that y is a VARIABLE and must be treated "symbolically" # We will use variables x and y below. x is already a variable by default. 
       
2x -3y # For 2x, you will input 2*x. Do same for the second term. An error will result if you forget to put the *. 
       
#insert the corrected expression here (BEFORE the "#" sign). Or create a new cell below. Evaluate. 
       
6x^2y + -13x - 28x #forgetting to put * to multiply factors within each term results in an "error message" #Cut and paste the expression to the cell below and make the correction. 
       
#insert the corrected expression here (before the "#" sign). Evaluate. SAGE will give you a simplified form. 
       
#DECLARING TWO OR MORE VARIABLES 
       
var('w,z') #This is how we DECLARE that w and z are VARIABLES and must be treated "symbolically" # We will use variables w and y below. 
       

ASSIGNing (using one equal sign =) a whole expression to another letter makes it easier to manipulate expressions. We demonstrate this below:

a = sqrt(5)+(12/30)*w^3 -(35/21*z^2) #The single equal sign "=" means ASSIGN to "a" the following expression. #In other words, we're saying, "Let "a" be equal to this expression" a #This says: just type what "a" is in text format- notice SAGE will try to simplify as much as possible. #Notice also in the result, SAGE got rid of the ( ) in the fractions. That means you can do #it also and SAGE will understand. 
       
show(a) #SAGE will try to maintain EXACT answer. That's why it didn't estimate the square root of 5. 
       
b = 34/24*w^3*z^7 #Now, assign another expression to b. b #This says: just type what "b" is -- notice SAGE will try to simplify as much as possible 
       
show(b) #prettier or traditional form 
       
a+b; a-b; a*b #SAGE can add, subtract, or multiply the expressions assigned to a and b. 
       
show(a+b);show(a-b);show(a*b) #show results in a prettier way. 
       
show(4*a^2*b - 5*b^3/a) #You can re-use your a and b to build more complex expressions. You don't have to use show(). 
       

GREATEST COMMON FACTOR(DENOMINATOR)

gcd(14,42) #greatest common factor/denominator #try larger numbers as well 
       
gcd(14234,426782) 
       
gcd([25,640,145]) #use the square brackets [ ] if there are more than two numbers 
       
gcd([15*x^2,150*x^3,30*x^7]) #Notice we have more than two expressions here so we use the [ ] 
       

LEAST COMMON MULTIPLE (LCM)

lcm(25,160) 
       
lcm(123456,654322) 
       
lcm([25,160,365]) #You need square brackets [ ] if more than two numbers. 
       
lcm(range(1,100)) #LCM of all numbers in the RANGE from 1 to 100. No [ ] needed. Very fast! Try up to 10000 
       
lcm([x^2,2*x^3,30*x^7]) #Oops! sometimes SAGE may not let you do some stuff directly. Possible conflict. 
       

Here's a "trick" you can use to get the LCM of two variable expressions like  x^{2} and 2x^{5}. First, make them the denominators of unit fractions (1 over your expression). Pretend you will add them all.  Then  use the factor command to force SAGE to combine them into one rational expression.  Then you can also use "show" to get a better view of the result. Finally, you can see that the LCD (or our LCM) here is 2x^{5}.

1/x^2 + 1/(2*x^5) 
       
show(_) #Show last result (underscore!) and write in prettier format. 
       
factor(_) #FACTOR the last result. SAGE tries to combine into one fraction. 
       
show(_) #What is the LCM/LCD (in this case, it's the denominator of the resulting fraction) 
       
#Create cells below and find the LCM for 12x, 24xy^2, and 36x^3y. Cut and paste and make corrections before evaluating. 
       
var('y') #we first declare y as variable again, then add fractions 
       
factor(_);show(_) #The LCM is ___ 
       

PRODUCTS: Forgetting to use *

6x**2 + -13x - 28 #forgetting to put * to multiply factors within each term results in an "error message" 
       
6*x**2 -13*x - 28 #we correct error made above or you can do it right on the above cell, then re-evaluate. 
       

SOLVING EQUATIONS

We use the double equal signs "= =" to write an equation.  The single equal sign "=" as in some examples above mean just assigning an expression to, say, a variable so we can reuse that expression without retyping it.

a = 8*x - 1 == 2*x + 5 #here we have assigned an EQUATION to "a" (not just an expression) show(a) #and we want to see it in traditional form 
       

We use solve(equation, variable to be solved).  Note: use parenthesis ( ) not square brackets [ ].

solve(a,x) #means solve(equation assigned to "a", for variable x) 
       
#Another example: 
       
b = (18*x - 13)/5 == (3*x + 5)/2 - (4 - x)/3 #here we have assigned an equation to "b". show(b) #and we want to see it in traditional form 
       
solve(b,x) #means solve(equation assigned to "b", for variable x) 
       

Or we can directly put the equation inside the solve( put equation here, put variable to be solved here) like this:

solve(3*x +4 == -8*x-5, x) #Means SOLVE(this equation, for x) 
       
solve(x**2+2*x==0, x) #more examples 
       
solve(6*x**2 -13*x - 28==0, x) #and more! 
       
#That's it. #I hope you enjoyed the ride! #Below are just some extras that you may want to tinker with. 
       

PLOTTING

#This part has been included for later use. 
       
var('t,q') #We tell SAGE that t and q must be treated as variables. 
       
q = 3*t +2 #We assign to q the given expression. Assume t stands for time show(q) 
       
plot(q,(-5,5)) #the (-5,5) means let t go from -5 and 5. You can change these and re-evaluate the plot! 
       

We can plot several graphs at a time for easy comparison:  x^{2} + 2 \, x - 1,   x^{2} + 2 \, x,   x^{2} + 2 \, x + 1,   x^{2} + 2 \, x + 2

plot([x**2+2*x-1,x**2+2*x,x**2+2*x+1,x**2+2*x+2],(-2,2)) #several at a time 
       
P(x)=x^2-4*x+3 show(P(x)) 
       
plot(P(x),(x,-4,10)).show(ymin=-1,ymax=1,figsize=4) #you can experiment with more parameters, changing the numbers and re-evaluating. 
       

SAGE can also do 3D plots and animation. See numerous published worksheets.

THE END

PS: More to come!