Intro to Sage

159 days ago by roed

Sage: Open Source Mathematics Software

David Roe

Worksheet inspired by William Stein's FoCM 2011 Plenary talk: http://sagenb.org/home/pub/2909/

3 + 2 + 1 
       
6
6
 
       
 
       
 
       
factorial(30) 
       
265252859812191058636308480000000
265252859812191058636308480000000

The goal of Sage is to create a viable open source alternative to Magma, Maple, Mathematica and Matlab.

Sage is:

  • Open source.  If you want to understand the implementation of a function, it's easy to look at the source that defines it.
  • Community-owned.  The development of Sage is public, with an open mailing list, bug-tracker and frequent workshops driving development.  It's easy to get involved.
  • Well documented.  86.1% of functions in Sage are documented with a description and examples.  These examples are regularly tested, and the documentation is collected into a reference manual, tutorial and thematic tutorials (e.g. Group Theory and Sage).
  • Expansive.  Sage includes 95 open source packages, including Pari, GAP, Singular, NumPy, Maxima, matplotlib and R.  It also includes half a million lines of new code, as well as interfaces to closed source software such as Magma and Mathematica.
  • Easy to Get Started.  You can try Sage online, and soon we will have our own Sage server.  If you want your own copy, building from source is as easy as typing "make"; there are also binaries available for many popular operating systems and a virtual machine install for Windows.
  • Fast.  Obviously not everything is as fast as possible.  But Sage's speed is competitive, and it includes Cython as an easy way to write compiled code.
 
       
 
       
 
       

Why not Magma, Maple, Matlab or Mathematica?

Sage's competitors obviously provide some outstanding strengths.  What drives Sage's developers to compete with them?

  1. Commercial.  They are expensive for our collaborators and students, and not controlled by the mathematical community.
  2. Closed.  The implementation of algorithms is often secret, impeding the advancement of computational mathematics.
  3. Frustrating.  The tight control of development makes it difficult for outsiders to add features.
  4. Copy protection.  Difficult to run on a supercomputer or my new laptop.
  5. Programming language.  All use their own special math-only language.
  6. Bugs.  Bug tracking is (mostly) done in secret.
  7. Compiler.  You (mostly) can't compile your programs written in their special-purpose languages.
 
       
 
       
 
       

Ways to use Sage

  • The Sage notebook.  A computer (yours or some server) runs Sage and a webserver.  You interact with Sage through the browser of your choice.
  • The command line.  Sage provides an interactive command line interface using IPython.
  • Write a script.  You can write a script which you can either execute on its own or attach to an interactive session.
 
       

Sage's Documentation

Ellip 
       
EllipticCurve 
       

File: /sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/schemes/elliptic_curves/constructor.py

Type: <type ‘function’>

Definition: EllipticCurve(x=None, y=None, j=None)

Docstring:

There are several ways to construct an elliptic curve:

y^2 + a_1 xy + a_3 y = x^3 + a_2 x^2 + a_4 x + a_6.
  • EllipticCurve([a1,a2,a3,a4,a6]): Elliptic curve with given a-invariants. The invariants are coerced into the parent of the first element. If all are integers, they are coerced into the rational numbers.
  • EllipticCurve([a4,a6]): Same as above, but a1=a2=a3=0.
  • EllipticCurve(label): Returns the elliptic curve over Q from the Cremona database with the given label. The label is a string, such as “11a” or “37b2”. The letters in the label must be lower case (Cremona’s new labeling).
  • EllipticCurve(R, [a1,a2,a3,a4,a6]): Create the elliptic curve over R with given a-invariants. Here R can be an arbitrary ring. Note that addition need not be defined.
  • EllipticCurve(j): Return an elliptic curve with j-invariant j. Warning: this is deprecated. Use EllipticCurve_from_j(j) or EllipticCurve(j=j) instead.

EXAMPLES: We illustrate creating elliptic curves.

sage: EllipticCurve([0,0,1,-1,0])
Elliptic Curve defined by y^2 + y = x^3 - x over Rational Field

We create a curve from a Cremona label:

sage: EllipticCurve('37b2')
Elliptic Curve defined by y^2 + y = x^3 + x^2 - 1873*x - 31833 over Rational Field
sage: EllipticCurve('5077a')
Elliptic Curve defined by y^2 + y = x^3 - 7*x + 6 over Rational Field
sage: EllipticCurve('389a')
Elliptic Curve defined by y^2 + y = x^3 + x^2 - 2*x over Rational Field

Unicode labels are allowed:

sage: EllipticCurve(u'389a')
Elliptic Curve defined by y^2 + y = x^3 + x^2 - 2*x over Rational Field

We create curves over a finite field as follows:

sage: EllipticCurve([GF(5)(0),0,1,-1,0])
Elliptic Curve defined by y^2 + y = x^3 + 4*x over Finite Field of size 5
sage: EllipticCurve(GF(5), [0, 0,1,-1,0])
Elliptic Curve defined by y^2 + y = x^3 + 4*x over Finite Field of size 5

Elliptic curves over \ZZ/N\ZZ with N prime are of type “elliptic curve over a finite field”:

sage: F = Zmod(101)
sage: EllipticCurve(F, [2, 3])
Elliptic Curve defined by y^2 = x^3 + 2*x + 3 over Ring of integers modulo 101
sage: E = EllipticCurve([F(2), F(3)])
sage: type(E)
<class 'sage.schemes.elliptic_curves.ell_finite_field.EllipticCurve_finite_field'>

In contrast, elliptic curves over \ZZ/N\ZZ with N composite are of type “generic elliptic curve”:

sage: F = Zmod(95)
sage: EllipticCurve(F, [2, 3])
Elliptic Curve defined by y^2 = x^3 + 2*x + 3 over Ring of integers modulo 95
sage: E = EllipticCurve([F(2), F(3)])
sage: type(E)
<class 'sage.schemes.elliptic_curves.ell_generic.EllipticCurve_generic'>

The following is a curve over the complex numbers:

sage: E = EllipticCurve(CC, [0,0,1,-1,0])
sage: E
Elliptic Curve defined by y^2 + 1.00000000000000*y = x^3 + (-1.00000000000000)*x over Complex Field with 53 bits of precision
sage: E.j_invariant()
2988.97297297297

We can also create elliptic curves by giving the Weierstrass equation:

sage: x, y = var('x,y')
sage: EllipticCurve(y^2 + y ==  x^3 + x - 9)
Elliptic Curve defined by y^2 + y = x^3 + x - 9 over Rational Field

sage: R.<x,y> = GF(5)[]
sage: EllipticCurve(x^3 + x^2 + 2 - y^2 - y*x)
Elliptic Curve defined by y^2 + x*y  = x^3 + x^2 + 2 over Finite Field of size 5

We can explicitly specify the j-invariant:

sage: E = EllipticCurve(j=1728); E; E.j_invariant(); E.label()
Elliptic Curve defined by y^2 = x^3 - x over Rational Field
1728
'32a2'

sage: E = EllipticCurve(j=GF(5)(2)); E; E.j_invariant()
Elliptic Curve defined by y^2 = x^3 + x + 1 over Finite Field of size 5
2

See trac #6657:

sage: EllipticCurve(GF(144169),j=1728)
Elliptic Curve defined by y^2 = x^3 + x over Finite Field of size 144169

TESTS:

sage: R = ZZ['u', 'v']
sage: EllipticCurve(R, [1,1])
Elliptic Curve defined by y^2 = x^3 + x + 1 over Multivariate Polynomial Ring in u, v
over Integer Ring

We create a curve and a point over QQbar (see #6879):

sage: E = EllipticCurve(QQbar,[0,1])
sage: E(0)
(0 : 1 : 0)
sage: E.base_field()
Algebraic Field

sage: E = EllipticCurve(RR,[1,2]); E; E.base_field()
Elliptic Curve defined by y^2 = x^3 + 1.00000000000000*x + 2.00000000000000 over Real Field with 53 bits of precision
Real Field with 53 bits of precision
sage: EllipticCurve(CC,[3,4]); E; E.base_field()
Elliptic Curve defined by y^2 = x^3 + 3.00000000000000*x + 4.00000000000000 over Complex Field with 53 bits of precision
Elliptic Curve defined by y^2 = x^3 + 1.00000000000000*x + 2.00000000000000 over Real Field with 53 bits of precision
Real Field with 53 bits of precision
sage: E = EllipticCurve(QQbar,[5,6]); E; E.base_field()
Elliptic Curve defined by y^2 = x^3 + 5*x + 6 over Algebraic Field
Algebraic Field

See trac #6657:

sage: EllipticCurve(3,j=1728)
Traceback (click to the left of this block for traceback)
...
                                
                            

File: /sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/schemes/elliptic_curves/constructor.py

Type: <type ‘function’>

Definition: EllipticCurve(x=None, y=None, j=None)

Docstring:

There are several ways to construct an elliptic curve:

y^2 + a_1 xy + a_3 y = x^3 + a_2 x^2 + a_4 x + a_6.
  • EllipticCurve([a1,a2,a3,a4,a6]): Elliptic curve with given a-invariants. The invariants are coerced into the parent of the first element. If all are integers, they are coerced into the rational numbers.
  • EllipticCurve([a4,a6]): Same as above, but a1=a2=a3=0.
  • EllipticCurve(label): Returns the elliptic curve over Q from the Cremona database with the given label. The label is a string, such as “11a” or “37b2”. The letters in the label must be lower case (Cremona’s new labeling).
  • EllipticCurve(R, [a1,a2,a3,a4,a6]): Create the elliptic curve over R with given a-invariants. Here R can be an arbitrary ring. Note that addition need not be defined.
  • EllipticCurve(j): Return an elliptic curve with j-invariant j. Warning: this is deprecated. Use EllipticCurve_from_j(j) or EllipticCurve(j=j) instead.

EXAMPLES: We illustrate creating elliptic curves.

sage: EllipticCurve([0,0,1,-1,0])
Elliptic Curve defined by y^2 + y = x^3 - x over Rational Field

We create a curve from a Cremona label:

sage: EllipticCurve('37b2')
Elliptic Curve defined by y^2 + y = x^3 + x^2 - 1873*x - 31833 over Rational Field
sage: EllipticCurve('5077a')
Elliptic Curve defined by y^2 + y = x^3 - 7*x + 6 over Rational Field
sage: EllipticCurve('389a')
Elliptic Curve defined by y^2 + y = x^3 + x^2 - 2*x over Rational Field

Unicode labels are allowed:

sage: EllipticCurve(u'389a')
Elliptic Curve defined by y^2 + y = x^3 + x^2 - 2*x over Rational Field

We create curves over a finite field as follows:

sage: EllipticCurve([GF(5)(0),0,1,-1,0])
Elliptic Curve defined by y^2 + y = x^3 + 4*x over Finite Field of size 5
sage: EllipticCurve(GF(5), [0, 0,1,-1,0])
Elliptic Curve defined by y^2 + y = x^3 + 4*x over Finite Field of size 5

Elliptic curves over \ZZ/N\ZZ with N prime are of type “elliptic curve over a finite field”:

sage: F = Zmod(101)
sage: EllipticCurve(F, [2, 3])
Elliptic Curve defined by y^2 = x^3 + 2*x + 3 over Ring of integers modulo 101
sage: E = EllipticCurve([F(2), F(3)])
sage: type(E)
<class 'sage.schemes.elliptic_curves.ell_finite_field.EllipticCurve_finite_field'>

In contrast, elliptic curves over \ZZ/N\ZZ with N composite are of type “generic elliptic curve”:

sage: F = Zmod(95)
sage: EllipticCurve(F, [2, 3])
Elliptic Curve defined by y^2 = x^3 + 2*x + 3 over Ring of integers modulo 95
sage: E = EllipticCurve([F(2), F(3)])
sage: type(E)
<class 'sage.schemes.elliptic_curves.ell_generic.EllipticCurve_generic'>

The following is a curve over the complex numbers:

sage: E = EllipticCurve(CC, [0,0,1,-1,0])
sage: E
Elliptic Curve defined by y^2 + 1.00000000000000*y = x^3 + (-1.00000000000000)*x over Complex Field with 53 bits of precision
sage: E.j_invariant()
2988.97297297297

We can also create elliptic curves by giving the Weierstrass equation:

sage: x, y = var('x,y')
sage: EllipticCurve(y^2 + y ==  x^3 + x - 9)
Elliptic Curve defined by y^2 + y = x^3 + x - 9 over Rational Field

sage: R.<x,y> = GF(5)[]
sage: EllipticCurve(x^3 + x^2 + 2 - y^2 - y*x)
Elliptic Curve defined by y^2 + x*y  = x^3 + x^2 + 2 over Finite Field of size 5

We can explicitly specify the j-invariant:

sage: E = EllipticCurve(j=1728); E; E.j_invariant(); E.label()
Elliptic Curve defined by y^2 = x^3 - x over Rational Field
1728
'32a2'

sage: E = EllipticCurve(j=GF(5)(2)); E; E.j_invariant()
Elliptic Curve defined by y^2 = x^3 + x + 1 over Finite Field of size 5
2

See trac #6657:

sage: EllipticCurve(GF(144169),j=1728)
Elliptic Curve defined by y^2 = x^3 + x over Finite Field of size 144169

TESTS:

sage: R = ZZ['u', 'v']
sage: EllipticCurve(R, [1,1])
Elliptic Curve defined by y^2 = x^3 + x + 1 over Multivariate Polynomial Ring in u, v
over Integer Ring

We create a curve and a point over QQbar (see #6879):

sage: E = EllipticCurve(QQbar,[0,1])
sage: E(0)
(0 : 1 : 0)
sage: E.base_field()
Algebraic Field

sage: E = EllipticCurve(RR,[1,2]); E; E.base_field()
Elliptic Curve defined by y^2 = x^3 + 1.00000000000000*x + 2.00000000000000 over Real Field with 53 bits of precision
Real Field with 53 bits of precision
sage: EllipticCurve(CC,[3,4]); E; E.base_field()
Elliptic Curve defined by y^2 = x^3 + 3.00000000000000*x + 4.00000000000000 over Complex Field with 53 bits of precision
Elliptic Curve defined by y^2 = x^3 + 1.00000000000000*x + 2.00000000000000 over Real Field with 53 bits of precision
Real Field with 53 bits of precision
sage: E = EllipticCurve(QQbar,[5,6]); E; E.base_field()
Elliptic Curve defined by y^2 = x^3 + 5*x + 6 over Algebraic Field
Algebraic Field

See trac #6657:

sage: EllipticCurve(3,j=1728)
Traceback (most recent call last):
...
ValueError: First parameter (if present) must be a ring when j is specified

sage: EllipticCurve(GF(5),j=3/5)
Traceback (most recent call last):
...
ValueError: First parameter must be a ring containing 3/5

If the universe of the coefficients is a general field, the object constructed has type EllipticCurve_field. Otherwise it is EllipticCurve_generic. See trac #9816:

sage: E = EllipticCurve([QQbar(1),3]); E
Elliptic Curve defined by y^2 = x^3 + x + 3 over Algebraic Field
sage: type(E)
<class 'sage.schemes.elliptic_curves.ell_field.EllipticCurve_field'>

sage: E = EllipticCurve([RR(1),3]); E
Elliptic Curve defined by y^2 = x^3 + 1.00000000000000*x + 3.00000000000000 over Real Field with 53 bits of precision
sage: type(E)
<class 'sage.schemes.elliptic_curves.ell_field.EllipticCurve_field'>

sage: E = EllipticCurve([i,i]); E
Elliptic Curve defined by y^2 = x^3 + I*x + I over Symbolic Ring
sage: type(E)
<class 'sage.schemes.elliptic_curves.ell_field.EllipticCurve_field'>
sage: is_field(SR)
True

sage: F = FractionField(PolynomialRing(QQ,'t'))
sage: t = F.gen()
sage: E = EllipticCurve([t,0]); E
Elliptic Curve defined by y^2 = x^3 + t*x over Fraction Field of Univariate Polynomial Ring in t over Rational Field
sage: type(E)
<class 'sage.schemes.elliptic_curves.ell_field.EllipticCurve_field'>
R.<x> = PolynomialRing(ZZ) 
       
R.completion? 
       

File: /sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/rings/polynomial/polynomial_ring.py

Type: <type ‘instancemethod’>

Definition: R.completion(p, prec=20, extras=None)

Docstring:

Return the completion of self with respect to the irreducible polynomial p. Currently only implemented for p=R.gen(), i.e. you can only complete R[x] with respect to x, the result being a ring of power series in x. The prec variable controls the precision used in the power series ring.

EXAMPLES:

sage: P.<x>=PolynomialRing(QQ)
sage: P
Univariate Polynomial Ring in x over Rational Field
sage: PP=P.completion(x)
sage: PP
Power Series Ring in x over Rational Field
sage: f=1-x
sage: PP(f)
1 - x
sage: 1/f
1/(-x + 1)
sage: 1/PP(f)
1 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8 + x^9 + x^10 + x^11 + x^12 + x^13 + x^14 + x^15 + x^16 + x^17 + x^18 + x^19 + O(x^20)

File: /sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/rings/polynomial/polynomial_ring.py

Type: <type ‘instancemethod’>

Definition: R.completion(p, prec=20, extras=None)

Docstring:

Return the completion of self with respect to the irreducible polynomial p. Currently only implemented for p=R.gen(), i.e. you can only complete R[x] with respect to x, the result being a ring of power series in x. The prec variable controls the precision used in the power series ring.

EXAMPLES:

sage: P.<x>=PolynomialRing(QQ)
sage: P
Univariate Polynomial Ring in x over Rational Field
sage: PP=P.completion(x)
sage: PP
Power Series Ring in x over Rational Field
sage: f=1-x
sage: PP(f)
1 - x
sage: 1/f
1/(-x + 1)
sage: 1/PP(f)
1 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8 + x^9 + x^10 + x^11 + x^12 + x^13 + x^14 + x^15 + x^16 + x^17 + x^18 + x^19 + O(x^20)
R.cyclotomic_polynomial?? 
       

File: /sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/rings/polynomial/polynomial_ring.py

Source Code (starting at line 867):

def cyclotomic_polynomial(self, n):
    """
    Return the nth cyclotomic polynomial as a polynomial in this
    polynomial ring. For details of the implementation, see the
    documentation for
    :func:`sage.rings.polynomial.cyclotomic.cyclotomic_coeffs`.

    EXAMPLES::

        sage: R = ZZ['x']
        sage: R.cyclotomic_polynomial(8)
        x^4 + 1
        sage: R.cyclotomic_polynomial(12)
        x^4 - x^2 + 1
        sage: S = PolynomialRing(FiniteField(7), 'x')
        sage: S.cyclotomic_polynomial(12)
        x^4 + 6*x^2 + 1
        sage: S.cyclotomic_polynomial(1)
        x + 6

    TESTS:

    Make sure it agrees with other systems for the trivial case::

        sage: ZZ['x'].cyclotomic_polynomial(1)
        x - 1
        sage: gp('polcyclo(1)')
        x - 1
    """
    if n <= 0:
        raise ArithmeticError, "n=%s must be positive"%n
    elif n == 1:
        return self.gen() - 1
    else:
        return self(cyclotomic.cyclotomic_coeffs(n), check=True)

File: /sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/rings/polynomial/polynomial_ring.py

Source Code (starting at line 867):

def cyclotomic_polynomial(self, n):
    """
    Return the nth cyclotomic polynomial as a polynomial in this
    polynomial ring. For details of the implementation, see the
    documentation for
    :func:`sage.rings.polynomial.cyclotomic.cyclotomic_coeffs`.

    EXAMPLES::

        sage: R = ZZ['x']
        sage: R.cyclotomic_polynomial(8)
        x^4 + 1
        sage: R.cyclotomic_polynomial(12)
        x^4 - x^2 + 1
        sage: S = PolynomialRing(FiniteField(7), 'x')
        sage: S.cyclotomic_polynomial(12)
        x^4 + 6*x^2 + 1
        sage: S.cyclotomic_polynomial(1)
        x + 6

    TESTS:

    Make sure it agrees with other systems for the trivial case::

        sage: ZZ['x'].cyclotomic_polynomial(1)
        x - 1
        sage: gp('polcyclo(1)')
        x - 1
    """
    if n <= 0:
        raise ArithmeticError, "n=%s must be positive"%n
    elif n == 1:
        return self.gen() - 1
    else:
        return self(cyclotomic.cyclotomic_coeffs(n), check=True)
sage.rings.polynomial.cyclotomic.cyclotomic_coeffs?? 
       

Traceback (click to the left of this block for traceback)
...

Traceback (most recent call last):
  File "", line 1, in 
  File "_sage_input_13.py", line 10, in 
    exec compile(u'print _support_.source_code("", globals(), system="sage")
  File "", line 1, in 
    
  File "/sagenb/sage_install/sage-4.7.2/devel/sagenb-flask/sagenb/misc/support.py", line 320, in source_code
    obj = eval(s, globs)
  File "", line 0
    
    ^
SyntaxError: unexpected EOF while parsing

Question Break 1 (of 2)

???

magma_free? 
       

File: /sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/interfaces/magma_free.py

Type: <type 'instance'>

Definition: magma_free(code, strip=True, columns=0)

Docstring:


    Evaluate MAGMA code without requiring that MAGMA be installed
    on your computer by using the free online MAGMA calculator.

    EXAMPLES:
        sage: magma_free("Factorization(9290348092384)")  # optional - internet
        [ <2, 5>, <290323377887, 1> ]

File: /sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/interfaces/magma_free.py

Type: <type 'instance'>

Definition: magma_free(code, strip=True, columns=0)

Docstring:


    Evaluate MAGMA code without requiring that MAGMA be installed
    on your computer by using the free online MAGMA calculator.

    EXAMPLES:
        sage: magma_free("Factorization(9290348092384)")  # optional - internet
        [ <2, 5>, <290323377887, 1> ]
 
       
 
       
 
       

File: /sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/rings/polynomial/polynomial_ring.py

Source Code (starting at line 867):

def cyclotomic_polynomial(self, n):
    """
    Return the nth cyclotomic polynomial as a polynomial in this
    polynomial ring. For details of the implementation, see the
    documentation for
    :func:`sage.rings.polynomial.cyclotomic.cyclotomic_coeffs`.

    EXAMPLES::

        sage: R = ZZ['x']
        sage: R.cyclotomic_polynomial(8)
        x^4 + 1
        sage: R.cyclotomic_polynomial(12)
        x^4 - x^2 + 1
        sage: S = PolynomialRing(FiniteField(7), 'x')
        sage: S.cyclotomic_polynomial(12)
        x^4 + 6*x^2 + 1
        sage: S.cyclotomic_polynomial(1)
        x + 6

    TESTS:

    Make sure it agrees with other systems for the trivial case::

        sage: ZZ['x'].cyclotomic_polynomial(1)
        x - 1
        sage: gp('polcyclo(1)')
        x - 1
    """
    if n <= 0:
        raise ArithmeticError, "n=%s must be positive"%n
    elif n == 1:
        return self.gen() - 1
    else:
        return self(cyclotomic.cyclotomic_coeffs(n), check=True)

File: /sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/rings/polynomial/polynomial_ring.py

Source Code (starting at line 867):

def cyclotomic_polynomial(self, n):
    """
    Return the nth cyclotomic polynomial as a polynomial in this
    polynomial ring. For details of the implementation, see the
    documentation for
    :func:`sage.rings.polynomial.cyclotomic.cyclotomic_coeffs`.

    EXAMPLES::

        sage: R = ZZ['x']
        sage: R.cyclotomic_polynomial(8)
        x^4 + 1
        sage: R.cyclotomic_polynomial(12)
        x^4 - x^2 + 1
        sage: S = PolynomialRing(FiniteField(7), 'x')
        sage: S.cyclotomic_polynomial(12)
        x^4 + 6*x^2 + 1
        sage: S.cyclotomic_polynomial(1)
        x + 6

    TESTS:

    Make sure it agrees with other systems for the trivial case::

        sage: ZZ['x'].cyclotomic_polynomial(1)
        x - 1
        sage: gp('polcyclo(1)')
        x - 1
    """
    if n <= 0:
        raise ArithmeticError, "n=%s must be positive"%n
    elif n == 1:
        return self.gen() - 1
    else:
        return self(cyclotomic.cyclotomic_coeffs(n), check=True)
preparse("n = 5") 
       
'n = Integer(5)'
'n = Integer(5)'
n.divisors() 
       
[1, 5]
[1, 5]

A Quick Tour

a = float(2^64); a 
       
1.8446744073709552e+19
1.8446744073709552e+19
math.sin(a) 
       
0.023598509904439558
0.023598509904439558
a = sin(2^64); a 
       
sin(18446744073709551616)
sin(18446744073709551616)
type(a) 
       
<type 'sage.symbolic.expression.Expression'>
<type 'sage.symbolic.expression.Expression'>
N(a, digits=1000) 
       
0.0235985099044395586343659228761347751887971250470813595052082575127870\
367195714805353268175666623235437729751383647524917627871288325844860738\
774780555533262957487022395763401807856585404052810226503822554355095528\
028310586788053564221509928173361541766973214641915324432036026757696249\
788261667623001542396934455889240433419116118797637288382051695173924449\
181581804029311144948276644785620162298683748498605311140905034937489190\
912440501083085575276067441836443904976066614700407935079718694250167780\
060282736897308702214472463724083609963439955539184511211192964943428037\
445515558183613980406937553891459323066921748683325795142578242683676201\
615993420650011493920650606656966134469661959900199302403661301884174196\
373458046150133147119106730368104179014971275456878541846106129727392370\
953571120197087441563843741360076443085933594082080125509731439475852770\
875606175218014179445677770819225855418035844180363843045553659301208637\
1338941013691045701740737164131815074217522503906093271710053729660
0.02359850990443955863436592287613477518879712504708135950520825751278703671957148053532681756666232354377297513836475249176278712883258448607387747805555332629574870223957634018078565854040528102265038225543550955280283105867880535642215099281733615417669732146419153244320360267576962497882616676230015423969344558892404334191161187976372883820516951739244491815818040293111449482766447856201622986837484986053111409050349374891909124405010830855752760674418364439049760666147004079350797186942501677800602827368973087022144724637240836099634399555391845112111929649434280374455155581836139804069375538914593230669217486833257951425782426836762016159934206500114939206506066569661344696619599001993024036613018841741963734580461501331471191067303681041790149712754568785418461061297273923709535711201970874415638437413600764430859335940820801255097314394758527708756061752180141794456777708192258554180358441803638430455536593012086371338941013691045701740737164131815074217522503906093271710053729660
a = [bernoulli(2*n) for n in [1..16]]; a 
       
[1/6, -1/30, 1/42, -1/30, 5/66, -691/2730, 7/6, -3617/510, 43867/798,
-174611/330, 854513/138, -236364091/2730, 8553103/6, -23749461029/870,
8615841276005/14322, -7709321041217/510]
[1/6, -1/30, 1/42, -1/30, 5/66, -691/2730, 7/6, -3617/510, 43867/798, -174611/330, 854513/138, -236364091/2730, 8553103/6, -23749461029/870, 8615841276005/14322, -7709321041217/510]
B._latex_() 
       
'\\left(\\begin{array}{rrrr}\n\\frac{1}{6} & -\\frac{1}{30} &
\\frac{1}{42} & -\\frac{1}{30} \\\\\n\\frac{5}{66} &
-\\frac{691}{2730} & \\frac{7}{6} & -\\frac{3617}{510}
\\\\\n\\frac{43867}{798} & -\\frac{174611}{330} &
\\frac{854513}{138} & -\\frac{236364091}{2730}
\\\\\n\\frac{8553103}{6} & -\\frac{23749461029}{870} &
\\frac{8615841276005}{14322} &
-\\frac{7709321041217}{510}\n\\end{array}\\right)'
'\\left(\\begin{array}{rrrr}\n\\frac{1}{6} & -\\frac{1}{30} & \\frac{1}{42} & -\\frac{1}{30} \\\\\n\\frac{5}{66} & -\\frac{691}{2730} & \\frac{7}{6} & -\\frac{3617}{510} \\\\\n\\frac{43867}{798} & -\\frac{174611}{330} & \\frac{854513}{138} & -\\frac{236364091}{2730} \\\\\n\\frac{8553103}{6} & -\\frac{23749461029}{870} & \\frac{8615841276005}{14322} & -\\frac{7709321041217}{510}\n\\end{array}\\right)'
B = matrix(4,4,a); show(B) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrr} \frac{1}{6} & -\frac{1}{30} & \frac{1}{42} & -\frac{1}{30} \\ \frac{5}{66} & -\frac{691}{2730} & \frac{7}{6} & -\frac{3617}{510} \\ \frac{43867}{798} & -\frac{174611}{330} & \frac{854513}{138} & -\frac{236364091}{2730} \\ \frac{8553103}{6} & -\frac{23749461029}{870} & \frac{8615841276005}{14322} & -\frac{7709321041217}{510} \end{array}\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrr} \frac{1}{6} & -\frac{1}{30} & \frac{1}{42} & -\frac{1}{30} \\ \frac{5}{66} & -\frac{691}{2730} & \frac{7}{6} & -\frac{3617}{510} \\ \frac{43867}{798} & -\frac{174611}{330} & \frac{854513}{138} & -\frac{236364091}{2730} \\ \frac{8553103}{6} & -\frac{23749461029}{870} & \frac{8615841276005}{14322} & -\frac{7709321041217}{510} \end{array}\right)
f = B.charpoly(); f 
       
x^4 + 8067801164850701/533715*x^3 -
58285217560700198615240633/1403923430910*x^2 +
8132809004828014213156640/4258567740427*x +
55574407859795565506560000/73612385227381
x^4 + 8067801164850701/533715*x^3 - 58285217560700198615240633/1403923430910*x^2 + 8132809004828014213156640/4258567740427*x + 55574407859795565506560000/73612385227381
plot(f, -5000, 5000) 
       
f.roots(CC) 
       
[(-1.51163123214893e10, 1), (-0.113796311512103, 1), (0.159804146544381,
1), (2746.38784246979, 1)]
[(-1.51163123214893e10, 1), (-0.113796311512103, 1), (0.159804146544381, 1), (2746.38784246979, 1)]
RDF 
       
Real Double Field
Real Double Field
a = sqrt(2) + 5/3; a 
       
sqrt(2) + 5/3
sqrt(2) + 5/3
R.<x> = ZZ[] 
       
x.parent() 
       
Univariate Polynomial Ring in x over Integer Ring
Univariate Polynomial Ring in x over Integer Ring
x + 1/3 
       
x + 1/3
x + 1/3
S = QQ['x'] S.coerce('1/3') 
       
Traceback (click to the left of this block for traceback)
...
TypeError: no canonical coercion from <type 'str'> to Univariate
Polynomial Ring in x over Rational Field
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_53.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("UyA9IFFRWyd4J10KUy5jb2VyY2UoJzEvMycp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmp89sEzk/___code___.py", line 3, in <module>
    exec compile(u"S.coerce('1/3')" + '\n', '', 'single')
  File "", line 1, in <module>
    
  File "parent.pyx", line 1067, in sage.structure.parent.Parent.coerce (sage/structure/parent.c:8016)
  File "parent.pyx", line 1094, in sage.structure.parent.Parent.coerce (sage/structure/parent.c:7958)
TypeError: no canonical coercion from <type 'str'> to Univariate Polynomial Ring in x over Rational Field
y = x + 1/3 y.parent() 
       
Univariate Polynomial Ring in x over Rational Field
Univariate Polynomial Ring in x over Rational Field
 
       
parent(5/3) 
       
Rational Field
Rational Field
a = 5/3 
       
a.parent() 
       
Rational Field
Rational Field
parent(sqrt(2)) 
       
Symbolic Ring
Symbolic Ring
C = ZZ.category(); C 
       
Category of euclidean domains
Category of euclidean domains
type(C) 
       
<class 'sage.categories.euclidean_domains.EuclideanDomains'>
<class 'sage.categories.euclidean_domains.EuclideanDomains'>
type(ZZ) 
       
<type 'sage.rings.integer_ring.IntegerRing_class'>
<type 'sage.rings.integer_ring.IntegerRing_class'>
ZZ.func 
       
C.all_super_categories() 
       
[Category of euclidean domains, Category of principal ideal domains,
Category of unique factorization domains, Category of gcd domains,
Category of integral domains, Category of commutative rings, Category of
domains, Category of rings, Category of rngs, Category of commutative
additive groups, Category of semirings, Category of commutative additive
monoids, Category of commutative additive semigroups, Category of
additive magmas, Category of monoids, Category of semigroups, Category
of magmas, Category of sets, Category of sets with partial maps,
Category of objects]
[Category of euclidean domains, Category of principal ideal domains, Category of unique factorization domains, Category of gcd domains, Category of integral domains, Category of commutative rings, Category of domains, Category of rings, Category of rngs, Category of commutative additive groups, Category of semirings, Category of commutative additive monoids, Category of commutative additive semigroups, Category of additive magmas, Category of monoids, Category of semigroups, Category of magmas, Category of sets, Category of sets with partial maps, Category of objects]
a = sin(2^64) v = RealIntervalField(100)(a); v 
       
0.0235985099044395586343659228762?
0.0235985099044395586343659228762?
v.lower() 
       
0.023598509904439558634365922876
0.023598509904439558634365922876
v.upper() 
       
0.023598509904439558634365922877
0.023598509904439558634365922877
f.roots(RealIntervalField(100)) 
       
[(-1.5116312321489265648220103115283?e10, 1),
(-0.11379631151210255188658154882301?, 1),
(0.15980414654438143948220086785611?, 1),
(2746.3878424697929411908606679704?, 1)]
[(-1.5116312321489265648220103115283?e10, 1), (-0.11379631151210255188658154882301?, 1), (0.15980414654438143948220086785611?, 1), (2746.3878424697929411908606679704?, 1)]
v = B.eigenvalues(); v 
       
[-1.511631232148927?e10, -0.1137963115121026?, 0.1598041465443815?,
2746.387842469793?]
[-1.511631232148927?e10, -0.1137963115121026?, 0.1598041465443815?, 2746.387842469793?]
B = matrix(RDF,2,2,[0,1,-1,0]) B.eigenvalues() 
       
[1.0*I, -1.0*I]
[1.0*I, -1.0*I]
alpha = v[0]; alpha 
       
-1.511631232148927?e10
-1.511631232148927?e10
matrix? 
       

File: /sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/matrix/constructor.py

Type: <type ‘function’>

Definition: matrix(*args, **kwds)

Docstring:

Create a matrix.

INPUT: The matrix command takes the entries of a matrix, optionally preceded by a ring and the dimensions of the matrix, and returns a matrix.

The entries of a matrix can be specified as a flat list of elements, a list of lists (i.e., a list of rows), a list of Sage vectors, a callable object, or a dictionary having positions as keys and matrix entries as values (see the examples). If you pass in a callable object, then you must specify the number of rows and columns. You can create a matrix of zeros by passing an empty list or the integer zero for the entries. To construct a multiple of the identity (cI), you can specify square dimensions and pass in c. Calling matrix() with a Sage object may return something that makes sense. Calling matrix() with a NumPy array will convert the array to a matrix.

The ring, number of rows, and number of columns of the matrix can be specified by setting the ring, nrows, or ncols parameters or by passing them as the first arguments to the function in the order ring, nrows, ncols. The ring defaults to ZZ if it is not specified or cannot be determined from the entries. If the numbers of rows and columns are not specified and cannot be determined, then an empty 0x0 matrix is returned.

  • ring - the base ring for the entries of the matrix.
  • nrows - the number of rows in the matrix.
  • ncols - the number of columns in the matrix.
  • sparse - create a sparse matrix. This defaults to True when the entries are given as a dictionary, otherwise defaults to False.

OUTPUT:

a matrix

EXAMPLES:

sage: m=matrix(2); m; m.parent()
[0 0]
[0 0]
Full MatrixSpace of 2 by 2 dense matrices over Integer Ring
sage: m=matrix(2,3); m; m.parent()
[0 0 0]
[0 0 0]
Full MatrixSpace of 2 by 3 dense matrices over Integer Ring
sage: m=matrix(QQ,[[1,2,3],[4,5,6]]); m; m.parent()
[1 2 3]
[4 5 6]
Full MatrixSpace of 2 by 3 dense matrices over Rational Field
sage: m = matrix(QQ, 3, 3, lambda i, j: i+j); m
[0 1 2]
[1 2 3]
[2 3 4]
sage: m = matrix(3, lambda i,j: i-j); m
[ 0 -1 -2]
[ 1  0 -1]
[ 2  1  0]
sage: matrix(QQ,2,3,lambda x, y: x+y)
[0 1 2]
[1 2 3]
sage: matrix(QQ,3,2,lambda x, y: x+y)
[0 1]
[1 2]
[2 3]
sage: v1=vector((1,2,3))
sage: v2=vector((4,5,6))
sage: m=matrix([v1,v2]); m; m.parent()
[1 2 3]
[4 5 6]
Full MatrixSpace of 2 by 3 dense matrices over Integer Ring
sage: m=matrix(QQ,2,[1,2,3,4,5,6]); m; m.parent()
[1 2 3]
[4 5 6]
Full MatrixSpace of 2 by 3 dense matrices over Rational Field
sage: m=matrix(QQ,2,3,[1,2,3,4,5,6]); m; m.parent()
[1 2 3]
[4 5 6]
Full MatrixSpace of 2 by 3 dense matrices over Rational Field
sage: m=matrix({(0,1): 2, (1,1):2/5}); m; m.parent()
[  0   2]
[  0 2/5]
Full MatrixSpace of 2 by 2 sparse matrices over Rational Field
sage: m=matrix(QQ,2,3,{(1,1): 2}); m; m.parent()
[0 0 0]
[0 2 0]
Full MatrixSpace of 2 by 3 sparse matrices over Rational Field
sage: import numpy
sage: n=numpy.array([[1,2],[3,4]],float)
sage: m=matrix(n); m; m.parent()
[1.0 2.0]
[3.0 4.0]
Full MatrixSpace of 2 by 2 dense matrices over Real Double Field
sage: v = vector(ZZ, [1, 10, 100])
sage: m=matrix(v); m; m.parent()
[  1  10 100]
Full MatrixSpace of 1 by 3 dense matrices over Integer Ring
sage: m=matrix(GF(7), v); m; m.parent()
[1 3 2]
Full MatrixSpace of 1 by 3 dense matrices over Finite Field of size 7
sage: g = graphs.PetersenGraph()
sage: m = matrix(g); m; m.parent()
[0 1 0 0 1 1 0 0 0 0]
[1 0 1 0 0 0 1 0 0 0]
[0 1 0 1 0 0 0 1 0 0]
[0 0 1 0 1 0 0 0 1 0]
[1 0 0 1 0 0 0 0 0 1]
[1 0 0 0 0 0 0 1 1 0]
[0 1 0 0 0 0 0 0 1 1]
[0 0 1 0 0 1 0 0 0 1]
[0 0 0 1 0 1 1 0 0 0]
[0 0 0 0 1 0 1 1 0 0]
Full MatrixSpace of 10 by 10 dense matrices over Integer Ring
sage: matrix(ZZ, 10, 10, range(100), sparse=True).parent()
Full MatrixSpace of 10 by 10 sparse matrices over Integer Ring
sage: R = PolynomialRing(QQ, 9, 'x')
sage: A = matrix(R, 3, 3, R.gens()); A
[x0 x1 x2]
[x3 x4 x5]
[x6 x7 x8]
sage: det(A)
-x2*x4*x6 + x1*x5*x6 + x2*x3*x7 - x0*x5*x7 - x1*x3*x8 + x0*x4*x8

TESTS:

sage: m=matrix(); m; m.parent()
[]
Full MatrixSpace of 0 by 0 dense matrices over Integer Ring
sage: m=matrix(QQ); m; m.parent()
[]
Full MatrixSpace of 0 by 0 dense matrices over Rational Field
sage: m=matrix(QQ,2); m; m.parent()
[0 0]
[0 0]
Full MatrixSpace of 2 by 2 dense matrices over Rational Field
sage: m=matrix(QQ,2,3); m; m.parent()
[0 0 0]
[0 0 0]
Full MatrixSpace of 2 by 3 dense matrices over Rational Field
sage: m=matrix([]); m; m.parent()
[]
Full MatrixSpace of 0 by 0 dense matrices over Integer Ring
sage: m=matrix(QQ,[]); m; m.parent()
[]
Full MatrixSpace of 0 by 0 dense matrices over Rational Field
sage: m=matrix(2,2,1); m; m.parent()
[1 0]
[0 1]
Full MatrixSpace of 2 by 2 dense matrices over Integer Ring
sage: m=matrix(QQ,2,2,1); m; m.parent()
[1 0]
[0 1]
Full MatrixSpace of 2 by 2 dense matrices over Rational Field
sage: m=matrix(2,3,0); m; m.parent()
[0 0 0]
[0 0 0]
Full MatrixSpace of 2 by 3 dense matrices over Integer Ring
sage: m=matrix(QQ,2,3,0); m; m.parent()
[0 0 0]
[0 0 0]
Full MatrixSpace of 2 by 3 dense matrices over Rational Field
sage: m=matrix([[1,2,3],[4,5,6]]); m; m.parent()
[1 2 3]
[4 5 6]
Full MatrixSpace of 2 by 3 dense matrices over Integer Ring
sage: m=matrix(QQ,2,[[1,2,3],[4,5,6]]); m; m.parent()
[1 2 3]
[4 5 6]
Full MatrixSpace of 2 by 3 dense matrices over Rational Field
sage: m=matrix(QQ,3,[[1,2,3],[4,5,6]]); m; m.parent()
Traceback (click to the left of this block for traceback)
...
                                
                            

File: /sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/matrix/constructor.py

Type: <type ‘function’>

Definition: matrix(*args, **kwds)

Docstring:

Create a matrix.

INPUT: The matrix command takes the entries of a matrix, optionally preceded by a ring and the dimensions of the matrix, and returns a matrix.

The entries of a matrix can be specified as a flat list of elements, a list of lists (i.e., a list of rows), a list of Sage vectors, a callable object, or a dictionary having positions as keys and matrix entries as values (see the examples). If you pass in a callable object, then you must specify the number of rows and columns. You can create a matrix of zeros by passing an empty list or the integer zero for the entries. To construct a multiple of the identity (cI), you can specify square dimensions and pass in c. Calling matrix() with a Sage object may return something that makes sense. Calling matrix() with a NumPy array will convert the array to a matrix.

The ring, number of rows, and number of columns of the matrix can be specified by setting the ring, nrows, or ncols parameters or by passing them as the first arguments to the function in the order ring, nrows, ncols. The ring defaults to ZZ if it is not specified or cannot be determined from the entries. If the numbers of rows and columns are not specified and cannot be determined, then an empty 0x0 matrix is returned.

  • ring - the base ring for the entries of the matrix.
  • nrows - the number of rows in the matrix.
  • ncols - the number of columns in the matrix.
  • sparse - create a sparse matrix. This defaults to True when the entries are given as a dictionary, otherwise defaults to False.

OUTPUT:

a matrix

EXAMPLES:

sage: m=matrix(2); m; m.parent()
[0 0]
[0 0]
Full MatrixSpace of 2 by 2 dense matrices over Integer Ring
sage: m=matrix(2,3); m; m.parent()
[0 0 0]
[0 0 0]
Full MatrixSpace of 2 by 3 dense matrices over Integer Ring
sage: m=matrix(QQ,[[1,2,3],[4,5,6]]); m; m.parent()
[1 2 3]
[4 5 6]
Full MatrixSpace of 2 by 3 dense matrices over Rational Field
sage: m = matrix(QQ, 3, 3, lambda i, j: i+j); m
[0 1 2]
[1 2 3]
[2 3 4]
sage: m = matrix(3, lambda i,j: i-j); m
[ 0 -1 -2]
[ 1  0 -1]
[ 2  1  0]
sage: matrix(QQ,2,3,lambda x, y: x+y)
[0 1 2]
[1 2 3]
sage: matrix(QQ,3,2,lambda x, y: x+y)
[0 1]
[1 2]
[2 3]
sage: v1=vector((1,2,3))
sage: v2=vector((4,5,6))
sage: m=matrix([v1,v2]); m; m.parent()
[1 2 3]
[4 5 6]
Full MatrixSpace of 2 by 3 dense matrices over Integer Ring
sage: m=matrix(QQ,2,[1,2,3,4,5,6]); m; m.parent()
[1 2 3]
[4 5 6]
Full MatrixSpace of 2 by 3 dense matrices over Rational Field
sage: m=matrix(QQ,2,3,[1,2,3,4,5,6]); m; m.parent()
[1 2 3]
[4 5 6]
Full MatrixSpace of 2 by 3 dense matrices over Rational Field
sage: m=matrix({(0,1): 2, (1,1):2/5}); m; m.parent()
[  0   2]
[  0 2/5]
Full MatrixSpace of 2 by 2 sparse matrices over Rational Field
sage: m=matrix(QQ,2,3,{(1,1): 2}); m; m.parent()
[0 0 0]
[0 2 0]
Full MatrixSpace of 2 by 3 sparse matrices over Rational Field
sage: import numpy
sage: n=numpy.array([[1,2],[3,4]],float)
sage: m=matrix(n); m; m.parent()
[1.0 2.0]
[3.0 4.0]
Full MatrixSpace of 2 by 2 dense matrices over Real Double Field
sage: v = vector(ZZ, [1, 10, 100])
sage: m=matrix(v); m; m.parent()
[  1  10 100]
Full MatrixSpace of 1 by 3 dense matrices over Integer Ring
sage: m=matrix(GF(7), v); m; m.parent()
[1 3 2]
Full MatrixSpace of 1 by 3 dense matrices over Finite Field of size 7
sage: g = graphs.PetersenGraph()
sage: m = matrix(g); m; m.parent()
[0 1 0 0 1 1 0 0 0 0]
[1 0 1 0 0 0 1 0 0 0]
[0 1 0 1 0 0 0 1 0 0]
[0 0 1 0 1 0 0 0 1 0]
[1 0 0 1 0 0 0 0 0 1]
[1 0 0 0 0 0 0 1 1 0]
[0 1 0 0 0 0 0 0 1 1]
[0 0 1 0 0 1 0 0 0 1]
[0 0 0 1 0 1 1 0 0 0]
[0 0 0 0 1 0 1 1 0 0]
Full MatrixSpace of 10 by 10 dense matrices over Integer Ring
sage: matrix(ZZ, 10, 10, range(100), sparse=True).parent()
Full MatrixSpace of 10 by 10 sparse matrices over Integer Ring
sage: R = PolynomialRing(QQ, 9, 'x')
sage: A = matrix(R, 3, 3, R.gens()); A
[x0 x1 x2]
[x3 x4 x5]
[x6 x7 x8]
sage: det(A)
-x2*x4*x6 + x1*x5*x6 + x2*x3*x7 - x0*x5*x7 - x1*x3*x8 + x0*x4*x8

TESTS:

sage: m=matrix(); m; m.parent()
[]
Full MatrixSpace of 0 by 0 dense matrices over Integer Ring
sage: m=matrix(QQ); m; m.parent()
[]
Full MatrixSpace of 0 by 0 dense matrices over Rational Field
sage: m=matrix(QQ,2); m; m.parent()
[0 0]
[0 0]
Full MatrixSpace of 2 by 2 dense matrices over Rational Field
sage: m=matrix(QQ,2,3); m; m.parent()
[0 0 0]
[0 0 0]
Full MatrixSpace of 2 by 3 dense matrices over Rational Field
sage: m=matrix([]); m; m.parent()
[]
Full MatrixSpace of 0 by 0 dense matrices over Integer Ring
sage: m=matrix(QQ,[]); m; m.parent()
[]
Full MatrixSpace of 0 by 0 dense matrices over Rational Field
sage: m=matrix(2,2,1); m; m.parent()
[1 0]
[0 1]
Full MatrixSpace of 2 by 2 dense matrices over Integer Ring
sage: m=matrix(QQ,2,2,1); m; m.parent()
[1 0]
[0 1]
Full MatrixSpace of 2 by 2 dense matrices over Rational Field
sage: m=matrix(2,3,0); m; m.parent()
[0 0 0]
[0 0 0]
Full MatrixSpace of 2 by 3 dense matrices over Integer Ring
sage: m=matrix(QQ,2,3,0); m; m.parent()
[0 0 0]
[0 0 0]
Full MatrixSpace of 2 by 3 dense matrices over Rational Field
sage: m=matrix([[1,2,3],[4,5,6]]); m; m.parent()
[1 2 3]
[4 5 6]
Full MatrixSpace of 2 by 3 dense matrices over Integer Ring
sage: m=matrix(QQ,2,[[1,2,3],[4,5,6]]); m; m.parent()
[1 2 3]
[4 5 6]
Full MatrixSpace of 2 by 3 dense matrices over Rational Field
sage: m=matrix(QQ,3,[[1,2,3],[4,5,6]]); m; m.parent()
Traceback (most recent call last):
...
ValueError: Number of rows does not match up with specified number.
sage: m=matrix(QQ,2,3,[[1,2,3],[4,5,6]]); m; m.parent()
[1 2 3]
[4 5 6]
Full MatrixSpace of 2 by 3 dense matrices over Rational Field
sage: m=matrix(QQ,2,4,[[1,2,3],[4,5,6]]); m; m.parent()
Traceback (most recent call last):
...
ValueError: Number of columns does not match up with specified number.
sage: m=matrix([(1,2,3),(4,5,6)]); m; m.parent()
[1 2 3]
[4 5 6]
Full MatrixSpace of 2 by 3 dense matrices over Integer Ring
sage: m=matrix([1,2,3,4,5,6]); m; m.parent()
[1 2 3 4 5 6]
Full MatrixSpace of 1 by 6 dense matrices over Integer Ring
sage: m=matrix((1,2,3,4,5,6)); m; m.parent()
[1 2 3 4 5 6]
Full MatrixSpace of 1 by 6 dense matrices over Integer Ring
sage: m=matrix(QQ,[1,2,3,4,5,6]); m; m.parent()
[1 2 3 4 5 6]
Full MatrixSpace of 1 by 6 dense matrices over Rational Field
sage: m=matrix(QQ,3,2,[1,2,3,4,5,6]); m; m.parent()
[1 2]
[3 4]
[5 6]
Full MatrixSpace of 3 by 2 dense matrices over Rational Field
sage: m=matrix(QQ,2,4,[1,2,3,4,5,6]); m; m.parent()
Traceback (most recent call last):
...
ValueError: entries has the wrong length
sage: m=matrix(QQ,5,[1,2,3,4,5,6]); m; m.parent()
Traceback (most recent call last):
...
TypeError: entries has the wrong length
sage: m=matrix({(1,1): 2}); m; m.parent()
[0 0]
[0 2]
Full MatrixSpace of 2 by 2 sparse matrices over Integer Ring
sage: m=matrix(QQ,{(1,1): 2}); m; m.parent()
[0 0]
[0 2]
Full MatrixSpace of 2 by 2 sparse matrices over Rational Field
sage: m=matrix(QQ,3,{(1,1): 2}); m; m.parent()
[0 0 0]
[0 2 0]
[0 0 0]
Full MatrixSpace of 3 by 3 sparse matrices over Rational Field
sage: m=matrix(QQ,3,4,{(1,1): 2}); m; m.parent()
[0 0 0 0]
[0 2 0 0]
[0 0 0 0]
Full MatrixSpace of 3 by 4 sparse matrices over Rational Field
sage: m=matrix(QQ,2,{(1,1): 2}); m; m.parent()
[0 0]
[0 2]
Full MatrixSpace of 2 by 2 sparse matrices over Rational Field
sage: m=matrix(QQ,1,{(1,1): 2}); m; m.parent()
Traceback (most recent call last):
...
IndexError: invalid entries list
sage: m=matrix({}); m; m.parent()
[]
Full MatrixSpace of 0 by 0 sparse matrices over Integer Ring
sage: m=matrix(QQ,{}); m; m.parent()
[]
Full MatrixSpace of 0 by 0 sparse matrices over Rational Field
sage: m=matrix(QQ,2,{}); m; m.parent()
[0 0]
[0 0]
Full MatrixSpace of 2 by 2 sparse matrices over Rational Field
sage: m=matrix(QQ,2,3,{}); m; m.parent()
[0 0 0]
[0 0 0]
Full MatrixSpace of 2 by 3 sparse matrices over Rational Field
sage: m=matrix(2,{}); m; m.parent()
[0 0]
[0 0]
Full MatrixSpace of 2 by 2 sparse matrices over Integer Ring
sage: m=matrix(2,3,{}); m; m.parent()
[0 0 0]
[0 0 0]
Full MatrixSpace of 2 by 3 sparse matrices over Integer Ring
sage: m=matrix(0); m; m.parent()
[]
Full MatrixSpace of 0 by 0 dense matrices over Integer Ring
sage: m=matrix(0,2); m; m.parent()
[]
Full MatrixSpace of 0 by 2 dense matrices over Integer Ring
sage: m=matrix(2,0); m; m.parent()
[]
Full MatrixSpace of 2 by 0 dense matrices over Integer Ring
sage: m=matrix(0,[1]); m; m.parent()
Traceback (most recent call last):
...
ValueError: entries has the wrong length
sage: m=matrix(1,0,[]); m; m.parent()
[]
Full MatrixSpace of 1 by 0 dense matrices over Integer Ring
sage: m=matrix(0,1,[]); m; m.parent()
[]
Full MatrixSpace of 0 by 1 dense matrices over Integer Ring
sage: m=matrix(0,[]); m; m.parent()
[]
Full MatrixSpace of 0 by 0 dense matrices over Integer Ring
sage: m=matrix(0,{}); m; m.parent()
[]
Full MatrixSpace of 0 by 0 sparse matrices over Integer Ring
sage: m=matrix(0,{(1,1):2}); m; m.parent()
Traceback (most recent call last):
...
IndexError: invalid entries list
sage: m=matrix(2,0,{(1,1):2}); m; m.parent()
Traceback (most recent call last):
...
IndexError: invalid entries list
sage: import numpy
sage: n=numpy.array([[numpy.complex(0,1),numpy.complex(0,2)],[3,4]],complex)
sage: m=matrix(n); m; m.parent()
[1.0*I 2.0*I]
[  3.0   4.0]
Full MatrixSpace of 2 by 2 dense matrices over Complex Double Field
sage: n=numpy.array([[1,2],[3,4]],'int32')
sage: m=matrix(n); m; m.parent()
[1 2]
[3 4]
Full MatrixSpace of 2 by 2 dense matrices over Integer Ring
sage: n = numpy.array([[1,2,3],[4,5,6],[7,8,9]],'float32')
sage: m=matrix(n); m; m.parent()
[1.0 2.0 3.0]
[4.0 5.0 6.0]
[7.0 8.0 9.0]
Full MatrixSpace of 3 by 3 dense matrices over Real Double Field
sage: n=numpy.array([[1,2,3],[4,5,6],[7,8,9]],'float64')
sage: m=matrix(n); m; m.parent()
[1.0 2.0 3.0]
[4.0 5.0 6.0]
[7.0 8.0 9.0]
Full MatrixSpace of 3 by 3 dense matrices over Real Double Field
sage: n=numpy.array([[1,2,3],[4,5,6],[7,8,9]],'complex64')
sage: m=matrix(n); m; m.parent()
[1.0 2.0 3.0]
[4.0 5.0 6.0]
[7.0 8.0 9.0]
Full MatrixSpace of 3 by 3 dense matrices over Complex Double Field
sage: n=numpy.array([[1,2,3],[4,5,6],[7,8,9]],'complex128')
sage: m=matrix(n); m; m.parent()
[1.0 2.0 3.0]
[4.0 5.0 6.0]
[7.0 8.0 9.0]
Full MatrixSpace of 3 by 3 dense matrices over Complex Double Field
sage: a = matrix([[1,2],[3,4]])
sage: b = matrix(a.numpy()); b
[1 2]
[3 4]
sage: a == b
True
sage: c = matrix(a.numpy('float32')); c
[1.0 2.0]
[3.0 4.0]
sage: matrix(numpy.array([[5]]))
[5]
sage: v = vector(ZZ, [1, 10, 100])
sage: m=matrix(ZZ['x'], v); m; m.parent()
[  1  10 100]
Full MatrixSpace of 1 by 3 dense matrices over Univariate Polynomial Ring in x over Integer Ring
sage: matrix(ZZ, 10, 10, range(100)).parent()
Full MatrixSpace of 10 by 10 dense matrices over Integer Ring
sage: m = matrix(GF(7), [[1/3,2/3,1/2], [3/4,4/5,7]]); m; m.parent()
[5 3 4]
[6 5 0]
Full MatrixSpace of 2 by 3 dense matrices over Finite Field of size 7
sage: m = matrix([[1,2,3], [RDF(2), CDF(1,2), 3]]); m; m.parent()
[        1.0         2.0         3.0]
[        2.0 1.0 + 2.0*I         3.0]
Full MatrixSpace of 2 by 3 dense matrices over Complex Double Field
sage: m=matrix(3,3,1/2); m; m.parent()
[1/2   0   0]
[  0 1/2   0]
[  0   0 1/2]
Full MatrixSpace of 3 by 3 dense matrices over Rational Field
sage: matrix([[1],[2,3]])
Traceback (most recent call last):
...
ValueError: List of rows is not valid (rows are wrong types or lengths)
sage: matrix([[1],2])
Traceback (most recent call last):
...
ValueError: List of rows is not valid (rows are wrong types or lengths)
sage: matrix(vector(RR,[1,2,3])).parent()
Full MatrixSpace of 1 by 3 dense matrices over Real Field with 53 bits of precision
sage: matrix(ZZ, [[0] for i in range(10^5)]).is_zero() # see #10158
True

AUTHORS:

  • ??: Initial implementation
  • Jason Grout (2008-03): almost a complete rewrite, with bits and pieces from the original implementation
alpha.minpoly() 
       
x^4 + 8067801164850701/533715*x^3 -
58285217560700198615240633/1403923430910*x^2 +
8132809004828014213156640/4258567740427*x +
55574407859795565506560000/73612385227381
x^4 + 8067801164850701/533715*x^3 - 58285217560700198615240633/1403923430910*x^2 + 8132809004828014213156640/4258567740427*x + 55574407859795565506560000/73612385227381

Components of Sage

a = gp("2") 
       
b = a._sage_?? 
       

File: /sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/interfaces/gp.py

Source Code (starting at line 809):

def _sage_(self):
    """
    Convert this GpElement into a Sage object, if possible.

    EXAMPLES::

        sage: gp(I).sage()
        i
        sage: gp(I).sage().parent()
        Maximal Order in Number Field in i with defining polynomial x^2 + 1

    ::

        sage: M = Matrix(ZZ,2,2,[1,2,3,4]); M
        [1 2]
        [3 4]
        sage: gp(M)
        [1, 2; 3, 4]
        sage: gp(M).sage()
        [1 2]
        [3 4]
        sage: gp(M).sage() == M
        True
    """
    return pari(str(self)).python()

File: /sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/interfaces/gp.py

Source Code (starting at line 809):

def _sage_(self):
    """
    Convert this GpElement into a Sage object, if possible.

    EXAMPLES::

        sage: gp(I).sage()
        i
        sage: gp(I).sage().parent()
        Maximal Order in Number Field in i with defining polynomial x^2 + 1

    ::

        sage: M = Matrix(ZZ,2,2,[1,2,3,4]); M
        [1 2]
        [3 4]
        sage: gp(M)
        [1, 2; 3, 4]
        sage: gp(M).sage()
        [1 2]
        [3 4]
        sage: gp(M).sage() == M
        True
    """
    return pari(str(self)).python()
type(b) 
       
<type 'sage.rings.integer.Integer'>
<type 'sage.rings.integer.Integer'>

Number Theory

factor(2839479287342398) # uses Pari 
       
2 * 4017089 * 353424991
2 * 4017089 * 353424991
c = factorial(25); c 
       
15511210043330985984000000
15511210043330985984000000
[c.valuation(p) for p in prime_range(2, 25)] 
       
[22, 10, 6, 3, 2, 1, 1, 1, 1]
[22, 10, 6, 3, 2, 1, 1, 1, 1]
L = divisors(c); len(L) 
       
340032
340032
L[17000], factor(L[17000]) 
       
(10680579, 3^6 * 7^2 * 13 * 23)
(10680579, 3^6 * 7^2 * 13 * 23)
a = mod(4/7,2301); a 
       
658
658
a.rational_reconstruction() 
       
4/7
4/7
time number_of_partitions(10^7) 
       
920271755026045466855962781668256054307294052810239793953285763517412985\
262323501978822916547103339332198764311128926694429965192014469337180574\
258854255101965669713692722439368861237049443900118466267242229358838809\
496460215546742114497122936318794382420922229797018587870350451317915617\
184990942766797810155029441933075045772129188981041614489343545384206438\
995186836592262593125170223430127680062490663477743842242002004914231357\
899486287124678626100600066102278733540933447719703464029124680195026177\
412964857500689657276787365748796835192363570613191348609145244276270764\
465804777408575949440508551447566418811489630464191115045309280131652547\
731782793747141150484980319367430611463990946023472819466195667158678183\
681130408875817996838721729445775753916663228712954510481120704923859087\
275241592392223665876910286300131474621294645735699401736284697581755151\
900016413451408993670931908590802671856117921704294651978579681174352991\
410791557056622496173369118595092855785844134417338169649142692589187435\
301742615221458864914330678814703958326474085781955660425664604942849123\
726120485051272439872545976606903238042522371480831216853001064737946906\
898017475046619462994720059814429480949267645638531717278315386109140567\
421507374975384275021240249642090800336572787686941682199463463099066866\
702204042926855402821066676615841472011557402435218180206292340119241672\
141006748199826744593298451761269206891815323035747468238859771982766512\
554781263236924319788525197851803361705413947686093663891147724061366833\
494127467087092245969359401102159299184733830143890656327648052676575908\
655135939782442443116808542177355365928232190011995697704315727188881084\
528176142957772556227153624081222658008489322761962675453544265567233476\
857358975517683731089773908740330164186182660688614126800469264172995743\
679921180655438244719647115856821466186517301168907732792274321379544884\
573350740004120158204881150331945483918135267242867162717503464701181877\
399958004314416087296985163324282563458624844066236399562733354725457411\
347627828347256593033627186674151973687980402192233716371343312564266298\
874305195901960277032311918990723575237081597193774377829718464719281814\
341332773477437283460627207869478917449115162489982657581564116298023459\
732495270075351700956243598913730840133454971118265085585706676644886436\
331834764264219387054191767502487300255270790939231320204783038516420542\
008888313103784838110884255487585083733240938734797302734376936793420810\
867020185209718779670927269908884747550412550772822753523086679809536245\
524746452957745548238178302717520159051754876562006809869627325035982116\
186671112647935832871470323287962444698515988848621901187755088113589185\
457016501136471407349024158013999313263656227842088073986822774216895311\
418550769982898380687461265543773217506522881285332749712463895138180510\
378178497047690889118184846592767949868064656342862993393444237184906674\
722873849811294966439997475566061223014020274110963671727713854372115721\
224489228469570767118124546455721433201289716309063821673453815865659403\
167483160827068775845409484961883982333993500194238336824233008140594931\
590134175918949793856506343243088419472771218165592793359389269115106835\
490432906902871027335713152227618464826154317860618134546336344159741794\
139420247061299867257344715523461677386135094707607583386378705799210071\
685144173415484815139532963734550586141746926780137597372467246969311252\
404574068882891540550303875935489428805492623836212595940806996986432453\
55453826567378500963781681659096276126857969078217677288980
Time: CPU 0.32 s, Wall: 0.32 s
92027175502604546685596278166825605430729405281023979395328576351741298526232350197882291654710333933219876431112892669442996519201446933718057425885425510196566971369272243936886123704944390011846626724222935883880949646021554674211449712293631879438242092222979701858787035045131791561718499094276679781015502944193307504577212918898104161448934354538420643899518683659226259312517022343012768006249066347774384224200200491423135789948628712467862610060006610227873354093344771970346402912468019502617741296485750068965727678736574879683519236357061319134860914524427627076446580477740857594944050855144756641881148963046419111504530928013165254773178279374714115048498031936743061146399094602347281946619566715867818368113040887581799683872172944577575391666322871295451048112070492385908727524159239222366587691028630013147462129464573569940173628469758175515190001641345140899367093190859080267185611792170429465197857968117435299141079155705662249617336911859509285578584413441733816964914269258918743530174261522145886491433067881470395832647408578195566042566460494284912372612048505127243987254597660690323804252237148083121685300106473794690689801747504661946299472005981442948094926764563853171727831538610914056742150737497538427502124024964209080033657278768694168219946346309906686670220404292685540282106667661584147201155740243521818020629234011924167214100674819982674459329845176126920689181532303574746823885977198276651255478126323692431978852519785180336170541394768609366389114772406136683349412746708709224596935940110215929918473383014389065632764805267657590865513593978244244311680854217735536592823219001199569770431572718888108452817614295777255622715362408122265800848932276196267545354426556723347685735897551768373108977390874033016418618266068861412680046926417299574367992118065543824471964711585682146618651730116890773279227432137954488457335074000412015820488115033194548391813526724286716271750346470118187739995800431441608729698516332428256345862484406623639956273335472545741134762782834725659303362718667415197368798040219223371637134331256426629887430519590196027703231191899072357523708159719377437782971846471928181434133277347743728346062720786947891744911516248998265758156411629802345973249527007535170095624359891373084013345497111826508558570667664488643633183476426421938705419176750248730025527079093923132020478303851642054200888831310378483811088425548758508373324093873479730273437693679342081086702018520971877967092726990888474755041255077282275352308667980953624552474645295774554823817830271752015905175487656200680986962732503598211618667111264793583287147032328796244469851598884862190118775508811358918545701650113647140734902415801399931326365622784208807398682277421689531141855076998289838068746126554377321750652288128533274971246389513818051037817849704769088911818484659276794986806465634286299339344423718490667472287384981129496643999747556606122301402027411096367172771385437211572122448922846957076711812454645572143320128971630906382167345381586565940316748316082706877584540948496188398233399350019423833682423300814059493159013417591894979385650634324308841947277121816559279335938926911510683549043290690287102733571315222761846482615431786061813454633634415974179413942024706129986725734471552346167738613509470760758338637870579921007168514417341548481513953296373455058614174692678013759737246724696931125240457406888289154055030387593548942880549262383621259594080699698643245355453826567378500963781681659096276126857969078217677288980
Time: CPU 0.32 s, Wall: 0.32 s
@interact def _(n=(25..10000)): P = plot(prime_pi, 0, n, gridlines='minor') Q = plot(x/log(x),2,n,color=(1,0,0)) R = plot(Li,2,n,color=(0,1,0)) (P+Q+R).show() 
       

Click to the left again to hide and once more to show the dynamic interactive window

Number Fields

R.<x> = ZZ[] K.<a> = NumberField(x^6 + 40*x^3 + 1372) K 
       
Number Field in a with defining polynomial x^6 + 40*x^3 + 1372
Number Field in a with defining polynomial x^6 + 40*x^3 + 1372
a^6 
       
-40*a^3 - 1372
-40*a^3 - 1372
G = K.galois_group() G 
       
Galois group of Number Field in a with defining polynomial x^6 + 40*x^3
+ 1372
Galois group of Number Field in a with defining polynomial x^6 + 40*x^3 + 1372
G.order() 
       
6
6
f = G.1 f(a) 
       
1/36*a^4 + 1/18*a
1/36*a^4 + 1/18*a
f(a)^6 + 40*f(a)^3 + 1372 
       
0
0
P = K.primes_above(2)[0] H = G.inertia_group(P); H 
       
Subgroup [(), (1,4,6)(2,5,3), (1,6,4)(2,3,5)] of Galois group of Number
Field in a with defining polynomial x^6 + 40*x^3 + 1372
Subgroup [(), (1,4,6)(2,5,3), (1,6,4)(2,3,5)] of Galois group of Number Field in a with defining polynomial x^6 + 40*x^3 + 1372
H.order() 
       
3
3
[G.artin_symbol(Q) for Q in K.primes_above(5)] 
       
[(1,5)(2,4)(3,6), (1,2)(3,4)(5,6), (1,3)(2,6)(4,5)]
[(1,5)(2,4)(3,6), (1,2)(3,4)(5,6), (1,3)(2,6)(4,5)]
A = NumberField(x^5 - x + 2, 'a').galois_group(type="pari"); A 
       
Galois group PARI group [120, -1, 5, "S5"] of degree 5 of the Number
Field in a with defining polynomial x^5 - x + 2
Galois group PARI group [120, -1, 5, "S5"] of degree 5 of the Number Field in a with defining polynomial x^5 - x + 2
L.<a> = NumberField(x^2 + 23) L.class_number() 
       
3
3
CyclotomicField(23).class_number(proof=False) 
       
3
3
proof.number_field(False) 
       
K.<a> = QuadraticField(-30) C = K.class_group() C 
       
Class group of order 4 with structure C2 x C2 of Number Field in a with
defining polynomial x^2 + 30
Class group of order 4 with structure C2 x C2 of Number Field in a with defining polynomial x^2 + 30
C.gens() 
       
[Fractional ideal class (5, a), Fractional ideal class (3, a)]
[Fractional ideal class (5, a), Fractional ideal class (3, a)]

Elliptic Curves

E = EllipticCurve('37a') 
       
E.rank() 
       
1
1
v = E.integral_points(both_signs=True); v 
       
[(-1 : -1 : 1), (-1 : 0 : 1), (0 : -1 : 1), (0 : 0 : 1), (1 : -1 : 1),
(1 : 0 : 1), (2 : -3 : 1), (2 : 2 : 1), (6 : -15 : 1), (6 : 14 : 1)]
[(-1 : -1 : 1), (-1 : 0 : 1), (0 : -1 : 1), (0 : 0 : 1), (1 : -1 : 1), (1 : 0 : 1), (2 : -3 : 1), (2 : 2 : 1), (6 : -15 : 1), (6 : 14 : 1)]
plot(E, xmax=7, gridlines=True) + points([z[:2] for z in v], color='red', pointsize=50) 
       
E.change_ring(GF(next_prime(10^50))).cardinality() 
       
100000000000000000000000001917684156174529696959920
100000000000000000000000001917684156174529696959920

Graph Theory

set_random_seed(1) G = graphs.RandomLobster(8, .6, .3) 
       
show(G, figsize=7) 
       
G.automorphism_group() 
       
Permutation Group with generators [(12,26)]
Permutation Group with generators [(12,26)]
G.chromatic_number() 
       
2
2
G.shortest_path(13,20) 
       
[13, 0, 1, 2, 3, 4, 5, 19, 20]
[13, 0, 1, 2, 3, 4, 5, 19, 20]
G = Graph({0:[1,13],1:[0,2,14],2:[1,3],3:[2,4,15],4:[3,5,17],5:[4,6,19],6:[5,7,21],7:[6,8],8:[7,9,22],9:[8,10],10:[9,11,24],11:[10,12,26],12:[11],13:[0],14:[1,27],15:[3,16],16:[15],17:[4,18],18:[17],19:[5,20],20:[19],21:[6],22:[8,23],23:[22],24:[10,25],25:[24],26:[11],27:[14]}); G.set_pos({0:[59.01325118963035,295.75251934606877],1:[94.36463578086565,286.04432694721777],2:[133.30963998762118,258.9301868699391],3:[179.99545918994187,230.9185690558532],4:[220.94569638353892,189.5405491129016],5:[255.53780531460455,193.66006066064193],6:[241.42300483955069,220.64477638661037],7:[207.24688263349074,216.294217659962],8:[171.93156299963962,207.8112323413829],9:[166.0508845760556,156.6569713830302],10:[167.145549092765,106.64751472831455],11:[181.78743153620886,64.52861700322893],12:[177.9915397622531,35],13:[35,303.3680516143165],14:[81,313],15:[193.38498733723105,265.60960759890276],16:[203.7527488812743,293.81972078992794],17:[244.4047669028139,148.9760323769727],18:[262.7735147030095,118.7511936438419],19:[289.4417315188959,186.0479380736545],20:[315,179.87133655914585],21:[263.8398063810127,240.815465279789],22:[159.42976668674544,254.61374301786665],23:[151.36632083811443,293.0030774459344],24:[144.22651404070498,86.34019199645445],25:[126.0053532049252,70.73123588302923],26:[198.49018000485754,37.72078507164241],27:[61,336]}); graph_editor(G); 
       
show(G, figsize=7) 
       
G.automorphism_group() 
       
Permutation Group with generators [(13,27)(14,28), (12,26)]
Permutation Group with generators [(13,27)(14,28), (12,26)]

Programming in Sage

  • Python.  The main language of Sage is Python, a widespread and actively developed programming language.
  • Cython.  Cython is a compiled language with syntax very similar to Python (any valid Python is also valid Cython).  But it also gives you the ability to define types of variables for speed.
class RSAPrivateKey: def __init__(self, p, q, e): assert p.is_prime() and q.is_prime() self.p = p self.q = q self.n = p * q self.e = e self.d = (~mod(e, (p-1)*(q-1))).lift() self.length = RR(p * q).log(256).floor() assert self.length > 0 def decrypt(self, ciphertext): blocks = ciphertext.digits(self.n) plaintext = "" for b in blocks: pt = (mod(b, self.n)^self.d).lift() for c in pt.digits(256): plaintext += chr(c) return plaintext class RSAPublicKey: def __init__(self, private_key): self.n = private_key.n self.e = private_key.e self.length = private_key.length def encrypt(self, plaintext): blocks = [plaintext[i*self.length:(i+1)*self.length] for i in range(ceil(len(plaintext)/self.length))] ciphertext = 0 for i, b in enumerate(blocks): pt = sum([256^j * ord(ch) for j, ch in enumerate(b)]) ct = (mod(pt, self.n)^self.e).lift() ciphertext += ct * self.n^i return ciphertext 
       
Priv = RSAPrivateKey(next_prime(120309), next_prime(1200), 31) Pub = RSAPublicKey(Priv) 
       
ct = Pub.encrypt("hello world");ct 
       
121827446329895738728628753088007
121827446329895738728628753088007
Priv.decrypt(ct) 
       
'hello world'
'hello world'

Need for speed

  • Smooth transition between Python and compiled C code.
  • Make code that involves lots of manipulation of C data structures fast
def python_sum2(n): s = 0 for i in xrange(1, n+1): s += i*i return s 
       
python_sum2(3) 
       
14
14
n = int(60) 
       
n.divisors() 
       
Traceback (click to the left of this block for traceback)
...
AttributeError: 'int' object has no attribute 'divisors'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_169.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("bi5kaXZpc29ycygp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmpQ4JLm8/___code___.py", line 2, in <module>
    exec compile(u'n.divisors()
  File "", line 1, in <module>
    
AttributeError: 'int' object has no attribute 'divisors'
divisors(n) 
       
[1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60]
[1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60]
python_sum2(2*10^6) 
       
2666668666667000000
2666668666667000000
timeit("python_sum2(2*10^6)") 
       
5 loops, best of 3: 2.44 s per loop
5 loops, best of 3: 2.44 s per loop
srange(1/3, 99/5,20/7) 
       
[1/3, 67/21, 127/21, 187/21, 247/21, 307/21, 367/21]
[1/3, 67/21, 127/21, 187/21, 247/21, 307/21, 367/21]
%cython def cython_sum2(long n): cdef long i, s = 0 for i in range(1, n+1): s += i*i return s 
cython_sum2(3) 
       
14
14
range(2,6) 
       
[2, 3, 4, 5]
[2, 3, 4, 5]
cython_sum2(2*10^6) 
       
2666668666667000000
2666668666667000000
 
       
timeit("cython_sum2(2*10^6)") 
       
125 loops, best of 3: 2.16 ms per loop
125 loops, best of 3: 2.16 ms per loop
204/2.16 
       
94.4444444444444
94.4444444444444

We get even better timings if we choose a better algorithm

var('k, n') show(factor(sum(k^2, k, 1, n))) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{6} \, {\left(n + 1\right)} {\left(2 \, n + 1\right)} n
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{6} \, {\left(n + 1\right)} {\left(2 \, n + 1\right)} n
def sum2(n): return n*(2*n+1)*(n+1)/6 
       
timeit("sum2(2*10^6)") 
       
625 loops, best of 3: 2.75 µs per loop
625 loops, best of 3: 2.75 µs per loop
%cython def c_sum2(long n): return n*(2*n+1)*(n+1)/6 
c_sum2(3) 
       
14
14
timeit("c_sum2(2*10^6)") 
       
625 loops, best of 3: 741 ns per loop
625 loops, best of 3: 741 ns per loop
c_sum2(2*10^6) 
       
-407788678951258603
-407788678951258603
n = 2*10^6; n*(2*n+1)*(n+1) > 2^63 
       
True
True

Solving Equations

x = var('x'); show(solve(x^3 + x - 1==0, x)[0]) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}x = -\frac{1}{2} \, {\left(i \, \sqrt{3} + 1\right)} {\left(\frac{1}{18} \, \sqrt{3} \sqrt{31} + \frac{1}{2}\right)}^{\left(\frac{1}{3}\right)} + \frac{-i \, \sqrt{3} + 1}{6 \, {\left(\frac{1}{18} \, \sqrt{3} \sqrt{31} + \frac{1}{2}\right)}^{\left(\frac{1}{3}\right)}}
\newcommand{\Bold}[1]{\mathbf{#1}}x = -\frac{1}{2} \, {\left(i \, \sqrt{3} + 1\right)} {\left(\frac{1}{18} \, \sqrt{3} \sqrt{31} + \frac{1}{2}\right)}^{\left(\frac{1}{3}\right)} + \frac{-i \, \sqrt{3} + 1}{6 \, {\left(\frac{1}{18} \, \sqrt{3} \sqrt{31} + \frac{1}{2}\right)}^{\left(\frac{1}{3}\right)}}
var('alpha, y') show(solve([3*x + 7*y == 2, alpha*x + 3*y == 8], x,y)[0]) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = \frac{50}{7 \, \alpha - 9}, y = \frac{2 \, {\left(\alpha - 12\right)}}{7 \, \alpha - 9}\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = \frac{50}{7 \, \alpha - 9}, y = \frac{2 \, {\left(\alpha - 12\right)}}{7 \, \alpha - 9}\right]
n = 500 A = random_matrix(QQ,n,n,num_bound=10, den_bound=10) v = random_matrix(QQ,n,1,num_bound=10, den_bound=10) A.visualize_structure() 
       
# Using IML -- http://www.cs.uwaterloo.ca/~astorjoh/iml.html time w = A \ v 
       
Time: CPU 5.03 s, Wall: 5.02 s
Time: CPU 5.03 s, Wall: 5.02 s
w.ncols(), w.nrows() 
       
(1, 500)
(1, 500)
A[0,0] 
       
-4/5
-4/5
w[0] 
       
(-3517028760688029064375524206860558503260764579046769622751008301275401\
420955261319182505472655403446871133375351213855350226815464192625416785\
427799378224204797564876553778968477994716339775767237820732075201307990\
308197757240294234285563184763491733437556772520094928741981494332871024\
651254158873412391649819482714094296169547438994929321661878392738308404\
302422041338086509328881011853757898318105389946174930237408436906944079\
223742786833914611551409557282902026689939169134355557855057198503380254\
232298641549695798842795869295699735107799201002441206465535505058980204\
950835831794506538432167500292525930179580649014029441779675269515653952\
412480597561602652331926698481809226942792117185857783063808851623050866\
691080434331157140193997647056564055323726374249881283029300372215738412\
042918521096448177111817100358266846323723274543880364648172938561865371\
929056955043176312431479911712917380077097901887716153891517164989985540\
840321675523448055303601794177709757529516100039489830377747503199111171\
567328320217277853953924414907309586892529729676752209887301451274362449\
437898145988592956277583816139788377093357552496933776948081224203156447\
925227374841051073109821248667385181018001296971075164387365769779414082\
743792702935093826539113749666422742791582865874227648824847568656015797\
308951586142050635510616384914735047939100560830346367036281042499099093\
587436174460147424354398136945622916521762837511897546642955136118061989\
276592413853260210146325042267865046486205868884565141198378935711191095\
339677561251599756523993225002326911920613591375951568107131173033272054\
209577856132806465246801784225456996705420023960118520687844642932420757\
463894793643138793520132790318668818665509323889749881958253860610185138\
652298499093125007922510871073642155237071601926820225661792045138028040\
026493480523906620291352797182396206857111808133310431261496891125281019\
791926011490935247820428687898616399735896224340600057359854531576146926\
251200089897481071278163938930472956127703898532177384677382415384545588\
699642998424638569295639063787810784484358257504405272277075561916922908\
257264535243509728643865291508953639079652724169694694026175524388621142\
937766905589219346239259093036752861934164525952986127809144111422162660\
187325167212561920817228095582629617487246379658007221535383208075836149\
083679376805804530940354095353962448272508728028384915074672326844898676\
575610800665429165430814274592676665502787407335232444362008294879391683\
83811194688142700374028190883369685908216732077/406778287548421232666331\
161732867083284451766898265331336067813926311992369988856628099086734855\
735667115529095643017475187509536025110579820674273512710409816506781413\
949219095421900994613025586328298832520783087510115995249000462483853695\
254032218916775883778046276732513287003348820832286627339151659920216118\
602434782892082868740908234817764021744897598451761291562171363826753518\
869638462172176786920750397046439419756522146331329906325922062334126886\
845307227747533166221063380004741504866680087220033007960420359539342564\
917059907398615028523186317797636660999604624165361705540811548027344347\
944843318379754195828447139714059075956823953574961456072422821379853471\
265957417978467907648030343371041680488666213418782854500371449004464628\
137151693278248611405436919105441634514379785943438321498699491374429974\
941411916188300692918447066207671860293291735706044903379459163329722215\
462487004764606673950227898048328401073006835733854423499982247502117755\
480969561242112088131965551967459900917790184504128348366688399796247714\
348331657116276519708370377748220107893500318506080007877295044619391333\
807678344042981270732923845847840661087831919093000529797399599904218182\
992292585508525349195563753868289669013772157736583348697099384043469144\
344547635488272911439832922066075313432817036914368704456808129124539273\
223910518127512694139126508695831281678860513712181333729105725904365496\
018849941845190976573970615503284681408750206307284620020356423014190813\
611899820786073110923736507592224098870834184189350561093842374187901309\
709249334375710994985735557672788610080629408146012015171554449734793702\
597134820310225203084105776449433119524680976123866181969647041370142480\
229859354877697883210865623491026410955602205911593318386075749335920440\
095933187160459299339643209960996075895440125644540573643065630406530215\
220384502737018981682502335606698154233159446864117915813842827734973170\
373721996301841763236329935471494386783276795049901144295223363777310171\
682399431489512674283791187221666388715006803961777072989016656984486829\
254799709389454885066301520728637334573706302170617918018593936439994981\
310508468063757045909826757786706357829130980704081337842853100001277249\
846738997785268785823852018104843245188926381179282263086482208903119981\
671229274976649707235429823900190171387464844391690441164936391687051774\
383726627139496012306231401783236358878349778653017133170776393229190077\
739628990310160502280814134853096788081965867629862390376323081486388081\
447209515623489102340)
(-351702876068802906437552420686055850326076457904676962275100830127540142095526131918250547265540344687113337535121385535022681546419262541678542779937822420479756487655377896847799471633977576723782073207520130799030819775724029423428556318476349173343755677252009492874198149433287102465125415887341239164981948271409429616954743899492932166187839273830840430242204133808650932888101185375789831810538994617493023740843690694407922374278683391461155140955728290202668993916913435555785505719850338025423229864154969579884279586929569973510779920100244120646553550505898020495083583179450653843216750029252593017958064901402944177967526951565395241248059756160265233192669848180922694279211718585778306380885162305086669108043433115714019399764705656405532372637424988128302930037221573841204291852109644817711181710035826684632372327454388036464817293856186537192905695504317631243147991171291738007709790188771615389151716498998554084032167552344805530360179417770975752951610003948983037774750319911117156732832021727785395392441490730958689252972967675220988730145127436244943789814598859295627758381613978837709335755249693377694808122420315644792522737484105107310982124866738518101800129697107516438736576977941408274379270293509382653911374966642274279158286587422764882484756865601579730895158614205063551061638491473504793910056083034636703628104249909909358743617446014742435439813694562291652176283751189754664295513611806198927659241385326021014632504226786504648620586888456514119837893571119109533967756125159975652399322500232691192061359137595156810713117303327205420957785613280646524680178422545699670542002396011852068784464293242075746389479364313879352013279031866881866550932388974988195825386061018513865229849909312500792251087107364215523707160192682022566179204513802804002649348052390662029135279718239620685711180813331043126149689112528101979192601149093524782042868789861639973589622434060005735985453157614692625120008989748107127816393893047295612770389853217738467738241538454558869964299842463856929563906378781078448435825750440527227707556191692290825726453524350972864386529150895363907965272416969469402617552438862114293776690558921934623925909303675286193416452595298612780914411142216266018732516721256192081722809558262961748724637965800722153538320807583614908367937680580453094035409535396244827250872802838491507467232684489867657561080066542916543081427459267666550278740733523244436200829487939168383811194688142700374028190883369685908216732077/406778287548421232666331161732867083284451766898265331336067813926311992369988856628099086734855735667115529095643017475187509536025110579820674273512710409816506781413949219095421900994613025586328298832520783087510115995249000462483853695254032218916775883778046276732513287003348820832286627339151659920216118602434782892082868740908234817764021744897598451761291562171363826753518869638462172176786920750397046439419756522146331329906325922062334126886845307227747533166221063380004741504866680087220033007960420359539342564917059907398615028523186317797636660999604624165361705540811548027344347944843318379754195828447139714059075956823953574961456072422821379853471265957417978467907648030343371041680488666213418782854500371449004464628137151693278248611405436919105441634514379785943438321498699491374429974941411916188300692918447066207671860293291735706044903379459163329722215462487004764606673950227898048328401073006835733854423499982247502117755480969561242112088131965551967459900917790184504128348366688399796247714348331657116276519708370377748220107893500318506080007877295044619391333807678344042981270732923845847840661087831919093000529797399599904218182992292585508525349195563753868289669013772157736583348697099384043469144344547635488272911439832922066075313432817036914368704456808129124539273223910518127512694139126508695831281678860513712181333729105725904365496018849941845190976573970615503284681408750206307284620020356423014190813611899820786073110923736507592224098870834184189350561093842374187901309709249334375710994985735557672788610080629408146012015171554449734793702597134820310225203084105776449433119524680976123866181969647041370142480229859354877697883210865623491026410955602205911593318386075749335920440095933187160459299339643209960996075895440125644540573643065630406530215220384502737018981682502335606698154233159446864117915813842827734973170373721996301841763236329935471494386783276795049901144295223363777310171682399431489512674283791187221666388715006803961777072989016656984486829254799709389454885066301520728637334573706302170617918018593936439994981310508468063757045909826757786706357829130980704081337842853100001277249846738997785268785823852018104843245188926381179282263086482208903119981671229274976649707235429823900190171387464844391690441164936391687051774383726627139496012306231401783236358878349778653017133170776393229190077739628990310160502280814134853096788081965867629862390376323081486388081447209515623489102340)
 
       

Symbolic Calculus

var('x, t') f = sqrt(1 - x^2) assume(t+1 > 0) g = integral(f, x, -1, t); show(g) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{4} \, \pi + \frac{1}{2} \, \sqrt{-t^{2} + 1} t + \frac{1}{2} \, \arcsin\left(t\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{4} \, \pi + \frac{1}{2} \, \sqrt{-t^{2} + 1} t + \frac{1}{2} \, \arcsin\left(t\right)
y = function('y',x) show(desolve(diff(y,x)==(x+y)^2,y,contrib_ode=True)) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[x = c - \arctan\left(\sqrt{t}\right), y\left(x\right) = -x - \sqrt{t}\right], \left[x = c + \arctan\left(\sqrt{t}\right), y\left(x\right) = -x + \sqrt{t}\right]\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[x = c - \arctan\left(\sqrt{t}\right), y\left(x\right) = -x - \sqrt{t}\right], \left[x = c + \arctan\left(\sqrt{t}\right), y\left(x\right) = -x + \sqrt{t}\right]\right]

2D Graphics

plot(x*sin(1/x^2),x,.01,.4) 
       
points([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/37)], color=(1,0,0), aspect_ratio=1) 
       
x,y = var('x,y') a=plot_vector_field((x,y), (x,-3,3),(y,-3,3),color='blue') b=plot_vector_field((y,-x),(x,-3,3),(y,-3,3),color='red') show(a+b) 
       

3D Graphics

f(x,y) = sin(x - y) * y * cos(x) plot3d(f, (x,-3,3), (y,-3,3), opacity=.9, color='red') 
       
t6 = Tachyon(camera_center=(0,-4,1), xres = 800, yres = 600, raydepth = 12, aspectratio=.75, antialiasing = True) t6.light((0.02,0.012,0.001), 0.01, (1,0,0)) t6.light((0,0,10), 0.01, (0,0,1)) t6.texture('s', color = (.8,1,1), opacity = .9, specular = .95, diffuse = .3, ambient = 0.05) t6.texture('p', color = (0,0,1), opacity = 1, specular = .2) t6.sphere((-1,-.57735,-0.7071),1,'s') t6.sphere((1,-.57735,-0.7071),1,'s') t6.sphere((0,1.15465,-0.7071),1,'s') t6.sphere((0,0,0.9259),1,'s') t6.plane((0,0,-1.9259),(0,0,1),'p') t6.show() 
       
P1 = Zp(3).plot(rgbcolor=(0,1,0)) P2 = Zp(7).plot(rgbcolor=(1,0,0)) P3 = text("Have a great break!\n\nQuestions?",(0.0,0.0),fontsize=20) (P1+P2+P3).show(axes=False) 
       
search_src("modular polynomial") 
       

Search Source Code: "modular polynomial"

  1. databases/db_modular_polynomials.py
  2. modular/ssmod/ssmod.py

Search Source Code: "modular polynomial"

  1. databases/db_modular_polynomials.py
  2. modular/ssmod/ssmod.py