# Take function as input for other function, access variable

I am very new to Mathematica, and I searched the documentation and google now for quite some time.

Let's say, I want to implement a mathematica function, that does the following mapping: $$f(t)\mapsto \int_0^t f(s)\ \mathrm{d}s+f'(t)$$. ($$f$$ is assumed to be differentiable and integrable.)

Edit: It should also work for $$\mathbb{R}\longrightarrow\mathbb{R}^n$$ functions.

How would you do that? It should be a function like

IntAndDiff[f_]=Integrate[f,{s,0,t}]+D[f,t]

which does of course not work. How am I able to "access" the variable of $$f$$ in this case, so that I can define the integration and differentiation accordingly?

Edit: I also want this function to behave like an operator, so the output should be a $$\mathbb{R}\longrightarrow\mathbb{R}^n$$ function again. I.e. I want to be able to apply this (and other such operators) on the result again...

• ClearAll[IntAndDiff];IntAndDiff[f_] = Integrate[f@s, {s, 0, t}] + D[f@t, t]?
– kglr
Dec 31, 2020 at 0:07
• As @kglr suggested, except use SetDelayed, i.e., IntAndDiff[f_] := Integrate[f[s], {s, 0, t}] + D[f[t], t] Examples: IntAndDiff /@ {Sin, Sin[#] &, Cos, Cos[#] &, Sqrt, Sqrt[#] &, #^2 + 2 # - 3 &} Dec 31, 2020 at 0:35

$$f(t)\mapsto \int_0^t f(s)\ \mathrm{d}s+f'(t)$$

A little analysis:

Input is actually a function $$f(t)$$, the output is also a function $$F(t)=\int_0^t f(s)\ \mathrm{d}s+f'(t)$$.

So an easy way is to introduce $$t$$

F[f_, t_]:= Integrate[f[s], {s, 0, t}] + f'[t]
F[Sin,t] (*1*)


Then use Function to make it a pure function

IntAddDiff[f_] := Function[t, Evaluate[Integrate[f[s], {s, 0, t}] + f'[t]]]


Then use # and & to remove t

IntAddDiff[f_] := Evaluate[Integrate[f[s], {s, 0, #}] + f'[#]] &

IntAddDiff[Sin] (*1 &*)
IntAddDiff[#^2 &] (*2 #1 + #1^3/3 &*)

• For me this looks like the solution that works best for my application. I will do some testing, and after this accept and mark the question as answered.
– NG98
Jan 3, 2021 at 10:29

By using Through we can deal with multiple function {F,G,H} map at t

intAndDiff[f___][t_] :=
Integrate[Through[{f}@s], {s, 0, t}] +
Through[(Derivative[1] /@ {f})@t];
intAndDiff[Sin, Cos, #^2 &][x]


{1, 0, 2 x + x^3/3}

Original

IntAndDiff[f_][t_] := Integrate[f[s], {s, 0, t}] + D[f[t], t];
IntAndDiff[#^2 &][x]
IntAndDiff[Sin][x]


2 x + x^3/3

1

• I think this suggestion is very close to the solution I search for. It does however not work as intended in the case, where f is a function, that takes a scalar as input and returns a vector, as I cannot "concatenate" it. I.e. I search for an operator, so I take a R->R^n function as input and get a R->R^n function as output...
– NG98
Jan 2, 2021 at 10:34

Same as @cvgmt's solution but defined as an operator, so that it also works on $$\mathbb{R}\to\mathbb{R}^n$$ functions. Both the input and the output are now pure functions:

IntAndDiff[f_] := Function[t, Evaluate[Integrate[f[s], {s, 0, t}] + D[f[t], t]]]

IntAndDiff[#^2 &]
(*    Function[t$$, 2 t$$ + t$^3/3] *) IntAndDiff[Sin] (* Function[t$, 1]    *)

IntAndDiff[{#, #^2, #^3} &]
(*    Function[t$$, {1 + t^2/2, 2 t + t^3/3, 3 t^2 + t$$^4/4}]    *)