# Write an expression as a full derivative

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]

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

=================

=================

1

=================

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

∂tCos[t]+g[t]∂tCos[t]+g[t]\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.

I’ll try this at once. I’ll let you know how it goes. Thanks.
– Faq
Jun 10 ’14 at 10:30

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
– Faq
Jun 10 ’14 at 12:05

@LoveLearning No, those things in (* … *) are Mathematica comments that I only included to show what the output of the previous line is.
– Jens
Jun 10 ’14 at 16:06

Yeah I noticed after some playing around. I thought “…” = comments but I guess…
– Faq
Jun 10 ’14 at 16:51