# Multiply function-valued matrices

I would like to create a notebook for calculations in supersymmetric quantum mechanics. The basic building blocks are two functions (or maybe better: operators):

A := (W[#1] #2 + D[#2, #1]) &;
B := (W[#1] #2 -D[#2, #1]) &;


With these definitions you can apply these operators to a pair of a variable x and a function f[x] like A[x,f[x]] and one obtains f[x] W[x] + Derivative[1][f][x].

Now, these functions A and B are embedded into matrices:

Q1 := ({{0, 0}, {A[#1, #2], 0}}) &;
Q2 := ({{0, B[#1, #2]}, {0, 0}}) &;


Again, it is possible to apply these matrices to a variable x and a function f[x] like Q[x,f[x]] and one obtains

{{0, 0}, {f[x] W[x] + Derivative[1][f][x], 0}}


I wonder, if there is a possibility to multiply the matrices Q1 and Q2 in such a way that the result is again a function that expects the two arguments [x,f[x]].

The trouble is, that the internal matrix operations have to distinguish between multiplication and addition of ordinary numbers and functions. I succeeded to implement addition and multiplication that is sensitive to the nature of the operands on the level of A and B,eg.

mul[a_, b_] := If[NumberQ[a] && NumberQ[b], a*b,
If[NumberQ[a], Through[a*b[#1, #2], 0] &,
If[NumberQ[b], Through[b*a[#1, #2], 0] &,
a[#1, b[#1, #2]] &
]]];


and

add[a_, b_] :=If[NumberQ[a] && NumberQ[b], a + b,
If[NumberQ[a], a + b[#1, #2] &,
If[NumberQ[b], b + a[#1, #2] &,
Through[a[#1, #2] + b[#1, #2], 0] &]]]
]];


I have no idea, how to deal with the matrix multiplication. For instance, how can I get the (2,1) component of Q as a function. If I write Q[[1,2,1]], I get #2 W[#1] but I would need W[#1] #2 + D[#2,#1]&?

• A = W[#1] #2 + HoldForm@D[#2, #1] &  ? – Fortsaint Jun 17 at 21:20

If you're looking for a generalized matrix product, then Inner will do the trick. This snippet performs a matrix product, where multiplication is replaced with mul and addition with add:

Inner[mul,{{a,b},{c,d}},{{w,x},{y,z}},add]


However, Q1 and Q2 are written as functions, rather than matrices with function-valued elements. Using them as written, the matrix product would be implemented like this:

Q = Q1[#1,#2].Q2[#1,#2]&


Then a particular component could be reached with Q[#1,#2][[row,col]]&.

• Thank you so far, but I need the combination of both ideas. Q1[#1,#2].Q2[#1,#2]& works fine, however, it just multiplies the functions of the components of the matrices. In the customized mul, the functions are composed A[#1,A[#1,#2]]&. So, I tried Mmul[a_,b_]:=Inner[mul,a[#1,#2],b[#1,#2],add]&, which doesn't work however. – p6majo Jun 18 at 9:08
• Also, just Mmul[a_, b_] :=Inner[mul, a, b, add]  does not work. What is the correct syntax for this situation? – p6majo Jun 18 at 9:42
• The reason Inner won't work is because Q isn't actually a matrix. Q[x,f[x]] is a matrix with numeric elements. If you define them like Q1 := ({{0, 0}, {A, 0}}), then Inner will work. – Woofmao Jun 18 at 13:44
• Fine, this works. Now I can access elements of Q=Inner[mul,Q1,Q2,Plus] via Through[Q[[2, 2]][x, f[x]]] for example. Is there a way to perform this for the full matrix in one step? Just Through[Q[x,f[x]]] doesn't work, since the matrix itself is not a function now but only its components. – p6majo Jun 18 at 20:07
• You can use Map[#[x,f]&, Q, {2}] to call the function for each cell of the matrix. – Woofmao Jun 19 at 13:33