\documentclass[12pt]{article} %You may change font size to 10pt or 11pt
\usepackage[top=.5in, bottom=1in, left=1in, right=1in]{geometry} %Change margins here
\title{Common Mistakes with Sage} %Change title here
\author{Aaron Tresham}
\date{\today}

\usepackage{parskip, amsmath, amssymb, latexsym, graphicx, enumerate, sagetex}

\begin{document}
\maketitle
\newcommand{\ds}{\displaystyle}

\vspace{.1in}

\section{Common Error Messages}

Many (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.



\subsection{SyntaxError: invalid syntax}

This is one of the broader errors, and many mistakes can lead to this error. Here are some examples:

\begin{itemize}

\item Missing Explicit Multiplication

Every multiplication in Sage must be explicitly typed.

For 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.

\item Extra End Parenthesis

Every 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))|.

\item Unmarked Comment

Sometimes 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).

\end{itemize}

\subsection{SyntaxError: unexpected EOF while parsing}

I have seen two kinds of mistakes that lead to this message:

\begin{itemize}

\item Missing End Parenthesis

This error usually results from missing an end parenthesis (or, equivalently, having an extra beginning parenthesis).

For example, \verb|f(x)=sin(x| will result in this error.

This mistake is more common than having an extra end parenthesis. If you see ``unexpected EOF,'' then check for unmatched parentheses.

\item Adding a Space after a \%

Another 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|.

\end{itemize}

\subsection{TypeError: \underline{\hspace{.5in}} object is not callable}

I have seen two kinds of mistakes result in this error:

\begin{itemize}

\item Missing Multiplication

Every 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.

\item Overwriting a Sage Command

This 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).

\end{itemize}

\subsection{TypeError: \underline{\hspace{.5in}} takes at least 1 argument (0 given)}

If 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).

For example, if you try to run \verb|plot()| with no function to graph, you will see

``\verb|TypeError: plot() takes at least 1 argument (0 given).|''

If you try \verb|solve(x^2==4)|, then you will see

``\verb|TypeError: solve() takes at least 1 positional argument (0 given).|''

This is because you forgot to specify the variable to solve for. You should have typed \verb|solve(x^2==4,x)|.

\subsection{TypeError: unable to simplify to float approximation}

There may be various ways for this error to arise.

For 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).

\subsection{TypeError: invalid integration algorithm}

You 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|.

\subsection{NameError: name '\underline{\hspace{.25in}}' is not defined}

There are a few different situations in which a ``NameError'' may arise.

\begin{itemize}

\item Failing to Declare a Variable

The variable $x$ is automatically declared, but any other variable must be explicitly declared using \verb|%var|.

For example, you may want to find $\frac{d}{dx} ax^2+bx+c$, so you type

\verb|derivative(a*x^2+b*x+c,x)|.

When you run this, you will get ``NameError: name 'a' is not defined.''

Instead, you need to do this:
\begin{verbatim}
%var a,b,c
derivative(a*x^2+b*x+c,x)
\end{verbatim}

Note: 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$.

\item Misspelling a Command Name

This 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.''

\item Forgetting Quote Marks

Some options, such as the plot options \verb|color| and \verb|linestyle|, will require quote marks.

For 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.''

\end{itemize}

\subsection{RuntimeError: Error in line(): option '\underline{\hspace{.25in}}' not valid}

Many 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.

For 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.''

\subsection{RuntimeError: f appears to have no zero on the interval}

This 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.

For 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|.

\subsection{RuntimeError: ECL says: Error executing code in Maxima}

Sage 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.

For example, if you try to compute $\ds\int\sqrt{1+\sin(x)^2}\,dx$ by typing

\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.

\subsection{ValueError: Assumption is inconsistent}

This 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|.

A 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.

\subsection{ValueError: Assumption is redundant}

This 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()|.

\textbf{You must remember to forget()!}

\subsection{ValueError: Integral is divergent}

Mathematically, this is really no error at all. This error may result when you try to calculate a divergent improper integral.

For example, try to find $\ds\int_0^1\frac{1}{x}\, dx$ by typing: \verb|integral(1/x,x,0,1)|.

\subsection{ValueError: Sum is divergent}

This is similar to the last error, but this message results when you try to calculate the sum of a divergent infinite series.

For example, try to find $\ds\sum_{n=1}^{\infty}\frac{1}{n}$ by typing: \verb|sum(1/n,n,1,+Infinity)|

(note: you must have \verb|%var n| before this line).

Some divergent series will not give you this error. Here are some examples:

\hspace{.25in}\verb|sum(n*(-2)^n,n,1,+Infinity)| will output \verb|Infinity|.

\hspace{.25in}\verb|sum(n*(-1)^n,n,1,+Infinity)| will output \verb|und|.

\hspace{.25in}\verb|sum(n*2^n,n,1,+Infinity)| will output \verb|+Infinity|.

\subsection{DeprecationWarning}

This 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.

For 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:

``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=...)''

Sage 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.

\subsection{verbose 0 (3757: plot.py, generate\_plot\_points) WARNING: When plotting, failed to evaluate function at 100 points.}

This 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.

For 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$.

\section{Other Common Mistakes}

Some 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!

\subsection{Missing Explicit Multiplication}

Sometimes 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!

\subsection{Missing Parentheses}

There are many situations when parentheses are not optional.

For 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}$.

Here's an example involving powers: $\verb|e^2*x|=e^{2}\cdot x\ne e^{2x}=\verb|e^(2*x)|$.

\subsection{Scientific Notation}

If 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.$

Here is another example: \verb|3.456400000e-12| is not close to 3.5, it is actually close to $0$:

$3.456400000\times 10^{-12}=0.0000000000034564\approx 0$.

\subsection{Derivatives}

There 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)$.

The derivative command in Sage answers the first question. If you want $f'(x)$, then type \verb|derivative(f,x)|.

To 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)|.

Now 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)$.

If you want the second derivative, then you add a 2 to the derivative command:

\verb|derivative(f,x,2)| is $f''(x)$.

Similarly, \verb|derivative(f,x,10)| is the 10th derivative, $f^{(10)}(x)$.

A 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)$.


\vspace{.5in}

Help me build this list! Tell me any other common mistakes you come across. Thanks.

\end{document}
