# how to replace a symbol with a function

I want to replace the symbol with a function to later take derivatives:

var("L r rdot theta thetadot t e d c G M r_s")
r = function("r")(t)
eq1=diff(K2,rdot)
eq1


yielding:

-2*rdot/(c^2*(rdot/c - 1)^2) + 2*((thetadot*r(t)^2 + rdot^2)/c^2 - 1)/(c*(rdot/c - 1)^3)


Now, I want to replace rdot, thetadot with derivatives of functions of time.

rdot = function("rdot")(t)
theta = function ('theta')(t)
rdot=diff(r,t)
eq2=diff(eq1,t)


I get a result but it is nonsense. 4thetadotr(t)diff(r(t), t)/(c^3(rdot/c - 1)^3)

How to do this properly?

Thank you

Marco

view(eq2)

edit retag close merge delete

What eq1=diff(K2,rdot) means when you substitute rdot(t) to rdot ? The rerivative of K2 mus be taken with respect to,what ?

( 2021-09-02 17:06:36 +0100 )edit

Sort by ยป oldest newest most voted

If r and theta are functions (of time, I assume), note them as such :

var("L r rdot theta thetadot t e d c G M r_s")
r = function("r")
theta = function("theta")
rdot(t) = diff(r(t), t)
# This is meaningless :
# eq1=diff(K2,rdot)
eq1 = diff(K2(t), t)
eq2 = diff(eq1(t), t)


eq1 is

sage: eq1
t |--> 2*((r(t)^2*diff(theta(t), t) + diff(r(t), t)^2)/c^2 - 1)*diff(r(t), t, t)/(c*(diff(r(t), t)/c - 1)^3) - (2*r(t)*diff(r(t), t)*diff(theta(t), t) + r(t)^2*diff(theta(t), t, t) + 2*diff(r(t), t)*diff(r(t), t, t))/(c^2*(diff(r(t), t)/c - 1)^2)


$$t \ {\mapsto}\ \frac{2 \, {\left(\frac{r\left(tright)^{2} \frac{\partial}{\partial t}theta\left(tright) + \frac{\partial}{\partial t}r\left(tright)^{2}}{c^{2}} - 1right)} \frac{\partial^{2}}{(\partial t)^{2}}r\left(tright)}{c {\left(\frac{\frac{\partial}{\partial t}r\left(tright)}{c} - 1right)}^{3}} - \frac{2 \, r\left(tright) \frac{\partial}{\partial t}r\left(tright) \frac{\partial}{\partial t}theta\left(tright) + r\left(tright)^{2} \frac{\partial^{2}}{(\partial t)^{2}}theta\left(tright) + 2 \, \frac{\partial}{\partial t}r\left(tright) \frac{\partial^{2}}{(\partial t)^{2}}r\left(tright)}{c^{2} {\left(\frac{\frac{\partial}{\partial t}r\left(tright)}{c} - 1right)}^{2}}$$

Similarly, eq2 is :

sage: eq2
t |--> 2*((r(t)^2*diff(theta(t), t) + diff(r(t), t)^2)/c^2 - 1)*diff(r(t), t, t, t)/(c*(diff(r(t), t)/c - 1)^3) - (2*diff(r(t), t)^2*diff(theta(t), t) + 2*r(t)*diff(r(t), t, t)*diff(theta(t), t) + 4*r(t)*diff(r(t), t)*diff(theta(t), t, t) + r(t)^2*diff(theta(t), t, t, t) + 2*diff(r(t), t, t)^2 + 2*diff(r(t), t)*diff(r(t), t, t, t))/(c^2*(diff(r(t), t)/c - 1)^2) - 6*((r(t)^2*diff(theta(t), t) + diff(r(t), t)^2)/c^2 - 1)*diff(r(t), t, t)^2/(c^2*(diff(r(t), t)/c - 1)^4) + 4*(2*r(t)*diff(r(t), t)*diff(theta(t), t) + r(t)^2*diff(theta(t), t, t) + 2*diff(r(t), t)*diff(r(t), t, t))*diff(r(t), t, t)/(c^3*(diff(r(t), t)/c - 1)^3)


$$t \ {\mapsto}\ \frac{2\, {\left(\frac{r\left(tright)^{2} \frac{\partial}{\partial t}theta\left(tright) + \frac{\partial}{\partial t}r\left(tright)^{2}}{c^{2}} - 1right)} \frac{\partial^{3}}{(\partial t)^{3}}r\left(tright)}{c {\left(\frac{\frac{\partial}{\partial t}r\left(tright)}{c} - 1right)}^{3}} - \frac{2 \, \frac{\partial}{\partial t}r\left(tright)^{2} \frac{\partial}{\partial t}theta\left(tright) + 2 \, r\left(tright) \frac{\partial^{2}}{(\partial t)^{2}}r\left(tright) \frac{\partial}{\partial t}theta\left(tright) + 4 \, r\left(tright) \frac{\partial}{\partial t}r\left(tright) \frac{\partial^{2}}{(\partial t)^{2}}theta\left(tright) + r\left(tright)^{2} \frac{\partial^{3}}{(\partial t)^{3}}theta\left(tright) + 2 \, \frac{\partial^{2}}{(\partial t)^{2}}r\left(tright)^{2} + 2 \, \frac{\partial}{\partial t}r\left(tright) \frac{\partial^{3}}{(\partial t)^{3}}r\left(tright)}{c^{2} {\left(\frac{\frac{\partial}{\partial t}r\left(tright)}{c} - 1right)}^{2}} - \frac{6 \, {\left(\frac{r\left(tright)^{2} \frac{\partial}{\partial t}theta\left(tright) + \frac{\partial}{\partial t}r\left(tright)^{2}}{c^{2}} - 1right)} \frac{\partial^{2}}{(\partial t)^{2}}r\left(tright)^{2}}{c^{2} {\left(\frac{\frac{\partial}{\partial t}r\left(tright)}{c} - 1right)}^{4}} + \frac{4 \, {\left(2 \, r\left(tright) \frac{\partial}{\partial t}r\left(tright) \frac{\partial}{\partial t}theta\left(tright) + r\left(tright)^{2} \frac{\partial^{2}}{(\partial t)^{2}}theta\left(tright) + 2 \, \frac{\partial}{\partial t}r\left(tright) \frac{\partial^{2}}{(\partial t)^{2}}r\left(tright)right)} \frac{\partial^{2}}{(\partial t)^{2}}r\left(tright)}{c^{3} {\left(\frac{\frac{\partial}{\partial t}r\left(tright)}{c} - 1right)}^{3}}$$

As already noted, if r is a function (presumably of time) and rdot its derivative w. r. t. time, diff(K2,rdot) is meaningless...

Note : eric_g answer is correct, but cannot be used in your case, since you want to replace a symbol by a function with the same name. You need to take a few steps :

1. r = var("r") # creates a variable denoted by r
2. ex = somexepression(r) # uses it
3. r1 = var("r1") # creates a new, temporary, symbol denoting a variable
4. ex1 = ex.subs({r:r1}) # a new, temporary, expression ; $r1=r \Rightarrow ex1 = ex$.
5. r = function("r") # the symbol r now denotes a function, whose expression is r-free
6. ex = ex1.subs({r1:r(t)}) # the variable r1 can now be replaced by the expression r(t)in ex1
7. reset("ex1, r1") # Housekeeping cleanup...

HTH,

more

The $\LaTeX$ code displayed fine while editing, but no longer renders correctly once the answer accepted by the site. Sorry...

Try view(eq1) and view(eq2) in a console (or show(...) in a notebook sheeet)...

( 2021-09-03 14:48:25 +0100 )edit

If rdot is a symbolic variable and r a symbolic function, eq.subs({rdot: diff(r(t), t)}) should work. For example:

sage: rdot, t = var('rdot t')
sage: eq = rdot^2 - 3*rdot
sage: r = function('r')
sage: eq.subs({rdot: diff(r(t), t)})
diff(r(t), t)^2 - 3*diff(r(t), t)
`
more