Sage Common Mistakes
Sage Common Mistakes
Aaron Tresham
This is one of the broader errors, and many mistakes can lead to this error. Here are some
examples:
• Missing Explicit Multiplication
Every multiplication in Sage must be explicitly typed.
For example, to define the function f (x) = 5x2 +3x−2, you must type a multiplication
after the 5 and 3: f(x)=5*x^2+3*x-2. If you try f(x)=5x^2+3x-2, you will get this
error.
• Extra End Parenthesis
Every set of parentheses must have a beginning and an end: ( · · · ). If you have an
extra end parenthesis, you will get this error. For example, try f(x)=sin(x)).
• 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).
1
• Missing End Parenthesis
This error usually results from missing an end parenthesis (or, equivalently, having an
extra beginning parenthesis).
For example, f(x)=sin(x will result in this error.
This mistake is more common than having an extra end parenthesis. If you see “un-
expected EOF,” then check for unmatched parentheses.
• Adding a Space after a %
Another mistake that results in this message is having a space after the % sign in a %
decorator. For example, % var (note the space after %) instead of %var.
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 plot() with no function to graph, you will see
“TypeError: plot() takes at least 1 argument (0 given).”
If you try solve(x^2==4), then you will see
“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
solve(x^2==4,x).
2
1.5 TypeError: unable to simplify to float approximation
You
Z 2 may see this error when using the numerical_integral command. For example,
1
dx ≈ numerical_integral(1/x,1,2). Notice that the regular integral command likes
1 x
to have the variable of integration specified: integral(1/x,x,1,2). The “x” specifies the
variable. However, 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., numerical_integral(1/x,x,1,2), then you will get this TypeError.
3
For example, if you want to change the plot color to black, you can add color=’black’
inside the plot command. If you leave off the quote marks, you will get “NameError:
name ’black’ is not defined.”
Many commands have optional arguments. For example, you can set the plot range using
the xmin and xmax options: 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 plot(x^2,xmin=0,xman=5), you will get
“RuntimeError: Error in line(): option ’xman’ not valid.”
This error occurs when you try to solve an equation using the find_root command, but the
equation has no solutions in the interval you specified.
For example, if you try 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 ≈ 0.74), and this solution is not in the
interval from −1 to 0 that you gave to find_root.
Sage uses another program called Maxima to perform many mathematical calculations; how-
ever, there are some calculations that Maxima is not able to do, which can result in this
error.
Z p
For example, if you try to compute 1 + sin(x)2 dx by typing
integral(sqrt(1+sin(x)^2),x), you will get this error, since Maxima is not able to eval-
uate this antiderivative. If you get this error from a definite integral, then you can use the
numerical_integral command to get a decimal approximation of the answer.
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: assume(x<-1); assume(x>1), you will get this ValueError.
A common way to get this error is failing to “forget” previous assumptions. You may use
assume(x<-1) for one problem, and then when you move on to a different problem you may
use assume(x>1). After you finish the first problem, you must run the command forget(),
which will clear the assumptions.
4
1.12 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: assume(x>5); assume(x>1), you will get this
ValueError. This can also result from failing to use forget().
You must remember to forget()!
Mathematically, this is really no error at all. This error may result when you try to calculate
a divergent improper integral.
Z 1
1
For example, try to find dx by typing: integral(1/x,x,0,1).
0 x
This is similar to the last error, but this message results when you try to calculate the sum
of a divergent infinite series.
∞
X 1
For example, try to find by typing: sum(1/n,n,1,+Infinity)
n=1
n
1.15 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.
d 2
For example, if you type derivative(x^2,x)(3), then you expect to get x = 6.
dx x=3
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=...)”
5
Sage is telling you to do this: 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.
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 plot(ln(x)), then the plot will use −1 ≤ x ≤ 1 by default. However,
logarithms are not defined for x ≤ 0 (if you want the output to be a real number). Sage will
produce a graph (with 0 ≤ x ≤ 1), but it will warn you that it has ignored −1 ≤ x ≤ 0.
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
f(x)=(x-3)*(x+2), with an explicit multiplication between the two factors. If you leave off
this multiplication, you will not get an error message, but you will get the wrong function!
If the output from Sage is 2.801232340e9, you may think this is a number close to 2.8.
However, this is actually a very large number (about 2.8 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 × 109 = 2, 801, 232, 340.
6
Here is another example: 3.456400000e-12 is not close to 3.5, it is actually close to 0:
3.456400000 × 10−12 = 0.0000000000034564 ≈ 0.
2.4 Derivatives
There are two kinds of derivative questions: (1) the derivative function, such as f 0 (x), and
(2) the derivative at a particular value, such as f 0 (3).
The derivative command in Sage answers the first question. If you want f 0 (x), then type
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 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, df(3) is f 0 (3), and df(-1) is f 0 (−1).
If you want the second derivative, then you add a 2 to the derivative command:
derivative(f,x,2) is f 00 (x).
Similarly, derivative(f,x,10) is the 10th derivative, f (10) (x).
A common mistake is to use derivative(f,x,2) when you want f 0 (2). Of course, f 00 (x) is
very different from f 0 (2).
Help me build this list! Tell me any other common mistakes you come across. Thanks.