1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
% General example LaTeX file for including Sage calculations and plots
% Build with: (pdf)latex example.tex; sage example.sage; pdflatex
% example.tex Please read README and the documentation of the SageTeX
% package for more information!
\documentclass{article}
\title{Examples of embedding Sage in \LaTeX}
\author{}
\usepackage{sagetex}
%
% If you want sagetex to use Imagemagick's convert utility to make eps
% files from png files when generating a dvi file, add the
% "imagemagick" option above:
%
% \usepackage[imagemagick]{sagetex}
\begin{document}
\maketitle
\section{Inline Sage, code blocks}
This is an example $2+2=\sage{2+2}$. If you raise the current year mod
$100$ ($\sage{mod(\the\year, 100)}$) to the power of the current day
($\the\day$), you get $\sage{Integer(mod(\the\year, 100))^\the\day}$.
Also, $\the\year$ modulo $42$ is $\sage{\the\year \percent 42}$.
Code block which uses a variable \texttt{s} to store the solutions:
\begin{sageblock}
var('a,b,c')
eqn = [a+b*c==1, b-a*c==0, a+b==5]
s = solve(eqn, a,b,c)
\end{sageblock}
Solutions of $\mbox{eqn}=\sage{eqn}$:
\[
\sage{s[0]}
\]
\[
\sage{s[1]}
\]
Now we evaluate the following block:
\begin{sageblock}
E = EllipticCurve("37a")
\end{sageblock}
You can't do assignment inside \verb|\sage| macros, since Sage doesn't
know how to typeset the output of such a thing. So you have to use a
code block. The elliptic curve $E$ given by $\sage{E}$
has discriminant $\sage{E.discriminant()}$.
You can do anything in a code block that you can do in Sage and/or
Python. Here we save an elliptic curve into a file.
\begin{sageblock}
try:
E = load('E2')
except IOError:
E = EllipticCurve([1,2,3,4,5])
E.anlist(100000)
E.save('E2')
\end{sageblock}
The 9999th Fourier coefficient of $\sage{E}$ is
$\sage{E.anlist(100000)[9999]}$.
The following code block doesn't appear in the typeset file\dots
\begin{sagesilent}
e = 2
e = 3*e + 1
\end{sagesilent}
but we can refer to whatever we did in that code block: $e=\sage{e}$.
\begin{sageblock}
var('x')
f = log(sin(x)/x)
\end{sageblock}
The Taylor Series of $f$ is: $\sage{ f.taylor(x, 0, 10) }$.
\section{Plotting}
Here's a plot of the elliptic curve $E$.
\sageplot{E.plot(-3,3)}
\begin{sagesilent}
var('x')
f=-x^3+3*x^2+7*x-4
\end{sagesilent}
You can use variables to hold plot objects and do stuff with them.
\begin{sageblock}
p = plot(f, x, -5, 5)
\end{sageblock}
Here's a small plot of $f$ from $-5$ to $5$, which I've centered:
\centerline{\sageplot[scale=.2]{p}}
On second thought, use the default size of $3/4$ the \verb|\textwidth|
and don't use axes:
\sageplot{p, axes=False}
Remember, you're using Sage, and can therefore call upon any of the
software packages Sage is built out of.
\begin{sageblock}
f = maxima('sin(x)^2*exp(x)')
g = f.integrate('x')
\end{sageblock}
Plot $g(x)$, but don't typeset it.
\begin{sagesilent}
# g is a Maxima thingy, it needs to get converted into a Sage object
plot1 = plot(g.sage(),x,-1,2*pi)
\end{sagesilent}
You can specify a file format and options for \verb|includegraphics|.
The default is for EPS and PDF files, which are the best choice in
almost all situations. (Although see the section on 3D plotting.)
\sageplot[angle=45, width=.5\textwidth][png]{plot1}
If you use regular \verb|latex| to make a DVI file, you'll see a box,
beause DVI files can't include PNG files. If you use \verb|pdflatex|
that will work. See the documentation for details.
When using \verb|\sageplot|, you can pass in just about anything that
Sage can call \verb|.save()| on to produce a graphics file:
\begin{center}
\sageplot{plot1 + plot(f.sage(),x,-1,2*pi,rgbcolor=hue(0.4)), figsize=[1,2]}
\end{center}
\sageplot{graphs.FlowerSnark().plot()}
\begin{sageblock}
G4 = DiGraph({1:[2,2,3,5], 2:[3,4], 3:[4], 4:[5,7], 5:[6]},\
multiedges=True)
G4plot = G4.plot(layout='circular')
\end{sageblock}
\sageplot{G4plot, axes=False}
Indentation and so on works fine.
\begin{sageblock}
s = 7
s2 = 2^s
P.<x> = GF(2)[]
M = matrix(parent(x),s2)
for i in range(s2):
p = (1+x)^i
pc = p.coeffs()
a = pc.count(1)
for j in range(a):
idx = pc.index(1)
M[i,idx+j] = pc.pop(idx)
matrixprogram = matrix_plot(M,cmap='Greys')
\end{sageblock}
And here's the picture:
\sageplot{matrixprogram}
\subsection{3D plotting}
3D plotting right now is problematic because there's no convenient way
to produce vector graphics. We can make PNGs, though, and since the
\verb|sageplot| command defaults to EPS and PDF, \emph{you must specify
a valid format for 3D plotting}. Sage right now (version 2.10.3) can't
produce EPS or PDF files from plot3d objects, so if you don't specify a
valid format, things will go badly. You can specify the
``\texttt{imagemagick}'' option, which will use the Imagemagick
\texttt{convert} utility to make EPS files. See the documentation for
details.
Here's the famous Sage cube graph:
\begin{sageblock}
G = graphs.CubeGraph(5)
\end{sageblock}
% need empty [] so sageplot knows you want png format, and aren't
% passing an option to includegraphics
\sageplot[][png]{G.plot3d(engine='tachyon')}
\end{document}
|