Matematikai szoftverek– sage

A Sage hosszútávú céljai

  • 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.

Sage, mint numerikus kalkulátor

Aritmetikai alapműveletek, elemi függvények

In [1]:
1+1
Out[1]:
2
In [2]:
13/5
Out[2]:
13/5
In [3]:
13/5.n()
Out[3]:
2.60000000000000
In [4]:
sin(1), sqrt(2)
Out[4]:
(sin(1), sqrt(2))
In [5]:
sin(1.0), sqrt(2).n()
Out[5]:
(0.841470984807897, 1.41421356237310)
In [6]:
sin(pi/3)
Out[6]:
1/2*sqrt(3)
In [7]:
_.n(1000)
Out[7]:
0.866025403784438646763723170752936183471402626905190314027903489725966508454400018540573093378624287837813070707703351514984972547499476239405827756047186824264046615951152791033987410050542337461632507656171633451661443325336127334460918985613523565830183930794009524993268689929694733825173753288025
In [8]:
111^111
Out[8]:
107362012888474225801214565046695501959850723994224804804775911175625076195783347022491226170093634621466103743092986967777786330067310159463303558666910091026017785587295539622142057315437069730229375357546494103400699864397711
In [9]:
13//5, 13%5
Out[9]:
(2, 3)

Indexek

Az indexelés 0-tól indul.

  • sorozat (1,2,3)
  • lista: [1,2,3]
In [10]:
(3+3*i)^4, (sqrt(3)+i)^6
Out[10]:
(-324, (sqrt(3) + I)^6)
In [11]:
expand(_[1])
Out[11]:
-64
In [12]:
l = [1, 3, 2, 4]
l[1:2]
Out[12]:
[3]
In [13]:
l = range(5); l
Out[13]:
[0, 1, 2, 3, 4]
In [14]:
l[1], l[-1], l[:2], l[2:], l[3:1], l[0:-1], l[0:-1:2]
Out[14]:
(1, 4, [0, 1], [2, 3, 4], [], [0, 1, 2, 3], [0, 2])
In [15]:
L = [factor(n) for n in range(10000, 10025)]
In [16]:
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]
In [17]:
P = [n for n in range(10000,10100) if is_prime(n)]
In [18]:
P
Out[18]:
[10007, 10009, 10037, 10039, 10061, 10067, 10069, 10079, 10091, 10093, 10099]

Logikai értékek

In [19]:
3==4
Out[19]:
False
In [20]:
3<>4, 3!=4
Out[20]:
(True, True)
In [21]:
3<4
Out[21]:
True

Képlettel definiált függvények

In [22]:
f(x)=x^3
In [23]:
f(5)
Out[23]:
125
In [24]:
var('c')
f(c)
Out[24]:
c^3
In [25]:
a=f(c^2-3)
In [26]:
expand(a)
Out[26]:
c^6 - 9*c^4 + 27*c^2 - 27

Sage, mint programnyelv

A Python nyelvre épül.

Változók

Változók típuskezelése dinamikus (nem statikus: nem kell deklarálni a típusukat, szabadon változtathatóak).

In [27]:
a=3
a=a+1
a=a^2
a
Out[27]:
16
In [28]:
a="ez egy szöveg"
In [29]:
a
Out[29]:
'ez egy sz\xc3\xb6veg'
In [30]:
print a
ez egy szöveg

Szimbólikus változó

Ellentétben a klasszikus programnyelvekkel (C, Python, Fortran,...), de hasonlóan a többi CAS-hez (computer algebra system) a sage-ben is lehet egy változó szimbólikus. Sage-ben induláskor az x változó szimbólikus, és a var függvénnyel bármely változó azzá tehetÅ‘.

In [31]:
diff(x^2*exp(x),x)
Out[31]:
x^2*e^x + 2*x*e^x
In [32]:
diff(x^2*exp(x),x,10)
Out[32]:
x^2*e^x + 20*x*e^x + 90*e^x
In [33]:
integral(9*x^2*exp(3*x^3), x)
Out[33]:
e^(3*x^3)
In [34]:
diff(y^2*exp(y),y)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-34-dde5799a5bda> in <module>()
----> 1 diff(y**Integer(2)*exp(y),y)

NameError: name 'y' is not defined
In [35]:
var('y')
Out[35]:
y
In [36]:
diff(y^2*exp(y),y)
Out[36]:
y^2*e^y + 2*y*e^y
In [37]:
solve([x+y==3, x-y-4], x, y)
Out[37]:
[[x == (7/2), y == (-1/2)]]
In [38]:
var('x y 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[38]:
[[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 [39]:
solve(cos(x)==sin(x), x)
Out[39]:
[sin(x) == cos(x)]
In [40]:
find_root(cos(x)==sin(x), 0, 1)
Out[40]:
0.7853981633974483
In [41]:
limit(arctan(-x)+exp(-x)*x^1000, x=oo)
Out[41]:
-1/2*pi

Feltételes utasítás: if

In [42]:
a = 11
if a%2 == 0:
    print "páros"
else:
    print "páratlan"
páratlan

Függvények és metódusok

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

Saját függvény írása

In [46]:
def lnko(a,b):
    if b==0:
        return abs(a)
    else:
        return lnko(b,a%b)
In [47]:
lnko(1111,1111111111)
Out[47]:
11

Halmazok

In [48]:
Szinek  = ["Treff", "Karo", "Kor", "Pikk"]
Ertekek = [2, 3, 4, 5, 6, 7, 8, 9, 10, "Bubi", "Dama", "Kiraly", "Asz"]
Kartyak = cartesian_product([Szinek, Ertekek])
In [49]:
Kartyak.random_element()
Out[49]:
('Pikk', 4)
In [50]:
Kartyak.cardinality()
Out[50]:
52
In [51]:
Set([Kartyak.random_element(), Kartyak.random_element()])
Out[51]:
{('Kor', 2), ('Treff', 'Asz')}
In [52]:
Kezek = Subsets(Kartyak, 5)
In [53]:
Kezek.random_element()
Out[53]:
{('Kor', 8), ('Pikk', 6), ('Kor', 'Bubi'), ('Kor', 'Dama'), ('Kor', 2)}
In [54]:
Kezek.cardinality()
Out[54]:
2598960
In [55]:
binomial(52,5)
Out[55]:
2598960
In [56]:
def par_e(kez):
    return len(set(ertek for (szin, ertek) in kez)) == 4
In [57]:
kez = Kezek.random_element(); kez
Out[57]:
{('Karo', 5), ('Kor', 'Kiraly'), ('Kor', 3), ('Pikk', 2), ('Treff', 10)}
In [58]:
par_e(kez)
Out[58]:
False
In [ ]:
 

Absztrakt matematikai struktúrák kezelése

Test fölötti polinomgyűrűk

In [59]:
n=2
In [60]:
n.is_unit()
Out[60]:
False
In [61]:
type(n)
Out[61]:
<type 'sage.rings.integer.Integer'>
In [62]:
polinomQ.<q> = PolynomialRing(QQ)
polinomR.<x> = PolynomialRing(RR)
polinomC.<z> = PolynomialRing(CC)
In [63]:
factor(q^4+2*q^2+2)
Out[63]:
q^4 + 2*q^2 + 2
In [64]:
factor(x^4+2*x^2+2)
Out[64]:
(x^2 - 0.910179721124455*x + 1.41421356237310) * (x^2 + 0.910179721124455*x + 1.41421356237310)
In [65]:
factor(z^4+2*z^2+2)
Out[65]:
(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 [66]:
factor(x^2-2)
Out[66]:
(x - 1.41421356237310) * (x + 1.41421356237310)
In [67]:
gcd(q^4-3*q^3+2*q^2-q+1,q^2-1)
Out[67]:
q - 1
In [68]:
(q^4-3*q^3+2*q^2-q+1)//(q^2-1)
Out[68]:
q^2 - 3*q + 3
In [69]:
(q^4-3*q^3+2*q^2-q+1)%(q^2-1)
Out[69]:
-4*q + 4
In [70]:
(q^2-1)*(q^2 - 3*q + 3)-4*q + 4
Out[70]:
q^4 - 3*q^3 + 2*q^2 - q + 1
In [71]:
p = q^4-3*q^3+2*q^2-q+1
In [72]:
p.is_irreducible()
Out[72]:
False
In [73]:
p.xgcd(q^2-1)
Out[73]:
(q - 1, -1/4, 1/4*q^2 - 3/4*q + 3/4)
In [74]:
-1/4*p+(1/4*q^2 - 3/4*q + 3/4)*(q^2-1)
Out[74]:
q - 1

Vektorterek, mátrixok

In [75]:
A = Matrix([[1,2,3],[3,2,1],[1,1,1]]); A
Out[75]:
[1 2 3]
[3 2 1]
[1 1 1]
In [76]:
A.LU()
Out[76]:
(
[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 [77]:
A.rank()
Out[77]:
2
In [78]:
A.nullity()
Out[78]:
1
In [79]:
A.left_kernel()  # bal nulltér: xA=0
Out[79]:
Free module of degree 3 and rank 1 over Integer Ring
Echelon basis matrix:
[ 1  1 -4]
In [80]:
A.right_kernel()  # jobb nulltér: Ax=0
Out[80]:
Free module of degree 3 and rank 1 over Integer Ring
Echelon basis matrix:
[ 1 -2  1]
In [81]:
A.row_space()
Out[81]:
Free module of degree 3 and rank 2 over Integer Ring
Echelon basis matrix:
[ 1  0 -1]
[ 0  1  2]
In [82]:
A.echelon_form()
Out[82]:
[ 1  0 -1]
[ 0  1  2]
[ 0  0  0]
In [83]:
A.column_space()
Out[83]:
Free module of degree 3 and rank 2 over Integer Ring
Echelon basis matrix:
[1 3 1]
[0 4 1]
In [84]:
B = A.transpose()
B.echelon_form()
Out[84]:
[1 3 1]
[0 4 1]
[0 0 0]
In [85]:
B.rref()
Out[85]:
[  1   0 1/4]
[  0   1 1/4]
[  0   0   0]
In [86]:
v = vector([1,1,-4])
In [87]:
A, v*A, A*v
Out[87]:
(
[1 2 3]                        
[3 2 1]                        
[1 1 1], (0, 0, 0), (-9, 1, -2)
)
In [88]:
w=vector([3,5,2])
In [89]:
A.solve_right(w)
Out[89]:
(1, 1, 0)
In [90]:
A.solve_left(w)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-90-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 [91]:
B=A.change_ring(GF(2))
In [92]:
B.echelon_form()
Out[92]:
[1 0 1]
[0 1 0]
[0 0 0]
In [93]:
B.base_ring()
Out[93]:
Finite Field of size 2

Grafika

2d grafika

In [94]:
plot(cos, (-5,5))
Out[94]:
In [95]:
plot([cos,sin], (-5,5), aspect_ratio=1)
Out[95]:
In [96]:
parametric_plot((cos(x),sin(x)^3),(x,0,2*pi),color='red')
Out[96]:
In [97]:
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()