Dear Christian,

Given the specifications, I suspect that the function collect
only works for variables. And indeed

sage: expr = (x+y)*(z+t) + (x+y)*(1+z^2) + 2

sage: expr

(z^2 + 1)*(x + y) + (t + z)*(x + y) + 2
sage: expr.collect(x+y)

(z^2 + 1)*(x + y) + (t + z)*(x + y) + 2

Instead of

(z^2 + t + z + 1)*(x + y) + 2

Secondly, what are you trying to collect? The D[0,0] terms or
f(xp, yp, zp, tp), x, 2) which are two different things? Given
what you are trying to do I guess it is the first one, in which
case you should have tried

sage: term = f(x,y,z,t).derivative(x,2).subs(x=xp,y=yp,z=zp,t=tp)

Finally, note also that there is some factorization happening in
your expression e, so before doing anything do

sage: e = e.expand()

Now, the only way I found to make it work was to implement my
own collecting routine (see attachment). With the function
implemented in the file that walks along the expression you
obtain

sage: L = my_collect(e, term)
sage: len(L)
2
sage: L[1]   # the terms linear in D[0,0](f)(xp, yp, zp, tp)
v^2/(c^2*(v^2/c^2 - 1)) - 1/(v^2/c^2 - 1)

The above term is one as can be checked with

sage: L[1].simplify_full()

1

Best
Vincent

Le 24/01/2021 à 17:23, Christian Seberino a écrit :
Emmanuel

But my question is more simple than that. I just want to know why the
collect method was not able to collect all the terms with the given second
derivative.

On Sun, Jan 24, 2021, 2:15 AM Emmanuel Charpentier <
emanuel.charpent...@gmail.com> wrote:

Sage has recently acquired a large set of tools relative to manifolds
<https://sagemanifolds.obspm.fr/>. A look at these tools and related
tutorials/references may be in order…

HTH,
​
Le samedi 23 janvier 2021 à 23:17:26 UTC+1, cseb...@gmail.com a écrit :

What you intend to do isn’t really clear… Could you try and clear your
goals ?

Emmanuel

Thanks so much for your help.  I'm trying to show that the wave equation (
https://en.wikipedia.org/wiki/Wave_equation)
is invariant under a certain coordinate transformation called the Lorentz
transformation (special relativity).

I represent the function that obeys the wave equation in the primed
coordinate system by f(xp, yp, zp, tp).

I also represent the primed coordinates by the coordinates in the
unprimed coordinate system.
Therefore,   f(xp, yp, zp, tp) =  f(xp(x, y, z, t), yp(x, y, z, t), zp(x,
y, z, t), tp(x, y, z, t)).

I then find a bunch of derivates of f(xp(x, y, z, t), yp(x, y, z, t),
zp(x, y, z, t), tp(x, y, z, t)) and try to collect terms.

All the coordinates should be real numbers.

Does that explain everything?




--
You received this message because you are subscribed to a topic in the
Google Groups "sage-support" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/sage-support/TDseIA1M7vY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
sage-support+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sage-support/068e5ac0-1d78-453f-a465-bc84e1d1fc90n%40googlegroups.com
<https://groups.google.com/d/msgid/sage-support/068e5ac0-1d78-453f-a465-bc84e1d1fc90n%40googlegroups.com?utm_medium=email&utm_source=footer>
.



--
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-support/eeb79950-4893-3933-4687-4bf7fa40533b%40gmail.com.
from operator import pow
from sage.symbolic.operators import add_vararg, mul_vararg

def my_collect(expression, X): 
    r"""
    Collect the coefficients of ``X`` inside ``expression`` as a list.

    EXAMPLES::

        sage: x, y, z, t = SR.var('x, y, z, t')
        sage: my_collect((x+y)**2 + x*t*(x+y)*z + 2, x+y)
        [2, t*x*z, 1]
    """
    if expression.operator() != add_vararg: 
        # if the expression is not a sum returns it in full
        return [expression] 

    # walk around the terms of the sum
    ans = [SR.zero()]
    for subexpression in expression.operands(): 
        if subexpression.operator() == mul_vararg: 
            # subexpression is a product in which X might be involved
            exponent = 0
            coefficient = SR.one()
            for term in subexpression.operands():
                if term == X:
                    exponent += 1
                elif term.operator() == pow and term.operands()[0] == X:
                    exponent += term.operands()[1]
                else:
                    coefficient *= term
        elif subexpression.operator() == pow and subexpression.operands()[0] == X:
            # subexpression is a power of X
            exponent = subexpression.operands()[1]
            coefficient = 1
        else:
            # subexpression is degree zero in X
            exponent = 0
            coefficient = subexpression

        # now update our answer
        if exponent >= len(ans):
            ans.extend([0] * (exponent - len(ans) + 1))
        ans[exponent] += coefficient

    return ans

Reply via email to