1
$\begingroup$

Is there a command that tells Mathematica to try to write the terms (if possible) of an expression as total derivates? Example: I want to tell mathematica to write

-Sin[t]+g[t] 

as

D[Cos[t],t]+g[t] 

instead. Is this possible?

Mathematically, this is basically (up to some constants) equivalent of Mathematica testing if the terms are analytically integrable (i.e. if they have closed form expressions).

$\endgroup$

1 Answer 1

6
$\begingroup$

Here I define the expression and integrate it term by term using Distribute. Then I differentiate each term but with a Defer wrapping it, so the derivatives aren't actually carried out. FInally, I replace the terms that weren't integrated by the original function:

expression = -Sin[t] + g[t]

(* ==> g[t] - Sin[t] *)

integrals = Distribute[Integrate[expression, t]]

(* ==> Cos[t] + \[Integral]g[t] \[DifferentialD]t *)

original = 
 Map[Defer[D[#, t]] &, integrals] /. 
  Defer[D[Integrate[f_, __], _]] :> f

$\partial_t \text{Cos}[t] + \text{g}[t]$

The reason I used Defer is that it allows you to copy the output and evaluate it by pasting it in a new cell. You could alternatively replace Defer with HoldForm if you want a more "stable" output. Then you have to explicitly use ReleaseHold if you want to evaluate the derivatives in the output expression.

$\endgroup$
4
  • $\begingroup$ I'll try this at once. I'll let you know how it goes. Thanks. $\endgroup$ Commented Jun 10, 2014 at 10:30
  • $\begingroup$ May I ask what is the role of e.g. (* ==> g[t] - Sin[t] *) is. Do I need to change that if I want to use different functions instead of Sin t and g[t]? @Jens $\endgroup$ Commented Jun 10, 2014 at 12:05
  • $\begingroup$ @LoveLearning No, those things in (* ... *) are Mathematica comments that I only included to show what the output of the previous line is. $\endgroup$
    – Jens
    Commented Jun 10, 2014 at 16:06
  • $\begingroup$ Yeah I noticed after some playing around. I thought "..." = comments but I guess... $\endgroup$ Commented Jun 10, 2014 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.