}}}
{{{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|
///
}}}