The following might get you started:
def difcse(cse, x, n=1):
r, e = cse
nreps = len(r)
diffs = []
for i in range(nreps):
v, ri = r[i]
dri = simplify(ri.diff(x, n))
if dri:
func = Symbol('d_'+v.name+'_d'+x.name+
str(n if n>1 else ''))
r.append((v, func(x)))
diffs.append((r[-1][1].diff(x, n), func, dri))
if len(r) > nreps:
derivs = r[nreps:]
for j in range(len(e)):
e[j] = e[j].subs(derivs).diff(x, n).subs(
[d[:2] for d in diffs]).subs(
[(b, a) for a, b in r[nreps:]])
for j in range(nreps, len(r)):
i = j - nreps
r[j] = diffs[i][-2:]
return r, e
>>> re = cse(cos(x+y)*(x**2+1)+exp((x+y)/(x**2+1)))
>>> print difcse(re, x, 1)
([(x0, x + y), (x1, x**2 + 1), (d_x0_dx, 1), (d_x1_dx, 2*x)],
[-d_x0_dx*x1*sin(x
0) + d_x1_dx*cos(x0) + (d_x0_dx/x1 - d_x1_dx*x0/x1**2)*exp(x0/x1)])
>>> re = cse(cos(x+y)*(x**2+1)+exp((x+y)/(x**2+1)))
>>> print difcse(re, x, 2)
([(x0, x + y), (x1, x**2 + 1), (d_x1_dx2, 2)],
[-d_x1_dx2*x0*exp(x0/x1)/x1**2 +
d_x1_dx2*cos(x0) + x0**2*exp(x0/x1)*Derivative(x1, x)**2/x1**4 +
2*x0*exp(x0/x1)
*Derivative(x1, x)**2/x1**3])
Notice that when taking the second derivative, some first derivatives
remain, so perhaps the difcse (which modifies the cse result in place)
should also always include the first derivative if n > 1.
/c
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.