010-"SAGEbyStein"-expanded&modified for Math020

403 days ago by kyleeg133


SAGE for HACC-Math 010 & Beyond

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

 


                                                    SAGE = Python + Local Web Interface + Tons of Work

 

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) 
       
-14
-14
3-(-17) 
       
20
20
3*17 
       
51
51
3/17 
       
3/17
3/17
(16-3*2+8)/(3-5) 
       
-9
-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
29/320
(3+2/5)+(1+7/9) #Mixed fractions: "3 and 2/5" can be written here as (3+2/5) 
       
233/45
233/45

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

233//45 #two forward slashes: just get the whole number part of the quotient 233/45 or how many times 45 goes into 233 
       
5
5
233%45 #the remainder part of the quotient 233/45 
       
8
8
233//45 + 233%45/45 #Let's check if gives us the original fraction 
       
233/45
233/45
#233/45 is also 5 and 8/45 or (5+8/45) in mixed form. 
       

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

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
2
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. 
       
sqrt(5)
sqrt(5)

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 or use n(sqrt()) command below. 
       
2.23606797749979
2.23606797749979

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=4) #The number "n" instruction lets you write the square root of 5 rounded up to 4 digits 
       
2.236
2.236
n(233/45, digits=10) #Convert the fraction to decimal and write only the first 10 digits 
       
5.177777778
5.177777778
sqrt(x^3) 
       
sqrt(x^3)
sqrt(x^3)
show(_) #write in familiar "prettier" form 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\sqrt{x^{3}}
\newcommand{\Bold}[1]{\mathbf{#1}}\sqrt{x^{3}}

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
6
_ + 2 #Use last result (from the cell last evaluated, normally the previous cell above) and add 2. 
       
8
8
sqrt(x^3) 
       
sqrt(x^3)
sqrt(x^3)
_ + x^2 + 1 
       
sqrt(x^3) + x^2 + 1
sqrt(x^3) + x^2 + 1
show(_) #In this case, we want to show(the last result)and type it in a prettier format 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\sqrt{x^{3}} + x^{2} + 1
\newcommand{\Bold}[1]{\mathbf{#1}}\sqrt{x^{3}} + x^{2} + 1
(_)^4/(x^(2/3)) #we raise the last result to 4th power and divide by "x raised to 2/3" 
       
(sqrt(x^3) + x^2 + 1)^4/x^(2/3)
(sqrt(x^3) + x^2 + 1)^4/x^(2/3)
show(_) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{{\left(\sqrt{x^{3}} + x^{2} + 1\right)}^{4}}{x^{\frac{2}{3}}}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{{\left(\sqrt{x^{3}} + x^{2} + 1\right)}^{4}}{x^{\frac{2}{3}}}
pi #SAGE knows what "pi" is! 
       
pi
pi
show(_) #Use standard "prettier" symbol for pi. 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\pi
\newcommand{\Bold}[1]{\mathbf{#1}}\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.14
3.1415926535897932384626433832795028841971693993751058209749445923078164\
06286208998628034825342117068
3.14
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
var ('C,r'); C = 2*pi*r #define variables C and r, then write formula for the circumference of a circle 
       
(C, r)
(C, r)
show(C) #and you can use C to calculate the circumference any number of times given diffrent radii. 
       
\newcommand{\Bold}[1]{\mathbf{#1}}2 \, \pi r
\newcommand{\Bold}[1]{\mathbf{#1}}2 \, \pi r

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: 

y, z = var('y,z') #This is how we declare that y and z 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 + y^z #The single equal sign "=" means ASSIGN to "a" the following expression. #I tried to put in all #the possible operations in "pemdas". You can make up your own "crazy" expressions. a #This says: just type what "a" is (not good for the eyes though.) #Aha...we can put more than one command on a given cell! 
       
pi + sqrt(2) + y^z + 5/3
pi + sqrt(2) + y^z + 5/3
show(a) #Type in prettier form 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\pi + \sqrt{2} + y^{z} + \frac{5}{3}
\newcommand{\Bold}[1]{\mathbf{#1}}\pi + \sqrt{2} + y^{z} + \frac{5}{3}
b=2*pi*sqrt(2*pi)^3*x^-7 #Now, assign another expression to b. Try to be creative here. 
       
show(b) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{4 \, \pi^{\left(\frac{5}{2}\right)} \sqrt{2}}{x^{7}}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{4 \, \pi^{\left(\frac{5}{2}\right)} \sqrt{2}}{x^{7}}
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 
       
pi + sqrt(2) + 4*pi^(5/2)*sqrt(2)/x^7 + y^z + 5/3
pi + sqrt(2) - 4*pi^(5/2)*sqrt(2)/x^7 + y^z + 5/3
4/3*(3*pi + 3*sqrt(2) + 3*y^z + 5)*pi^(5/2)*sqrt(2)/x^7
pi + sqrt(2) + 4*pi^(5/2)*sqrt(2)/x^7 + y^z + 5/3
pi + sqrt(2) - 4*pi^(5/2)*sqrt(2)/x^7 + y^z + 5/3
4/3*(3*pi + 3*sqrt(2) + 3*y^z + 5)*pi^(5/2)*sqrt(2)/x^7
show(a+b);show(a-b);show(a*b) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\pi + \sqrt{2} + \frac{4 \, \pi^{\left(\frac{5}{2}\right)} \sqrt{2}}{x^{7}} + y^{z} + \frac{5}{3}
\newcommand{\Bold}[1]{\mathbf{#1}}\pi + \sqrt{2} - \frac{4 \, \pi^{\left(\frac{5}{2}\right)} \sqrt{2}}{x^{7}} + y^{z} + \frac{5}{3}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{4 \, {\left(3 \, \pi + 3 \, \sqrt{2} + 3 \, y^{z} + 5\right)} \pi^{\left(\frac{5}{2}\right)} \sqrt{2}}{3 \, x^{7}}
\newcommand{\Bold}[1]{\mathbf{#1}}\pi + \sqrt{2} + \frac{4 \, \pi^{\left(\frac{5}{2}\right)} \sqrt{2}}{x^{7}} + y^{z} + \frac{5}{3}
\newcommand{\Bold}[1]{\mathbf{#1}}\pi + \sqrt{2} - \frac{4 \, \pi^{\left(\frac{5}{2}\right)} \sqrt{2}}{x^{7}} + y^{z} + \frac{5}{3}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{4 \, {\left(3 \, \pi + 3 \, \sqrt{2} + 3 \, y^{z} + 5\right)} \pi^{\left(\frac{5}{2}\right)} \sqrt{2}}{3 \, x^{7}}
4*a^2 - 5*b^-3 #And more. show(_) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{4 \, {\left(3 \, \pi + 3 \, \sqrt{2} + 3 \, y^{z} + 5\right)} \pi^{\left(\frac{5}{2}\right)} \sqrt{2}}{3 \, x^{7}}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{4 \, {\left(3 \, \pi + 3 \, \sqrt{2} + 3 \, y^{z} + 5\right)} \pi^{\left(\frac{5}{2}\right)} \sqrt{2}}{3 \, x^{7}}
a/b #Dividing expressions show(_) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{4 \, {\left(3 \, \pi + 3 \, \sqrt{2} + 3 \, y^{z} + 5\right)} \pi^{\left(\frac{5}{2}\right)} \sqrt{2}}{3 \, x^{7}}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{4 \, {\left(3 \, \pi + 3 \, \sqrt{2} + 3 \, y^{z} + 5\right)} \pi^{\left(\frac{5}{2}\right)} \sqrt{2}}{3 \, x^{7}}


gcd(14,42) #greatest common factor (or denominator) #try large numbers as well like gcd(123456,653445) 
       
14
14
gcd([24,64,142]) #we have to use the square brackets [ ] if there are more than two numbers 
       
2
2
gcd([15*x^2,150*x^3,30*x^7]) #Notice we have more than two expressions here so we use the [ ] 
       
15*x^2
15*x^2

lcm(12,18) #you can test SAGE on small numbers or denominators 
       
36
36
lcm(123456,654322) #or even large numbers 
       
40389988416
40389988416
lcm(range(1,100)) #LCM of all numbers in the range from 1 to 100. Very fast! Try up to 10000 
       
69720375229712477164533808935312303556800
69720375229712477164533808935312303556800

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.   We have used the "ladder method" to do this (see your notes).

factor(1/(x^2) + 1/(2*x^5)) #you will do an in-depth study of Factoring in Math 020 
       
1/2*(2*x^3 + 1)/x^5
1/2*(2*x^3 + 1)/x^5
show(_) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{2 \, x^{3} + 1}{2 \, x^{5}}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{2 \, x^{3} + 1}{2 \, x^{5}}
solve(a,x) #means solve(equation assigned to "a", for variable x) 
       
[]
[]

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.

Let us say we want to solve the following:   3x +4 = -8x- 5. Then  we would type this  as:

solve(3*x +4 == -8*x-5, x) #Means SOLVE(this equation, for x) SAGE needs to know what variable you're interested in solving! 
       
[x == (-9/11)]
[x == (-9/11)]
solve(x**2+2*x==0, x) #more examples 
       
[x == -2, x == 0]
[x == -2, x == 0]
solve(6*x**2 -13*x - 28==0, x) #and more! 
       
[x == (7/2), x == (-4/3)]
[x == (7/2), x == (-4/3)]
solve(3*x + 4 == -8*y-5,x) #Let's see what SAGE will do if we have an equation containing two variables x and y. show(_) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = \left(\frac{7}{2}\right), x = \left(-\frac{4}{3}\right)\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = \left(\frac{7}{2}\right), x = \left(-\frac{4}{3}\right)\right]
solve(3*x + 4 == -8*y-5,y) #This time we solve for y! We have defined y as variable previously. show(_) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = \left(\frac{7}{2}\right), x = \left(-\frac{4}{3}\right)\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = \left(\frac{7}{2}\right), x = \left(-\frac{4}{3}\right)\right]

PLOTTING

x,y,z = var('x,y,z') #Let's setup our activities below by defining x,y, and z as variables. 
       
y = 2*x - 5 #Here's a linear equation containing x and y. It is called "linear" because it will plot a straight line. plot(y,(-10,10)) #We tell SAGE to plot y with x ranging from -10 to +10. 
       
z=-3*x+7 plot([y,z],(-10,10)) #Note the use of [ ] to enclose the two lines y and z we want to plot. 
       
f = sin(3*x)*x+log(x) + 1/(x+1)^2 #Here's a more complicated expression. We assign to f the given expression show(f) #Show f in prettier form 
       
\newcommand{\Bold}[1]{\mathbf{#1}}x \sin\left(3 \, x\right) + \frac{1}{{\left(x + 1\right)}^{2}} + \log\left(x\right)
\newcommand{\Bold}[1]{\mathbf{#1}}x \sin\left(3 \, x\right) + \frac{1}{{\left(x + 1\right)}^{2}} + \log\left(x\right)
plot(f,(0.01,1)) 
       
plot([x**2+2*x-1,x**2+2*x,x**2+2*x+1,x**2+2*x+2],(-2,2)) #We can plot several graphs at a time to compare them! 
       

 

PS: Feel free to contact me in my email on MML. 
       
Traceback (click to the left of this block for traceback)
...
SyntaxError: invalid syntax
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_65.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("UFM6IEZlZWwgZnJlZSB0byBjb250YWN0IG1lIGluIG15IGVtYWlsIG9uIE1NTC4="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmpRCG8nj/___code___.py", line 2
    PS: Feel free to contact me in my email on MML.
      ^
SyntaxError: invalid syntax