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!


sage: 1+1
sage: V = QQ^3
sage: V.[tab key]
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.0000000000000000000000000000000 ..
sage: b = sin(a); b
[-0.84147098480789650665250232163084 ..
sage: c = a*b; c.diameter()
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?
Type:        builtin_function_or_method
Base Class:  <type 'builtin_function_or_method'>
String Form: <built-in method diameter ...>
Namespace:   Interactive
  If (0 in self), returns self.absolute_diameter(),
  otherwise self.relative_diameter().
    sage: RIF(1, 2).diameter()
The example above shows that there are thousands of functions and methods. SageMath comes with a built-in help system and you don't 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: print(f)
-sin(x)^2 + 1
sage: print(maxima(f))  # pretty printing
                                  1 - sin (x)
sage: f.simplify_trig()
sage: f(x=pi/2)
sage: f(x=pi/3)
sage: integrate(f, x).simplify_trig()
1/2*sin(x)*cos(x) + 1/2*x
sage: print(maxima(integrate(f, x).simplify_trig()))
                               cos(x) sin(x)   x
                               ------------- + -
                                     2         2
sage: f.differentiate(2).substitute({x: 3/pi})
2*sin(3/pi)^2 - 2*cos(3/pi)^2
sage: print(maxima(f.differentiate(2).substitute({x: 3/pi})))
                                2  3          2  3
                           2 sin (---) - 2 cos (---)
                                  %pi           %pi
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)

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

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

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

sage: minimize(f,[1,2,3],disp=1,algorithm='powell')
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