 CoCalc Public FilesMisc Stuff / Sage Common Mistakes / Sage Common Mistakes.tex
Author: Aaron Tresham
Views : 58
Compute Environment: Ubuntu 18.04 (Deprecated)
1\documentclass[12pt]{article} %You may change font size to 10pt or 11pt
2\usepackage[top=.5in, bottom=1in, left=1in, right=1in]{geometry} %Change margins here
3\title{Common Mistakes with Sage} %Change title here
4\author{Aaron Tresham}
5\date{\today}
6
7\usepackage{parskip, amsmath, amssymb, latexsym, graphicx, enumerate, sagetex}
8
9\begin{document}
10\maketitle
11\newcommand{\ds}{\displaystyle}
12
13\vspace{.1in}
14
15\section{Common Error Messages}
16
17Many (but not all) mistakes that you make in Sage will result in an error message. This is usually several lines of output all in red. You can ignore most of the message; just look down at the last line, which tells you the type of error.
18
19
20
21\subsection{SyntaxError: invalid syntax}
22
23This is one of the broader errors, and many mistakes can lead to this error. Here are some examples:
24
25\begin{itemize}
26
27\item Missing Explicit Multiplication
28
29Every multiplication in Sage must be explicitly typed.
30
31For example, to define the function $f(x)=5x^2+3x-2$, you must type a multiplication after the 5 and 3: \verb|f(x)=5*x^2+3*x-2|. If you try \verb|f(x)=5x^2+3x-2|, you will get this error.
32
33\item Extra End Parenthesis
34
35Every set of parentheses must have a beginning and an end: $(\ \cdots )$. If you have an extra end parenthesis, you will get this error. For example, try \verb|f(x)=sin(x))|.
36
37\item Unmarked Comment
38
39Sometimes you may want to write a note in Sage. You can do this by putting a pound sign (\#) at the beginning of the line. This tells Sage that the rest of the line is a comment and should be ignored when processing. If you leave of the comment marker, Sage will try to intepret your comment as a command, often resulting in a syntax error (may depend on exactly what's in your comment).
40
41\end{itemize}
42
43\subsection{SyntaxError: unexpected EOF while parsing}
44
45I have seen two kinds of mistakes that lead to this message:
46
47\begin{itemize}
48
49\item Missing End Parenthesis
50
51This error usually results from missing an end parenthesis (or, equivalently, having an extra beginning parenthesis).
52
53For example, \verb|f(x)=sin(x| will result in this error.
54
55This mistake is more common than having an extra end parenthesis. If you see unexpected EOF,'' then check for unmatched parentheses.
56
57\item Adding a Space after a \%
58
59Another mistake that results in this message is having a space after the \% sign in a \% decorator. For example, \verb|% var| (note the space after \%) instead of \verb|%var|.
60
61\end{itemize}
62
63\subsection{TypeError: \underline{\hspace{.5in}} object is not callable}
64
65I have seen two kinds of mistakes result in this error:
66
67\begin{itemize}
68
69\item Missing Multiplication
70
71Every multiplication must be explicitly typed (with a *), and missing multiplications may result in this error. For example, try \verb|3(5+2)|. The reason is that Sage confuses this with function notation, such as \verb|f(5+2)|. When you type \verb|3(5+2)|, Sage wants to call a function named 3,'' but of course 3 is not a function, so it is not callable.'' What you want to type is \verb|3*(5+2)|, with an explicit multiplication.
72
73\item Overwriting a Sage Command
74
75This error may also result if you try to use a Sage command that you have accidentally overwritten. For example, if you type \verb|cos=5|, the cos function will be overwritten with the number 5. Then if you try \verb|cos(2)|, you will get a object is not callable'' error (Sage tries to do 5(2), as if 5 were a function).
76
77\end{itemize}
78
79\subsection{TypeError: \underline{\hspace{.5in}} takes at least 1 argument (0 given)}
80
81If you fail to give a Sage command the number of arguments it expects, you will get an error like this (the numbers may be different).
82
83For example, if you try to run \verb|plot()| with no function to graph, you will see
84
85\verb|TypeError: plot() takes at least 1 argument (0 given).|''
86
87If you try \verb|solve(x^2==4)|, then you will see
88
89\verb|TypeError: solve() takes at least 1 positional argument (0 given).|''
90
91This is because you forgot to specify the variable to solve for. You should have typed \verb|solve(x^2==4,x)|.
92
93\subsection{TypeError: unable to simplify to float approximation}
94
95There may be various ways for this error to arise.
96
97For example, if you try \verb|find_root(x^2==4,x,0,5)|, you will get this error. The problem is the x'' -- the correct syntax would be \verb|find_root(x^2==4,0,5)| (note: you need the extra x'' when using the \verb|solve| command, since \verb|solve| can handle equations with more than one variable).
98
99\subsection{TypeError: invalid integration algorithm}
100
101You may see this error when using the \verb|numerical_integral| command. For example, $\ds\int_1^2\frac{1}{x}\,dx\approx$ \verb|numerical_integral(1/x,1,2)|. Notice that the regular integral command likes to have the variable of integration specified: \verb|integral(1/x,x,1,2)|. The x'' specifies the variable. However, \verb|numerical_integral| does not need the variable specified, since there can be only one variable involved (so the answer will be a number). If you try to specify the variable anyway, e.g., \verb|numerical_integral(1/x,x,1,2)|, then you will get this \verb|TypeError|.
102
103\subsection{NameError: name '\underline{\hspace{.25in}}' is not defined}
104
105There are a few different situations in which a NameError'' may arise.
106
107\begin{itemize}
108
109\item Failing to Declare a Variable
110
111The variable $x$ is automatically declared, but any other variable must be explicitly declared using \verb|%var|.
112
113For example, you may want to find $\frac{d}{dx} ax^2+bx+c$, so you type
114
115\verb|derivative(a*x^2+b*x+c,x)|.
116
117When you run this, you will get NameError: name 'a' is not defined.''
118
119Instead, you need to do this:
120\begin{verbatim}
121%var a,b,c
122derivative(a*x^2+b*x+c,x)
123\end{verbatim}
124
125Note: There are certain contexts that do not require a variable declaration. For example, if you type \verb|y=10|, you do not have to declare $y$. In this case, $y$ is not really a variable, it's just another name for $10$.
126
127\item Misspelling a Command Name
128
129This error can also result from a simple typo. For example, if you try \verb|f(x)=son(x)| instead of \verb|f(x)=sin(x)| you will get NameError: name 'son' is not defined.''
130
131\item Forgetting Quote Marks
132
133Some options, such as the plot options \verb|color| and \verb|linestyle|, will require quote marks.
134
135For example, if you want to change the plot color to black, you can add \verb|color='black'| inside the plot command. If you leave off the quote marks, you will get NameError: name 'black' is not defined.''
136
137\end{itemize}
138
139\subsection{RuntimeError: Error in line(): option '\underline{\hspace{.25in}}' not valid}
140
141Many commands have optional arguments. For example, you can set the plot range using the xmin and xmax options: \verb|plot(f(x),xmin=-10,xmax=10)|. If you misspell one of these options, you may get this error.
142
143For example, if you misspell xmax'' and type \verb|plot(x^2,xmin=0,xman=5)|, you will get RuntimeError: Error in line(): option 'xman' not valid.''
144
145\subsection{RuntimeError: f appears to have no zero on the interval}
146
147This error occurs when you try to solve an equation using the \verb|find_root| command, but the equation has no solutions in the interval you specified.
148
149For example, if you try \verb|find_root(cos(x)==x,-1,0)|, you will get this error. This is because the equation $\cos(x)=x$ has only one solution ($x\approx 0.74$), and this solution is not in the interval from $-1$ to $0$ that you gave to \verb|find_root|.
150
151\subsection{RuntimeError: ECL says: Error executing code in Maxima}
152
153Sage uses another program called Maxima to perform many mathematical calculations; however, there are some calculations that Maxima is not able to do, which can result in this error.
154
155For example, if you try to compute $\ds\int\sqrt{1+\sin(x)^2}\,dx$ by typing
156
157\verb|integral(sqrt(1+sin(x)^2),x)|, you will get this error, since Maxima is not able to evaluate this antiderivative. If you get this error from a definite integral, then you can use the \verb|numerical_integral| command to get a decimal approximation of the answer.
158
159\subsection{ValueError: Assumption is inconsistent}
160
161This error arises when you set assumptions about variables (we do this when dealing with improper integrals). If your assumptions are contradictory, you will get this error. For example, if you try: \verb|assume(x<-1); assume(x>1)|, you will get this \verb|ValueError|.
162
163A common way to get this error is failing to forget'' previous assumptions. You may use \verb|assume(x<-1)| for one problem, and then when you move on to a different problem you may use \verb|assume(x>1)|. After you finish the first problem, you must run the command \verb|forget()|, which will clear the assumptions.
164
165\subsection{ValueError: Assumption is redundant}
166
167This is similar to the last error, except you have two assumptions that are redundant rather than contradictory. For example, if you try: \verb|assume(x>5); assume(x>1)|, you will get this \verb|ValueError|. This can also result from failing to use \verb|forget()|.
168
169\textbf{You must remember to forget()!}
170
171\subsection{ValueError: Integral is divergent}
172
173Mathematically, this is really no error at all. This error may result when you try to calculate a divergent improper integral.
174
175For example, try to find $\ds\int_0^1\frac{1}{x}\, dx$ by typing: \verb|integral(1/x,x,0,1)|.
176
177\subsection{ValueError: Sum is divergent}
178
179This is similar to the last error, but this message results when you try to calculate the sum of a divergent infinite series.
180
181For example, try to find $\ds\sum_{n=1}^{\infty}\frac{1}{n}$ by typing: \verb|sum(1/n,n,1,+Infinity)|
182
183(note: you must have \verb|%var n| before this line).
184
185Some divergent series will not give you this error. Here are some examples:
186
187\hspace{.25in}\verb|sum(n*(-2)^n,n,1,+Infinity)| will output \verb|Infinity|.
188
189\hspace{.25in}\verb|sum(n*(-1)^n,n,1,+Infinity)| will output \verb|und|.
190
191\hspace{.25in}\verb|sum(n*2^n,n,1,+Infinity)| will output \verb|+Infinity|.
192
193\subsection{DeprecationWarning}
194
195This is not an error, but it will result in a block of red in the output. When you get a deprecation warning, Sage will output the answer you expect. However, a future version of Sage may eliminate the syntax you used to get that output.
196
197For example, if you type \verb|derivative(x^2,x)(3)|, then you expect to get $\ds\left.\frac{d}{dx}x^2\right|_{x=3}=6$. You do indeed get 6, but you also get this warning message:
198
199DeprecationWarning: 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=...)''
200
201Sage is telling you to do this: \verb|derivative(x^2,x)(x=3)|. Sage wants you to explicitly indicate that $x=3$. Right now, this is optional (although preferred). In a future version of Sage, $x=3$ may be necessary.
202
203\subsection{verbose 0 (3757: plot.py, generate\_plot\_points) WARNING: When plotting, failed to evaluate function at 100 points.}
204
205This message appears in black rather than red. You will get this warning before a graph if your plot window includes input values not in the domain of the function you are plotting.
206
207For example, if you try \verb|plot(ln(x))|, then the plot will use $-1\le x \le 1$ by default. However, logarithms are not defined for $x\le 0$ (if you want the output to be a real number). Sage will produce a graph (with $0\le x \le 1$), but it will warn you that it has ignored $-1 \le x \le 0$.
208
209\section{Other Common Mistakes}
210
211Some mistakes commonly made when using Sage \textit{do not} result in an error message, but you don't get the right answer. You must be very careful about these!
212
213\subsection{Missing Explicit Multiplication}
214
215Sometimes missing multiplications will result in an error message (see above). However, here is an example that does not: To define the function $f(x)=(x-3)(x+2)$, you must type \verb|f(x)=(x-3)*(x+2)|, with an explicit multiplication between the two factors. If you leave off this multiplication, you will \emph{not} get an error message, but you \emph{will} get the wrong function!
216
217\subsection{Missing Parentheses}
218
219There are many situations when parentheses are not optional.
220
221For example, to write the fraction $\ds\frac{x+y}{5}$ in Sage, you must type \verb|(x+y)/5|. If you leave the parentheses off, you will not get an error message, you'll just get the wrong fraction: $\ds x+y/5=x+\frac{y}{5}\ne\frac{x+y}{5}$.
222
223Here's an example involving powers: $\verb|e^2*x|=e^{2}\cdot x\ne e^{2x}=\verb|e^(2*x)|$.
224
225\subsection{Scientific Notation}
226
227If the output from Sage is \verb|2.801232340e9|, you may think this is a number close to 2.8. However, this is actually a very large number (about 2.8 \emph{billion}). You have to watch out for scientific notation in Sage, which is indicated by the little e" in the output above. This output is actually $2.801232340\times 10^{9}=2,801,232,340.$
228
229Here is another example: \verb|3.456400000e-12| is not close to 3.5, it is actually close to $0$:
230
231$3.456400000\times 10^{-12}=0.0000000000034564\approx 0$.
232
233\subsection{Derivatives}
234
235There are two kinds of derivative questions: (1) the derivative function, such as $f'(x)$, and (2) the derivative at a particular value, such as $f'(3)$.
236
237The derivative command in Sage answers the first question. If you want $f'(x)$, then type \verb|derivative(f,x)|.
238
239To answer the second question, you should use a two step process. First, assign the derivative function a name. I like to use df" for derivative of f", but you can use any name you want. Type \verb|df(x)=derivative(f,x)|.
240
241Now that you have the derivative function, the second step is to plug the particluar value into this. For example, \verb|df(3)| is $f'(3)$, and \verb|df(-1)| is $f'(-1)$.
242
243If you want the second derivative, then you add a 2 to the derivative command:
244
245\verb|derivative(f,x,2)| is $f''(x)$.
246
247Similarly, \verb|derivative(f,x,10)| is the 10th derivative, $f^{(10)}(x)$.
248
249A common mistake is to use \verb|derivative(f,x,2)| when you want $f'(2)$. Of course, $f''(x)$ is very different from $f'(2)$.
250
251
252\vspace{.5in}
253
254Help me build this list! Tell me any other common mistakes you come across. Thanks.
255
256\end{document}
257