Quickstart

This is a short introduction in how to interact with Sage. 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 Sage in the Sage Tutorial!

Basics

sage: 1+1
2
sage: V = QQ^3
sage: V.[tab key]
...
V.base_ring
V.basis
V.coordinates
...
Sage 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, Sage 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 ..
           0.00000000000000000000000000000000]
sage: b = sin(a); b
[-0.84147098480789650665250232163084 ..
           0.00000000000000000000000000000000]
sage: c = a*b; c.diameter()
0.84147098480789650665250232163
As you can see in the previous example, Sage 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 Sage 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
Docstring:
  If (0 in self), returns self.absolute_diameter(),
  otherwise self.relative_diameter().
  EXAMPLES:
    sage: RIF(1, 2).diameter()
    0.666666666666667
    ...
The example above shows that there are thousands of functions and methods. Sage 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
                                         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: 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)
1.4096240040025754

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