︠1011ec3f-1299-4fa5-9079-a8a3cd57a197i︠ %html
Part II of Sage Workshop: Introduction to packages
Sage has a variety of packages. In this section, feel free to try out different packages that may be useful for your research, class, or just to practice with Sage more.
A. Polynomials and Symbolic Computation in Sage
Polynomials versus expressions
Warm-up (Example 1)
Define the expression $f=x^4-5x^3+8x^2-4x$. We know that $f$ is a polynomial with rational (in particular, integer) coefficients.
Plot the graph of this polynomial in sage. Be sure that the plot displays all its (real) zeros. Note that $f$ has at least one integer zero in particular, x=2). Let's try to divide $f$ by $g=x-2$ by using the command quo_rem:
f.quo_rem(g)
Try out this command. What happens?
︡dcc589db-c3f5-4263-ada2-84cd6d7df558︡{"html": "Part II of Sage Workshop: Introduction to packages
\nSage has a variety of packages. In this section, feel free to try out different packages that may be useful for your research, class, or just to practice with Sage more.
\n\nA. Polynomials and Symbolic Computation in Sage
\nPolynomials versus expressions
\nWarm-up (Example 1)
\nDefine the expression $f=x^4-5x^3+8x^2-4x$. We know that $f$ is a polynomial with rational (in particular, integer) coefficients.
\nPlot the graph of this polynomial in sage. Be sure that the plot displays all its (real) zeros. Note that $f$ has at least one integer zero in particular, x=2). Let's try to divide $f$ by $g=x-2$ by using the command quo_rem:
\nf.quo_rem(g)
\nTry out this command. What happens?
"}︡ ︠398029ef-2ebd-46de-9178-15c29a6931fa︠ #Example 1 ︡02cbd490-d19c-4123-8d13-0cddafd73a6d︡︡ ︠c8fd1afa-f94d-4e6f-8f60-3cec642d2ed8i︠ %htmlEven though some of the expressions we have run into in examples previously look to us like polynomials, Sage would not recognize them that way. Sage makes a distinction between general symbolic expressions and polynomials. This means, for instance, that there are operations permitted on one type of object that are not defined on the other (for example, quo_rem does not work on an expression). This is somewhat annoying at times, but fortunately, there are ways to get Sage to display what type of thing a given object is and to change that if necessary.
Defining the ring of polynomials with coefficients in a specific ring
Say we want to perform computations with polynomials in
the ring $R = {\bf Q}[x]$ (What does $\bf{Q}[x]$ mean?). Sage lets us define this structure
in several different ways. Here is the most direct:
R.<x> = PolynomialRing(QQ)
(Other possibilities for the coefficient ring would be ZZ -- the ring of integers, Integers(n) for the integers mod $n$ (fill in the specific integer $n$, of course!), GF(p) for the finite field of order $p$, and many, many others.)
Try it out! (Example 2)
Define the ring $R = {\bf Q}[x]$.
︡aa05d0bc-8902-4615-8ca8-b6b8b8ea12e8︡{"html": "Even though some of the expressions we have run into in examples previously look to us like polynomials, Sage would not recognize them that way. Sage makes a distinction between general symbolic expressions and polynomials. This means, for instance, that there are operations permitted on one type of object that are not defined on the other (for example, quo_rem does not work on an expression). This is somewhat annoying at times, but fortunately, there are ways to get Sage to display what type of thing a given object is and to change that if necessary.
\nDefining the ring of polynomials with coefficients in a specific ring
\nSay we want to perform computations with polynomials in
the ring $R = {\\bf Q}[x]$ (What does $\\bf{Q}[x]$ mean?). Sage lets us define this structure
in several different ways. Here is the most direct:
R.<x> = PolynomialRing(QQ)
\n(Other possibilities for the coefficient ring would be ZZ -- the ring of integers, Integers(n) for the integers mod $n$ (fill in the specific integer $n$, of course!), GF(p) for the finite field of order $p$, and many, many others.)
\nTry it out! (Example 2)
\nDefine the ring $R = {\\bf Q}[x]$.
"}︡ ︠2c0e5854-e21f-4f72-8e40-560825d8b3c7︠ #Example 2 ︡4c8887e4-7c4b-4ee1-abeb-fa48ba0b6045︡︡ ︠c9059e69-8022-4dab-b7cc-0b16d2dc3459i︠ %htmlIf you need to determine whether something (say f) is really a polynomial (for example, if it is causing an error message), you can enter a command like this:
f.parent()
Try it out! (Example 3)
What is your $f$ that you defined before?
︡bd93e469-885f-42dd-bbac-422c8e0632de︡{"html": "If you need to determine whether something (say f) is really a polynomial (for example, if it is causing an error message), you can enter a command like this:
\nf.parent()
\nTry it out! (Example 3)
\nWhat is your $f$ that you defined before?
"}︡ ︠e1c90466-445a-48ab-9ad3-6b24edd6cbd4︠ #Example 3 ︡ff5e6f2c-b308-4d88-a9a3-2250534e82fc︡︡ ︠1897fe54-3749-45e4-b403-171f69973b7ai︠ %htmlThe output will indicate the ``parent structure'' in which f is defined. It should be something like Univariate Polynomial ring over x over Rational Field for some of the following commands to work. If the output is something like Symbolic Ring, then the object is not a polynomial; it is just a general symbolic expression.
All is not lost, though. If you have a symbolic polynomial expression that contains only the variable $x$, you can ``force'' Sage to treat it as an element of the ring $R$ defined above with a command like this:
fR = R(f)
(you could also use the same name if you wanted to).
Try it out! (Example 4)
Use your newfound knowledge of defining polynomials to redefine $f$ as a polynomial.
︡2d0d9ab4-5e02-4152-82ec-842f4fc6f51b︡{"html": "The output will indicate the ``parent structure'' in which f is defined. It should be something like Univariate Polynomial ring over x over Rational Field for some of the following commands to work. If the output is something like Symbolic Ring, then the object is not a polynomial; it is just a general symbolic expression.
\nAll is not lost, though. If you have a symbolic polynomial expression that contains only the variable $x$, you can ``force'' Sage to treat it as an element of the ring $R$ defined above with a command like this:
\nfR = R(f)
\n(you could also use the same name if you wanted to).
\nTry it out! (Example 4)
\nUse your newfound knowledge of defining polynomials to redefine $f$ as a polynomial.
"}︡ ︠cf6165d6-ddbf-4493-9cfc-12e03b921e60︠ #Example 4 ︡d1565a62-13c5-4b6f-a7b9-d1be6c647324︡︡ ︠65f32cca-8d20-465b-904e-7d2e3a6de575i︠ %htmlOperations on polynomials
Sage has built-in commands:
Try it out! (Example 5)
Factor $f$ over the rationals.
︡bac4cee9-5b54-4ed5-a58d-67ae5364b377︡{"html": "Operations on polynomials
\nSage has built-in commands:
\nTry it out! (Example 5)
\nFactor $f$ over the rationals.
"}︡ ︠cdc4d911-fa6c-4fa3-a301-1515f0184335︠ #Example 5 ︡c89021b8-63db-40f9-a4e3-2568ad36614c︡︡ ︠f6214d77-5405-43c2-9edf-e859605d8faai︠ %htmlExample 6
Factor the following polynomials over the rationals:
a.) $p=x^2-2x+1$
b.) $s=x^3-3x^2+4$
c.) $t=x^2-2$.
︡4ba6241f-9b89-452d-95f9-acaa43a5cef4︡{"html": "Example 6
\nFactor the following polynomials over the rationals:
\na.) $p=x^2-2x+1$
\nb.) $s=x^3-3x^2+4$
\nc.) $t=x^2-2$.
"}︡ ︠f2d30623-d8d9-4a62-9b18-3053de252c67︠ #Example 6 ︡32b79be2-2eab-4ff2-a521-d2f1a83ac84f︡︡ ︠aa540ff1-2a1c-4889-81ce-07a7f09c8cdfi︠ %htmlTry it out! (Example 7)
Compute the following:
a.) Divide $x^4-5x^3+8x^2-4x$ by $x-2$ (what do you expect to get?)
b.) Divide $x-2$ by $x^4-5x^3+9x^2-4x$.
c.) Divide $x^3-4x-5$ by $x^2-2x-4$.
︡8d4054f6-5928-42a0-ac69-c07ab982c9f4︡{"html": "Try it out! (Example 7)
\nCompute the following:
\na.) Divide $x^4-5x^3+8x^2-4x$ by $x-2$ (what do you expect to get?)
\nb.) Divide $x-2$ by $x^4-5x^3+9x^2-4x$.
\nc.) Divide $x^3-4x-5$ by $x^2-2x-4$.
"}︡ ︠d0de2d73-2b3e-47eb-8663-f197d1271521︠ #Example 7 ︡0a85bb8b-0b94-49c3-afef-4f54376d99e4︡︡ ︠e9e490da-154e-4e1d-a921-5651c16d0b38i︠ %htmlTry it out! (Example 8)
Compute the gcds of the following pairs of polynomials. Given your results from Example 7, think critically about whether or not these answers make sense.
a.) $\gcd(x^5-2x^4-4x^3+8x^2-2x+4, x-2)$
b.) $\gcd(x^3-4x-5, x^2-2x-4)$
Example 9
Here is a sample sequence of Sage input lines illustrating some of these commands. Try entering and executing them in sequence. Look carefully at the output and make sure you understand what happened in each case:
R.<x> = PolynomialRing(QQ)
cube = (x**2 - 16)**3
print cube
s = (x**2-x+1)*(x - 4)
print s
(q,r) = cube.quo_rem(s)
print "q = ", q," , r = ", r
q*s + r == s
︡aeb2dcf6-1265-4696-bb65-86361baf280f︡{"html": "Try it out! (Example 8)
\nCompute the gcds of the following pairs of polynomials. Given your results from Example 7, think critically about whether or not these answers make sense.
\na.) $\\gcd(x^5-2x^4-4x^3+8x^2-2x+4, x-2)$
\nb.) $\\gcd(x^3-4x-5, x^2-2x-4)$
\nExample 9
\nHere is a sample sequence of Sage input lines illustrating some of these commands. Try entering and executing them in sequence. Look carefully at the output and make sure you understand what happened in each case:
\nR.<x> = PolynomialRing(QQ)
\ncube = (x**2 - 16)**3
\nprint cube
\ns = (x**2-x+1)*(x - 4)
\nprint s
\n(q,r) = cube.quo_rem(s)
\nprint \"q = \", q,\" , r = \", r
\nq*s + r == s
"}︡ ︠a35da2f5-641f-4c07-91ad-144f05558851︠ #Example 9 ︡78f1e019-bffa-45f6-99d4-bc020bd431fa︡︡ ︠8e560573-5b45-414d-9e77-f6cdd44fe02ci︠ %htmlB. Graph Theory
Sage can be used to generate graphs.
For example, you can use sage to create any complete graph with $n$ vertices using graphs.CompleteGraph(n)
Example 1
See below for the complete graph on 5 vertices.
︡7b23e808-e879-4402-916e-429708fc0842︡{"html": "B. Graph Theory
\nSage can be used to generate graphs.
\n\n
For example, you can use sage to create any complete graph with $n$ vertices using graphs.CompleteGraph(n)
\nExample 1
\nSee below for the complete graph on 5 vertices.
"}︡ ︠58d2fb74-a134-4ad5-be65-045ae504c76ds︠ G=graphs.CompleteGraph(5);show(G); ︡bfc3994b-385a-4398-b447-61fc7c97db05︡{"d3":{"data":{"charge":0,"directed":false,"edge_labels":false,"edge_thickness":2,"gravity":0,"height":null,"link_distance":50,"link_strength":0,"links":[{"color":"#aaa","curve":0,"name":"","source":0,"strength":0,"target":1},{"color":"#aaa","curve":0,"name":"","source":0,"strength":0,"target":2},{"color":"#aaa","curve":0,"name":"","source":0,"strength":0,"target":3},{"color":"#aaa","curve":0,"name":"","source":0,"strength":0,"target":4},{"color":"#aaa","curve":0,"name":"","source":1,"strength":0,"target":2},{"color":"#aaa","curve":0,"name":"","source":1,"strength":0,"target":3},{"color":"#aaa","curve":0,"name":"","source":1,"strength":0,"target":4},{"color":"#aaa","curve":0,"name":"","source":2,"strength":0,"target":3},{"color":"#aaa","curve":0,"name":"","source":2,"strength":0,"target":4},{"color":"#aaa","curve":0,"name":"","source":3,"strength":0,"target":4}],"loops":[],"nodes":[{"group":"0","name":"0"},{"group":"0","name":"1"},{"group":"0","name":"2"},{"group":"0","name":"3"},{"group":"0","name":"4"}],"pos":[[6.123233995736766e-17,-1],[-0.9510565162951535,-0.3090169943749475],[-0.5877852522924732,0.8090169943749473],[0.5877852522924729,0.8090169943749476],[0.9510565162951536,-0.3090169943749472]],"vertex_labels":true,"vertex_size":7,"width":null},"viewer":"graph"}}︡{"done":true}︡ ︠0922ba87-67b9-41aa-8a38-1a3d53b6ed2f︠ for n in ︡a47a7130-157d-4dce-be67-05a390754774︡ ︠13dba963-eabb-4319-9068-3ebefa72e7e1i︠ %htmlYou can also figure out properties of graphs using commands:
Try it out! (Example 3)
Try out some of these commands on our previous graph $G$.
︡69bbe3f3-e548-439b-b25d-3477a8947dd3︡{"html": "You can also figure out properties of graphs using commands:
\nTry it out! (Example 3)
\nTry out some of these commands on our previous graph $G$.
"}︡ ︠9b4b9b95-294b-4d99-88ae-a47ad25438c4︠ #Example 3 ︡c31d070b-de22-4947-948e-5bbd03549f87︡︡ ︠2259a310-4033-43a7-ba37-aa55bf2cfca1i︠ %htmlHow do you input graphs?
To create a graph, you can use one of the ready-made commands like CompleteGraph or Bipartite. If your graph is a little more unusual, you could use the adjacency matrix instead:
$M=(m_{ij})$, where $m_{ij}=1$ if and only if there is an edge between vertex $i$ and vertex $j$.
Then you would create the graph as:
Graph(M)
Try it out! (Example 4)
Try creating a graph using an adjacency matrix. How would you create loops?
︡5132b143-7131-4a43-9b8f-c4ca17858d00︡{"html": "How do you input graphs?
\nTo create a graph, you can use one of the ready-made commands like CompleteGraph or Bipartite. If your graph is a little more unusual, you could use the adjacency matrix instead:
\n$M=(m_{ij})$, where $m_{ij}=1$ if and only if there is an edge between vertex $i$ and vertex $j$.
\nThen you would create the graph as:
\nGraph(M)
\nTry it out! (Example 4)
\nTry creating a graph using an adjacency matrix. How would you create loops?
"}︡ ︠1ea51fbc-c9e3-47f1-836c-579f9a08346f︠ #Example 4 ︡e8a44995-509f-42fd-8d61-7f1beb47ee47︡︡ ︠fc23b1ca-5eee-4d7d-820a-01d5d2c2f6e0i︠ %htmlC. Game Theory
The game theory package has some options for creating common games, such as the Prisoners Dilemma.
︡34f888e5-572a-47f4-87b1-2e8155f6c070︡{"html": "C. Game Theory
\nThe game theory package has some options for creating common games, such as the Prisoners Dilemma.
"}︡ ︠c5a4bca1-f368-4836-aa7b-40f7ec46d4e8s︠ g=game_theory.normal_form_games.PrisonersDilemma ︡aa9a0d08-5976-44ca-9cd6-76b8d5eb3a97︡{"done":true} ︠73a51eda-c67a-475a-9c86-dd0ca8c74d14i︠ %htmlNotice that the assumed matrices are
$$A=\begin{bmatrix} R & S \\ T & P \end{bmatrix}=\begin{bmatrix} -2 & -5 \\ 0 & -4\end{bmatrix}$$
$$B=\begin{bmatrix} R & T \\ S & P \end{bmatrix}=\begin{bmatrix} -2 & 0 \\ -5 & -4\end{bmatrix}$$
You can obtain the Nash equilibrium for this via the command g.obtain_nash()
Try it out! (Example 1)
Find the Nash equilibrium for the above system.
︡f7983db7-0ada-4d75-b843-407dfb02d55c︡{"html": "Notice that the assumed matrices are
\n$$A=\\begin{bmatrix} R & S \\\\ T & P \\end{bmatrix}=\\begin{bmatrix} -2 & -5 \\\\ 0 & -4\\end{bmatrix}$$
\n$$B=\\begin{bmatrix} R & T \\\\ S & P \\end{bmatrix}=\\begin{bmatrix} -2 & 0 \\\\ -5 & -4\\end{bmatrix}$$
\nYou can obtain the Nash equilibrium for this via the command g.obtain_nash()
\nTry it out! (Example 1)
\nFind the Nash equilibrium for the above system.
"}︡ ︠1a42f0d4-be31-405a-b437-7bc598510efb︠ #Example 1 ︡9a3b13f4-0d51-412f-8479-834da593087a︡︡ ︠001b2432-e301-4dd8-b6e9-944ddbd1d719i︠ %htmlIf you'd like to change the values of $R, P, S, T$, you can do so via the command
game_theory.normal_form_games.PrisonersDilemma(R=0, P=-1, S=-4, T=5)
Try it out! (Example 2)
Try out different values for the game, along with computing the Nash equilbirium. Which values are prohibited?
︡5d423b9f-580b-4330-94ea-a0fe9c6148ad︡{"html": "If you'd like to change the values of $R, P, S, T$, you can do so via the command
\ngame_theory.normal_form_games.PrisonersDilemma(R=0, P=-1, S=-4, T=5)
\nTry it out! (Example 2)
\nTry out different values for the game, along with computing the Nash equilbirium. Which values are prohibited?
"}︡ ︠d2958b6f-132e-49aa-97b5-5be81d2949c4︠ #Example 2 ︡890b6aee-16d9-4248-9c67-7aff3c0cb1b6︡︡ ︠8d39c02d-e1af-4527-9e80-5119c7154fe8i︠ %htmlSome other games (look them up if interested)
Some other games (look them up if interested)
\n