# Defining a matrix with elements acting as operators

This question is directly related with my previous one.

I'll try to be more specific here. I have a matrix

{{a[x],b},{c,d}},


where a[x] is a function of its argument x and b,c,d are some arbitrary expressions. I want this matrix to act in the following way

{{a[x], b},{c, d}}.{{h}, {k}} = {{a[h] + b k}, {c h + d k}}.


It means that if a[x] is multiplied on some expression (multiplied from the left), it acts on it instead of just being multiplied. Expressions are multiplied in ordinary way.

One way to accomplish this is apllying

Inner[#1[#2] &, {{a, b*# &}, {c*# &, d*# &}}, {h, k}].


Now let us consider more sophisticated situation. I want this matrix to be able to act from the right as well. I mean I want to implement following operation

{{a[x], b},{c, d}}.M.Transpose[{{a[x], b},{c, d}}],


where M is arbitrary 2x2 matrix, e.g. M = {{x, 0},{0, y}}.

In the case of simple inner product this operation leads to

{{a, b},{c, d}} . {{x, 0},{0, y}}.{{a, c}, {b, d}} = {{a^2 x + b^2 y, a c x + b d y}, {a c x + b d y, c^2 x + d^2 y}}


And I want this operation to give me the same matrix with replacing: a^2by a[a[x]] in [[1,1]] element, a c x by c*a[x] in [[1,2]] element and a c x by a[c x] in [[2,1]] element.

I would be very grateful for any ideas.

• If h and k are elements of a finite set then you can write everything as matrices because a[x] is then discrete, too. Then one could use KroneckerProduct to construct the required larger matrix. – Jens May 17 '16 at 19:10

This could be done with a custom function for the first argument of Inner that treats a differently (it's just a more convenient form of expressing your original idea). For example, consider this:

ClearAll[f];
f[a, x_] := a[x]
f[x_, a] := a[x]
f[x_, y_] := x y


Inner[f, {{a, b}, {c, d}}, {h, k}]
(* {b k + a[h], c h + d k} *)


For the second example, note that Dot[a, b] is just Inner[Times, a, b]. So we can simply rephrase the first expression in terms of Inner and use our custom function f.

ClearAll@dot
dot[lists__List] := Fold[Inner[f, #, #2] &, First@{lists}, Rest@{lists}]

dot[{{a, b}, {c, d}}, {{x, 0}, {0, y}}, {{a, c}, {b, d}}]
(* {{b (b y + a[0]) + a[a[x]], d (b y + a[0]) + c a[x]},
{b d y + a[c x],c^2 x + d^2 y}} *)

• I would have been tempted to use NonCommutativeMultiply[] with Inner[] and then use replacement rules at the end, but this is more stylish. :) – J. M. will be back soon May 17 '16 at 17:44
• Thank you! That is exactly what I was looking for – Nikita Vostrosablin May 17 '16 at 18:06