elso_ea

503 days ago by Gaebor

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.

 

restart 
       

 

 

 

Sage, mint numerikus kalkulátor

Aritmetikai alapműveletek, elemi függvények

1+1 
       
13/5 
       
13/5.n() 
       
sin(1), sqrt(2) 
       
sin(1.0), sqrt(2).n() 
       
sin(pi/3) 
       
_.n(1000) 
       
111^111 
       
13//5, 13%5 
       

 

Indexek

Az indexelés 0-tól indul. 

  • sorozat (1,2,3)
  • lista: [1,2,3]
(3+3*i)^4, (sqrt(3)+i)^6 
       
expand(_[1]) 
       
l = [1, 3, 2, 4] l[1:2] 
       
l = range(5); l 
       
l[1], l[-1], l[:2], l[2:], l[3:1], l[0:-1], l[0:-1:2] 
       
L = [factor(n) for n in range(10000, 10025)] 
       
print L 
       
P = [n for n in range(10000,10100) if is_prime(n)] 
       
       

 

Logikai értékek

3==4 
       
3<>4, 3!=4 
       
3<4 
       

 

Képlettel definiált függvények

f(x)=x^3 
       
f(5) 
       
var('c') f(c) 
       
a=f(c^2-3) 
       
expand(a) 
       

 

 

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

a=3 a=a+1 a=a^2 a 
       
a="ez egy szöveg" 
       
       
print a 
       

 

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

diff(x^2*exp(x),x) 
       
diff(x^2*exp(x),x,10) 
       
integral(9*x^2*exp(3*x^3), x) 
       
diff(y^2*exp(y),y) 
       
var('y') 
       
diff(y^2*exp(y),y) 
       
solve([x+y==3, x-y-4], x, y) 
       
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) 
       
solve(cos(x)==sin(x), x) 
       
find_root(cos(x)==sin(x), 0, 1) 
       
limit(arctan(-x)+exp(-x)*x^1000, x=oo) 
       

 

Feltételes utasítás: if

a = 11 if a%2 == 0: print "páros" else: print "páratlan" 
       

 

Függvények és metódusok

f(x,y) = x^3*exp(x^2)*y^3 
       
f.diff(y) 
       
f.integral(x) 
       
 
       

 

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

def lnko(a,b): if b==0: return abs(a) else: return lnko(b,a%b) 
       
lnko(1111,1111111111) 
       

 

Halmazok

Szinek = ["Treff", "Karo", "Kor", "Pikk"] Ertekek = [2, 3, 4, 5, 6, 7, 8, 9, 10, "Bubi", "Dama", "Kiraly", "Asz"] Kartyak = CartesianProduct(Szinek, Ertekek).map(tuple) 
       
Kartyak.random_element() 
       
Kartyak.cardinality() 
       
Set([Kartyak.random_element(), Kartyak.random_element()]) 
       
Kezek = Subsets(Kartyak, 5) 
       
Kezek.random_element() 
       
Kezek.cardinality() 
       
binomial(52,5) 
       
def par_e(kez): return len(set(ertek for (szin, ertek) in kez)) == 4 
       
kez = Kezek.random_element(); kez 
       
par_e(kez) 
       
 
       

 

 

 

Absztrakt matematikai struktúrák kezelése

Test fölötti polinomgyűrűk

n=2 
       
n.is_unit() 
       
type(n) 
       
polinomQ.<q> = PolynomialRing(QQ) polinomR.<x> = PolynomialRing(RR) polinomC.<z> = PolynomialRing(CC) 
       
factor(q^4+2*q^2+2) 
       
factor(x^4+2*x^2+2) 
       
factor(z^4+2*z^2+2) 
       
factor(x^2-2) 
       
gcd(q^4-3*q^3+2*q^2-q+1,q^2-1) 
       
(q^4-3*q^3+2*q^2-q+1)//(q^2-1) 
       
(q^4-3*q^3+2*q^2-q+1)%(q^2-1) 
       
(q^2-1)*(q^2 - 3*q + 3)-4*q + 4 
       
p = q^4-3*q^3+2*q^2-q+1 
       
p.is_irreducible() 
       
p.xgcd(q^2-1) 
       
-1/4*p+(1/4*q^2 - 3/4*q + 3/4)*(q^2-1) 
       

 

Vektorterek, mátrixok

A = Matrix([[1,2,3],[3,2,1],[1,1,1]]); A 
       
A.LU() 
       
A.rank() 
       
A.nullity() 
       
A.left_kernel() # bal nulltér: xA=0 
       
A.right_kernel() # jobb nulltér: Ax=0 
       
A.row_space() 
       
A.echelon_form() 
       
A.column_space() 
       
B = A.transpose() B.echelon_form() 
       
B.rref() 
       
v = vector([1,1,-4]) 
       
A, v*A, A*v 
       
w=vector([3,5,2]) 
       
A.solve_right(w) 
       
A.solve_left(w) 
       
B=A.change_ring(GF(2)) 
       
B.echelon_form() 
       
B.base_ring() 
       

 

 

 

Grafika

2d grafika

plot(cos, (-5,5)) 
       
plot([cos,sin], (-5,5), aspect_ratio=1) 
       
parametric_plot((cos(x),sin(x)^3),(x,0,2*pi),color='red') 
       
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() 
       
%hide @interact(layout=[['x0'],['eps']]) def _(x0=slider(0, 5, default=1.9, label="x<sub>0</sub>"), eps=slider(0.01, 0.6, default=0.5, label="ε")): f=sin(x)+x/2 pont = point2d((x0,f(x=x0)),rgbcolor=(0.5,0,0),size=60) p = plot(f,(0,5)) maxx = 5 d = diff(f)(x=x0) saveps = polygon2d([[x0,f(x=x0)],[maxx,f(x=x0)+(d+eps)*(maxx-x0)],[maxx,f(x=x0)+(d-eps)*(maxx-x0)]],rgbcolor=(0,1,0),alpha=0.2)+polygon2d([[x0,f(x=x0)],[0,f(x=x0)-(d+eps)*(x0)],[0,f(x=x0)-(d-eps)*(x0)]],rgbcolor=(0,1,0),alpha=0.2) delta=min(5-x0,x0) var('z') while True: try: delta = find_root(f(x=x0)+(z-x0)*(d-eps)==f(x=z),x0+0.001,x0+delta-0.001) - x0 except: break while True: try: delta = find_root(f(x=x0)+(z-x0)*(d+eps)==f(x=z),x0+0.001,x0+delta-0.001) - x0 except: break while True: try: delta = x0 - find_root(f(x=x0)+(z-x0)*(d-eps)==f(x=z),x0-delta+0.001,x0-0.001) except: break while True: try: delta = x0 - find_root(f(x=x0)+(z-x0)*(d+eps)==f(x=z),x0-delta+0.001,x0-0.001) except: break savdel = polygon2d([[x0-delta,-0.2],[x0-delta,3],[x0+delta,3],[x0+delta,-0.2]],rgbcolor=(1,0,0),alpha=0.2) # t = text('x0+delta',(x0+delta,-0.1),rgbcolor=(0,0,0)) # t2 = text('x0-delta',(x0-delta,-0.2),rgbcolor=(0,0,0)) l=line([[x0,-0.2],[x0,3]],rgbcolor=(1,0,0),alpha=0.2,thickness=2) l2=line([[0,f(x=x0)-x0*d],[maxx,f(x=x0)+(maxx-x0)*d]],rgbcolor=(0,1,0),alpha=0.2,thickness=2) show(pont+p+saveps+savdel+l+l2,aspect_ratio=1,xmin=-0.2,ymin=-0.2,ymax=3) 
       

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

var('x,y') plot3d(x^2-y^2,(x,-4,4),(y,-4,4)) 
       
u, v = var('u,v') fx = cos(v)*(3+cos(u)) fy = sin(v)*(3+cos(u)) fz = sin(u) parametric_plot3d([fx, fy, fz], (u, 0, 2*pi), (v, 0, 2*pi),frame=False, aspect_ratio=1, color="red")