2
$\begingroup$

How can a scalar be replaced with a matrix to perform operations on the matrix, not its elements?

For example,

A = RandomInteger[{-1, 1}, {4, 4}]
Sin[x] /. x -> A

returns something like

{{Sin[1], Sin[1], Sin[1], -Sin[1]}, 
{-Sin[1], -Sin[1], Sin[1], 0}, 
{0, -Sin[1], -Sin[1], 0}, 
{-Sin[1], Sin[1], -Sin[1], Sin[1]}}

instead of matrix sine.

$\endgroup$
1
  • $\begingroup$ Functions of matrices are tricky beasts, as usually ones needs the eigensystem of the matrix etc. Thus, I don't think this question is well posed $\endgroup$
    – Hans Olo
    Feb 18 at 19:54

1 Answer 1

7
$\begingroup$

"MatrixFunction" will do what you want. Here is an example with "Sqrt":

a = RandomReal[{-1, 1}, {4, 4}];
sq = MatrixFunction[Sqrt, a];

Chop[sq . sq - a]

{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}

I used "Chop" because of numerical errors due to machine precision.

$\endgroup$
3
  • $\begingroup$ Also MatrixPower[sq, 2] - a // Chop $\endgroup$
    – Bob Hanlon
    Feb 18 at 21:28
  • $\begingroup$ Does it work with user-defined things? Will something like MatrixFunction[Sin/2 + Sqrt + 1, a] work? $\endgroup$ Feb 18 at 23:00
  • 3
    $\begingroup$ @homocomputeris For that, you'll need to define a pure function: MatrixFunction[Sin[#]/2 + Sqrt[#] + 1 &, a]. $\endgroup$
    – march
    Feb 19 at 2:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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