I want to realize the following idea in Mathematica.

I've got a matrix


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.

  • 2
    $\begingroup$ Have you seen Inner, the generalization of Dot? $\endgroup$ May 9 '16 at 21:34
  • $\begingroup$ 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? $\endgroup$
    – march
    May 10 '16 at 15:41
  • 1
    $\begingroup$ 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. $\endgroup$
    – march
    May 10 '16 at 15:43
  • $\begingroup$ @march Concerning your question: I want this matrix to act to the left on M $\endgroup$ May 10 '16 at 17:00
  • 1
    $\begingroup$ 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. $\endgroup$
    – march
    May 10 '16 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}]
  • 2
    $\begingroup$ Or just Inner[#1[#2] &, {{a, b}, {c, d}}, {h, k}]... $\endgroup$
    – ciao
    May 9 '16 at 23:20
  • $\begingroup$ 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 $\endgroup$ May 10 '16 at 8:17
  • 1
    $\begingroup$ @NikitaVostrosablin Inner[Function[{a, b}, If[NumericQ[a], a*b, a[b]]], {{Sin, 2}, {3, d}}, {h, k}] $\endgroup$
    – ciao
    May 10 '16 at 8:25
  • $\begingroup$ @ciao Thank you! It works :) $\endgroup$ May 10 '16 at 8:41
  • 1
    $\begingroup$ @Nikita, maybe you should consider posting what your actual problem is; I suspect there might be a better way to get what you want. $\endgroup$
    – J. M.'s torpor
    May 10 '16 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]}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.