The problem lies in cse which is used to preprocess the expression by
trying to replace commonly occurring subexpressions with temporary
variables:

>>> eq=diff(theta(t), t)*sin(phi(t))*sin(psi(t))*sin(theta(t))
>>> cse(eq)
([(x0, theta(t))], [0])

The first list is the replacement that was used: theta(t) was replaced
with x0.
The second list is the equation that results when the substitutions
are made. It's zero! That's because when theta(t) was replaced with a
symbol, the derivative collapsed: there was no longer a t in the
expression, so the derivative went to 0.

I'm not sure what the best thing to have cse do is right now, but what
you could try doing is "masking" the derivatives--replace them with
symbols of your own before cse gets its hands on them :-)

def mask(eq, what):
    """return `eq` with `what` replaced with dummy variables;
    the replacement dictionary in reversed form is also returned and
can be used to
    restore the equation to its original state."""
    from sympy import C
    reps = dict((i, C.Dummy(str(i))) for i in eq.atoms(what))
    eq = eq.subs(reps)
    rev = dict([(v, k) for k, v in reps.iteritems()])
    return eq, rev

Using that would look like this:

>>> eq
D(theta(t), t)*sin(phi(t))*sin(psi(t))*sin(theta(t))
>>> eq, rev = mask(eq, Derivative)
>>> eq
_D(theta(t), t)*sin(phi(t))*sin(psi(t))*sin(theta(t))
>>> rev
{_D(theta(t), t): D(theta(t), t)}

It looks like a derivative is there in eq, but that's because the
string form of it was used as the dummy symbol name--look carefully
for the underscore in front of the D indicating that that is a dummy
variable.

>>> trigsimp(eq,1,1)
_D(theta(t), t)*sin(phi(t))*sin(psi(t))*sin(theta(t))

Notice that the expression no longer goes to zero when recursively
trigsimping.

>>> _.subs(rev)
D(theta(t), t)*sin(phi(t))*sin(psi(t))*sin(theta(t))

And now you have your original (trigsimped) equation back.


Regading the sum, yes that has been replaced with this:

>>> sum(x, (x, 1, 3))
6

But you can do 1 of 2 things:

1) use the sympy Add:

>>> Add(*[1,2,3])
6
>>> Add(1,2,3)
6


or 2) import the python sum as sum (if you don't want sympy's sum) or
as a nonclashing name if you want both sympy's and python's sum
available:

>>> from __builtin__ import sum as pysum
>>> pysum([1,2,3])
6

Others who have given sympy some of its physics abilities may be able
to comment more on what you are doing, but from my perspective I think
you'll learn a lot about sympy (and find more areas for improvement)
as you work through the text. We're interested to hear what you learn
as you go!

Best regards,
/chris

-- 
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.

Reply via email to