Pěkné tabulky

{{{id=41| table( [ [ i*j for i in range(1,11)] for j in range(1,11)], header_row=True, header_column=True, frame=True, align='right') ///
}}} {{{id=42| latex(table( [ [ i*j for i in range(1,11)] for j in range(1,11)], header_row=True, header_column=True, frame=True, align='right')) /// \begin{tabular}{|r||r|r|r|r|r|r|r|r|r|} \hline $1$ & $2$ & $3$ & $4$ & $5$ & $6$ & $7$ & $8$ & $9$ & $10$ \\ \hline \hline $2$ & $4$ & $6$ & $8$ & $10$ & $12$ & $14$ & $16$ & $18$ & $20$ \\ \hline $3$ & $6$ & $9$ & $12$ & $15$ & $18$ & $21$ & $24$ & $27$ & $30$ \\ \hline $4$ & $8$ & $12$ & $16$ & $20$ & $24$ & $28$ & $32$ & $36$ & $40$ \\ \hline $5$ & $10$ & $15$ & $20$ & $25$ & $30$ & $35$ & $40$ & $45$ & $50$ \\ \hline $6$ & $12$ & $18$ & $24$ & $30$ & $36$ & $42$ & $48$ & $54$ & $60$ \\ \hline $7$ & $14$ & $21$ & $28$ & $35$ & $42$ & $49$ & $56$ & $63$ & $70$ \\ \hline $8$ & $16$ & $24$ & $32$ & $40$ & $48$ & $56$ & $64$ & $72$ & $80$ \\ \hline $9$ & $18$ & $27$ & $36$ & $45$ & $54$ & $63$ & $72$ & $81$ & $90$ \\ \hline $10$ & $20$ & $30$ & $40$ & $50$ & $60$ & $70$ & $80$ & $90$ & $100$ \\ \hline \end{tabular} }}}

Sčítání -- staré i nové

{{{id=1| sum( [2^k for k in range(11)] ) /// }}} {{{id=2| print k /// Traceback (most recent call last): File "", line 1, in File "_sage_input_2.py", line 10, in exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("cHJpbnQgaw=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in File "/tmp/tmp_vKQH2/___code___.py", line 2, in exec compile(u'print k' + '\n', '', 'single') File "", line 1, in NameError: name 'k' is not defined }}} {{{id=4| k=var('k') n=var('n') sum(2^k, k, 0, n) /// 2^(n + 1) - 1 }}} {{{id=54| n=1.5 sum(2^k, k, 0, n) /// }}} {{{id=57| sum(2^(-k), k, 0, Infinity) /// 2 }}} {{{id=56| sum(k*2^(-k), k, 0, Infinity) /// 2 }}} {{{id=55| sum(k*2^(-k), k, 0, oo) /// 2 }}} {{{id=58| sum(1/n^2, n, 1, oo) /// Traceback (most recent call last): File "", line 1, in File "_sage_input_8.py", line 10, in exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("c3VtKDEvbl4yLCBuLCAxLCBvbyk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in File "/tmp/tmpwee0iS/___code___.py", line 3, in exec compile(u'sum(_sage_const_1 /n**_sage_const_2 , n, _sage_const_1 , oo)' + '\n', '', 'single') File "", line 1, in File "sage/rings/integer.pyx", line 2022, in sage.rings.integer.Integer.__pow__ (/home/samal/local/sage-7.3/src/build/cythonized/sage/rings/integer.c:13573) TypeError: unsupported operand type(s) for ** or pow(): 'function' and 'int' }}} {{{id=61| n=5 /// }}} {{{id=59| sum((-1)^n/n, n, 1, oo) /// Traceback (most recent call last): File "", line 1, in File "_sage_input_20.py", line 10, in exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("c3VtKCgtMSlebi9uLCBuLCAxLCBvbyk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in File "/tmp/tmpwIcLU9/___code___.py", line 3, in exec compile(u'sum((-_sage_const_1 )**n/n, n, _sage_const_1 , oo)' + '\n', '', 'single') File "", line 1, in File "/home/samal/local/sage-7.3/local/lib/python2.7/site-packages/sage/misc/functional.py", line 569, in symbolic_sum return SR(expression).sum(*args, **kwds) File "sage/symbolic/expression.pyx", line 11472, in sage.symbolic.expression.Expression.sum (/home/samal/local/sage-7.3/src/build/cythonized/sage/symbolic/expression.cpp:59989) File "/home/samal/local/sage-7.3/local/lib/python2.7/site-packages/sage/calculus/calculus.py", line 578, in symbolic_sum raise TypeError("need a summation variable") TypeError: need a summation variable }}} {{{id=62| prod( (2*n)*(2*n)/( (2*n-1)*(2*n+1)), n, 1, oo) /// Traceback (most recent call last): File "", line 1, in File "_sage_input_21.py", line 10, in exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("cHJvZCggKDIqbikqKDIqbikvKCAoMipuLTEpKigyKm4rMSkpLCBuLCAxLCBvbyk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in File "/tmp/tmpuewr8M/___code___.py", line 3, in exec compile(u'prod( (_sage_const_2 *n)*(_sage_const_2 *n)/( (_sage_const_2 *n-_sage_const_1 )*(_sage_const_2 *n+_sage_const_1 )), n, _sage_const_1 , oo)' + '\n', '', 'single') File "", line 1, in File "sage/misc/misc_c.pyx", line 71, in sage.misc.misc_c.prod (/home/samal/local/sage-7.3/src/build/cythonized/sage/misc/misc_c.c:1436) TypeError: prod() takes at most 3 positional arguments (4 given) }}} {{{id=60| sum( log((2*n)*(2*n)/( (2*n-1)*(2*n+1))), n, 1, oo) /// log(1/2*pi) }}}

Symbolické proměnné

{{{id=48| print var('x y z') print var('x, y, z') print var(['x', 'y', 'z']) print var('x', 'y', 'z') print var('x'), var('y'), var('z') /// (x, y, z) (x, y, z) (x, y, z) (x, y, z) x y z }}} {{{id=53| x^2 + y^2 == z^2 /// }}} {{{id=165| f = x^2 + y^2 - z^2 f /// x^2 + y^2 - z^2 }}} {{{id=166| f(x=var('x2')) /// x2^2 + y^2 - z^2 }}} {{{id=65| x=var('x') /// }}}

různé obory proměnných

{{{id=50| var('x'); x.is_real() /// }}} {{{id=51| var('x', domain=RR) print x.is_real() print bool(x>0) /// }}} {{{id=52| var('x', domain='positive') print x.is_real() print bool(x>0) /// True True }}} {{{id=183| simplify(sqrt(y^2)) /// y }}} {{{id=184| print assumptions() forget() print assumptions() /// [y >= 0] [] }}}

Assumptions

{{{id=180| simplify(sqrt(y^2)) /// y }}} {{{id=182| assume(y>=0) /// }}}

zobrazovací fígl

{{{id=24| x = var('sui', latex_name="\\color{red}{s_{u,i}}") /// }}} {{{id=68| x^2 + y^2 == z^2 /// sui^2 + y^2 == z^2 }}} {{{id=26| view(x^2 + y^2 == z^2) /// }}} {{{id=8| print x._latex_() /// {\color{red}{s_{u,i}}} }}}

Různé druhy funkcí

Pythonovská funkce

{{{id=74| def f(zz): return zz^2 /// }}} {{{id=77| print f(2) print type(f) /// 4 }}} {{{id=78| integrate(f, 0, 1) /// Traceback (most recent call last): File "", line 1, in File "_sage_input_76.py", line 10, in exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("aW50ZWdyYXRlKGYsIDAsIDEp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in File "/tmp/tmpQfpK3o/___code___.py", line 3, in exec compile(u'integrate(f, _sage_const_0 , _sage_const_1 )' + '\n', '', 'single') File "", line 1, in File "/home/samal/local/sage-7.3/local/lib/python2.7/site-packages/sage/misc/functional.py", line 668, in integral return SR(x).integral(*args, **kwds) File "sage/structure/parent.pyx", line 1107, in sage.structure.parent.Parent.__call__ (/home/samal/local/sage-7.3/src/build/cythonized/sage/structure/parent.c:9905) File "sage/structure/coerce_maps.pyx", line 110, in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (/home/samal/local/sage-7.3/src/build/cythonized/sage/structure/coerce_maps.c:4652) File "sage/structure/coerce_maps.pyx", line 105, in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (/home/samal/local/sage-7.3/src/build/cythonized/sage/structure/coerce_maps.c:4531) File "sage/symbolic/ring.pyx", line 353, in sage.symbolic.ring.SymbolicRing._element_constructor_ (/home/samal/local/sage-7.3/src/build/cythonized/sage/symbolic/ring.cpp:6507) TypeError: unable to convert to a symbolic expression }}} {{{id=169| f = lambda z: z^2 type(f) /// }}} {{{id=170| QQ = var('QQ') print type(QQ) QQ = 2; print QQ /// 2 }}} {{{id=171| restore('QQ') print type(QQ) QQ(2.5) /// 5/2 }}}

Sage symbolic expression

{{{id=79| type(z) /// }}} {{{id=80| type(f(z)) /// }}} {{{id=81| f(z) /// z^2 }}} {{{id=82| integrate(f(z), z, 0, 1) /// 1/3 }}}

Callable symbolic expression

{{{id=91| s /// Traceback (most recent call last): File "", line 1, in File "_sage_input_2.py", line 10, in exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("cw=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in File "/tmp/tmpKcEElM/___code___.py", line 2, in exec compile(u's' + '\n', '', 'single') File "", line 1, in NameError: name 's' is not defined }}} {{{id=83| g(s) = s^2 /// }}} {{{id=88| g /// s |--> s^2 }}} {{{id=89| print g(3) /// 9 }}} {{{id=95| print g /// s |--> s^2 }}} {{{id=94| print g(s) /// s^2 }}} {{{id=93| print g(z) /// z^2 }}} {{{id=92| print g(x) /// sui^2 }}}

Předdefinovaná funkce

{{{id=172| h=sin type(h) /// }}} {{{id=174| integrate(h(x),x,0,pi) /// 2 }}} {{{id=176| supersin = function('supersin', latex_name="\\color{red}{supersin}") /// }}} {{{id=175| view(supersin(y) - sin(y)) /// }}} {{{id=177| supersin(y).diff() /// D[0](supersin)(y) }}} {{{id=178| diff(supersin(y)) /// D[0](supersin)(y) }}}

Ukázka zacházení s funkcemi -- Hermitovy polynomy

Polynomy, které jsou ortogonální při váze exp(-y^2).

Používají se ve fyzice, pravděpodobnosti, numerice, ...

{{{id=14| def hermite(n,y): """ Defines a Hermite polynomial of order n evaluated at y. """ if n == 1: return 2*y if n == 0: return 1 return 2*y*hermite(n-1,y) - 2*(n-1)*hermite(n-2,y) # return expand(2*y*hermite(n-1,y) - 2*(n-1)*hermite(n-2,y)) /// }}} {{{id=71| hermite? ///

File: /tmp/tmpoVRk0F/___code___.py

Type: <type 'function'>

Definition: hermite(n, y)

Docstring:


    Defines a Hermite polynomial of order n evaluated at y.
}}} {{{id=70| hermite(2,3) /// 34 }}} {{{id=72| hermite(2,y) /// 4*y^2 - 2 }}} {{{id=73| integral(hermite(2,y)*hermite(2,y)*exp(-y^2), y, -oo, oo) /// 8*sqrt(pi) }}}

Potíže s funkcemi

 

Potíž 1 -- konstanta

{{{id=107| F = x /// }}} {{{id=112| f = F.derivative() /// }}} {{{id=111| f(3) /// Traceback (most recent call last): File "", line 1, in File "_sage_input_33.py", line 10, in exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("ZigzKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in File "/tmp/tmptxCB6B/___code___.py", line 3, in exec compile(u'f(_sage_const_3 )' + '\n', '', 'single') File "", line 1, in File "sage/symbolic/expression.pyx", line 4985, in sage.symbolic.expression.Expression.__call__ (/home/samal/local/sage-7.3/src/build/cythonized/sage/symbolic/expression.cpp:28299) File "sage/symbolic/ring.pyx", line 879, in sage.symbolic.ring.SymbolicRing._call_element_ (/home/samal/local/sage-7.3/src/build/cythonized/sage/symbolic/ring.cpp:10166) ValueError: the number of arguments must be less than or equal to 0 }}} {{{id=110| print f.variables() print f.arguments() /// () () }}}

Náprava 1a

{{{id=118| f(x=3) /// 1 }}}

Náprava 1b

{{{id=109| F(x) = x f = F.derivative() print f(3) print f.variables() print f.arguments() /// 1 () (x,) }}}

Náprava 1c

{{{id=114| F = x f(x) = F.derivative() print f(3) print f.variables() print f.arguments() /// 1 () (x,) }}}

Problém 2 -- dosazení

{{{id=120| def h(x): if x<2: return 0 else: return x-2 /// }}} {{{id=121| h(x) /// x - 2 }}} {{{id=122| plot(h(x), x,0,3) /// }}} {{{id=123| plot(h, x,0,3) /// }}} {{{id=15| /// }}}

Úpravy výrazů

{{{id=150| x=var('x') type(x) /// }}} {{{id=142| expr = (x+1)^3 expr2 = expr.expand(); expr2 /// x^3 + 3*x^2 + 3*x + 1 }}} {{{id=143| expr2.simplify() /// x^3 + 3*x^2 + 3*x + 1 }}} {{{id=144| expr2.full_simplify() /// x^3 + 3*x^2 + 3*x + 1 }}} {{{id=145| expr2.factor() /// (x + 1)^3 }}} {{{id=164| expr2.coeff(x^2) /// 3 }}} {{{id=146| expr3 = 1/x + 1/(x+1); expr3 /// 1/(x + 1) + 1/x }}} {{{id=147| expr3.simplify_rational() /// (2*x + 1)/(x^2 + x) }}} {{{id=148| expr4 = 1/(x^2-1); expr4 /// 1/(x^2 - 1) }}} {{{id=149| expr4.partial_fraction() /// -1/2/(x + 1) + 1/2/(x - 1) }}}

Solving equations

Needs variables!!!

{{{id=125| ex^2 + y^2 == z^2 /// x^2 + y^2 == z^2 }}} {{{id=126| x + 4 == 2*x - 2 /// x + 4 == 2*x - 2 }}} {{{id=127| eq = x + 4 == 2*x - 2 eq + 2 /// x + 6 == 2*x }}} {{{id=128| solve(eq,x) /// [x == 6] }}} {{{id=130| d=solve(eq,x,solution_dict=True); d /// [{x: 6}] }}} {{{id=129| eq.subs(d) /// 10 == 10 }}} {{{id=131| solve( x^2 - 3*x + 2, x) /// [x == 1, x == 2] }}} {{{id=132| solve( [x^2 == y^2 + 2, y-x==1], [x,y]) /// [[x == (-3/2), y == (-1/2)]] }}} {{{id=133| solve( [x^2 == y^2 + 2, (y-x)^2==1], [x,y]) /// [[x == (-3/2), y == (-1/2)], [x == (3/2), y == (1/2)]] }}} {{{id=134| solve( tan(x) == x, x) /// [x == tan(x)] }}} {{{id=135| find_root(tan(x) == 2*x, .1,1.3) /// 1.1655611852072114 }}} {{{id=137| find_root(tan(x) == 2*x, .1,2) /// 1.5707963267964462 }}} {{{id=136| plot( [tan(x), 2*x], [x,-1,1.3]) /// }}} {{{id=35| tan(1.5707) == 2*1.5707 /// False }}} {{{id=158| x = var('x') solve( x^2 > 2, x) /// [[x < -sqrt(2)], [x > sqrt(2)]] }}} {{{id=160| solve_mod(x^3 == 1, 7) /// [(1,), (2,), (4,)] }}} {{{id=162| solve_diophantine(x^2 + y^2 == 25, [x,y]) /// [(4, -3), (0, -5), (-4, -3), (-4, 3), (0, 5), (4, 3)] }}}

Ukázka bokem: řešení diferenciálních rovnice

{{{id=155| t = var('t') # define a variable t x = function('x')(t) # define x to be a function of that variable DE = diff(x, t) + x - 1 desolve(DE, [x,t]) /// (_C + e^t)*e^(-t) }}}

Ochutnávka sympy -- převod lineárních rovnic na maticový tvar

{{{id=31| from sympy import linear_eq_to_matrix, symbols x, y, z = symbols('x, y, z') eqns = [x + 2*y + 3*z - 1, 3*x + y + z + 6, 2*x + 4*y + 9*z - 2] A, b = linear_eq_to_matrix(eqns, [x, y, z]) print A, b /// Matrix([[1, 2, 3], [3, 1, 1], [2, 4, 9]]) Matrix([[1], [-6], [2]]) }}}

Ochutnávka sympy -- řešení rekurence

{{{id=38| from sympy import * y = Function('y') n = Symbol('n',integer=True) sol = (rsolve(y(n+2)-(y(n)+y(n+1)), y(n), {y(0):0,y(1):1})) /// }}} {{{id=138| type(sol) /// }}} {{{id=139| show(sol._sage_()) /// }}} {{{id=140| /// }}}