Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168732
Image: ubuntu2004

SAGE for HACC-Math 010

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) 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
  • Reference Manual of over 3000 pages

 

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 can change these numbers to, say, 35 + -17
20
3-17
-14
3*17
51
3/17
3/17
(16-3*2+8)/(3-5)
-9

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
29/320
(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:

#Skip

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 #Should yield the same result as above.
5*2^3 -1 #By default SAGE uses the standard order of operations
x^2-30**x+3^2 + 12*x #SAGE will try to combine like terms and simplify expressions as much as possible

SQUARE ROOTS

Use 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
2*I
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.

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
6
_ + 2 #Use last result (from the cell last evaluated, normally the previous cell above) and add 2.
8
sqrt(x^3)
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.
var ('C,r'); C = 2*pi*r #define variables C and r, then write formula for the circumference of a circle
show(C)

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: 

w, y = var('w,y') #This is how we declare that w and y are variables and must be treated "symbolically"

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

a = 1 + sqrt(2) + pi + 2/3 + w^y #The single equal sign "=" means ASSIGN to "a" the following expression. a #This says: just type what "a" is
show(a)
b=2*pi*sqrt(2*pi)^3*x^-7 #Now, assign another expression to b.
show(b)
a+b; a-b; a*b #SAGE can add, subtract, or multiply the expressions assigned to a and b. Note "b" is not a polynomial because x is in the denominator
show(a+b);show(a-b);show(a*b)
4*a^2 - 5*b^-3 #And more.
a/b #Dividing expressions
show(_)
###NOTE: In the below, disregard "Dividing Polynomials", "Negative Exponents", "Plotting", and "Animation".
##-------temporarily disabled sections do not have cells available below them-just skip these sections----### ##-------they are not yet relevant to Math 010 -----------------------------------------------------------###

DIVIDING POLYNOMIALS

This is a bit tricky: How does one tell SAGE how to divide two polynomials?  Answer: It depends on (1) SAGE's default format and (2) on what format you want it to be displayed. SAGE's default format is try to write things in simplest form w/c means as a factored form. So sometimes, one would think it's not doing anything.

Won't explain this one in detail. You can study it in the manual for details. The important thing is this is how you can divide and format   the result as a sum of terms in descending order.

This is the format we want. The remainder is 432. Hence, we can write the overall result also as:  

2*x^4 - 10*x^3 + 28*x^2 - 8*x + 96 + 432/(x-5)

Another way to do this would be to use the "cryptic" code.  W(P(x)) says consider P(x) as a polynomial. Then take the quotient plus the remainder using (x-5). Oh well....

NEGATIVE EXPONENTS are automatically converted to (simplified to) positive exponents

GREATEST COMMON FACTOR(DENOMINATOR)

gcd(14,42) #greatest common factor/denominator #try large numbers as well
gcd([24,64,142]) #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(123456,654322)
lcm(range(1,100)) #LCM of all numbers in the range from 1 to 100. Very fast! Try up to 10000

Here's a "trick" you can use to get 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.

factor(1/(x^2) + 1/(2*x^5))
show(_)

FACTORING

#Skip Factoring Section. Proceed to Solving Equations below.

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 = (18*x - 13)/5 == (3*x + 5)/2 - (4 - x)/3 #here we have assigned an equation to "a". show(a) #and we want to see it in traditional and prettier form #this is cool because you can check if you typed in the equation properly

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)

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(12/x==2.4/0.35, x) #proportion problem note the use of "==" instead of just one "="!!
solve(6*x**2 -13*x - 28==0, x) #and more!
#Plotting and Animation temporarily disabled so your worksheet can run faster

PLOTTING


An Example of a 3D plot you can play around with!

ANIMATION where time is involved! 

Visit:  http://sage.math.washington.edu/home/mhansen/sage-epydoc/sage.plot.animate.Animation-class.html

THE END

/published by dan@hacc2010