本文整理汇总了Python中sympy.core.function.diff函数的典型用法代码示例。如果您正苦于以下问题:Python diff函数的具体用法?Python diff怎么用?Python diff使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了diff函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: solve_ODE_first_order
def solve_ODE_first_order(eq, f):
"""
solves many kinds of first order odes, different methods are used
depending on the form of the given equation. Now the linear
and Bernoulli cases are implemented.
"""
from sympy.integrals.integrals import integrate
x = f.args[0]
f = f.func
#linear case: a(x)*f'(x)+b(x)*f(x)+c(x) = 0
a = Wild('a', exclude=[f(x)])
b = Wild('b', exclude=[f(x)])
c = Wild('c', exclude=[f(x)])
r = eq.match(a*diff(f(x),x) + b*f(x) + c)
if r:
t = C.exp(integrate(r[b]/r[a], x))
tt = integrate(t*(-r[c]/r[a]), x)
return (tt + Symbol("C1"))/t
#Bernoulli case: a(x)*f'(x)+b(x)*f(x)+c(x)*f(x)^n = 0
n = Wild('n', exclude=[f(x)])
r = eq.match(a*diff(f(x),x) + b*f(x) + c*f(x)**n)
if r:
t = C.exp((1-r[n])*integrate(r[b]/r[a],x))
tt = (r[n]-1)*integrate(t*r[c]/r[a],x)
return ((tt + Symbol("C1"))/t)**(1/(1-r[n]))
#other cases of first order odes will be implemented here
raise NotImplementedError("solve_ODE_first_order: Cannot solve " + str(eq))
示例2: _do
def _do(f, ab):
dab_dsym = diff(ab, sym)
if not dab_dsym:
return S.Zero
if isinstance(f, Integral):
limits = [(x, x) if (len(l) == 1 and l[0] == x) else l for l in f.limits]
f = self.func(f.function, *limits)
return f.subs(x, ab) * dab_dsym
示例3: solve_ODE_second_order
def solve_ODE_second_order(eq, f):
"""
solves many kinds of second order odes, different methods are used
depending on the form of the given equation. So far the constants
coefficients case and a special case are implemented.
"""
x = f.args[0]
f = f.func
#constant coefficients case: af''(x)+bf'(x)+cf(x)=0
a = Wild('a', exclude=[x])
b = Wild('b', exclude=[x])
c = Wild('c', exclude=[x])
r = eq.match(a*f(x).diff(x,x) + c*f(x))
if r:
return Symbol("C1")*C.sin(sqrt(r[c]/r[a])*x)+Symbol("C2")*C.cos(sqrt(r[c]/r[a])*x)
r = eq.match(a*f(x).diff(x,x) + b*diff(f(x),x) + c*f(x))
if r:
r1 = solve(r[a]*x**2 + r[b]*x + r[c], x)
if r1[0].is_real:
if len(r1) == 1:
return (Symbol("C1") + Symbol("C2")*x)*exp(r1[0]*x)
else:
return Symbol("C1")*exp(r1[0]*x) + Symbol("C2")*exp(r1[1]*x)
else:
r2 = abs((r1[0] - r1[1])/(2*S.ImaginaryUnit))
return (Symbol("C2")*C.cos(r2*x) + Symbol("C1")*C.sin(r2*x))*exp((r1[0] + r1[1])*x/2)
#other cases of the second order odes will be implemented here
#special equations, that we know how to solve
a = Wild('a')
t = x*exp(f(x))
tt = a*t.diff(x, x)/t
r = eq.match(tt.expand())
if r:
return -solve_ODE_1(f(x), x)
t = x*exp(-f(x))
tt = a*t.diff(x, x)/t
r = eq.match(tt.expand())
if r:
#check, that we've rewritten the equation correctly:
#assert ( r[a]*t.diff(x,2)/t ) == eq.subs(f, t)
return solve_ODE_1(f(x), x)
neq = eq*exp(f(x))/exp(-f(x))
r = neq.match(tt.expand())
if r:
#check, that we've rewritten the equation correctly:
#assert ( t.diff(x,2)*r[a]/t ).expand() == eq
return solve_ODE_1(f(x), x)
raise NotImplementedError("solve_ODE_second_order: cannot solve " + str(eq))
示例4: line_integrate
def line_integrate(field, curve, vars):
"""line_integrate(field, Curve, variables)
Compute the line integral.
Examples
========
>>> from sympy import Curve, line_integrate, E, ln
>>> from sympy.abc import x, y, t
>>> C = Curve([E**t + 1, E**t - 1], (t, 0, ln(2)))
>>> line_integrate(x + y, C, [x, y])
3*sqrt(2)
See Also
========
integrate, Integral
"""
from sympy.geometry import Curve
F = sympify(field)
if not F:
raise ValueError(
"Expecting function specifying field as first argument.")
if not isinstance(curve, Curve):
raise ValueError("Expecting Curve entity as second argument.")
if not is_sequence(vars):
raise ValueError("Expecting ordered iterable for variables.")
if len(curve.functions) != len(vars):
raise ValueError("Field variable size does not match curve dimension.")
if curve.parameter in vars:
raise ValueError("Curve parameter clashes with field parameters.")
# Calculate derivatives for line parameter functions
# F(r) -> F(r(t)) and finally F(r(t)*r'(t))
Ft = F
dldt = 0
for i, var in enumerate(vars):
_f = curve.functions[i]
_dn = diff(_f, curve.parameter)
# ...arc length
dldt = dldt + (_dn * _dn)
Ft = Ft.subs(var, _f)
Ft = Ft * sqrt(dldt)
integral = Integral(Ft, curve.limits).doit(deep=False)
return integral
示例5: _eval_imageset
def _eval_imageset(self, f):
from sympy.functions.elementary.miscellaneous import Min, Max
from sympy.solvers import solve
from sympy.core.function import diff
from sympy.series import limit
from sympy.calculus.singularities import singularities
# TODO: handle piecewise defined functions
# TODO: handle functions with infinitely many solutions (eg, sin, tan)
# TODO: handle multivariate functions
expr = f.expr
if len(expr.free_symbols) > 1 or len(f.variables) != 1:
return
var = f.variables[0]
if not self.start.is_comparable or not self.end.is_comparable:
return
try:
sing = [x for x in singularities(expr, var) if x.is_real and x in self]
except NotImplementedError:
return
if self.left_open:
_start = limit(expr, var, self.start, dir="+")
elif self.start not in sing:
_start = f(self.start)
if self.right_open:
_end = limit(expr, var, self.end, dir="-")
elif self.end not in sing:
_end = f(self.end)
if len(sing) == 0:
solns = solve(diff(expr, var), var)
extr = [_start, _end] + [f(x) for x in solns
if x.is_real and x in self]
start, end = Min(*extr), Max(*extr)
left_open, right_open = False, False
if _start <= _end:
# the minimum or maximum value can occur simultaneously
# on both the edge of the interval and in some interior
# point
if start == _start and start not in solns:
left_open = self.left_open
if end == _end and end not in solns:
right_open = self.right_open
else:
if start == _end and start not in solns:
left_open = self.right_open
if end == _start and end not in solns:
right_open = self.left_open
return Interval(start, end, left_open, right_open)
else:
return imageset(f, Interval(self.start, sing[0],
self.left_open, True)) + \
Union(*[imageset(f, Interval(sing[i], sing[i + 1]), True, True)
for i in range(1, len(sing) - 1)]) + \
imageset(f, Interval(sing[-1], self.end, True, self.right_open))
示例6: _set_function
def _set_function(f, x):
from sympy.functions.elementary.miscellaneous import Min, Max
from sympy.solvers.solveset import solveset
from sympy.core.function import diff, Lambda
from sympy.series import limit
from sympy.calculus.singularities import singularities
from sympy.sets import Complement
# TODO: handle functions with infinitely many solutions (eg, sin, tan)
# TODO: handle multivariate functions
expr = f.expr
if len(expr.free_symbols) > 1 or len(f.variables) != 1:
return
var = f.variables[0]
if expr.is_Piecewise:
result = S.EmptySet
domain_set = x
for (p_expr, p_cond) in expr.args:
if p_cond is true:
intrvl = domain_set
else:
intrvl = p_cond.as_set()
intrvl = Intersection(domain_set, intrvl)
if p_expr.is_Number:
image = FiniteSet(p_expr)
else:
image = imageset(Lambda(var, p_expr), intrvl)
result = Union(result, image)
# remove the part which has been `imaged`
domain_set = Complement(domain_set, intrvl)
if domain_set.is_EmptySet:
break
return result
if not x.start.is_comparable or not x.end.is_comparable:
return
try:
sing = [i for i in singularities(expr, var)
if i.is_real and i in x]
except NotImplementedError:
return
if x.left_open:
_start = limit(expr, var, x.start, dir="+")
elif x.start not in sing:
_start = f(x.start)
if x.right_open:
_end = limit(expr, var, x.end, dir="-")
elif x.end not in sing:
_end = f(x.end)
if len(sing) == 0:
solns = list(solveset(diff(expr, var), var))
extr = [_start, _end] + [f(i) for i in solns
if i.is_real and i in x]
start, end = Min(*extr), Max(*extr)
left_open, right_open = False, False
if _start <= _end:
# the minimum or maximum value can occur simultaneously
# on both the edge of the interval and in some interior
# point
if start == _start and start not in solns:
left_open = x.left_open
if end == _end and end not in solns:
right_open = x.right_open
else:
if start == _end and start not in solns:
left_open = x.right_open
if end == _start and end not in solns:
right_open = x.left_open
return Interval(start, end, left_open, right_open)
else:
return imageset(f, Interval(x.start, sing[0],
x.left_open, True)) + \
Union(*[imageset(f, Interval(sing[i], sing[i + 1], True, True))
for i in range(0, len(sing) - 1)]) + \
imageset(f, Interval(sing[-1], x.end, True, x.right_open))
示例7: _eval_derivative
def _eval_derivative(self, s):
return Piecewise(*[(diff(e, s), c) for e, c in self.args])
示例8: _eval_imageset
def _eval_imageset(self, f):
from sympy import Dummy
from sympy.functions.elementary.miscellaneous import Min, Max
from sympy.solvers import solve
from sympy.core.function import diff
from sympy.series import limit
from sympy.calculus.singularities import singularities
# TODO: handle piecewise defined functions
# TODO: handle functions with infinitely many solutions (eg, sin, tan)
# TODO: handle multivariate functions
# var and expr are being defined this way to
# support Python lambda and not just sympy Lambda
try:
var = Dummy()
expr = f(var)
if len(expr.free_symbols) > 1:
raise TypeError
except TypeError:
raise NotImplementedError("Sorry, Multivariate imagesets are"
" not yet implemented, you are welcome"
" to add this feature in Sympy")
if not self.start.is_comparable or not self.end.is_comparable:
raise NotImplementedError("Sets with non comparable/variable"
" arguments are not supported")
sing = [x for x in singularities(expr, var) if x.is_real and x in self]
if self.left_open:
_start = limit(expr, var, self.start, dir="+")
elif self.start not in sing:
_start = f(self.start)
if self.right_open:
_end = limit(expr, var, self.end, dir="-")
elif self.end not in sing:
_end = f(self.end)
if len(sing) == 0:
solns = solve(diff(expr, var), var)
extr = [_start, _end] + [f(x) for x in solns
if x.is_real and x in self]
start, end = Min(*extr), Max(*extr)
left_open, right_open = False, False
if _start <= _end:
# the minimum or maximum value can occur simultaneously
# on both the edge of the interval and in some interior
# point
if start == _start and start not in solns:
left_open = self.left_open
if end == _end and end not in solns:
right_open = self.right_open
else:
if start == _end and start not in solns:
left_open = self.right_open
if end == _start and end not in solns:
right_open = self.left_open
return Interval(start, end, left_open, right_open)
else:
return imageset(f, Interval(self.start, sing[0],
self.left_open, True)) + \
Union(*[imageset(f, Interval(sing[i], sing[i + 1]), True, True)
for i in range(1, len(sing) - 1)]) + \
imageset(f, Interval(sing[-1], self.end, True, self.right_open))