Skip to main content
added maple result for comparing
Source Link
Nasser
  • 150.5k
  • 12
  • 161
  • 374

I agree the imaginary parts should be zero. I do not know why they are not zero. But this is what I found, too small to put in comment.

First, Matlab does give zero for the exact same input:

format long g
mat = [2, 2.161209223472559 + 1.682941969615793*1j; 
       2.161209223472559 - 1.682941969615793*1j, 2]

inv(mat)
-0.570919803469126 + 0i                   0.616938572560308 + 0.480412449271497i
0.616938572560308 - 0.480412449271497i    -0.570919803469126+0i

You can get same output in Mathematica by doing the direct computation itself without calling Inverse

foo[x11_, x12_, x21_, x22_] := 
    Module[{}, {{x22, -x12}, {-x21, x11}}/(x11*x22 - x21*x12)]
foo[2, 2.161209223472559 + 1.682941969615793 I, 
     2.161209223472559 - 1.682941969615793 I, 2]

Mathematica graphics

It is an exact zero for the complex part:

Mathematica graphics

And can see it match the Matlab output. Even compiled version did not resolve the issue (even though told it to run in hardware floating point)

cf = Compile[{{x11, _Complex},{x12, _Complex},{x21, _Complex},
        {x22,_Complex}},
   Inverse[{{x22, -x12}, {-x21, x11}}], RuntimeOptions -> "Speed"
   ];

cf[2, 2.161209223472559^1 + 1.682941969615793^1 I, 
 2.161209223472559^1 - 1.682941969615793^1 I, 2]

Mathematica graphics

If we do break the inverse to 2 parts, and do one by 'hand' and then use Det only, then now the accuracy improves a little, and now it is of order 10^-17

foo1[x11_, x12_, x21_, x22_] := 
 Module[{}, {{x22, -x12}, {-x21, x11}}/Det[{{x11, x12}, {x21, x22}}]]
foo1[2, 2.161209223472559 + 1.682941969615793 I, 
 2.161209223472559 - 1.682941969615793 I, 2]

Mathematica graphics

Here is Maple 2015 result also:

mat:=<<2|2.161209223472559 + 1.682941969615793*I>,
      <2.161209223472559 - 1.682941969615793*I|2>>;
LinearAlgebra[MatrixInverse](mat);

Mathematica graphics

I agree the imaginary parts should be zero. I do not know why they are not zero. But this is what I found, too small to put in comment.

First, Matlab does give zero for the exact same input:

format long g
mat = [2, 2.161209223472559 + 1.682941969615793*1j; 
       2.161209223472559 - 1.682941969615793*1j, 2]

inv(mat)
-0.570919803469126 + 0i                   0.616938572560308 + 0.480412449271497i
0.616938572560308 - 0.480412449271497i    -0.570919803469126+0i

You can get same output in Mathematica by doing the direct computation itself without calling Inverse

foo[x11_, x12_, x21_, x22_] := 
    Module[{}, {{x22, -x12}, {-x21, x11}}/(x11*x22 - x21*x12)]
foo[2, 2.161209223472559 + 1.682941969615793 I, 
     2.161209223472559 - 1.682941969615793 I, 2]

Mathematica graphics

It is an exact zero for the complex part:

Mathematica graphics

And can see it match the Matlab output. Even compiled version did not resolve the issue (even though told it to run in hardware floating point)

cf = Compile[{{x11, _Complex},{x12, _Complex},{x21, _Complex},
        {x22,_Complex}},
   Inverse[{{x22, -x12}, {-x21, x11}}], RuntimeOptions -> "Speed"
   ];

cf[2, 2.161209223472559^1 + 1.682941969615793^1 I, 
 2.161209223472559^1 - 1.682941969615793^1 I, 2]

Mathematica graphics

If we do break the inverse to 2 parts, and do one by 'hand' and then use Det only, then now the accuracy improves a little, and now it is of order 10^-17

foo1[x11_, x12_, x21_, x22_] := 
 Module[{}, {{x22, -x12}, {-x21, x11}}/Det[{{x11, x12}, {x21, x22}}]]
foo1[2, 2.161209223472559 + 1.682941969615793 I, 
 2.161209223472559 - 1.682941969615793 I, 2]

Mathematica graphics

I agree the imaginary parts should be zero. I do not know why they are not zero. But this is what I found, too small to put in comment.

First, Matlab does give zero for the exact same input:

format long g
mat = [2, 2.161209223472559 + 1.682941969615793*1j; 
       2.161209223472559 - 1.682941969615793*1j, 2]

inv(mat)
-0.570919803469126 + 0i                   0.616938572560308 + 0.480412449271497i
0.616938572560308 - 0.480412449271497i    -0.570919803469126+0i

You can get same output in Mathematica by doing the direct computation itself without calling Inverse

foo[x11_, x12_, x21_, x22_] := 
    Module[{}, {{x22, -x12}, {-x21, x11}}/(x11*x22 - x21*x12)]
foo[2, 2.161209223472559 + 1.682941969615793 I, 
     2.161209223472559 - 1.682941969615793 I, 2]

Mathematica graphics

It is an exact zero for the complex part:

Mathematica graphics

And can see it match the Matlab output. Even compiled version did not resolve the issue (even though told it to run in hardware floating point)

cf = Compile[{{x11, _Complex},{x12, _Complex},{x21, _Complex},
        {x22,_Complex}},
   Inverse[{{x22, -x12}, {-x21, x11}}], RuntimeOptions -> "Speed"
   ];

cf[2, 2.161209223472559^1 + 1.682941969615793^1 I, 
 2.161209223472559^1 - 1.682941969615793^1 I, 2]

Mathematica graphics

If we do break the inverse to 2 parts, and do one by 'hand' and then use Det only, then now the accuracy improves a little, and now it is of order 10^-17

foo1[x11_, x12_, x21_, x22_] := 
 Module[{}, {{x22, -x12}, {-x21, x11}}/Det[{{x11, x12}, {x21, x22}}]]
foo1[2, 2.161209223472559 + 1.682941969615793 I, 
 2.161209223472559 - 1.682941969615793 I, 2]

Mathematica graphics

Here is Maple 2015 result also:

mat:=<<2|2.161209223472559 + 1.682941969615793*I>,
      <2.161209223472559 - 1.682941969615793*I|2>>;
LinearAlgebra[MatrixInverse](mat);

Mathematica graphics

Source Link
Nasser
  • 150.5k
  • 12
  • 161
  • 374

I agree the imaginary parts should be zero. I do not know why they are not zero. But this is what I found, too small to put in comment.

First, Matlab does give zero for the exact same input:

format long g
mat = [2, 2.161209223472559 + 1.682941969615793*1j; 
       2.161209223472559 - 1.682941969615793*1j, 2]

inv(mat)
-0.570919803469126 + 0i                   0.616938572560308 + 0.480412449271497i
0.616938572560308 - 0.480412449271497i    -0.570919803469126+0i

You can get same output in Mathematica by doing the direct computation itself without calling Inverse

foo[x11_, x12_, x21_, x22_] := 
    Module[{}, {{x22, -x12}, {-x21, x11}}/(x11*x22 - x21*x12)]
foo[2, 2.161209223472559 + 1.682941969615793 I, 
     2.161209223472559 - 1.682941969615793 I, 2]

Mathematica graphics

It is an exact zero for the complex part:

Mathematica graphics

And can see it match the Matlab output. Even compiled version did not resolve the issue (even though told it to run in hardware floating point)

cf = Compile[{{x11, _Complex},{x12, _Complex},{x21, _Complex},
        {x22,_Complex}},
   Inverse[{{x22, -x12}, {-x21, x11}}], RuntimeOptions -> "Speed"
   ];

cf[2, 2.161209223472559^1 + 1.682941969615793^1 I, 
 2.161209223472559^1 - 1.682941969615793^1 I, 2]

Mathematica graphics

If we do break the inverse to 2 parts, and do one by 'hand' and then use Det only, then now the accuracy improves a little, and now it is of order 10^-17

foo1[x11_, x12_, x21_, x22_] := 
 Module[{}, {{x22, -x12}, {-x21, x11}}/Det[{{x11, x12}, {x21, x22}}]]
foo1[2, 2.161209223472559 + 1.682941969615793 I, 
 2.161209223472559 - 1.682941969615793 I, 2]

Mathematica graphics