# Replacing multiplication of matrix elements by application of these elements as functions

I want to realize the following idea in Mathematica.

I've got a matrix

{{a,b},{c,d}}


which is multiplied to a vector {h,k} leading to

{{a h + b k}, {c h + d k}}.


Imagine now that a is an operator and I want to apply it to h, instead of multiplying. Primitive substitution {a h -> a@h} helps, but it is not quite a good approach while it's not working in more sophisticated cases.

Thank you!

EDIT1: The problem is solved partially, all comments are very useful. But still I am a little bit stacked, so I'm posting the update trying to explain my exact problem.

The problem is following. I want to construct the matrix

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


where a[x] is an operator (function) and b,c and d are arbitrary expressions (which are symbolic in general). After applying the operation

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


I want to obtain

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


I want this operation to work not only for numbers and functions as it was proposed in answers below but with arbitrary symbolic expressions. I mean I want Mathematica to understand that if x and p are not functions but just variables, then x*p means multiplication, otherwise it means x[p].

Moreover I want this operation to work in more general cases, e.g.

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


where M is an arbitrary matrix.

I would be very grateful for any ideas.

• Have you seen Inner, the generalization of Dot? Commented May 9, 2016 at 21:34
• For your last example, what is the expected output? Does the a[x] in Transpose[{{a[x], b},{c, d}}] act to the left on M (in which case an order of operations needs to be specified), or does it act to the right, leaving the expression as a function? Commented May 10, 2016 at 15:41
• One of the ways to accomplish this is to replace the non-functions with pure functions that are products, e.g. replace b with b*# & and leave a as a, and use the Inner answer below, but see my last comment: we need an answer to this before giving a complete answer. Commented May 10, 2016 at 15:43
• @march Concerning your question: I want this matrix to act to the left on M Commented May 10, 2016 at 17:00
• You say to replace a c x by c*a[x] in [[1,2]] and a c x by a[c x] in [[2, 1]]. That is exactly what I mean by non-associative. I think this means that you have to do these calculations left to right. Let me see if I can come up with something. Commented May 10, 2016 at 18:00

Picking up on Marius tip on Inner in the comments:

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


And @ciao offered a better version in comments:

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

• Or just Inner[#1[#2] &, {{a, b}, {c, d}}, {h, k}]...
– ciao
Commented May 9, 2016 at 23:20
• Yes, thank you. But it works only if all the elements of the matrix are functions. And what about the case if only 'a' is a function? @ciao Commented May 10, 2016 at 8:17
• @NikitaVostrosablin Inner[Function[{a, b}, If[NumericQ[a], a*b, a[b]]], {{Sin, 2}, {3, d}}, {h, k}]
– ciao
Commented May 10, 2016 at 8:25
• @ciao Thank you! It works :) Commented May 10, 2016 at 8:41
• @Nikita, maybe you should consider posting what your actual problem is; I suspect there might be a better way to get what you want. Commented May 10, 2016 at 13:13

If all the elements in the matrix are functions, you can also use

Block[{Times = (# @ #2 &)}, {{a, b}, {c, d}}.{h, k}]


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

• Of course, the latter only works if a sorts before h, b sorts before k, etc. Commented May 9, 2016 at 23:23
• @march, right -- and that is quite an if. I guess i should delete that part.
– kglr
Commented May 9, 2016 at 23:41