Dear All
One of the most appropriate solution to my problem would be:
with this test case
F=F1*cos(x*F2)+exp(F1-F2)
F1=x*exp(x)
F2=x+F3
F3=sin(x)
from sympy import *
x=symbols('x')
F1=Function('F1')(x)
F2=Function('F1')(x)
F3=Function('F3')(x)
eqs=[]
eqs.append(sin(x))
eqs.append(x+F3)
eqs.append(x*exp(x))
eqs.append(F1*cos(x*F2)+exp(F1-F2))
deqs=[]
for eq in eqs:
deqs.append(diff(eq,x))
>>> eqs
[sin(x), x + F3(x), x*exp(x), F1(x)*cos(x*F1(x)) + 1]
>>> deqs
[cos(x), Derivative(F3(x), x) + 1, x*exp(x) + exp(x), -
(x*Derivative(F1(x), x) + F1(x))*F1(x)*sin(x*F1(x)) +
cos(x*F1(x))*Derivative(F1(x), x)]
so I get all the ingredients I need in a compact way, but I would need
to finally convert the
Functions and their derivatives to symbols, eg
Derivative(F3(x), x) --> dF3dx
F1(x) -->> F1
what would be the simplest way to do that?
Thanks
V
On Apr 5, 8:14 pm, vweber <[email protected]> wrote:
> Would it be possible to define a tree of equations as:
>
> root: f(x)=f1(x)*f3(x)
> leaf 1: f1(x)=...
> leaf 0: f3(x)=...+f2(x)
> leaf 00: f2(x)=...
>
> or equivalently
>
> leaf 1
> /
> root
> \
> leaf 0
> \
> leaf 00
>
> and then recurse the tree by applying the diff operator to the current
> equation?
>
> Thanks
> V
>
> On Apr 5, 2:31 am, Chris Smith <[email protected]> wrote:
>
>
>
>
>
>
>
> > 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.