SharedZáklady_práce.sagewsOpen in CoCalc
Author: Roman Plch
Views : 4
Description: Základy práce v systému Sage
%typeset_mode True
%typeset_mode True
Komentář x2+y2x^2+y^2.
1+1;#komentar
2
#Příkaz provedeme klávesou Shift+Enter, odřádkování Enter - změnu provedeme v nastavení účtu
2\displaystyle 2
1+1;2+2 #Více příkazů na jednom řádku oddělujeme středníkem (;)
2 4
factor? #Nápověda
/ext/sage/sage-8.0/local/lib/python2.7/site-packages/urllib3/contrib/pyopenssl.py:46: DeprecationWarning: OpenSSL.rand is deprecated - you should use os.urandom instead import OpenSSL.SSL
File: /ext/sage/sage-8.0/local/lib/python2.7/site-packages/sage/arith/misc.py Signature : factor(n, proof=None, int_=False, algorithm='pari', verbose=0, **kwds) Docstring : Returns the factorization of "n". The result depends on the type of "n". If "n" is an integer, returns the factorization as an object of type "Factorization". If n is not an integer, "n.factor(proof=proof, **kwds)" gets called. See "n.factor??" for more documentation in this case. Warning: This means that applying "factor" to an integer result of a symbolic computation will not factor the integer, because it is considered as an element of a larger symbolic ring.EXAMPLES: sage: f(n)=n^2 sage: is_prime(f(3)) False sage: factor(f(3)) 9 INPUT: * "n" - an nonzero integer * "proof" - bool or None (default: None) * "int_" - bool (default: False) whether to return answers as Python ints * "algorithm" - string * "'pari'" - (default) use the PARI c library * "'kash'" - use KASH computer algebra system (requires the optional kash package be installed) * "'magma'" - use Magma (requires magma be installed) * "verbose" - integer (default: 0); PARI's debug variable is set to this; e.g., set to 4 or 8 to see lots of output during factorization. OUTPUT: * factorization of n The qsieve and ecm commands give access to highly optimized implementations of algorithms for doing certain integer factorization problems. These implementations are not used by the generic factor command, which currently just calls PARI (note that PARI also implements sieve and ecm algorithms, but they aren't as optimized). Thus you might consider using them instead for certain numbers. The factorization returned is an element of the class "Factorization"; see Factorization?? for more details, and examples below for usage. A Factorization contains both the unit factor (+1 or -1) and a sorted list of (prime, exponent) pairs. The factorization displays in pretty-print format but it is easy to obtain access to the (prime,exponent) pairs and the unit, to recover the number from its factorization, and even to multiply two factorizations. See examples below. EXAMPLES: sage: factor(500) 2^2 * 5^3 sage: factor(-20) -1 * 2^2 * 5 sage: f=factor(-20) sage: list(f) [(2, 2), (5, 1)] sage: f.unit() -1 sage: f.value() -20 sage: factor( -next_prime(10^2) * next_prime(10^7) ) -1 * 101 * 10000019 sage: factor(-500, algorithm='kash') # optional - kash -1 * 2^2 * 5^3 sage: factor(-500, algorithm='magma') # optional - magma -1 * 2^2 * 5^3 sage: factor(0) Traceback (most recent call last): ... ArithmeticError: factorization of 0 is not defined sage: factor(1) 1 sage: factor(-1) -1 sage: factor(2^(2^7)+1) 59649589127497217 * 5704689200685129054721 Sage calls PARI's factor, which has proof False by default. Sage has a global proof flag, set to True by default (see "sage.structure.proof.proof", or proof.[tab]). To override the default, call this function with proof=False. sage: factor(3^89-1, proof=False) 2 * 179 * 1611479891519807 * 5042939439565996049162197 sage: factor(2^197 + 1) # long time (2s) 3 * 197002597249 * 1348959352853811313 * 251951573867253012259144010843 Any object which has a factor method can be factored like this: sage: K.<i> = QuadraticField(-1) sage: factor(122 - 454*i) (-3*i - 2) * (-i - 2)^3 * (i + 1)^3 * (i + 4) To access the data in a factorization: sage: f = factor(420); f 2^2 * 3 * 5 * 7 sage: [x for x in f] [(2, 2), (3, 1), (5, 1), (7, 1)] sage: [p for p,e in f] [2, 3, 5, 7] sage: [e for p,e in f] [2, 1, 1, 1] sage: [p^e for p,e in f] [4, 3, 5, 7]

Sage má automatické doplňování příkazů. Zkuste napsat do následujícího políčka text fac a stisknout tabelátor. Ukážou se všechny příkazy začínající těmito slovy. Vyberte si kterýkoliv z nich, připište za něj otazník  a stiskněte opět tabelátor. Objeví se nápověda k tomutu příkazu.

factor v1=factorial(20)-factorial(12);v1
<function factor at 0x7f906af03e60>
2432902007697638400\displaystyle 2432902007697638400
factor(v1);
2103552711341976119\displaystyle 2^{10} \cdot 3^{5} \cdot 5^{2} \cdot 7 \cdot 11^{3} \cdot 41976119
v1.next_prime()
2432902007697638557\displaystyle 2432902007697638557
(2^30/3^20)*sqrt(2) #Sage pracuje v přesné aritmetice.
107374182434867844012\displaystyle \frac{1073741824}{3486784401} \, \sqrt{2}
(2^30/3^20)*sqrt(2).n()
0.435501618497698\displaystyle 0.435501618497698
N((2^30/3^20)*sqrt(2))
0.435501618497698\displaystyle 0.435501618497698
var('i') #Narozdíl od Maplu a Maximy je třeba proměnné (kromě proměnné x) předem deklarovat.
i\displaystyle i
sum((1+i)/(1+i^4), i,1,20)
2753108915513951266418473494479966526667515770634998143458420014321653762264974190884124027285387789492160976952723410943046016049\displaystyle \frac{27531089155139512664184734944799665266675157706349981434584200143}{21653762264974190884124027285387789492160976952723410943046016049}
N(sum((1+i)/(1+i^4), i,1,20), digits=30)
1.27142289724276359903349287003\displaystyle 1.27142289724276359903349287003
sum((1+i)/(1+i^4), i,1,20).n(digits=30)
1.27142289724276359903349287003\displaystyle 1.27142289724276359903349287003
var('j')
j\displaystyle j
sum((1+j)/(1+j^4), j,1,20)
2753108915513951266418473494479966526667515770634998143458420014321653762264974190884124027285387789492160976952723410943046016049\displaystyle \frac{27531089155139512664184734944799665266675157706349981434584200143}{21653762264974190884124027285387789492160976952723410943046016049}
N(pi);n(pi)
3.14159265358979\displaystyle 3.14159265358979
3.14159265358979\displaystyle 3.14159265358979
((3+5*I)*(7+4*I)); #Komplexní jednotku zadáváme pomocí I.
47i+1\displaystyle 47 i + 1
var('y')
y\displaystyle y
(x+y)^3*(x-y)^2
(x+y)3(xy)2\displaystyle {\left(x + y\right)}^{3} {\left(x - y\right)}^{2}
((x+y)^3*(x-y)^2).expand() #roznásobení závorek
x5+x4y2x3y22x2y3+xy4+y5\displaystyle x^{5} + x^{4} y - 2 \, x^{3} y^{2} - 2 \, x^{2} y^{3} + x y^{4} + y^{5}
((x+y)^3*(x-y)^2).expand().factor() #rozklad na součin
(x+y)3(xy)2\displaystyle {\left(x + y\right)}^{3} {\left(x - y\right)}^{2}
simplify(x/2+x/5) #zjednoduseni vyrazu
710x\displaystyle \frac{7}{10} \, x

Uložení výrazu do proměnné.

f=(x^3-x)/(x^4-1);f
x3xx41\displaystyle \frac{x^{3} - x}{x^{4} - 1}
f.simplify_full()
xx2+1\displaystyle \frac{x}{x^{2} + 1}
vyraz=sin(x)^2+cos(x)^2;vyraz.simplify_trig()
1\displaystyle 1
(cos(x)^2).trig_reduce()
12cos(2x)+12\displaystyle \frac{1}{2} \, \cos\left(2 \, x\right) + \frac{1}{2}

Jedno znaméno = se v programu Sage používá pro přiřazení hodnoty do proměnné. Pro rovnice se používají dvě rovnítka za sebou.

%typeset_mode True
x^2-7==0 #rovnice
x27=0\displaystyle x^{2} - 7 = 0
(x^2-7==0).rhs();(x^2-7==0).lhs() #prava a leva strana
0\displaystyle 0
x27\displaystyle x^{2} - 7
solve(x^2-7==0, x)
[x=7\displaystyle x = -\sqrt{7}, x=7\displaystyle x = \sqrt{7}]

Získání všech řešení.

solve(sin(3*x+pi/3)==1/2,x) #neziskame vsechna reseni
[x=118π\displaystyle x = -\frac{1}{18} \, \pi]
solve(sin(3*x+pi/3)==1/2,x,to_poly_solve='force')
[x=118π+23πz61\displaystyle x = -\frac{1}{18} \, \pi + \frac{2}{3} \, \pi z_{61}, x=16π+23πz63\displaystyle x = \frac{1}{6} \, \pi + \frac{2}{3} \, \pi z_{63}]
eq1=y==3*x+1;eq2=y==x-1;eq1;eq2
y=3x+1\displaystyle y = 3 \, x + 1
y=x1\displaystyle y = x - 1
sol=solve([eq1,eq2],x,y); sol #reseni soustavy rovnic
[[x=(1)\displaystyle x = \left(-1\right), y=(2)\displaystyle y = \left(-2\right)]]

Práce s nn-ticemi

A=solve(x^4-2*x^2==0,x);A
[x=2\displaystyle x = -\sqrt{2}, x=2\displaystyle x = \sqrt{2}, x=0\displaystyle x = 0]
A[0] #prvni prvek (index 0)
x=2\displaystyle x = -\sqrt{2}
A[1]
x=2\displaystyle x = \sqrt{2}

Deklarace funkce

f(x)=x^2
f
x  x2\displaystyle x \ {\mapsto}\ x^{2}
f(4)
16\displaystyle 16
%typeset_mode True f=x^2 #deklarace f jako vyrazu, ne funkce
f
x2\displaystyle x^{2}
f(x=4)
16\displaystyle 16
f.subs(x=4)
16\displaystyle 16
%html Práce s grafikou
Práce s grafikou
plot(ln(x), (x,0,10))
plot(ln(x), (x,0,10)).save('obrazek.pdf')
plot(ln(x), (x,0,10))+text('Graf funkce $f(x)=\ln(x)$',(2,2), fontsize=12)
(plot(ln(x), (x,0,10))+text("Graf funkce $f(x)=\ln(x)$",(2,2), fontsize=12)).save('obrazek1.pdf')
plot(e^x, (x,-3,5),ymax=100,ymin=0)
plot(tan(x), (x,-3,3), ymax=10, ymin=-10,detect_poles=True)
%html <p>Úskalí</p>

Úskalí

plot(e^x+ln(abs(4-x)), (x,0,5))
plot(e^x+ln(abs(4-x)), (x,3.99,4.01),detect_poles=True)
rov1=sqrt(x-2)==sqrt(2*x-3);rov1
x2=2x3\displaystyle \sqrt{x - 2} = \sqrt{2 \, x - 3}
res1=solve(rov1, x, to_poly_solve='force');res1
[x=1\displaystyle x = 1]
p1=plot(sqrt(x-2), (x,2,7));p2=plot(sqrt(2*x-3), (x,1.5,7));p1+p2
rov1.subs(x=(res1[0].rhs()))
i=i\displaystyle i = i
log(x+4)+log(x2)=log(x+3)\displaystyle \log\left(x + 4\right) + \log\left(x - 2\right) = \log\left(x + 3\right)
res2=solve(rov2,x, to_poly_solve='force');res2
[x=32512\displaystyle x = -\frac{3}{2} \, \sqrt{5} - \frac{1}{2}, x=32512\displaystyle x = \frac{3}{2} \, \sqrt{5} - \frac{1}{2}]
((res2[0])).rhs().n();((res2[1])).rhs().n()
3.85410196624968\displaystyle -3.85410196624968
2.85410196624968\displaystyle 2.85410196624968
p1=plot(log(x+4)+log(x-2), (x,2,4));p2=plot(log(x+3), (x,-3,4));p1+p2
%typeset_mode True
rov4=2*(2^x)^2-7*2^x+3==0; rov4
222x72x+3=0\displaystyle 2 \cdot 2^{2 \, x} - 7 \cdot 2^{x} + 3 = 0
%typeset_mode True solve(2*(2^x)^2-7*2^x+3==0, x, to_poly_solve='force')
[]
plot(2*(2^x)^2-7*2^x+3, (x,-2,2))
solve(2*(y)^2-7*y+3==0, y, to_poly_solve='force')
[y=(12)\displaystyle y = \left(\frac{1}{2}\right), y=3\displaystyle y = 3]
solve(2^x==3, x);solve(2^x==1/2,x)
[x=log(3)log(2)\displaystyle x = \frac{\log\left(3\right)}{\log\left(2\right)}]
[x=(1)\displaystyle x = \left(-1\right)]
(solve(2^x==3, x)[0]).rhs().n()
1.58496250072116\displaystyle 1.58496250072116