Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for Blaec (CSO) Tasks and Strategy Outline.
Download

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.

https://creativecommons.org/licenses/by-sa/3.0/

386 views
ubuntu2204
Kernel: SageMath 10.4

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:

tan?
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 tan(π)\tan(\pi):

tan(pi)
0

Calculate tan(3.1415)\tan(3.1415):

tan(3.1415)
-0.0000926535900581913

Calculate tan(3.1415/4)\tan(3.1415/4):

tan(3.1415/4)
0.999953674278156

Calculate tan(π/4)\tan(\pi/4):

tan(pi/4)
1

Evaluate tan(12)\tan(\frac{1}{2}) as an element in the Real Field with infinite precision:

tan(1/2)
tan(1/2)

Convert tan(12)\tan(\frac{1}{2}) to a real number with fixed floating-point precision:

RR(tan(1/2))
0.546302489843790

Query documentation for log2\log_2:

log2?
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 log2\log_2:

log2
log2

Convert log2\log_2 to a floating-point number:

float(log2)
0.6931471805599453

Convert log2\log_2 to a real number with fixed floating-point precision:

RR(log2)
0.693147180559945

Create a real field with 200 bits of precision and convert log2\log_2 to it:

R = RealField(200); R R(log2)
0.69314718055994530941723212145817656807550013436025525412068

Define and evaluate an expression involving log2\log_2:

l = (1 - log2) / (1 + log2); l R(l)
0.18123221829928249948761381864650311423330609774776013488056

Use Maxima to convert log2\log_2:

maxima(log2) maxima(log2).float()
0.6931471805599453

Use GP (PARI/GP) to convert log2\log_2:

gp(log2)
0.69314718055994530941723212145817656807

Query documentation for the sudoku function:

sudoku?
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:

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 ]) 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]

Solve the Sudoku puzzle:

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]