All published worksheets from http://sagenb.org
Image: ubuntu2004
A tour at SAGEMATH
Get SAGE!
Sage is available at http://www.sagemath.org
Use its online notebook for trying-out!
Disclaimer:
This notes are an introduction to SageMath, and most of details were ommited. These will be carefully filled in during classes.
Getting started
Assignment & Arithmetic
Changing types on-the-fly...
Changing basis...
HEEEEEEEEEELP!!!!!!!!!!
Just add "?" at the end of the command.
Use autocompletion pressing the TAB key.
File: /home/pedro/sage-4.7/devel/sage/sage/rings/integer_ring.pyx
Type: <type ‘builtin_function_or_method’>
Definition: IntegerRing()
Docstring:
Return the integer ring
EXAMPLE:
sage: IntegerRing() Integer Ring sage: ZZ==IntegerRing() True
File: /home/pedro/sage-4.7/local/lib/python2.6/site-packages/sage/rings/arith.py
Type: <type ‘function’>
Definition: factor(n, proof=None, int_=False, algorithm=’pari’, verbose=0, **kwds)
Docstring:
Returns the factorization of n. The result depends on the type of n.
If n is an integer, returns the factorization as an object of type Factorization.
If n is not an integer, n.factor(proof=proof, **kwds) gets called. See n.factor?? for more documentation in this case.
Warning
This means that applying factor to an integer result of a symbolic computation will not factor the integer, because it is considered as an element of a larger symbolic ring.
EXAMPLE:
sage: f(n)=n^2 sage: is_prime(f(3)) False sage: factor(f(3)) 9INPUT:
- n - an nonzero integer
- proof - bool or None (default: None)
- int_ - bool (default: False) whether to return answers as Python ints
- algorithm - string
- 'pari' - (default) use the PARI c library
- 'kash' - use KASH computer algebra system (requires the optional kash package be installed)
- 'magma' - use Magma (requires magma be installed)
- verbose - integer (default: 0); PARI’s debug variable is set to this; e.g., set to 4 or 8 to see lots of output during factorization.
OUTPUT:
- factorization of n
The qsieve and ecm commands give access to highly optimized implementations of algorithms for doing certain integer factorization problems. These implementations are not used by the generic factor command, which currently just calls PARI (note that PARI also implements sieve and ecm algorithms, but they aren’t as optimized). Thus you might consider using them instead for certain numbers.
The factorization returned is an element of the class Factorization; see Factorization?? for more details, and examples below for usage. A Factorization contains both the unit factor (+1 or -1) and a sorted list of (prime, exponent) pairs.
The factorization displays in pretty-print format but it is easy to obtain access to the (prime,exponent) pairs and the unit, to recover the number from its factorization, and even to multiply two factorizations. See examples below.
EXAMPLES:
sage: factor(500) 2^2 * 5^3 sage: factor(-20) -1 * 2^2 * 5 sage: f=factor(-20) sage: list(f) [(2, 2), (5, 1)] sage: f.unit() -1 sage: f.value() -20 sage: factor( -next_prime(10^2) * next_prime(10^7) ) -1 * 101 * 10000019sage: factor(-500, algorithm='kash') # optional - kash -1 * 2^2 * 5^3sage: factor(-500, algorithm='magma') # optional - magma -1 * 2^2 * 5^3sage: factor(0) Traceback (most recent call last): ... ArithmeticError: Prime factorization of 0 not defined. sage: factor(1) 1 sage: factor(-1) -1 sage: factor(2^(2^7)+1) 59649589127497217 * 5704689200685129054721Sage calls PARI’s factor, which has proof False by default. Sage has a global proof flag, set to True by default (see sage.structure.proof.proof, or proof.[tab]). To override the default, call this function with proof=False.
sage: factor(3^89-1, proof=False) 2 * 179 * 1611479891519807 * 5042939439565996049162197sage: factor(2^197 + 1) # long time 3 * 197002597249 * 1348959352853811313 * 251951573867253012259144010843Any object which has a factor method can be factored like this:
sage: K.<i> = QuadraticField(-1) sage: factor(122+454*i) (-1) * (-2*i - 3) * (-i + 4) * (i + 1)^3 * (-i + 2)^3To access the data in a factorization:
sage: f = factor(420); f 2^2 * 3 * 5 * 7 sage: [x for x in f] [(2, 2), (3, 1), (5, 1), (7, 1)] sage: [p for p,e in f] [2, 3, 5, 7] sage: [e for p,e in f] [2, 1, 1, 1] sage: [p^e for p,e in f] [4, 3, 5, 7]
Create functions
Define your own functions.
Solving equations
Drawing and painting
Scaling matters!
The command show(c, aspect_ratio=1) accomplishes the same thing, or you can save the picture using c.save('filename.png',aspect_ratio=1).
3D!
Interact
Rings, fields and vector spaces
Constructing the Galois field.
We have considered the euclidean domain and its irreducible element , which in turn is equivalent to the ideal being maximal.
The quocient is a field and is a cyclic group with order .
Linear algebra
Numbers!
Euclid's algorithm allows to compute the gcd of the integers , n.s.z., and also to solve the linear equation , with .
Set as the set of prime numbers..
Create your own function that show the non primes .
Twin primes...
Interact...
Fermat's little theorem: prime implies , if ., This gives an elementary way for checking if a number is not prime.
This gives an overflow error. We may avoid this by using a fast modular exponential algorithm.
Or by taking, in sagemath, the right algebraic struture.
We may also compute the multicative inverse of an element, in case it exists.
Revisiting Erastosthenes
Classic cripto...
Sagemath has some classic criptosystems...
Elliptic curves
The "standard elliptic curve" has the form $$y^2= x^3 + ax + b,$$
for some fixed and . (This is called a Weierstrass equation).
In characteristic 2 or 3 the form is slightly more complicated.
Diffie-Helmann with EC
Alice and Bob choose an e.c. modulus .
We now have an "authorized" e.c.
Alice and Bob pick a random point in .
Alice picks and Bob picks .
Alice sends to Bob, and Bob sends to Alice. Now both can compute .