M010Summer11-"SAGEbyStein"-tutorial-Ver4b

363 days ago by dg1234


SAGE for Math 010 (Summer 2011)

Software for Algebra and Geometry Experimentation

A practical alternative to
Maple, 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) are good enough (if used alone)"
  • 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
  • Reference Manual of over 3000 pages

 

Introduction: 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 #You may change these numbers to, say, 35 + -17 
       
3-17 
       
3*17 
       
3/17 
       
2^3 
       
(16-3*2+8)/(3-5) #Order of operations example 
       

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:

#Notice that nothing happens if we divide an already simplified fraction: 233/45 
       
#How many times does 45 go into 233? Use double slash: 233//45 
       
# What is the remainder of 233 divided by 45? Use % symbol as shown. Here % does not mean percent. 233%45 
       
#So, 233 divided by 45 is equal to 5 r. 8 
       

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

2^34 #Raising a number to a power is done using the caret ^ symbol (it's on the "6" of your keyboard) 
       
2**34 #Double asterisk ** works like the caret ^: should yield the same result as above. 
       
15*2^3 +(14-5*30) #By default SAGE uses the standard order of operations (PEMDAS) 
       
2*2*2*x*x*x*y*y 
       

SQUARE ROOTS

Use the square root function  sqrt( )

sqrt(4) #The square root of 4. 4 happens to have an exact square root. 
       
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 and decides to give you exact answer. Unless you want an approximation...(see next line below). 
       

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 rounded up to 5 digits 
       
n(233/45, digits=10) #Convert the fraction to decimal and write only the first 10 digits 
       
sqrt(x^3) 
       
show(_) #write in familiar "prettier" form 
       

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.

2+4 
       
_ + 2 #Use last result (from the cell last evaluated, normally the previous cell above) and add 2. 
       
#Important: Be careful when you evaluate cells by going back-and-forth in your worksheet. # The "underscore _" would remember the last cell you evaluated! So it may turn out not the way you expected. 
       
sqrt(x^3) 
       
_ + x^2 + 1 
       
show(_) #In this case, we want to show(the last result)and type it in a prettier format 
       
(_)^4/(x^(2/3)) #we raise the last result to 4th power and divide by "x raised to 2/3" 
       
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 "3" 
       

PREALGEBRA stuff:  VARIABLES, EXPRESSIONS, AND EQUATIONS

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

w, y = var('w,y') #This is how we declare that w and y are variables and must be treated "symbolically" #Now we can write expressions/equations involving y and w! (Below) 
       
-2*y + 5*w -4*y - 12*w + 2*x - 8 #Note: SAGE automatically simplifies your expression w/o being told! 
       
show(_) #show in prettier form 
       

ASSIGNMENT (=)

There are times we want to re-use an expression(s) or an equation(s)  and we don't want to retype those. What we want is to have some way of "storing" them.  Indeed, in SAGE we used the "assignment" symbol:  the single equal sign "=":

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

# We can store/assign symbol numbers to say "a" and "b" (both act like memory cells or storage boxes): a = 4 #This means assign to "a" the value 4. This is not an equation in SAGE. b = -5 a; b #Print contents of a and b. Use a semicolon ";" to separate two commands in a single line 
       

We can now perform operations on "a" and "b":

a+b; a-b; a*b; a/b #SAGE remembers what you stored until you store something else in a and b. 
       

Or we can assign complicated expressions that you can think of.  This will erase the previous values assigned to both a and b !

a = 1 + sqrt(2) + pi + 2/3 + w^y + 2*w^y #The single equal sign "=" means ASSIGN to "a" the following expression. a #This command line says: just type what "a" is 
       
show(a) #Show in prettier form #Note that SAGE has forgotten about our previous assignment a = 4 #because we replaced it with a new expression! 
       
b=2*pi*sqrt(2*pi)^3*x^-7 #Now, assign another expression to b. show(b) #Note that SAGE has forgotten about previous assignment for b! 
       
a+b; a-b; a*b #SAGE can add, subtract, or multiply the expressions assigned to a and b. #Looks ugly though! (Even so, this is the language that SAGE understands.) 
       
show(a+b);show(a-b);show(a*b) 
       

We can form expressions out of expressions stored in a and b:

c = 4*a^2 - 5*b^-3 show(c) 
       

Greatest Common Denominator (GCD) or Factor (GCF)

(You can skip this for now!)

#What is the GCD or GCF or 14 and 42? gcd(14,42) #greatest common factor/denominator #try large numbers as well 
       
#What is the GCD or GCF of 24, 64, and 142? gcd([24,64,142]) #use the square brackets [ ] if there are more than two numbers 
       
#We can also store the result using "assign": smith=gcd([15*x^2,150*x^3,30*x^7]) #Notice we have more than two expressions here so we use the [ ] show(smith) 
       

Least Common Multiple (LCM)

(You may skip this section for now!)

#Find the LCM of 12 and 18 lcm(12,18) 
       
lcm(12345,678910) 
       
lcm(range(1,100)) #LCM of all numbers in the range from 1 to 100. Very fast! Try up to 10000 
       
#Skip factoring for now puthere=factor(1/(x^2) + 1/(2*x^5)) show(puthere) 
       




SOLVING EQUATIONS

We use the double equal signs "= =" to write an equation.  Recall that the single equal sign "=" means just assigning or storing an expression to a symbol.

Consider the equation: 8x + 13 = -11.  Then store this equation in "a":

a = 8*x + 13 == -11 #Here we have assigned an equation to "a". we have "stored" an equation in memory "a"! show(a) #...and we want to see it in traditional and prettier form 
       

We use the solve command:

 solve( type your equation here, type the 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" containing fractions show(b) #This is cool because you can check if you typed in the equation properly #...especially if fractions are present! 
       
solve(b,x) #solve equation stored in "b" for variable x 
       
solve(3*x +4 == -8*x-5, x) #Means SOLVE(this equation, for x) 
       

A proportion problem:  12 is to x  as 2.4 is to 0.35

prop= 12/x==2.4/0.35 solve(prop, x) #A proportion is an equation: ratio = ratio 
       
#Skip Plotting for now! #A simple plot. Let's control the range of x values from -5 to 5 over which we would plot this expression plot(3*x + 4 , (-5,5)) 
       

 

THE END

 

published by dg1234 (summer 2011)