Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168756
Image: ubuntu2004

A Quick Introduction to Sage 4.3

Tim McLarnan
Earlham College

Getting Started

To use Sage from the Notebook interface, you need to connect to a Sage server.  There are several ways to do this.  If you are just playing with Sage for the first time, one easy thing is to go to http://sagenb.org/ (sagenb stands for Sage Notebook) and sign up for a free account.  Alternatively, you can use Earlham's own Sage Notebook server.

If you have installed Sage on your machine and want to use your own Sage installation, start Sage (on the Mac, by opening /Applications/sage/sage with the Terminal) and type notebook() at the sage: prompt.  If the Notebook server is already running on your machine, just open a browser window and connect to http://localhost:8000.

Once you have opened a new Worksheet, you are ready to go.  Type Sage commands in the rectangular input cells.  Send those commands to Sage by hitting shift-Enter.  Multiple Sage commands can be placed in a single input cell, either on separate lines (gotten by just hitting Return) or separated by semicolons.

New input cells can be created by clicking on the blue line that appears when you mouse above an existing cell.  New cells for html input are created by shift-clicking on these bars.  There is more detail about text editing in Sage at the end of this document.

Now let's do some mathematics.

Basic Commands

+, -, * and / do what you expect:

2+5
\newcommand{\Bold}[1]{\mathbf{#1}}7

Unlike most calculators, though, Sage does operations with fractions exactly.

1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{223}{140}

Other arithmetic operations are a^b or a**b, which means aba^b, and n.factorial() or factorial(n), which means n!=123nn! = 1*2*3*\cdots*n.

2^1000
\newcommand{\Bold}[1]{\mathbf{#1}}10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
print(2^1000)
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
(355/113)**10
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{31789487802830871103515625}{339456738992222314849}
factorial(4)
\newcommand{\Bold}[1]{\mathbf{#1}}24
4.factorial()
\newcommand{\Bold}[1]{\mathbf{#1}}24
print(125.factorial())
188267717688892609974376770249160085759540364871492425887598231508353156331613598866882932889495923133646405445930057740630161919341380597818883457558547055524326375565007131770880000000000000000000000000000000

Is it a surprise that 125! ends in so many zeros?  Can you find a way to compute how many zeros are at the end of n!n! for any nn?

The past couple of lines, and the rest of this Worksheet, show an important thing about Sage.  Some common Sage commands are implemented as functions applied to arguments, with syntax like f(x).  Many other commands are implemented as methods applied to objects, with the less familiar syntax x.f().  Some commands, like factorial, can be used either way.  There are good software engineering reasons for doing things this way, and those of you who are CS students either know or will learn what they are.  For the rest of us, all we need to know is that some commands have one form, and some have the other, and that we need to remember which commands work which way.  Of course, if one forgets, one can try it both ways and see what works.

factor factors an integer into primes.  Like many Sage functions, it can either be called as a function sent the argument nn: factor(n), or as a message sent to the object nn: n.factor().

factor(315)
\newcommand{\Bold}[1]{\mathbf{#1}}3^{2} \cdot 5 \cdot 7
315.factor()
\newcommand{\Bold}[1]{\mathbf{#1}}3^{2} \cdot 5 \cdot 7
20.factorial() - 12.factorial()
\newcommand{\Bold}[1]{\mathbf{#1}}2432902007697638400

The result of the previous computation in Sage is denoted _.  Thus, if we want to factor the number we just computed, we can just say

factor(_)
\newcommand{\Bold}[1]{\mathbf{#1}}2^{10} \cdot 3^{5} \cdot 5^{2} \cdot 7 \cdot 11^{3} \cdot 41976119

Notice that this result is pretty remarkable: Sage has asserted (and very quickly) that 41976119 is a prime.

Decimal Approximations

Sage's usual mode of operation is to do exact arithmetic with no roundoff or decimal approximation.

2^30/3^20*sqrt(2)
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1073741824}{3486784401} \, \sqrt{2}

The function (or method) n gives a numerical approximation to this number.

_.n()
\newcommand{\Bold}[1]{\mathbf{#1}}0.435501618497698

If this isn't yet enough precision, n can be given a second optional argument specifying how many digits precision you want:

(2^30/3^20*sqrt(2)).n(digits=100)
\newcommand{\Bold}[1]{\mathbf{#1}}0.4355016184976975426780034699980734981419044126751400227013960351069068541814735297648401904204298624

Would you like to know the first few digits of π\pi?

print(pi.n(digits=5000))
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275900994657640789512694683983525957098258226205224894077267194782684826014769909026401363944374553050682034962524517493996514314298091906592509372216964615157098583874105978859597729754989301617539284681382686838689427741559918559252459539594310499725246808459872736446958486538367362226260991246080512438843904512441365497627807977156914359977001296160894416948685558484063534220722258284886481584560285060168427394522674676788952521385225499546667278239864565961163548862305774564980355936345681743241125150760694794510965960940252288797108931456691368672287489405601015033086179286809208747609178249385890097149096759852613655497818931297848216829989487226588048575640142704775551323796414515237462343645428584447952658678210511413547357395231134271661021359695362314429524849371871101457654035902799344037420073105785390621983874478084784896833214457138687519435064302184531910484810053706146806749192781911979399520614196634287544406437451237181921799983910159195618146751426912397489409071864942319615679452080951465502252316038819301420937621378559566389377870830390697920773467221825625996615014215030680384477345492026054146659252014974428507325186660021324340881907104863317346496514539057962685610055081066587969981635747363840525714591028970641401109712062804390397595156771577004203378699360072305587631763594218731251471205329281918261861258673215791984148488291644706095752706957220917567116722910981690915280173506712748583222871835209353965725121083579151369882091444210067510334671103141267111369908658516398315019701651511685171437657618351556508849099898599823873455283316355076479185358932261854896321329330898570642046752590709154814165498594616371802709819943099244889575712828905923233260972997120844335732654893823911932597463667305836041428138830320382490375898524374417029132765618093773444030707469211201913020330380197621101100449293215160842444859637669838952286847831235526582131449576857262433441893039686426243410773226978028073189154411010446823252716201052652272111660396665573092547110557853763466820653109896526918620564769312570586356620185581007293606598764861179104533488503461136576867532494416680396265797877185560845529654126654085306143444318586769751456614068007002378776591344017127494704205622305389945613140711270004078547332699390814546646458807972708266830634328587856983052358089330657574067954571637752542021149557615814002501262285941302164715509792592309907965473761255176567513575178296664547791745011299614890304639947132962107340437518957359614589019389713111790429782856475032031986915140287080859904801094121472213179476477726224142548545403321571853061422881375850430633217518297986622371721591607716692547487389866549494501146540628433663937900397692656721463853067360965712091807638327166416274888800786925602902284721040317211860820419000422966171196377921337575114959501566049631862947265473642523081770367515906735023507283540567040386743513622224771589150495309844489333096340878076932599397805419341447377441842631298608099888687413260472

Variable Expressions

Sage also works with variable expressions.  To tell it that xx and yy are variables, we have to use the syntax below.

var('x y')
\newcommand{\Bold}[1]{\mathbf{#1}}\left(x, y\right)
(2*x+7) * (x^2+3) * (x+2)^5
\newcommand{\Bold}[1]{\mathbf{#1}}{\left(x + 2\right)}^{5} {\left(2 \, x + 7\right)} {\left(x^{2} + 3\right)}
expand(_)
\newcommand{\Bold}[1]{\mathbf{#1}}2 \, x^{8} + 27 \, x^{7} + 156 \, x^{6} + 521 \, x^{5} + 1170 \, x^{4} + 1944 \, x^{3} + 2384 \, x^{2} + 1872 \, x + 672

That saved a bit of work, but Sage can do far more than this.  At this point, Sage no longer remembers where this polynomial came from.  It's just a random polynomial.  But Sage can still factor it:

_.factor()
\newcommand{\Bold}[1]{\mathbf{#1}}{\left(x + 2\right)}^{5} {\left(2 \, x + 7\right)} {\left(x^{2} + 3\right)}

Could you have factored this polynomial as fast as Sage did?  Could you have factored it at all?  (If you think about it a bit, your answers to these questions should be "no" and either "yes" or "maybe".)

Assignment and Simplification

Often we need to assign a name to the result of a computation.  Sage does this using the syntax variable = value.  In making an assignment, Sage does some obvious simplifications first.

e1 = (x+y)^3*(x+y)^2

Notice that an assignment statement doesn't print any return value.  If we want the value if e1, we need to ask for it explicitly.  We can do this either in a separate Sage command or by doing two Sage commands together, separated either by a newline or by a semicolon.

e1
\newcommand{\Bold}[1]{\mathbf{#1}}{\left(x + y\right)}^{5}
e2 = (x+y)^4*(x+y)^2 e2
\newcommand{\Bold}[1]{\mathbf{#1}}{\left(x + y\right)}^{6}
e3 = (x+y)^4*(x+y)^3; e3
\newcommand{\Bold}[1]{\mathbf{#1}}{\left(x + y\right)}^{7}

Often we need to simplify mathematical expressions.  This is a complicated task, since there are so many manipulations we could do to most expressions, and since deciding what form is simplest is sometimes a judgement call.  Sage has a number of methods that can be used to try to simplify expressions.  Sometimes one has to try several in order to get the right result.  Here we assign a new value to e2 and then try calling first simplify() and then simplify_rational().  There are also methods called simplify_log(), simplify_trig(), simplify_exp(), simplify_radical(), and simplify_full().

e2 = (x^3-y^3)/(x^2+x-y-y^2) e2
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{{\left(x^{3} - y^{3}\right)}}{{\left(x^{2} - y^{2} + x - y\right)}}
e2.simplify()
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{{\left(x^{3} - y^{3}\right)}}{{\left(x^{2} - y^{2} + x - y\right)}}
e2.simplify_rational()
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{{\left(x^{2} + x y + y^{2}\right)}}{{\left(x + y + 1\right)}}

Can we understand where this simplification came from?  Well, what if we take the numerator and denominator of e2 and factor them separately:

e2.numerator()
\newcommand{\Bold}[1]{\mathbf{#1}}x^{3} - y^{3}
_.factor()
\newcommand{\Bold}[1]{\mathbf{#1}}{\left(x - y\right)} {\left(x^{2} + x y + y^{2}\right)}
e2.denominator().factor()
\newcommand{\Bold}[1]{\mathbf{#1}}{\left(x - y\right)} {\left(x + y + 1\right)}

Ah, so the simplification just came from factoring both the top and bottom and then cancelling the common factor.  We could get the same result by just factoring e2.

e2.factor()
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{{\left(x^{2} + x y + y^{2}\right)}}{{\left(x + y + 1\right)}}

Solving Equations

Sage can also solve equations, even symbolic ones.  Notice the syntax of the commands below.  You have to specify first the equation, then the variables you want to solve for.  Also, since = is already taken as the assignment operator, equations are written using ==.  This agrees with the notation used in the C programming language, or in Python, in which Sage is written.

solve(x^2 + 5*x + 2 == 0, x)
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = -\frac{1}{2} \, \sqrt{17} - \frac{5}{2}, x = \frac{1}{2} \, \sqrt{17} - \frac{5}{2}\right]

Obviously the quadratic equation has 2 roots, just as you would expect.  If we want to show the two solutions separately, we can write a little loop in Sage like this:

solns = solve(x^2 + 5*x + 2 == 0, x) for i in solns: show(i)
x = -\frac{1}{2} \, \sqrt{17} - \frac{5}{2}
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{}
x = \frac{1}{2} \, \sqrt{17} - \frac{5}{2}
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{}

Want numerical solutions?  There are a couple of options.  You can get the exact solutions and then approximate them.  Here we take the right hand side of each of the equations in solns and then approximate it.

for i in solns: i.rhs().n()
\newcommand{\Bold}[1]{\mathbf{#1}}-4.56155281280883
\newcommand{\Bold}[1]{\mathbf{#1}}-0.438447187191170

Alternatively, we can use find_root() to look for numerical solutions between two values of xx.  This works even for equations Sage cannot solve symbolically.  This probably works best if we have already plotted the curve so we know where to look for roots, but here's an example.

find_root(x^2 + 5*x + 2 == 0, -1, 0)
\newcommand{\Bold}[1]{\mathbf{#1}}-0.438447187191
find_root(x^2 + 5*x + 2 == 0, -5, -1)
\newcommand{\Bold}[1]{\mathbf{#1}}-4.56155281281

solve() can also handle more than one equation at a time.  Here's an example.

solve([x+y==5, x-y==2], x, y)
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[x = \left(\frac{7}{2}\right), y = \left(\frac{3}{2}\right)\right]\right]

What if we get more adventuresome and try equations with symbolic coefficients?

var('a b c d e f')
\newcommand{\Bold}[1]{\mathbf{#1}}\left(a, b, c, d, e, f\right)
quadratic = a*x^2 + b*x + c == 0 quadratic
\newcommand{\Bold}[1]{\mathbf{#1}}a x^{2} + b x + c = 0
solve(quadratic,x)
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = -\frac{1}{2} \, \frac{{\left(b + \sqrt{-4 \, a c + b^{2}}\right)}}{a}, x = -\frac{1}{2} \, \frac{{\left(b - \sqrt{-4 \, a c + b^{2}}\right)}}{a}\right]
for i in _: i.rhs().show()
-\frac{1}{2} \, \frac{{\left(b + \sqrt{-4 \, a c + b^{2}}\right)}}{a}
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{}
-\frac{1}{2} \, \frac{{\left(b - \sqrt{-4 \, a c + b^{2}}\right)}}{a}
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{}

OK, so Sage knows the quadratic formula.  Big deal.  So do I.  But how about equations of higher degree?

cubic = a*x^3 + b*x^2 + c*x + d == 0 cubic
\newcommand{\Bold}[1]{\mathbf{#1}}a x^{3} + b x^{2} + c x + d = 0
print(solve(cubic,x))
[ x == -1/2*(I*sqrt(3) + 1)*(1/18*sqrt(27*a^2*d^2 + 4*a*c^3 - b^2*c^2 - 2*(9*a*b*c - 2*b^3)*d)*sqrt(3)/a^2 - 1/54*(27*a^2*d - 9*a*b*c + 2*b^3)/a^3)^(1/3) - 1/3*b/a + 1/18*(-I*sqrt(3) + 1)*(3*a*c - b^2)/((1/18*sqrt(27*a^2*d^2 + 4*a*c^3 - b^2*c^2 - 2*(9*a*b*c - 2*b^3)*d)*sqrt(3)/a^2 - 1/54*(27*a^2*d - 9*a*b*c + 2*b^3)/a^3)^(1/3)*a^2), x == -1/2*(-I*sqrt(3) + 1)*(1/18*sqrt(27*a^2*d^2 + 4*a*c^3 - b^2*c^2 - 2*(9*a*b*c - 2*b^3)*d)*sqrt(3)/a^2 - 1/54*(27*a^2*d - 9*a*b*c + 2*b^3)/a^3)^(1/3) - 1/3*b/a + 1/18*(I*sqrt(3) + 1)*(3*a*c - b^2)/((1/18*sqrt(27*a^2*d^2 + 4*a*c^3 - b^2*c^2 - 2*(9*a*b*c - 2*b^3)*d)*sqrt(3)/a^2 - 1/54*(27*a^2*d - 9*a*b*c + 2*b^3)/a^3)^(1/3)*a^2), x == (1/18*sqrt(27*a^2*d^2 + 4*a*c^3 - b^2*c^2 - 2*(9*a*b*c - 2*b^3)*d)*sqrt(3)/a^2 - 1/54*(27*a^2*d - 9*a*b*c + 2*b^3)/a^3)^(1/3) - 1/3*b/a - 1/9*(3*a*c - b^2)/((1/18*sqrt(27*a^2*d^2 + 4*a*c^3 - b^2*c^2 - 2*(9*a*b*c - 2*b^3)*d)*sqrt(3)/a^2 - 1/54*(27*a^2*d - 9*a*b*c + 2*b^3)/a^3)^(1/3)*a^2) ]
for i in solve(cubic,x): i.rhs().show()
-\frac{1}{2} \, {\left(I \, \sqrt{3} + 1\right)} {\left(\frac{1}{18} \, \frac{\sqrt{27 \, a^{2} d^{2} + 4 \, a c^{3} - b^{2} c^{2} - 2 \, {\left(9 \, a b c - 2 \, b^{3}\right)} d} \sqrt{3}}{a^{2}} - \frac{1}{54} \, \frac{{\left(27 \, a^{2} d - 9 \, a b c + 2 \, b^{3}\right)}}{a^{3}}\right)}^{\left(\frac{1}{3}\right)} - \frac{1}{3} \, \frac{b}{a} + \frac{1}{18} \, \frac{{\left(-I \, \sqrt{3} + 1\right)} {\left(3 \, a c - b^{2}\right)}}{{\left(\frac{1}{18} \, \frac{\sqrt{27 \, a^{2} d^{2} + 4 \, a c^{3} - b^{2} c^{2} - 2 \, {\left(9 \, a b c - 2 \, b^{3}\right)} d} \sqrt{3}}{a^{2}} - \frac{1}{54} \, \frac{{\left(27 \, a^{2} d - 9 \, a b c + 2 \, b^{3}\right)}}{a^{3}}\right)}^{\left(\frac{1}{3}\right)} a^{2}}
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{}
-\frac{1}{2} \, {\left(-I \, \sqrt{3} + 1\right)} {\left(\frac{1}{18} \, \frac{\sqrt{27 \, a^{2} d^{2} + 4 \, a c^{3} - b^{2} c^{2} - 2 \, {\left(9 \, a b c - 2 \, b^{3}\right)} d} \sqrt{3}}{a^{2}} - \frac{1}{54} \, \frac{{\left(27 \, a^{2} d - 9 \, a b c + 2 \, b^{3}\right)}}{a^{3}}\right)}^{\left(\frac{1}{3}\right)} - \frac{1}{3} \, \frac{b}{a} + \frac{1}{18} \, \frac{{\left(I \, \sqrt{3} + 1\right)} {\left(3 \, a c - b^{2}\right)}}{{\left(\frac{1}{18} \, \frac{\sqrt{27 \, a^{2} d^{2} + 4 \, a c^{3} - b^{2} c^{2} - 2 \, {\left(9 \, a b c - 2 \, b^{3}\right)} d} \sqrt{3}}{a^{2}} - \frac{1}{54} \, \frac{{\left(27 \, a^{2} d - 9 \, a b c + 2 \, b^{3}\right)}}{a^{3}}\right)}^{\left(\frac{1}{3}\right)} a^{2}}
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{}
{\left(\frac{1}{18} \, \frac{\sqrt{27 \, a^{2} d^{2} + 4 \, a c^{3} - b^{2} c^{2} - 2 \, {\left(9 \, a b c - 2 \, b^{3}\right)} d} \sqrt{3}}{a^{2}} - \frac{1}{54} \, \frac{{\left(27 \, a^{2} d - 9 \, a b c + 2 \, b^{3}\right)}}{a^{3}}\right)}^{\left(\frac{1}{3}\right)} - \frac{1}{3} \, \frac{b}{a} - \frac{1}{9} \, \frac{{\left(3 \, a c - b^{2}\right)}}{{\left(\frac{1}{18} \, \frac{\sqrt{27 \, a^{2} d^{2} + 4 \, a c^{3} - b^{2} c^{2} - 2 \, {\left(9 \, a b c - 2 \, b^{3}\right)} d} \sqrt{3}}{a^{2}} - \frac{1}{54} \, \frac{{\left(27 \, a^{2} d - 9 \, a b c + 2 \, b^{3}\right)}}{a^{3}}\right)}^{\left(\frac{1}{3}\right)} a^{2}}
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{}

Wow!  So there's a cubic formula like the quadratic formula, one giving 3 roots instead of 2 and involving the complex numbers.  (The formulas actually continue past the edge of the page, and I=1I=\sqrt{-1} is the imaginary square root of 1-1.)  There's also a quartic formula that Sage knows, but the output for it fills several pages.

How about quintics?

quintic = a*x^5 + b*x^4 + c*x^3 + d*x^2 + e*x + f == 0 quintic
\newcommand{\Bold}[1]{\mathbf{#1}}a x^{5} + b x^{4} + c x^{3} + d x^{2} + e x + f = 0
solve(quintic,x)
\newcommand{\Bold}[1]{\mathbf{#1}}\left[0 = a x^{5} + b x^{4} + c x^{3} + d x^{2} + x e + f\right]

Sage just gives us back the original equation.  Why?  Because Évariste Galois proved shortly before his death at the age of 20 that there is no general formula like the quadratic formula (one using ++, -, \cdot, // and radicals) that solves polynomial equations of degree 5 and higher.  (That is, there is no formula solving all  such equations.  For particular values of the coefficients, there are often simple solutions.)

find_root() still works to find approximate solutions to equations of high degree:

find_root(x^5 + x + 1 ==0, -10, 10)
\newcommand{\Bold}[1]{\mathbf{#1}}-0.754877666247

Plotting Graphs

Sage only found one solution to the last equation.  How can we convince ourselves there is only one?  One way to begin to build evidence might be to graph the function.

plot(x^5 + x + 1, (x, -2, 2))

This at least suggests that there is only one root between 2-2 and 2.

Sage sometimes makes an unhelpful choice of axes.

plot(x^3 - 1/x^2, (x, -2, 2))

The problem here was the vertical asymptote at x=0x=0Sage picked a scale that tried to show that asymptote, but ended up showing nothing - the whole graph ended up inside the axes at that scale.  (For the record, the axis label -8e6 means 8×106-8\times10^6.)   In this case, we can show the plot with a more limited range of yy values.

plot(x^3 - 1/x^2, (x, -2, 2)).show(ymin = -10, ymax = 10)

plot() and show() have an enormous number of options.  Here are a few of them:

p = plot(x^3 - 1/x^2, (x, -2, 2), color='blue', thickness=3, fill='axis', fillcolor='green') show(p, ymin=-10, ymax=10)

Sage can plot multiple functions together.

plot([abs(x), -abs(x), x*sin(1/x)], (x, -0.1, 0.1))

One difficulty with this is that you may not know which curve is which.  In that case, you can give the curves distinct colors in their own plot statements, and then show all the plots together by joining them with + signs:

p1 = plot(abs(x), (x, -0.1, 0.1), color='blue') p2 = plot(-abs(x), (x, -0.1, 0.1), color='green') p3 = plot(x*sin(1/x), (x, -0.1, 0.1), color='red') show(p1+p2+p3, aspect_ratio=1)

Color blind or printing the result on a monochrome printer?  The linestyle option can be your friend.

p1 = plot(sin(x), (x, -pi, 3*pi), linestyle='-') p2 = plot(cos(x), (x, -pi, 3*pi), linestyle=':') p3 = plot(arctan(x), (x, -pi, 3*pi), linestyle='--') p4 = plot(x^2, (x, -pi, 3*pi), linestyle='-.') show(p1+p2+p3+p4, ymin=-1.2, ymax=3)

3D plots are also possible.

plot3d(x^2+y^2, (x,-2,2), (y,-2,2))

Help!

There are numerous sources of help within Sage.  If you know the name of the function on which you want help, you can enter its name followed by a question mark:

numerator?

File: /Applications/sage/local/lib/python2.6/site-packages/sage/misc/functional.py

Type: <type ‘function’>

Definition: numerator(x)

Docstring:

Returns the numerator of x.

EXAMPLES:

sage: R.<x> = PolynomialRing(QQ)
sage: F = FractionField(R)
sage: r = (x+1)/(x-1)
sage: numerator(r)
x + 1
sage: numerator(17/11111)
17

If you're not certain of the name of a command, you can type the first few letters and then hit the <TAB> key to get a pop-up with possible completions.  Try typing sol<TAB> to see all command names that start with the letters sol. (Here <TAB> denotes a single keystroke.  Just hit the key labeled TAB.)  Even more extensively, try quintic.<TAB> to see a large pop-up with every single message that could be sent to the object quintic.

For even more help, click the Help link at the top of any Sage Notebook.

Substitutions, Etc.

Here are a few more commands which are useful in calculus.  We begin by defining an expression, making some substitutions, and using limits to compute its derivative directly from the definition:

f = x^2+x f
\newcommand{\Bold}[1]{\mathbf{#1}}x^{2} + x
f.substitute(x=3)
\newcommand{\Bold}[1]{\mathbf{#1}}12
var('h') f.substitute(x=x+h)
\newcommand{\Bold}[1]{\mathbf{#1}}{\left(h + x\right)}^{2} + h + x
expand(_)
\newcommand{\Bold}[1]{\mathbf{#1}}h^{2} + 2 \, h x + x^{2} + h + x
_ - f
\newcommand{\Bold}[1]{\mathbf{#1}}h^{2} + 2 \, h x + h
_/h
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{{\left(h^{2} + 2 \, h x + h\right)}}{h}
_.simplify_rational()
\newcommand{\Bold}[1]{\mathbf{#1}}h + 2 \, x + 1
limit(_, h=0)
\newcommand{\Bold}[1]{\mathbf{#1}}2 \, x + 1

Defining Functions

An irritating feature of this calculation was using f.substitute(x=3) to compute f when x=3x=3.  We'd like to be able just to say f(3), but we can't, because f is just an expression, not a function.  So how would we define a function in Sage?  There are several ways.  For simple one-line functions, we can do  this:

g(x) = x^2 + x
f
\newcommand{\Bold}[1]{\mathbf{#1}}x^{2} + x
g
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ x^{2} + x
f(3)
__main__:5: DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)
\newcommand{\Bold}[1]{\mathbf{#1}}12
g(3)
\newcommand{\Bold}[1]{\mathbf{#1}}12

More complicated functions can be written in Python:

def biggest(x, y): if x >= y: return(x) else: return(y)
biggest(pi, 3)
\newcommand{\Bold}[1]{\mathbf{#1}}\pi
biggest(pi,4)
\newcommand{\Bold}[1]{\mathbf{#1}}4

Similarly, piecewise-defined functions can be handled in a couple of ways.  To get a plotable expression that is not a function, you can use Sage's Piecewise function.

peas = Piecewise([[(-2,0),x^2], [(0,pi/2),1], [(pi/2,3*pi/2), cos(x)]]) peas
\newcommand{\Bold}[1]{\mathbf{#1}}{x2on (2,0)1on (0,1/2pi)cos(x)on (1/2pi,3/2pi)\begin{cases} x^{2} &\text{on $(-2, 0)$}\cr 1 &\text{on $(0, 1/2*pi)$}\cr \cos\left(x\right) &\text{on $(1/2*pi, 3/2*pi)$}\cr \end{cases}
plot(peas)

To get a proper function, you have to write a Python function using the different cases:

def beans(x): if x <= 0: return(x^2) else: if x < pi/2: return(1) else: return(cos(x))
beans(0.5)
\newcommand{\Bold}[1]{\mathbf{#1}}1
beans(3*pi/4)
\newcommand{\Bold}[1]{\mathbf{#1}}-\frac{1}{2} \, \sqrt{2}
plot(beans, (x,-2,3*pi/2))

There is one small technicality here.  We need to plot beans, not beans(x).  Otherwise the function beans(x) gets evaluated at the start of the plot command, returning one of the three functions that make up beans, and then this function only gets plotted.

plot(beans(x), (x,-2,3*pi/2))

More Calculus Functions

Sage can directly compute derivatives (including second and third derivatives and so on), sums, integrals and limits.  Here are some examples:

derivative(sin(x^2),x)
\newcommand{\Bold}[1]{\mathbf{#1}}2 \, x \cos\left(x^{2}\right)

Here are the first, second, third, and twentieth derivatives of the function

$$

f(x)=x^2+\frac1x.

$$

f(x) = x^2 + 1/x f(x)
\newcommand{\Bold}[1]{\mathbf{#1}}x^{2} + \frac{1}{x}
derivative(f(x),x)
\newcommand{\Bold}[1]{\mathbf{#1}}2 \, x - \frac{1}{x^{2}}
derivative(f(x),x,2)
\newcommand{\Bold}[1]{\mathbf{#1}}2 \, \frac{1}{x^{3}} + 2
derivative(f(x),x,3)
\newcommand{\Bold}[1]{\mathbf{#1}}-6 \, \frac{1}{x^{4}}
derivative(f(x),x,20)
\newcommand{\Bold}[1]{\mathbf{#1}}2432902008176640000 \, \frac{1}{x^{21}}

derivative() can also be written as diff().

diff(ln(x),x)
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{x}
integral(x^2,x)
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{3} \, x^{3}
integral(x^2, x,1,4)
\newcommand{\Bold}[1]{\mathbf{#1}}21
limit((x^2-3*x+2)/(x-1), x=1)
\newcommand{\Bold}[1]{\mathbf{#1}}-1
[1/n^2 for n in [1..10]]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[1, \frac{1}{4}, \frac{1}{9}, \frac{1}{16}, \frac{1}{25}, \frac{1}{36}, \frac{1}{49}, \frac{1}{64}, \frac{1}{81}, \frac{1}{100}\right]
sum([1/n^2 for n in [1..10]])
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1968329}{1270080}

The current version of Sage allows a nicer syntax for sum(), which even supports infinite sums.  Here's an example that is just too cool to leave out, showing that

$$

\sum_{n=1}^\infty\frac1{n^2}=\frac\pi6.

$$

var('n') sum(1/n^2, n, 1, 10)
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1968329}{1270080}
sum(1/n^2, n, 1, infinity)
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{6} \, \pi^{2}