Tutorial Release 10.4 The Sage Development Team https://doc.sagemath.org/pdf/en/tutorial/sage_tutorial.pdf
Sage is free, open-source math software that supports research and teaching in algebra, geometry, number theory, cryptography, numerical computation, and related areas. Both the Sage development model and the technology in Sage itself are distinguished by an extremely strong emphasis on openness, community, cooperation, and collaboration: we are building the car, not reinventing the wheel. The overall goal of Sage is to create a viable, free, open-source alternative to Maple, Mathematica, Magma, and MATLAB.
This tutorial is the best way to become familiar with Sage in only a few hours. You can read it in HTML or PDF versions, or from the Sage notebook (click Help, then click Tutorial to interactively work through the tutorial from within Sage).
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
ubuntu2204
2.2 Getting Help
Sage has extensive built-in documentation, accessible by typing the name of a function or a constant (for example), followed by a question mark:
Type: Function_tan
String form: tan
File: /ext/sage/10.4/src/sage/functions/trig.py
Docstring:
The tangent function.
EXAMPLES:
sage: tan(3.1415)
-0.0000926535900581913
sage: tan(3.1415/4)
0.999953674278156
sage: tan(pi)
0
sage: tan(pi/4)
1
sage: tan(1/2)
tan(1/2)
sage: RR(tan(1/2))
0.546302489843790
We can prevent evaluation using the "hold" parameter:
sage: tan(pi/4, hold=True)
tan(1/4*pi)
To then evaluate again, we currently must use Maxima via
"sage.symbolic.expression.Expression.simplify()":
sage: a = tan(pi/4, hold=True); a.simplify()
1
If possible, the argument is also reduced modulo the period length
\pi, and well-known identities are directly evaluated:
sage: k = var('k', domain='integer')
sage: tan(1 + 2*k*pi)
tan(1)
sage: tan(k*pi)
0
Init docstring:
The tangent function.
EXAMPLES:
sage: tan(3.1415)
-0.0000926535900581913
sage: tan(3.1415/4)
0.999953674278156
sage: tan(pi)
0
sage: tan(pi/4)
1
sage: tan(1/2)
tan(1/2)
sage: RR(tan(1/2))
0.546302489843790
We can prevent evaluation using the "hold" parameter:
sage: tan(pi/4, hold=True)
tan(1/4*pi)
To then evaluate again, we currently must use Maxima via
"sage.symbolic.expression.Expression.simplify()":
sage: a = tan(pi/4, hold=True); a.simplify()
1
If possible, the argument is also reduced modulo the period length
\pi, and well-known identities are directly evaluated:
sage: k = var('k', domain='integer')
sage: tan(1 + 2*k*pi)
tan(1)
sage: tan(k*pi)
0
Call docstring:
Evaluate this function on the given arguments and return the
result.
EXAMPLES:
sage: exp(5)
e^5
sage: gamma(15)
87178291200
Python float, Python complex, mpmath mpf and mpc as well as numpy
inputs are sent to the relevant "math", "cmath", "mpmath" or
"numpy" function:
sage: cos(1.r)
0.5403023058681398
sage: assert type(_) is float
sage: gamma(4.r)
6.0
sage: assert type(_) is float
sage: cos(1jr) # abstol 1e-15
(1.5430806348152437-0j)
sage: assert type(_) is complex
sage: import mpmath
sage: cos(mpmath.mpf('1.321412'))
mpf('0.24680737898640387')
sage: cos(mpmath.mpc(1,1))
mpc(real='0.83373002513114902', imag='-0.98889770576286506')
sage: import numpy
sage: sin(numpy.int32(0))
0.0
sage: type(_)
<class 'numpy.float64'>
EXAMPLES:
Calculate :
Calculate :
Calculate :
Calculate :
Evaluate as an element in the Real Field with infinite precision:
Convert to a real number with fixed floating-point precision:
Query documentation for :
Type: Expression
String form: log2
File: /ext/sage/10.4/src/sage/symbolic/expression.pyx
Docstring:
Nearly all expressions are created by calling
new_Expression_from_*, but we need to make sure this at least does
not leave self._gobj uninitialized and segfault.
Init docstring:
Nearly all expressions are created by calling
new_Expression_from_*, but we need to make sure this at least does
not leave self._gobj uninitialized and segfault.
Call docstring:
Call the "subs()" on this expression.
EXAMPLES:
sage: var('x,y,z')
(x, y, z)
sage: (x+y)(x=z^2, y=x^y)
z^2 + x^y
EXAMPLES:
Retrieve the value of :
Convert to a floating-point number:
Convert to a real number with fixed floating-point precision:
Create a real field with 200 bits of precision and convert to it:
Define and evaluate an expression involving :
Use Maxima to convert :
Use GP (PARI/GP) to convert :
Query documentation for the sudoku
function:
Signature: sudoku(m)
Docstring:
Solves Sudoku puzzles described by matrices.
INPUT:
* "m" -- a square Sage matrix over \ZZ, where zeros are blank
entries
OUTPUT:
A Sage matrix over \ZZ containing the first solution found,
otherwise "None".
This function matches the behavior of the prior Sudoku solver and
is included only to replicate that behavior. It could be safely
deprecated, since all of its functionality is included in the
"Sudoku" class.
EXAMPLES:
An example that was used in previous doctests.
sage: A = matrix(ZZ,9,[5,0,0, 0,8,0, 0,4,9, 0,0,0, 5,0,0, 0,3,0, 0,6,7, 3,0,0, 0,0,1, 1,5,0, 0,0,0, 0,0,0, 0,0,0, 2,0,8, 0,0,0, 0,0,0, 0,0,0, 0,1,8, 7,0,0, 0,0,4, 1,5,0, 0,3,0, 0,0,2, 0,0,0, 4,9,0, 0,5,0, 0,0,3])
sage: A
[5 0 0 0 8 0 0 4 9]
[0 0 0 5 0 0 0 3 0]
[0 6 7 3 0 0 0 0 1]
[1 5 0 0 0 0 0 0 0]
[0 0 0 2 0 8 0 0 0]
[0 0 0 0 0 0 0 1 8]
[7 0 0 0 0 4 1 5 0]
[0 3 0 0 0 2 0 0 0]
[4 9 0 0 5 0 0 0 3]
sage: sudoku(A)
[5 1 3 6 8 7 2 4 9]
[8 4 9 5 2 1 6 3 7]
[2 6 7 3 4 9 5 8 1]
[1 5 8 4 6 3 9 7 2]
[9 7 4 2 1 8 3 6 5]
[3 2 6 7 9 5 4 1 8]
[7 8 2 9 3 4 1 5 6]
[6 3 5 1 7 2 8 9 4]
[4 9 1 8 5 6 7 2 3]
Using inputs that are possible with the "Sudoku" class, other than
a matrix, will cause an error.
sage: sudoku('.4..32....14..3.')
Traceback (most recent call last):
...
ValueError: sudoku function expects puzzle to be a matrix, perhaps use the Sudoku class
Init docstring: Initialize self. See help(type(self)) for accurate signature.
File: /ext/sage/10.4/src/sage/games/sudoku.py
Type: function
EXAMPLE:
Create a 9x9 Sudoku puzzle matrix and display it:
Solve the Sudoku puzzle: