The fundamental algebraic operations are addition, subtraction, multiplication, division, and perhaps we should include exponentiation and square root (though of course the square root can be expressed using an exponent). You have long practice with writing expressions using these operators, and are presumably aware of the usual precedence of the operators, sometimes referred to as the order of operations. In brief: exponentiation is done first, then multiplication and division, then addition and subtraction. Mixed multiplication and division are done left to right, and likewise for addition and subtraction. Parentheses can be used to change the order, and at times it is wise to use extra parentheses for clarity.
Possibly you have seen a somewhat careless use of these rules at times. In particular, there is sometimes a bit of confusion about the meaning of, for example, $1/2x$. You might be inclined to read this as $1\over 2x$, but this is incorrect. Since division and multiplication have the same precedence, they are done left to right, so first we compute $1/2$ and then multiply by $x$. With parentheses for clarity, this is $1/2x=(1/2)x={x\over 2}$. If you really mean $1\over 2x$, you should write $1/(2x)$.
You will also be familiar with implied multiplication, as used already; for example, $2x$ means $2\cdot x$ (it is a good idea not to use the times symbol, $\times$, as it can be confused occasionally with $x$). In Sage it is not permissible to use implied multiplication; the actual symbol must be used.
Expressions in Sage must all be typed on a line, meaning the dot for
multiplication is not available (since it does not appear on standard
keyboards), and exponents and square roots must be typed using symbols
you may not be used to. We use *
for multiplication, ^
for
exponentiation, and sqrt
for the square root. So for example,
$x^{2a}$ is x^(2*a)
; the parentheses are required, as x^2*a
means $x^2a$, since the exponentiation takes precedence over the
multiplication. As a more complicated example, here is how we could
express $3^2+{2+4\over 7-5}-\sqrt{9}$:
Click on the "Evaluate'' button to see the result. You may edit the
expression to try some other expressions. For example, try
${2\cdot 6\over 3}+2^{3-4}$ which shoud evaluate to $9/2$. You might
also try some expressions using the variable $x$, since it might be
easier to see a problem—try typing 1/2*x
, for example.
You will need to deal with functions in order to do any calculus. Functions can be defined as shown in the next Sage cell; once a function is defined, it can be called as you would expect.
Notice that when we display f
, as opposed to f(x)
, we don't
simply get the expression that we would normally call $f$; the form
$x\mapsto x^3+4x^2-2x+5$ indicates that f
is a function that
takes an input and returns an output; it is more than just a formula.
Sage knows how to plot functions. Here is (a portion of) the previous function:
Sage knows about many common functions, such as the trigonometric
functions and the logarithm function. The trigonometric functions are
as you would expect, sin(x)
, cos(x)
, and so on; log(x)
is the natural logarithm, usually expressed in print as
$\ln(x)$. Instead of writing a complicated exponential as, for
example, e^(x^2-x+2)
, you may also write exp(x^2-x+2)
.
Sage may not automatically write an expression in the form
you might expect; two useful ways to get a rewrite are the expand
and simplify
functions.
Sometimes you may want to approximate a complicated numerical
expression as a decimal number. Use the function N
:
As mentioned above, you can use the variable $x$ in expressions. You can use any other letters or combinations of letters, but you generally must tell Sage that it is a variable. (As in the previous example, you do not need to do this when the variable appears on the left side of an equation assigning a value to the variable.)
You will sometimes need to solve an equation. Of course, not all equations can be solved exactly. When they can, Sage can often find the solutions. Note the use of the double equal sign to indicate that this is an equation, not an assignment of one value to another.
When a solution can't be found exactly you may want an approximate solution. Here you need to help Sage out a little bit by giving it an interval on which to look; you may be able to find the interval by graphing a function. Graphing $\sin(x)-\cos(x)$ shows that there is a solution in $[0,1]$ (there are in all an infinite number of solutions, since sine and cosine are periodic). Here we define a variable to be the entire equation, which makes it easy to refer to the equation more than once without typing the whole equation multiple times.
Note that solve
was unable to find a solution and so simply
displayed the equation.