Quickstart

This is a short introduction in how to interact with SageMath. Make sure you have installed it according to the installation guide. There are also nice Screencasts for introduction available. Make sure to read more about SageMath in the SageMath Tutorial!

Basics

sage: 1+1
2
sage: V = QQ^3
sage: V.[tab key]
...
V.base_ring
V.basis
V.coordinates
...
SageMath uses the basic user-interface principle of "question and answer" found in many other software systems. You enter an expression and after pressing the Return key in the command line interface or hitting Shift+Return in the notebook interface, SageMath evaluates your expression and returns the answer. – read more
Tab-completion helps you enter commands. On the command-line, it also provides history and reverse lookup search. – read more

Classes of Objects

sage: R = RealIntervalField(100)
sage: R
Real Interval Field with 100 bits of precision
sage: a = R((-1,0)); a
-1.?
sage: b = sin(a); b
-1.?
sage: c = a*b; c.diameter()
0.84147098480789650665250232163
As you can see in the previous example, SageMath knows about mathematical objects embedded into the Python language. Every quantity - a real number, a polynomial, a matrix, and so on - belongs to a parent, and this tells SageMath how to perform operations on the quantity.
In the example on the left, R is defined as the class of all intervals of real values with 100 bits of precision. Then an interval is created and stored in a, the function sin is applied and stored in b. In the end they are multiplied and the diameter is calculated. sin is a function that "knows" about intervals, as well as the multiplication does, and diameter() is a method of an interval instance object.

Interactive Help

sage: c.diameter?
Docstring:
   If 0 is in "self", then return "absolute_diameter()", otherwise
   return "relative_diameter()".

   EXAMPLES:

      sage: RIF(1, 2).diameter()
      0.666666666666667
      ...
The example above shows that there are thousands of functions and methods. SageMath comes with a built-in help system and you do not have to memorize them all. Entering a question mark after a method shows the description and additional information about that method. The example on the left shows the documentation for the diameter method from the previous example.

Symbolic Maths

sage: f = 1 - sin(x)^2
sage: f
-sin(x)^2 + 1
sage: unicode_art(f)  # pretty printing
       2
1 - sin (x)
sage: f.simplify_trig()
cos(x)^2
sage: f(x=pi/2)
0
sage: f(x=pi/3)
1/4
sage: integrate(f, x).simplify_trig()
1/2*sin(x)*cos(x) + 1/2*x
sage: unicode_art(integrate(f, x).simplify_trig())
x   sin(x)⋅cos(x)
─ + ─────────────
2         2
sage: f.differentiate(2).substitute({x: 3/pi})
2*sin(3/pi)^2 - 2*cos(3/pi)^2
sage: unicode_art(f.differentiate(2).substitute({x: 3/pi}))
       2⎛3⎞        2⎛3⎞
- 2⋅cos ⎜─⎟ + 2⋅sin ⎜─⎟
        ⎝π⎠         ⎝π⎠
Here we define a function $$f$$, simplify it, evaluate it at $$\pi/2$$ and $$\pi/3$$ and integrate it with simplification. After that, $$f$$ is differentiated two times and $$x$$ is substituted by $$3/\pi$$.

Numerical Maths

sage: g = sin(x) + (1- x^2)

sage: find_root(g, 0, 2)
1.4096240040025754

sage: var('x y z')
(x, y, z)

sage: f = (1 + (y+x^2)^2 + (1+z+y^2)^2)^2
sage: f

            2     2         2 2     2
     ((z + y  + 1)  + (y + x )  + 1)

sage: minimize(f,[1,2,3],algorithm='powell',verbose=1)
Optimization terminated successfully.
     Current function value: 1.000059
     Iterations: 2
     Function evaluations: 84
(-0.000607497243458, 0.00486816565959, -1.00243223164)
On the left side, a root of the function
$$g(x) = sin(x) + (1-x^2)$$
is found numerically.
Below, the function
$$f(x,y,z) = (1 + (y + x^2)^2 + (1+z+y^2)^2)^2$$
is minimized using the "powell" algorithm.
Tour Research · Tour Graphics