Mathematical softwares – sage (Software for Arithmetic Geometry Experimentation)

Misson: a free alternative of Magma, Maple, Mathematica and Matlab.

Long term goals

  • Useful: Sage’s intended audience is mathematics students (from high school to graduate school), teachers, and research mathematicians. The aim is to provide software that can be used to explore and experiment with mathematical constructions in algebra, geometry, number theory, calculus, numerical computation, etc. Sage helps make it easier to interactively experiment with mathematical objects.
  • Efficient:Be fast. Sage uses highly-optimized mature software like GMP, PARI, GAP, and NTL, and so is very fast at certain operations.
  • Free and open source:The source code must be freely available and readable, so users can understand what the system is really doing and more easily extend it. Just as mathematicians gain a deeper understanding of a theorem by carefully reading or at least skimming the proof, people who do computations should be able to understand how the calculations work by reading documented source code. If you use Sage to do computations in a paper you publish, you can rest assured that your readers will always have free access to Sage and all its source code, and you are even allowed to archive and re-distribute the version of Sage you used.
  • Easy to compile:Sage should be easy to compile from source for Linux, OS X and Windows users. This provides more flexibility for users to modify the system.
  • Cooperation:Provide robust interfaces to most other computer algebra systems, including PARI, GAP, Singular, Maxima, KASH, Magma, Maple, and Mathematica. Sage is meant to unify and extend existing math software.
  • Well documented:Tutorial, programming guide, reference manual, and how-to, with numerous examples and discussion of background mathematics.
  • Extensible:Be able to define new data types or derive from built-in types, and use code written in a range of languages.
  • User friendly: It should be easy to understand what functionality is provided for a given object and to view documentation and source code. Also attain a high level of user support.

Source

Sage, numerical calculator

Arithmetic operations, elementry functions

In [1]:
1 + 1
Out[1]:
2

Sage can caluclate with rational numbers. It switches to floating point numbers with the n() method.

In [2]:
13/5
Out[2]:
13/5
In [3]:
n(13/5)
Out[3]:
2.60000000000000
In [4]:
sin(1), sqrt(2)
Out[4]:
(sin(1), sqrt(2))
In [5]:
sqrt(2).n(), sqrt(2).n(2000)   # n() can be used as a method
Out[5]:
(1.41421356237310,
 1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572735013846230912297024924836055850737212644121497099935831413222665927505592755799950501152782060571470109559971605970274534596862014728517418640889198609552329230484308714321450839762603627995251407989687253396546331808829640620615258352395054745750287759961729835575220337531857011354374603408498847160386899970699004815030544027790316454247823068492936918621580578463111596668713013015618568987237235288509264861249497715421833420428568606014682472077143585487415565706967765372022648544701585880162)
In [6]:
sin(1.0)
Out[6]:
0.841470984807897
In [7]:
sin(pi/3)
Out[7]:
1/2*sqrt(3)
In [8]:
show(sin(pi/3))
In [9]:
_.n(1000)      # _ the previous result
Out[9]:
0.866025403784438646763723170752936183471402626905190314027903489725966508454400018540573093378624287837813070707703351514984972547499476239405827756047186824264046615951152791033987410050542337461632507656171633451661443325336127334460918985613523565830183930794009524993268689929694733825173753288025
In [10]:
111^111
Out[10]:
107362012888474225801214565046695501959850723994224804804775911175625076195783347022491226170093634621466103743092986967777786330067310159463303558666910091026017785587295539622142057315437069730229375357546494103400699864397711
In [11]:
13//5, 13%5
Out[11]:
(2, 3)
In [12]:
64^(1/6)   # root
Out[12]:
2
In [13]:
factor(24)    # text completeion with TAB, try it with writing only fact and pressing TAB
Out[13]:
2^3 * 3
In [14]:
exp(2), e^2
Out[14]:
(e^2, e^2)
In [15]:
e^2.n()
Out[15]:
7.38905609893065
In [16]:
log(e), log(100), log(100.), log(100,10), log(1024,2)
Out[16]:
(1, 2*log(10), 4.60517018598809, 2, 10)
In [17]:
arcsin(1/2)
Out[17]:
1/6*pi
In [18]:
apple = 10^10           # creating a variable
apple%3
Out[18]:
1

A question symbol ? after a command shows detailed help.

Lists, indices

Indexing startsfrom 0.

  • series (1,2,3)
  • list: [1,2,3]
In [19]:
((3 + 3*I)^4, (sqrt(3) + i)^6)        # a series with 2 elements, normal parenthesis can omitted
Out[19]:
(-324, (sqrt(3) + I)^6)
In [20]:
expand(_[0])     # this expands the second expression
Out[20]:
-324
In [21]:
l = [1, 3, 2, 4]
l[1:3]
Out[21]:
[3, 2]
In [22]:
l = range(7); l    # with ; we can write multiple commands in a line
Out[22]:
[0, 1, 2, 3, 4, 5, 6]

list[c:d] from element c to d, d is not included
list[c:d:e] from element c to d with e steps

In [23]:
l[1], l[-1], l[:2], l[4:], l[3:1], l[0:-1], l[0:-1:2]
Out[23]:
(1, 6, [0, 1], [4, 5, 6], [], [0, 1, 2, 3, 4, 5], [0, 2, 4])

List comprehension

[expression for variable in something]

Similar to how we define sets in mathematics. Example: $\{ f(x) \mid x\in H\}$.
(In sage lists and sets are different, in a set the order of its elements aren't given.)

In [24]:
L = [factor(n) for n in range(10000, 10025)]
In [25]:
print L
[2^4 * 5^4, 73 * 137, 2 * 3 * 1667, 7 * 1429, 2^2 * 41 * 61, 3 * 5 * 23 * 29, 2 * 5003, 10007, 2^3 * 3^2 * 139, 10009, 2 * 5 * 7 * 11 * 13, 3 * 47 * 71, 2^2 * 2503, 17 * 19 * 31, 2 * 3 * 1669, 5 * 2003, 2^5 * 313, 3^3 * 7 * 53, 2 * 5009, 43 * 233, 2^2 * 3 * 5 * 167, 11 * 911, 2 * 5011, 3 * 13 * 257, 2^3 * 7 * 179]

List comprehension with a condition

[expression for variable in something if condition]

Mathematically: $\{ f(x) \mid x\in H \text{ such that } g(x)\}$.

In [26]:
P = [n for n in range(10000,10100) if is_prime(n)]
print P
[10007, 10009, 10037, 10039, 10061, 10067, 10069, 10079, 10091, 10093, 10099]

Logic values

In [27]:
3 == 4
Out[27]:
False
In [28]:
3 <> 4, 3 != 4
Out[28]:
(True, True)
In [29]:
3 < 4
Out[29]:
True

Functions defined by a mathematical expression

In [30]:
var('x')
f(x) = x^3
In [31]:
f(3)
Out[31]:
27
In [32]:
var('c')
f(c)
Out[32]:
c^3
In [33]:
a = f(c^2-3)
In [34]:
expand(a)
Out[34]:
c^6 - 9*c^4 + 27*c^2 - 27
In [35]:
a
Out[35]:
(c^2 - 3)^3

Sage, as a programming language

The basis of sage is the pythong programming language.

Variables

Variable types are dynamic (as opposed to static: we do not need to define the type of the variables, their types can freely change).

In [36]:
a = 3
a = a+1
a = a^2
a
Out[36]:
16
In [37]:
a = "this is a text"
In [38]:
a
Out[38]:
'this is a text'

Symbolic variables, sage, as a symbolic calculator

As opposed to usual programming languages (C, Python, Fortran,...), but similar to other CAS (computer algebra system) a variable can be symbolic in sage. By default the variable x is symbolic, using the var function any variable can be made symbolic.

In [39]:
show(x^2*exp(x))
show(diff(x^2*exp(x), x))
In [40]:
diff(x^2*exp(x), x, 10)
Out[40]:
x^2*e^x + 20*x*e^x + 90*e^x
In [41]:
integral(9*x^2*exp(3*x^3), x)
Out[41]:
e^(3*x^3)
In [42]:
diff(y^2*exp(y), y)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-42-e5ce4bdc70dd> in <module>()
----> 1 diff(y**Integer(2)*exp(y), y)

NameError: name 'y' is not defined
In [43]:
var('y')
Out[43]:
y
In [44]:
diff(y^2*exp(y), y, 10)
Out[44]:
y^2*e^y + 20*y*e^y + 90*e^y
In [45]:
solve([x+y==3], x)
Out[45]:
[x == -y + 3]
In [46]:
var('x y z p q')
eq1 = p+q==5
eq2 = p*x+q*y==-5
eq3 = p*x^2+q*y^2==10
solve([eq1,eq2,eq3], q, x, y)
Out[46]:
[[q == -p + 5, x == -(p - sqrt(-p^2 + 5*p))/p, y == -(p - sqrt(-p^2 + 5*p) - 5)/(p - 5)], [q == -p + 5, x == -(p + sqrt(-p^2 + 5*p))/p, y == -(p + sqrt(-p^2 + 5*p) - 5)/(p - 5)]]
In [47]:
solve( [2*x + 3*y + 2*z == -1, x + y + z == 0], x, y )
Out[47]:
[[x == -z + 1, y == -1]]
In [48]:
solve( [2*x + 3*y + 2*z == -1, x + y + z == 0], x, y, z )
Out[48]:
[[x == -r1 + 1, y == -1, z == r1]]
In [49]:
solve(cos(x)==sin(x), x)
Out[49]:
[sin(x) == cos(x)]
In [50]:
find_root(cos(x)==sin(x), 0, 10)
Out[50]:
0.7853981633973393
In [51]:
limit(arctan(-x) + exp(-x)*x^1000, x=oo)
Out[51]:
-1/2*pi

Conditional command: if

In [52]:
a = 13
if a%2 == 0:
    print "even"
else:
    print "odd"
odd

Functions and methods

In [53]:
f(x,y) = x^3*exp(x^2)*y^3
In [54]:
f.diff(y)
Out[54]:
(x, y) |--> 3*x^3*y^2*e^(x^2)
In [55]:
f.integral(x)
Out[55]:
(x, y) |--> 1/2*(x^2 - 1)*y^3*e^(x^2)

Writing a function (programming function)

In [56]:
def gcd(a,b):
    if b==0:
        return abs(a)
    else:
        return gcd(b,a%b)
In [57]:
gcd(1111,1111111111)
Out[57]:
11

Sets

In [58]:
Colors  = ["Spades", "Diamond", "Hearts", "Clubs"]
Values = [2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King", "Ace"]
Cards = Set(Colors).cartesian_product(Set(Values))    #.map(tuple)
In [59]:
Cards.random_element()
Out[59]:
('Clubs', 4)
In [60]:
Cards.cardinality()
Out[60]:
52
In [61]:
Hands = Subsets(Cards, 5)
In [62]:
Hands.random_element()
Out[62]:
{('Hearts', 5), ('Clubs', 6), ('Diamond', 4), ('Hearts', 7), ('Diamond', 5)}
In [63]:
Hands.cardinality()
Out[63]:
2598960
In [64]:
binomial(52,5)
Out[64]:
2598960
In [65]:
def is_it_a_pair(hand):
    return len(set(value for (color, value) in hand)) == 4
In [66]:
hand = Hands.random_element(); hand
Out[66]:
{('Hearts', 10), ('Hearts', 'Jack'), ('Spades', 'King'), ('Hearts', 'Ace'), ('Diamond', 3)}
In [67]:
is_it_a_pair(hand)
Out[67]:
False

Abstract mathematical structures

Polynomial rings over a given field

In [68]:
n = -1.1
In [69]:
n.is_unit()
Out[69]:
True
In [70]:
type(n)
Out[70]:
<type 'sage.rings.real_mpfr.RealLiteral'>
In [71]:
polinomQ.<q> = PolynomialRing(QQ)
polinomR.<x> = PolynomialRing(RR)
polinomC.<z> = PolynomialRing(CC)
In [72]:
factor(q^4 + 2*q^2 + 2)
Out[72]:
q^4 + 2*q^2 + 2
In [73]:
factor(x^4 + 2*x^2 + 2)
Out[73]:
(x^2 - 0.910179721124455*x + 1.41421356237310) * (x^2 + 0.910179721124455*x + 1.41421356237310)
In [74]:
factor(z^4 + 2*z^2 + 2)
Out[74]:
(z - 0.455089860562227 - 1.09868411346781*I) * (z - 0.455089860562227 + 1.09868411346781*I) * (z + 0.455089860562227 - 1.09868411346781*I) * (z + 0.455089860562227 + 1.09868411346781*I)
In [75]:
factor(x^2 - 2)
Out[75]:
(x - 1.41421356237310) * (x + 1.41421356237310)
In [76]:
(q^4 - 3*q^3 + 2*q^2 - q + 1) // (q^2 - 1)
Out[76]:
q^2 - 3*q + 3
In [77]:
(q^4 - 3*q^3 + 2*q^2 - q + 1) % (q^2 - 1)
Out[77]:
-4*q + 4
In [78]:
(q^2 - 1)*(q^2 - 3*q + 3) - 4*q + 4
Out[78]:
q^4 - 3*q^3 + 2*q^2 - q + 1
In [79]:
p = q^4 - 3*q^3 + 2*q^2 - q + 1
In [80]:
p.is_irreducible()
Out[80]:
False
In [81]:
p.xgcd(q^2-1)
Out[81]:
(q - 1, -1/4, 1/4*q^2 - 3/4*q + 3/4)
In [82]:
-1/4*p + (1/4*q^2 - 3/4*q + 3/4) * (q^2 - 1)
Out[82]:
q - 1

Vector spaces, matrices

In [83]:
A = Matrix([[1,2,3],[3,2,1],[1,1,1]]); A
Out[83]:
[1 2 3]
[3 2 1]
[1 1 1]
In [84]:
A.LU()
Out[84]:
(
[0 1 0]  [  1   0   0]  [  3   2   1]
[1 0 0]  [1/3   1   0]  [  0 4/3 8/3]
[0 0 1], [1/3 1/4   1], [  0   0   0]
)
In [85]:
P, L, U = A.LU()
In [86]:
P*L*U
Out[86]:
[1 2 3]
[3 2 1]
[1 1 1]
In [87]:
A.rank()
Out[87]:
2
In [88]:
A.nullity()
Out[88]:
1
In [89]:
A.left_kernel()  # left zerospace: xA=0
Out[89]:
Free module of degree 3 and rank 1 over Integer Ring
Echelon basis matrix:
[ 1  1 -4]
In [90]:
A.right_kernel()  # right zerospace: Ax=0
Out[90]:
Free module of degree 3 and rank 1 over Integer Ring
Echelon basis matrix:
[ 1 -2  1]
In [91]:
A.row_space()
Out[91]:
Free module of degree 3 and rank 2 over Integer Ring
Echelon basis matrix:
[ 1  0 -1]
[ 0  1  2]
In [92]:
A.echelon_form()
Out[92]:
[ 1  0 -1]
[ 0  1  2]
[ 0  0  0]
In [93]:
A.column_space()
Out[93]:
Free module of degree 3 and rank 2 over Integer Ring
Echelon basis matrix:
[1 3 1]
[0 4 1]
In [94]:
B = A.transpose()
B.echelon_form()
Out[94]:
[1 3 1]
[0 4 1]
[0 0 0]
In [95]:
B.rref()
Out[95]:
[  1   0 1/4]
[  0   1 1/4]
[  0   0   0]
In [96]:
v = vector([1,1,-4])
v
Out[96]:
(1, 1, -4)
In [97]:
A, v*A, A*v
Out[97]:
(
[1 2 3]                        
[3 2 1]                        
[1 1 1], (0, 0, 0), (-9, 1, -2)
)
In [98]:
w = vector([3,5,2])
In [99]:
A.solve_right(w)
Out[99]:
(1, 1, 0)
In [100]:
A \ w
Out[100]:
(1, 1, 0)
In [101]:
A
Out[101]:
[1 2 3]
[3 2 1]
[1 1 1]
In [102]:
A.solve_left(w)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-102-5b7cf759f46e> in <module>()
----> 1 A.solve_left(w)

/opt/SageMath/local/lib/python2.7/site-packages/sage/matrix/matrix2.pyx in sage.matrix.matrix2.Matrix.solve_left (build/cythonized/sage/matrix/matrix2.c:6066)()
    223                 return self.transpose().solve_right(B, check=check)
    224             except ValueError as e:
--> 225                 raise ValueError(str(e).replace('row', 'column'))
    226         else:
    227             try:

ValueError: matrix equation has no solutions
In [103]:
B = A.change_ring(GF(2))
In [104]:
B.echelon_form()
Out[104]:
[1 0 1]
[0 1 0]
[0 0 0]
In [105]:
B.base_ring()
Out[105]:
Finite Field of size 2

Graphics

2d graphics

In [106]:
plot(cos, (-5,5))
Out[106]:
In [107]:
plot([cos,sin], (-5,5), aspect_ratio=0.5)
Out[107]:
In [108]:
parametric_plot((cos(x),sin(x)^3),(x,0,2*pi),color='red')
Out[108]:
In [109]:
var('x')
p  = plot(2*x^4-2*x^3+3*x^2-3*x+4, (x,-1,1), color = 'cyan', thickness=3)
p += plot(2*x^4, (x,-1,1), color = 'magenta', thickness=3)
p.show()