3
$\begingroup$

I am a beginner to Mathematica. I am trying to define a function which would evalute the Cayley matrix transform, namely taking a matrix $M$ such that $I + M$ is invertible to $(I-M)(I+M)^{-1}$. I guess I would first like to cover 2-by-2 matrices.

My code is

cayley[x_] := 
  (IdentityMatrix[2] - x) . (Inverse[IdentityMatrix[2] + x]) \; 
  Det[IdentityMatrix[2] + x] != 0

I am getting an error.

cayley[x_] cannot be followed by ...

Surely there is some very basic syntax error, but after some trial and error I cannot figure it out from the documentation or the content of the error message. Any help much appreciated!

P.S. bonus points for helping me to add the size of the matrix as another parameter. Would this simply be cayley[x_,n_] := ..., and replacing 2 by n in the formula?

$\endgroup$
  • $\begingroup$ Something is funky - what is \\; supposed to be? I think that's what the compaint is about. It looks like you're trying to set a condition under which the previous statement will evaluate, e.g. cayley[x_]/; (Det[IdentityMatrix[2] + x] != 0) :=(IdentityMatrix[2] - x) . (Inverse[IdentityMatrix[2] + x]); will evaluate if the condition is true, and return unevaluated otherwise. $\endgroup$ – N.J.Evans Aug 21 '20 at 15:05
  • 2
    $\begingroup$ cayley[x_?MatrixQ] := Module[{n = Dimensions[x][[1]]}, (IdentityMatrix[n] - x).Inverse[IdentityMatrix[n] + x]] /; (Equal @@Dimensions[x]) && Det[IdentityMatrix[n] + x] != 0 $\endgroup$ – Bob Hanlon Aug 21 '20 at 15:20
  • 2
    $\begingroup$ Oh wait @Evans, my \; is supposed to be /; , now it works! Wish I could accept your comment as an answer for I was mostly interested in what's wrong with my code $\endgroup$ – the_lar Aug 21 '20 at 16:11
  • 1
    $\begingroup$ Thanks @Bob, this seems to work. $\endgroup$ – the_lar Aug 21 '20 at 16:11
  • 1
    $\begingroup$ Actually it doesn't work if n has a global value. It should be cayley[x_?MatrixQ] := Module[{n = Dimensions[x][[1]]}, (IdentityMatrix[n] - x).Inverse[IdentityMatrix[n] + x]] /; (Equal @@ Dimensions[x]) && Det[IdentityMatrix[Dimensions[x][[1]]] + x] != 0 However, you got a better answer from @mgoldberg $\endgroup$ – Bob Hanlon Aug 21 '20 at 16:15
5
$\begingroup$

Will this work for you?

cayley[x_] /;
    (SquareMatrixQ[x] && Det[IdentityMatrix[Length[x]] + x] != 0) :=
  Module[{i = IdentityMatrix[Length[x]]},
    (i - x).Inverse[i + x]]

Some tests

m = Partition[Range[16], 4];cayley[m]
{{-(13/9), -(4/3), -(2/9), 8/9}, {-(6/5), 1/5, -(2/5), 0}, 
 {2/45, -(4/15), 19/45, -(8/9)}, {58/45, 4/15, -(34/45), -(7/9)}}

Not a square matrix.

mx = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; cayley[mx]
cayley[{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}]

$M+I$ not invertible.

my = -IdentityMatrix[4]; cayley[my] 
cayley[{{-1, 0, 0, 0}, {0, -1, 0, 0}, {0, 0, -1, 0}, {0, 0, 0, -1}}]
$\endgroup$
  • $\begingroup$ Thanks @m_goldberg $\endgroup$ – the_lar Aug 21 '20 at 16:12

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.