Section B.1 Basics
Basic computations.
Use the Evaluate button below the Sage cell to try it out.xxxxxxxxxx
3+4
xxxxxxxxxx
cos(pi)
Creating and evaluating a function.
xxxxxxxxxx
f(x) = 2*x^2 + 4
f(3)
x
in your expressions, you need to tell Sage about it.
xxxxxxxxxx
var('x,y')
g(x,y) = x^2 + y^2
g(3,-1)
Constants and standard functions.
Sage knows about all the usual constants and functions. Careful, Sage usesI
as the imaginary number.
xxxxxxxxxx
sin(pi)
xxxxxxxxxx
log(e)
xxxxxxxxxx
I^2
Referring to previous results.
You can use an underscore to refer to the previous output, two underscores to refer to the output from two commands ago, and three underscores to refer to the output from three commands ago. (I think that's as far as it goes, but give it a try.) Here, the βpreviousβ output will be from the last cell that computesI^2
above.
xxxxxxxxxx
_ + 1
xxxxxxxxxx
__ + 1
xxxxxxxxxx
___ + 1
Working with objects.
Sage treats everything as a Python object, so you can apply methods to them.xxxxxxxxxx
a = (x + 1)*(x - 3)
a.expand()
x
to the variable a
. If you want confirmation of your input, use the print command. (If you are using a Sage command line installed locally on your computer, you can just type the variable and hit enter without the print
part.)
xxxxxxxxxx
print(a)
xxxxxxxxxx
b = x^2 - 2 * x + 1
b.factor()
xxxxxxxxxx
c = x^2 - 2 * x + x + 1
c.simplify()
Evaluating numerical results to a decimal number.
Use then()
method to evaluate to a decimal number.
xxxxxxxxxx
pi.n()