︠556e420a-f306-460e-9a80-df3e5a5e1e0ai︠ %html
This lecture is an introduction to using the computer algebra system Sage. In the process, we will also introduce the Python programming language, on which Sage is built; however, we'll keep the programming discussion to a minimum because it is easy to find Python tutorials and other documentation online. (For example, try the Beginner's Guide to Python.)
All of the lectures in this minicourse are meant to be interactive, so you are strongly encouraged to follow along on your own computer as we go along. To make this possible, we begin by explaining how to run Sage on your own computer. There are two natural ways to do this.
To simplify the exposition, I'm mostly going to stick to explaining the notebook interface. If you have questions about using the command line, ask during the Q&A session.
If you have successfully started a notebook session, you should see something that looks much like this window, except that instead of all of this text, you should just see an empty box like the one below. (That is, unless you followed my instructions to edit a copy of my worksheet. In that case, you should still have an empty box like the one below, but it will be surrounded by the other text that you see here!)
︡fcac7a32-e7d0-4f38-85eb-627982807998︡{"html": "This lecture is an introduction to using the computer algebra system Sage. In the process, we will also introduce the Python programming language, on which Sage is built; however, we'll keep the programming discussion to a minimum because it is easy to find Python tutorials and other documentation online. (For example, try the Beginner's Guide to Python.)
\nAll of the lectures in this minicourse are meant to be interactive, so you are strongly encouraged to follow along on your own computer as we go along. To make this possible, we begin by explaining how to run Sage on your own computer. There are two natural ways to do this.
\nTo simplify the exposition, I'm mostly going to stick to explaining the notebook interface. If you have questions about using the command line, ask during the Q&A session.
\nIf you have successfully started a notebook session, you should see something that looks much like this window, except that instead of all of this text, you should just see an empty box like the one below. (That is, unless you followed my instructions to edit a copy of my worksheet. In that case, you should still have an empty box like the one below, but it will be surrounded by the other text that you see here!)
"}︡ ︠81f857d9-292b-44e7-af4d-198104261b9ci︠ %htmlThis box is called a cell, and can be used to evaluate any Sage command. For example, try clicking on the cell, typing 2+2, and clicking "evaluate". (A shortcut for "evaluate" is Shift+Enter.)
︡d6b95eb0-3069-43b7-914d-efe0bca40331︡{"html": "This box is called a cell, and can be used to evaluate any Sage command. For example, try clicking on the cell, typing 2+2, and clicking \"evaluate\". (A shortcut for \"evaluate\" is Shift+Enter.)
"}︡ ︠38b77fb5-8b71-4834-af53-534619c10c45︠ 2+2 ︡61ae7fbc-5448-45a9-bf51-3b69374de04a︡{"stdout": "4"}︡ ︠5ed065f8-b08e-4728-a7cc-38a4d1940216i︠ %htmlNote that the answer appears immediately below, and then a new cell is generated so that you can type another command. You can also go back and edit the previous cell, but the answer won't change until you evaluate again. You can even insert new cells between existing cells: if you click on the blue bar that shows up when you mouse between two existing cells, a new cell will pop up in between.
If you Shift-click instead, you get a text box like this, which supports some formatting and even basic TeX commands. This is one way to add annotations to a notebook; a more direct one is simply to insert comments in cells themselves. The # character denotes a comment, and forces everything to the end of the line to be ignored.
︡3b612f59-b647-46e6-830b-9d9bcb4e3a2d︡{"html": "Note that the answer appears immediately below, and then a new cell is generated so that you can type another command. You can also go back and edit the previous cell, but the answer won't change until you evaluate again. You can even insert new cells between existing cells: if you click on the blue bar that shows up when you mouse between two existing cells, a new cell will pop up in between.
\nIf you Shift-click instead, you get a text box like this, which supports some formatting and even basic TeX commands. This is one way to add annotations to a notebook; a more direct one is simply to insert comments in cells themselves. The # character denotes a comment, and forces everything to the end of the line to be ignored.
"}︡ ︠9fdd1c3c-a883-4bec-96bd-586c19f1ec55︠ 2+2 # should equal 4 ︡32911de7-df00-41ae-852a-cce9818a7e4e︡{"stdout": "4"}︡ ︠d3e9f9ab-3c8e-4fa2-834e-8123a749d29fi︠ %htmlBesides individual calculator-style expressions, a cell can contain a sequence of instructions; hit Enter to separate instructions. (This is why Shift-Enter is the shortcut for evaluation, not Enter.) The last expression is evaluated and printed; you can also add "print" commands to force additional printing.
︡7260ce18-e606-4885-b183-84d118ede5a7︡{"html": "Besides individual calculator-style expressions, a cell can contain a sequence of instructions; hit Enter to separate instructions. (This is why Shift-Enter is the shortcut for evaluation, not Enter.) The last expression is evaluated and printed; you can also add \"print\" commands to force additional printing.
"}︡ ︠f66445f5-e8ab-4fd9-a7fd-673afd21124b︠ x = 5 y = 7 print x+y x*y ︡c37fba03-5873-4340-9588-6655983a9fe9︡{"stdout": "12\n35"}︡ ︠6dd405f0-fab1-4fbe-97fa-62a25c641395i︠ %htmlEach cell evaluation remembers any definitions from cells that were evaluated previously, including variables and functions (more on which later).
︡d6a50725-509e-4b04-b94f-f71b868d1446︡{"html": "Each cell evaluation remembers any definitions from cells that were evaluated previously, including variables and functions (more on which later).
"}︡ ︠43983678-2877-4844-bbdb-a33e82731bcb︠ x+y ︡e0b9745f-5ca2-43e5-b320-aaa635ba3355︡{"stdout": "12"}︡ ︠43abf641-4beb-4e53-ba19-a2a710858fcfi︠ %htmlWarning: Sage will let you evaluate cells out of order. It is the order of evaluation that counts, not the order of appearance in the worksheet.
If you try to evaluate a cell which contains a mistake, such as trying to use a variable which is not yet defined, Sage will give you a (hopefully) useful error message. You can click on the output to see a more verbose error message, which may help you isolate the problem if it occurs in a large cell.
︡cf3cecd9-844d-4a8c-844f-d24ffdb262d8︡{"html": "Warning: Sage will let you evaluate cells out of order. It is the order of evaluation that counts, not the order of appearance in the worksheet.
\nIf you try to evaluate a cell which contains a mistake, such as trying to use a variable which is not yet defined, Sage will give you a (hopefully) useful error message. You can click on the output to see a more verbose error message, which may help you isolate the problem if it occurs in a large cell.
"}︡ ︠bd684cea-9a71-4800-95b2-70525de9589e︠ z ︡4f8fc74a-674c-4f5e-9cdd-84b065e41efe︡{"stderr": "Traceback (most recent call last):\n File \"Let's get familiar with some basic types of Python/Sage objects. These will all be things that can be assigned to variables, which we have already seen how to do in the case of integers.
︡e516462e-8812-4e9e-ba46-8d5d1bd43812︡{"html": "Let's get familiar with some basic types of Python/Sage objects. These will all be things that can be assigned to variables, which we have already seen how to do in the case of integers.
"}︡ ︠24666e58-fddc-4ac3-9cd6-65ee7076e9e1︠ x = 2+2 print x ︡854a4dd7-38cd-47db-9b1d-e9f0bf4c0dd9︡{"stdout": "4"}︡ ︠f04960dc-58e0-4e99-abff-fad9b3cddbb4i︠ %htmlNote that variables do not need to be declared before being used; in particular, there is no need to specify in advance what type of object is going to be assigned to a given variable.
So far our variable names have only been one character long. However, this is not required: you may use any sequence of letters, numbers, and the underscore _ as a variable name as long as it starts with a letter and does not coincide with a Python reserved wood (such as "print"). Considered choice of variable names may make your code easier for a human to understand.
︡cf0daa12-f423-434a-a153-cecaf051a8cb︡{"html": "Note that variables do not need to be declared before being used; in particular, there is no need to specify in advance what type of object is going to be assigned to a given variable.
\nSo far our variable names have only been one character long. However, this is not required: you may use any sequence of letters, numbers, and the underscore _ as a variable name as long as it starts with a letter and does not coincide with a Python reserved wood (such as \"print\"). Considered choice of variable names may make your code easier for a human to understand.
"}︡ ︠fd69625d-f6ff-4ad6-a156-edcbdbd75287︠ square_root_of_2 = sqrt(2) print square_root_of_2^4 ︡6f58e57c-3cfe-4209-b042-2030a81c539e︡{"stdout": "4"}︡ ︠59da8340-63fd-4498-9296-4e7412bfd1aci︠ %htmlBesides integers, some other basic types of objects include rational numbers, real numbers, and complex numbers (where capital I denotes the chosen square root of -1). These support the usual arithmetic operations.
︡ffac06ec-3907-42a1-95bf-c90be9beda20︡{"html": "Besides integers, some other basic types of objects include rational numbers, real numbers, and complex numbers (where capital I denotes the chosen square root of -1). These support the usual arithmetic operations.
"}︡ ︠040fd784-0a09-4ef0-b661-41d576f5d10d︠ print 7 % 3 ## modular reduction print 7 // 3 ## integer division print 10/6 ## returns a rational number print exp(2.7) print (2+I)*(3+I) ︡b49200ed-9d9f-466a-b5de-f45e9caf2784︡{"stdout": "1\n2\n5/3\n14.8797317248728\n5*I + 5"}︡ ︠51c9e4d7-df58-4185-86ee-0e757039066di︠ %htmlAnother basic object type is strings, which are enclosed in either single or double quotes (there is a subtle distinction which is not relevant here). One common operation on strings is concatenation.
︡95940a56-d1d2-4133-9831-a08f62cfc9ec︡{"html": "Another basic object type is strings, which are enclosed in either single or double quotes (there is a subtle distinction which is not relevant here). One common operation on strings is concatenation.
"}︡ ︠896dd52e-40f7-4cb9-99c5-156acb7d8529︠ '3' + '4' ︡fbb626db-4f41-4c6d-a362-832687b3ccba︡{"stdout": "'34'"}︡ ︠a1a093b6-1a94-432b-a2fb-c29c1c78e4ebi︠ %htmlBesides simple types like numbers and strings, one also has compound types like arrays. Arrays can be specified using square brackets (the entries need not be all of one type).
︡40ec4862-cbd8-4988-9945-c94f3bb7a935︡{"html": "Besides simple types like numbers and strings, one also has compound types like arrays. Arrays can be specified using square brackets (the entries need not be all of one type).
"}︡ ︠2ef2f138-edc8-47ca-bcdf-66ddf57d2fd1︠ print [3, 4] + [5, 'six'] ## plus sign denotes composition ︡18be5287-cc89-48fa-932d-112d75b2d690︡{"stdout": "[3, 4, 5, 'six']"}︡ ︠5dbd4cd1-1996-4f0e-8fbe-434929ddd8cci︠ %htmlOne also uses square brackets to access the individual entries of an array, or even to assign them. Two important things to note:
One also uses square brackets to access the individual entries of an array, or even to assign them. Two important things to note:
\nSome other ways to construct arrays are the following.
Some other ways to construct arrays are the following.
\nWarning: arrays are copied by reference, not by value. This is in fact true for all Python/Sage objects, but the fact that arrays can be modified after they are created (that is, they are mutable) creates a real danger.
︡127ef202-c042-44ef-b88a-55be21ef8e78︡{"html": "Warning: arrays are copied by reference, not by value. This is in fact true for all Python/Sage objects, but the fact that arrays can be modified after they are created (that is, they are mutable) creates a real danger.
"}︡ ︠0f8e1abf-4cdc-48dd-bbc9-e13f1fa7ee90︠ a = [1,2,3] b = a ## does not create a new array! b[1] = -1 print "a = ", a ## has now been modified c = a[:] ## this does create a new array c[-1] = 4 print "a = ", a print "c = ", c ︡322a5350-e4ed-432c-a399-f7e9b08821f3︡{"stdout": "a = [1, -1, 3]\na = [1, -1, 3]\nc = [1, -1, 4]"}︡ ︠40c8d1cb-ec9f-42a1-a444-a5749903ddc2i︠ %htmlLike most programming languages, Python supports conditional evaluation via such mechanisms as for loops, while loops, and if-then-else constructions. Here is an example.
︡4e646ae9-02f7-458a-aac0-f4d5ea5c236d︡{"html": "Like most programming languages, Python supports conditional evaluation via such mechanisms as for loops, while loops, and if-then-else constructions. Here is an example.
"}︡ ︠22e86c7b-5456-436e-92a2-a34019eabc28︠ for i in range(5): if i%2 == 0: print i, "is an even", else: print i, "is an odd", print "number" print "loop complete" ︡42a2a699-2a62-423d-8d37-6f815ff8d306︡{"stdout": "0 is an even number\n1 is an odd number\n2 is an even number\n3 is an odd number\n4 is an even number\nloop complete"}︡ ︠096b1c5a-e899-4441-9f05-e4d7c704e155i︠ %htmlLet's step through this example in detail.
Here is another example, this time of a while loop. Note the effect of the "continue" and "break" statements.
︡d8b87f3f-74cf-4367-b798-3f42e60e72ae︡{"html": "Let's step through this example in detail.
\nHere is another example, this time of a while loop. Note the effect of the \"continue\" and \"break\" statements.
"}︡ ︠7245fbac-8ef9-4cb0-9e13-456ed9675b2d︠ t = 4 while t < 10: t += 1 ## has the same effect as t = t + 1 if t%2 == 0: continue ## jump to the next iteration of the loop print t if t > 8: break ## jump out of the loop ︡048e051e-c432-4ea0-b375-63c3b820453a︡{"stdout": "5\n7\n9"}︡ ︠716d2caf-ee30-4447-a445-7a2890e37510i︠ %htmlBy now, you have probably noticed that much Python functionality is provided via callable functions, such as "range". Sage extends this functionality by specifying many new advanced mathematical functions.
︡4b6a872f-b6a2-4dad-9ba2-45c189c6af31︡{"html": "By now, you have probably noticed that much Python functionality is provided via callable functions, such as \"range\". Sage extends this functionality by specifying many new advanced mathematical functions.
"}︡ ︠ca663194-27eb-4eca-ac2f-bc97a5a3f13b︠ prime_pi(1000) ## The number of primes up to 1000 ︡9ff40499-63e3-49f5-ad6b-920c6c4f835e︡{"stdout": "168"}︡ ︠feaeb6b8-6ec2-4367-a866-48854606a51di︠ %htmlSage includes a couple of handy techniques for inquiring about functions. One of these is tab completion: if you type part of the name of a function and hit Tab, Sage will attempt to complete the name of the function. On one hand, this can save a lot of typing.
︡bded953a-ddff-4182-aed2-eeda4f5db449︡{"html": "Sage includes a couple of handy techniques for inquiring about functions. One of these is tab completion: if you type part of the name of a function and hit Tab, Sage will attempt to complete the name of the function. On one hand, this can save a lot of typing.
"}︡ ︠ad49274a-0d6e-4561-9f6b-10ea00b51cea︠ charac ︡14bb750d-c9cf-48aa-9ae4-00913fe51560︡{"stderr": "Traceback (most recent call last):\n File \"Perhaps more usefully, if more than one completion is possible, Sage will show you all possible completions; this can be a useful way to find out about what is available in Sage. (This technique is even more possible when applied to object methods; see below.)
︡0fd97c61-da2e-4799-8d9f-aca27d78737b︡{"html": "Perhaps more usefully, if more than one completion is possible, Sage will show you all possible completions; this can be a useful way to find out about what is available in Sage. (This technique is even more possible when applied to object methods; see below.)
"}︡ ︠a7582844-3572-4a94-82dd-3fc2ef4ea8ed︠ ran ︡4fa58cb4-7d45-4b58-be5e-5df538c5ed27︡{"stderr": "Traceback (most recent call last):\n File \"Another important inquiry technique is introspection: if one evaluates the name of a function followed by a ?, Python will show you a bit of documentation about that function. For instance, if you have forgotten how to specify a range other than 0, ..., n-1, we can use introspection on the range function.
︡8bdf4a23-7ed6-4730-9c76-e41c9f583623︡{"html": "Another important inquiry technique is introspection: if one evaluates the name of a function followed by a ?, Python will show you a bit of documentation about that function. For instance, if you have forgotten how to specify a range other than 0, ..., n-1, we can use introspection on the range function.
"}︡ ︠0516b1d8-96ec-468c-afea-432618e41220︠ range? ︡93f61ab8-a84d-4e20-a872-4237abd4bf1a︡{"html": "\nType: <type 'builtin_function_or_method'>
\nDefinition: range( [noargspec] )
\nDocstring:
\n\n\nrange([start,] stop[, step]) -> list of integers\n\nReturn a list containing an arithmetic progression of integers.\nrange(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\nWhen step is given, it specifies the increment (or decrement).\nFor example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\nThese are exactly the valid indices for a list of 4 elements.
Python and Sage also allow for user-defined functions. As with variables, these may be defined in one cell and then used in later cell evaluations. (These will not admit introspection unless you do some extra work, except for source code viewing, but they will admit tab completion.)
︡d29e98db-b20b-43da-84d4-d24584df6adc︡{"html": "Python and Sage also allow for user-defined functions. As with variables, these may be defined in one cell and then used in later cell evaluations. (These will not admit introspection unless you do some extra work, except for source code viewing, but they will admit tab completion.)
"}︡ ︠77204cf7-b6d6-4340-870a-5b1e6f47233d︠ def strange_sum(a, b): c = a+b return(a+b+c) ︡5565b003-d076-416c-80b5-91c09c8a0186︡︡ ︠a237baa7-e989-4e14-83f3-62a4d3df0c65︠ strange_sum(2, 3) ︡1960ef8e-5835-47bf-9c72-b8464df96f49︡{"stdout": "10"}︡ ︠cf04b360-94b5-47fd-85ea-a9e7ded43bd5︠ strange_sum? ︡80917e56-b680-4d27-b0bb-4bb5aa7e2b73︡{"html": "\nFile: /tmp/tmptnWB2h/___code___.py
\nType: <type 'function'>
\nDefinition: strange_sum(a, b)
\nDocstring:
\n\n\nx.__init__(...) initializes x; see x.__class__.__doc__ for signature
File: /tmp/tmptnWB2h/___code___.py
\nSource Code (starting at line 2):
\ndef strange_sum(a, b):\n c = a+b\n return(a+b+c)\n
One important difference between Python and some lower-level programming languages like C is that Python is object-oriented. That is, besides the variables and functions that one can define globally, one can also create objects with their own variables and functions attached to them. In Sage, this framework is used to implement the construction of mathematical objects in a rigorous fashion which corresponds pretty well to the way mathematicians are used to thinking about these objects. For example, one can create such objects as the ring of integers and the field of rational numbers.
︡c6d1345e-c558-458f-b395-4a2e5a2b75e6︡{"html": "One important difference between Python and some lower-level programming languages like C is that Python is object-oriented. That is, besides the variables and functions that one can define globally, one can also create objects with their own variables and functions attached to them. In Sage, this framework is used to implement the construction of mathematical objects in a rigorous fashion which corresponds pretty well to the way mathematicians are used to thinking about these objects. For example, one can create such objects as the ring of integers and the field of rational numbers.
"}︡ ︠5592ed67-db69-49a1-bee1-2d2f1442bb4c︠ Z = IntegerRing() Q = RationalField() ︡1efab56e-3d2c-41fa-9f6d-ade0e1c0439d︡︡ ︠6da4b3cc-9193-4eef-a350-3e71230b6935i︠ %htmlOne can then feed these into more complicated constructions, like the ring of polynomials in a single variable.
︡b6100a36-d73d-4ce3-ab5e-f0ac98bf5e2b︡{"html": "One can then feed these into more complicated constructions, like the ring of polynomials in a single variable.
"}︡ ︠f100ec24-a87e-4d21-a70f-b415d1f1bbd6︠ R.In this example, we have created a polynomial ring over the rationals with a distinguished generator, to which we have assigned the name T. We can now generate elements of the ring R simply by writing down expressions in T.
︡2724b895-a18c-419f-87b8-3cf77f157d9f︡{"html": "
In this example, we have created a polynomial ring over the rationals with a distinguished generator, to which we have assigned the name T. We can now generate elements of the ring R simply by writing down expressions in T.
\n"}︡ ︠347bb03d-e221-44bd-8fb4-07266fe72eea︠ poly = T^3 + 1 print poly.parent() print poly.parent() == R ︡c14a8c83-7330-4707-8206-de747c2de17d︡{"stdout": "Univariate Polynomial Ring in T over Rational Field\nTrue"}︡ ︠65fac4c0-f39c-4b3e-8ecb-61c9aa37f05bi︠ %html
The "parent" command here is an example of a method of the object poly.
Now that we can make polynomials over the rationals, what can we do with them? An excellent way to find out is to use tab completion: if one types "poly." and then hits Tab, one is presented a list of all of the methods associated to poly.
︡3aeb2869-f39d-4a70-827c-c0b9526c7fd4︡{"html": "The \"parent\" command here is an example of a method of the object poly.
\nNow that we can make polynomials over the rationals, what can we do with them? An excellent way to find out is to use tab completion: if one types \"poly.\" and then hits Tab, one is presented a list of all of the methods associated to poly.
"}︡ ︠07822e01-4fc3-4908-909a-81510bb5f3ee︠ poly.factor() ## can also be invoked as factor(poly) ︡695261ba-e2dd-4ac8-aa91-770eca396932︡{"stdout": "(T + 1) * (T^2 - T + 1)"}︡ ︠3941d337-9ed2-40c6-8006-3f87cdba4ee1i︠ %htmlIn the previous example, the "factor" method did not require any additional arguments. In other examples, one or more arguments may be required.
︡0de99f2c-8bc4-4b21-a284-1d8ceef6c237︡{"html": "In the previous example, the \"factor\" method did not require any additional arguments. In other examples, one or more arguments may be required.
"}︡ ︠ed81d316-316c-4a0f-b95c-f3cdf6d2baaf︠ poly.xgcd(T-2) ## extended GCD; can also be invoked as xgcd(poly, T-2) ︡4f6950e0-3d4f-47a5-a924-6e21aedc77f3︡{"stdout": "(1, 1/9, -1/9*T^2 - 2/9*T - 4/9)"}︡ ︠1f703dfb-b863-4872-952a-5e694cad4ea7i︠ %htmlThis syntax might seem a bit strange: the extended GCD operation is essentially symmetric in its variables, so calling it in an asymmetric fashion may be counterintuitive. There is an extremely good reason for using the object-oriented syntax, namely tab completion. One can for instance type "poly." and his Tab to see the list of all of the methods associated to poly!
The ease of looking up the operations available on a given type of mathematical object is one of my favorite features of Sage. Even Magma, which has both object orientation and tab completion, fails on this point because functions are not viewed as object methods, but intsead all inhabit a single namespace.
︡f8f3ca30-a4c4-47dc-a77b-7d93439ac314︡{"html": "This syntax might seem a bit strange: the extended GCD operation is essentially symmetric in its variables, so calling it in an asymmetric fashion may be counterintuitive. There is an extremely good reason for using the object-oriented syntax, namely tab completion. One can for instance type \"poly.\" and his Tab to see the list of all of the methods associated to poly!
\nThe ease of looking up the operations available on a given type of mathematical object is one of my favorite features of Sage. Even Magma, which has both object orientation and tab completion, fails on this point because functions are not viewed as object methods, but intsead all inhabit a single namespace.
"}︡ ︠6935a194-2d5d-4107-bb96-7726e7132924︠ poly. ︡7f41871a-a942-4809-b18f-28740049ec40︡{"stderr": "Traceback (most recent call last):\n File \"As for bare functions, one can also type a few characters and get only the methods that start with the characters you type. For example, if you can't remember whether the method for factoring a polynomial is called "factor", "factorize", "factorise", "factorization", or "factorisation", you can check by doing a tab completion:
︡d1f7d0f9-93a1-4e42-8fcb-b2e1ca02f8d7︡{"html": "As for bare functions, one can also type a few characters and get only the methods that start with the characters you type. For example, if you can't remember whether the method for factoring a polynomial is called \"factor\", \"factorize\", \"factorise\", \"factorization\", or \"factorisation\", you can check by doing a tab completion:
"}︡ ︠ef589df3-3ddf-4067-86af-e28469577a28︠ poly.factor ︡d4aa179b-628f-4986-aa2f-e70e5bc881b7︡{"stdout": "There can sometimes be some ambiguity about what the correct parent of a mathematical object should be. For instance, 3/4 is a rational number, but it can also be viewed as a polynomial over the rationals, e.g., when one wants to add or multiply it with another such polynomial. For the most part, Sage will take care of this for you automatically.
︡d21f697e-e9fe-4a1c-b4f6-18cc207b064a︡{"html": "There can sometimes be some ambiguity about what the correct parent of a mathematical object should be. For instance, 3/4 is a rational number, but it can also be viewed as a polynomial over the rationals, e.g., when one wants to add or multiply it with another such polynomial. For the most part, Sage will take care of this for you automatically.
"}︡ ︠36c826d5-0802-4ea3-bdb1-d1cacd9bf527︠ poly2 = poly + 3/4 print poly2.parent() ︡a358decc-cd60-4ff6-961c-94646fa12674︡{"stdout": "Univariate Polynomial Ring in T over Rational Field"}︡ ︠aaf7e86b-edd6-4a81-9da1-c46546856824i︠ %htmlSometimes, however, one needs to make this change of parent explicit, e.g., in case one wants to call a method which exists for polynomials but not for rationals. For example, this fails:
︡867593ec-fb0c-4128-9b68-530e176d6e68︡{"html": "Sometimes, however, one needs to make this change of parent explicit, e.g., in case one wants to call a method which exists for polynomials but not for rationals. For example, this fails:
"}︡ ︠ec6b90b5-2b33-4b0e-b8e9-d5bbbb0ef6d6︠ poly2 = 3/4 poly2.coefficients() ︡970d1799-a549-4790-8395-4ea77af70f50︡{"stderr": "Traceback (most recent call last):\n File \"This change of parent (called coercion) is usually accomplished by the following syntax, which looks like plugging the element into the new parent viewed as a function.
︡d0036732-2034-41a2-8468-bb8624127ece︡{"html": "This change of parent (called coercion) is usually accomplished by the following syntax, which looks like plugging the element into the new parent viewed as a function.
"}︡ ︠9baf4271-4364-4342-b737-1a50334afbb3︠ poly2 = R(3/4) poly2.coefficients() ︡bc0c0673-5030-4d33-96f9-8099e1218493︡{"stdout": "[3/4]"}︡ ︠36a24598-8cf1-4be2-874e-d41a7bf147fai︠ %htmlSage has many more mathematical features than we can introduce here (but which are well-documented). One feature which will be used later is plotting of various kinds. (We only need 2D plotting her; there are also extremely cool 3D plotting capabilities, but you can discover those on your own.)
︡32801233-1b10-4e66-9f8a-dafc7758b9c9︡{"html": "Sage has many more mathematical features than we can introduce here (but which are well-documented). One feature which will be used later is plotting of various kinds. (We only need 2D plotting her; there are also extremely cool 3D plotting capabilities, but you can discover those on your own.)
"}︡ ︠ce6dc591-c347-4669-b44b-f6e33767f154︠ plot(sin, 0, pi) ## Plotting a built-in function ︡ecefc21b-8713-4153-938e-c6262f29bebd︡{"html": "In addition to its normal use as a Sage (and Python) interpreter, the Sage notebook can be used as an interface to numerous other packages. For example, Elkies's lecture in this Short Course uses gp, which is the direct interface to Pari. Although most of Pari's functionality is available in Python-wrapped form, one can also initiate a gp session and use its syntax directly, without leaving the notebook! (One way to tell that we're not using Sage syntax is that this example depends on gp being case-insensitive. Another is that the comment syntax in gp differs from that of Sage.)
︡cd72da37-6434-4e84-8cd6-ac673f7f9375︡{"html": "In addition to its normal use as a Sage (and Python) interpreter, the Sage notebook can be used as an interface to numerous other packages. For example, Elkies's lecture in this Short Course uses gp, which is the direct interface to Pari. Although most of Pari's functionality is available in Python-wrapped form, one can also initiate a gp session and use its syntax directly, without leaving the notebook! (One way to tell that we're not using Sage syntax is that this example depends on gp being case-insensitive. Another is that the comment syntax in gp differs from that of Sage.)
"}︡ ︠0c9c53b9-5672-4af6-99c3-fd0a0dbf51c4︠ %gp \\ Ask gp what the function "ellinit" does ?ellinit \\ the elliptic curve with a1=a2=a3=0 and prescribed c4,c6 E1(c4, c6) = ellinit([0, 0, 0, -c4/48, -c6/864]) ︡bbc66ab0-9ec8-4e05-b419-d31203558288︡{"stdout": "ellinit(x,{flag=0}): x being the vector [a1,a2,a3,a4,a6] defining the curve Y^2 \n+ a1.XY + a3.Y = X^3 + a2.X^2 + a4.X + a6, gives the vector: \n[a1,a2,a3,a4,a6,b2,b4,b6,b8,c4,c6,disc,j,[e1,e2,e3],w1,w2,eta1,eta2,area]. If \nthe curve is defined over a p-adic field, the last six components are replaced \nby root,u^2,u,q,w,0. If optional flag is 1, omit them altogether. x can also be \na string, in this case the coefficients of the curve with matching name are \nlooked in the elldata database if available.\n\n(c4,c6)->ellinit([0,0,0,-c4/48,-c6/864])"}︡ ︠bd36e71f-ba36-4eb9-a57b-38df57ca6e49i︠ %htmlJust like calls to Sage, external packages used from the Sage notebook remember their state from one cell evaluation to the next.
︡2313784b-308b-44a3-990a-9bbce5dac6e7︡{"html": "Just like calls to Sage, external packages used from the Sage notebook remember their state from one cell evaluation to the next.
"}︡ ︠c109120a-3ab7-458a-bb94-b1a42d34dc39︠ %gp \\ here's the syntax for getting the discriminant and j-invariant E1(C4,C6).disc E1(C4,C6).j ︡df45c64a-de18-40de-b6a2-152459acb1d6︡{"stdout": "1728*C4^3/(C4^3 - C6^2)"}︡ ︠834fcb58-1271-4c92-8610-a284a616edcei︠ %htmlBy the way, one can also access external packages in the following way. Note that this is the same gp session as I had before; it remembers the assignments I made in the earlier cell!
︡5835f091-5633-4965-8c53-2de721425eaa︡{"html": "By the way, one can also access external packages in the following way. Note that this is the same gp session as I had before; it remembers the assignments I made in the earlier cell!
"}︡ ︠1024e229-518c-414d-9f8f-5f4f16767831︠ print gp("E1") ︡7ac64269-e6eb-49e3-99c4-4afcb7b1fafc︡{"stdout": "(c4,c6)->ellinit([0,0,0,-c4/48,-c6/864])"}︡ ︠a9b7c118-5b22-4bcd-8a49-3fc2fb237445i︠ %htmlSimilar methods can be used to access packages not provided with Sage, as long as the server has access to them. For example, the machine on which I composed this notebook runs Magma, so the following works there. (One way to tell we're not using Sage syntax is that the Magma assignment operator is := rather than =.)
︡20b0b658-6c4e-427c-97a5-7e62e5e7a995︡{"html": "Similar methods can be used to access packages not provided with Sage, as long as the server has access to them. For example, the machine on which I composed this notebook runs Magma, so the following works there. (One way to tell we're not using Sage syntax is that the Magma assignment operator is := rather than =.)
"}︡ ︠7afae815-6ae0-4416-844f-045f841e91c5︠ %magma Q := RationalField(); print(Q); /* Should print "Rational Field" if magma is available */ ︡e1ed0746-fbf8-4f47-9fb6-1d270bb96081︡{"stdout": "Rational Field"}︡ ︠43f76854-47fd-46e4-bbd9-20bf6cd826cei︠ %htmlThis last point is a bit technical, but it will arise in the later lectures. As we saw earlier, if you make a mistake, Sage normally interrupts the computation to return an error message explaining what went wrong.
︡b7196e0a-3065-4875-8809-4ea4c13eeda3︡{"html": "This last point is a bit technical, but it will arise in the later lectures. As we saw earlier, if you make a mistake, Sage normally interrupts the computation to return an error message explaining what went wrong.
"}︡ ︠c9ef5bbf-aba1-4758-b113-6fca77fbf3a4︠ print (3 / (1-1)) ︡64a94c3c-13a7-4de9-ab61-010add934653︡{"stderr": "Traceback (most recent call last):\n File \"One can explicitly create these errors as a debugging tool.
︡224a6412-129e-4c6d-9be4-2f6471fb4fd6︡{"html": "One can explicitly create these errors as a debugging tool.
"}︡ ︠ed8fc2e3-0fbf-4b6b-9b99-f0211fa3c795︠ def f(n): if n < 0: raise ValueError, "Argument of f must be nonnegative" return(sqrt(n)) print f(1) print f(-1) ︡77421bf3-1d9a-4909-81ee-8cb5d48e33aa︡{"stdout": "1\n"}︡{"stderr": "Traceback (most recent call last): print f(-1)\n File \"\", line 1, inOne can also catch these errors to a limited extent, using the try and except commands.
︡dcd359d0-c3f7-4f22-a060-d9f3e58bd8b7︡{"html": "One can also catch these errors to a limited extent, using the try and except commands.
"}︡ ︠f3a80052-4033-4f88-a273-710e52443d7c︠ def f(x,y): try: n = 1/x print "inversion complete" return(n + y[0]) except ZeroDivisionError: return 0 print f(1, [2,3]) ## normal evaluation print f(0, [4,5]) ## exception encountered, skipping the internal print statement print f(2, []) ## this exception is not caught, so causes an error print f(3, [3, 4]) ## we never get this far ︡3b231e14-411c-4dcd-9f00-4b1e6e9a1a3b︡{"stdout": "inversion complete\n3\n0\ninversion complete\n"}︡{"stderr": "Traceback (most recent call last): return 0\n File \"\", line 1, inTo conclude, we point out a few more advanced things that can be accomplished using Sage, just to give the flavor of what the possibilities are. These are mostly complementary to the advanced functionality for elliptic curves that will be highlighted in the subsequent lectures.
To conclude, we point out a few more advanced things that can be accomplished using Sage, just to give the flavor of what the possibilities are. These are mostly complementary to the advanced functionality for elliptic curves that will be highlighted in the subsequent lectures.
\n