How can one define in a functional way a 1st-order linear differential operator involving several independent variables that can then be applied to a function of that many variables?
Consider an example with two variables, where one wants to form D[f[x, y], x] + 3 D[f[x, y], y]
from a function f
. Of course one could define
diff[f_][x_, y_] := D[f[x, y], x] + 3 D[f[x, y], y]
so that for a function such as
g[x_, y_] := x^2 y + Cos[x + 2 y]
we just evaluate:
diff[g][x, y]
2 x y + 3 (x^2 - 2 Sin[x + 2 y]) - Sin[x + 2 y] (* desired final output *)
But how can such an operator be defined functionally, that is, without explicitly using variables initially?
We could try
diffOp[y_] := Derivative[1, 0][y] + 3 Derivative[0, 1][y]
and then
diffOp[g]
3 (-2 Sin[#1 + 2 #2] + #1^2 &) + (-Sin[#1 + 2 #2] + 2 #1 #2 &)
But now how does one use such a combination of pure functions of several variables so as to produce the same result as from diff[g][x,y]
?
The crux of the difficulty appears in the following simpler problem. Consider two functions of two variables:
g1 = (#1^2 + #2) &
g2 = Cos[#1 #2] &
How can one produce the same result as, say,
g1[x, y] + 3 g2[x, y]
(* x^2 + y + 3 Cos[x y] *)
directly from the functional linear combination g1 + 3 g2
-- by forming an expression of the form oper[g1 + 3 g2][x, y]
?
By contrast with the single-variable situation, where a simple Through
would serve as the oper',
Through` will not work in the multi-variable situation here:
Through[(g1 + 3 g2)[x, y]]
(* x^2 + y + (3 (Cos[#1 #2] &))[x, y] *)
A pure function embedded in that output.
Note that a simple sum g1 + g2
instead of the linear combination g1 + 3 g2
, Through
will work (just as it does for a single variable):
Through[(g1 + g2)[x, y]]
(* x^2 + y + Cos[x y] *)
Through[(g1 + g2)[x, y]]
? $\endgroup$Through[(f1 + 3 f2)[x]]
$\endgroup$thru[expr_[vars__]] := expr /. f_Function :> f[vars]
$\endgroup$