question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to suppress complex numbers in the calculation of the square root of a positive semi-definite matrix using scipy.linalg.sqrtm()

See original GitHub issue

Say I have a matrix A (this matrix is quite large so I did not specify this here), and applying scipy.linalg.sqrtm(A), I get the following output matrix which contains complex numbers:

array([[ 64.06444692+8.96831017e-44j,  -6.28785787+1.57772181e-30j,
         -9.15586255+3.94430453e-31j,  -9.00198187+1.97215226e-31j,
         -7.71376342+7.88860905e-31j,  -6.28785787-1.97215226e-30j,
          2.97380679+0.00000000e+00j,   1.29893642+7.88860905e-31j,
          1.31682879+1.57772181e-30j,  -0.351662  +7.88860905e-31j,
         -9.15586255-6.90253292e-31j,   1.29893642+1.97215226e-31j,
          2.36636534+4.48415509e-44j,  -0.57657211+9.86076132e-31j,
          0.39917092-2.95822839e-31j,  -9.00198187-3.94430453e-31j,
          1.31682879-1.57772181e-30j,  -0.57657211-1.97215226e-31j,
          2.36648045+4.48415509e-44j,   0.28162   +1.97215226e-31j,
         -7.71376342-7.88860905e-31j,  -0.351662  +0.00000000e+00j,
          0.39917092+6.90253292e-31j,   0.28162   -3.94430453e-31j,
          1.21720917+3.58732407e-43j],
       [ -6.28785787+0.00000000e+00j,  37.43772093+0.00000000e+00j,
         -4.54039284+6.61744490e-24j,  -4.70636743+3.30872245e-24j,
         -2.75237216+6.61744490e-24j,  37.43772062-2.64697796e-23j,
         -5.74532523-2.25694915e-36j,  -3.84385982-1.32348898e-23j,
         -4.65882698+3.30872245e-24j,  -2.99673941+2.64697796e-23j,
         -4.54039267+0.00000000e+00j,  -3.84386008+1.32348898e-23j,
         -0.51143448+3.00926554e-36j,  -6.96326972+3.97046694e-23j,
          2.14326501-3.97046694e-23j,  -4.7063676 +0.00000000e+00j,
         -4.65882709-3.30872245e-24j,  -6.96326948+6.61744490e-24j,
          0.72851489+1.69271187e-36j,   2.15291748+0.00000000e+00j,
         -2.75237195-1.32348898e-23j,  -2.99674005-2.64697796e-23j,
          2.14326499+2.64697796e-23j,   2.15291766+0.00000000e+00j,
          0.2575331 +0.00000000e+00j],
       [ -9.15586255-3.76158192e-37j,  -4.54039278+8.27180613e-24j,
         32.72911898-4.96308368e-24j,  -1.85021394-1.65436123e-24j,
         -2.53702773+9.92616735e-24j,  -4.54039273-1.48892510e-23j,
          1.83872342-1.12847458e-36j,  -1.86092192-6.61744490e-24j,
         -4.29572397+9.92616735e-24j,   1.09027491-2.64697796e-23j,
         32.72911818+0.00000000e+00j,  -1.86092174+6.61744490e-24j,
         -9.24894513-9.40395481e-38j,  -4.93062942+3.30872245e-24j,
         -2.40960071+2.31610572e-23j,  -1.85021376+0.00000000e+00j,
         -4.29572395-6.61744490e-24j,  -4.93062949+0.00000000e+00j,
          1.40879764+6.58276836e-37j,   1.34936369+1.98523347e-23j,
         -2.53702818-5.79026429e-24j,   1.090275  +1.32348898e-23j,
         -2.40960075-1.98523347e-23j,   1.34936389-1.65436123e-23j,
          0.47563635+3.00926554e-36j],
       [ -9.00198187+5.17217514e-36j,  -4.70636737+3.30872245e-23j,
         -1.85021382+1.65436123e-23j,  32.3679699 -3.30872245e-24j,
         -2.62837621+1.65436123e-23j,  -4.70636767-3.30872245e-23j,
          1.64638076-7.52316385e-37j,  -4.2159266 -2.97785021e-23j,
         -0.86792129+6.61744490e-24j,   0.99791227-5.29395592e-23j,
         -1.85021388-1.15805286e-23j,  -4.21592618+2.81241408e-23j,
          1.17619748+6.01853108e-36j,  -3.93042699-6.61744490e-24j,
          1.17237038+6.61744490e-24j,  32.36796885+6.61744490e-24j,
         -0.86792107-6.61744490e-24j,  -3.93042699+6.61744490e-24j,
        -10.37559738+1.12847458e-36j,  -2.60535084-5.62482817e-23j,
         -2.62837615-1.65436123e-23j,   0.99791236+4.63221143e-23j,
          1.17237044-6.61744490e-24j,  -2.60535074+5.62482817e-23j,
          0.59574761-3.00926554e-36j],
       [ -7.71376342+4.51389831e-36j,  -2.75237237+1.98523347e-23j,
         -2.53702781+3.30872245e-24j,  -2.62837605+3.30872245e-24j,
          5.69625179+1.32348898e-23j,  -2.75237175-1.98523347e-23j,
         -0.56750495+1.12847458e-36j,   1.03933818-1.32348898e-23j,
          1.0435955 -6.61744490e-24j,   0.3804354 -1.32348898e-23j,
         -2.5370281 -3.30872245e-24j,   1.03933817+1.32348898e-23j,
          0.42008941+3.00926554e-36j,   2.3668576 -6.61744490e-24j,
         -0.24619723-5.29395592e-23j,  -2.62837631+0.00000000e+00j,
          1.04359542+0.00000000e+00j,   2.3668577 +8.27180613e-24j,
          0.34120424+3.76158192e-37j,  -0.2668792 -5.29395592e-23j,
          5.6962513 -6.61744490e-24j,   0.38043513-6.61744490e-24j,
         -0.24619695+3.97046694e-23j,  -0.2668791 +4.30133919e-23j,
         -1.24019775+0.00000000e+00j],
       [ -6.28785787+0.00000000e+00j,  37.43772062-2.64697796e-23j,
         -4.54039267-1.32348898e-23j,  -4.7063676 +0.00000000e+00j,
         -2.75237195-3.30872245e-24j,  37.43772093+2.64697796e-23j,
         -5.74532523+7.52316385e-37j,  -3.84386008+0.00000000e+00j,
         -4.65882709+0.00000000e+00j,  -2.99674005-2.64697796e-23j,
         -4.54039284+1.98523347e-23j,  -3.84385982+0.00000000e+00j,
         -0.51143448-3.00926554e-36j,  -6.96326948-2.64697796e-23j,
          2.14326499+3.97046694e-23j,  -4.70636743-6.61744490e-24j,
         -4.65882698+0.00000000e+00j,  -6.96326972+0.00000000e+00j,
          0.72851489-1.78675141e-36j,   2.15291766+0.00000000e+00j,
         -2.75237216+3.30872245e-24j,  -2.99673941+1.32348898e-23j,
          2.14326501-3.30872245e-23j,   2.15291748-1.32348898e-23j,
          0.2575331 +0.00000000e+00j],
       [  2.97380679+2.24207754e-44j,  -5.74532523+0.00000000e+00j,
          1.83872342-1.57772181e-30j,   1.64638076-3.69778549e-31j,
         -0.56750495+1.57772181e-30j,  -5.74532523+7.88860905e-31j,
         66.4932094 +0.00000000e+00j, -10.70816264+2.56379794e-30j,
        -11.08111069+1.97215226e-31j,  -7.43474479-1.97215226e-31j,
          1.83872342+7.88860905e-31j, -10.70816264-1.57772181e-30j,
          1.86080059+2.69049305e-43j,   0.19541758+7.88860905e-31j,
          0.56304712+1.57772181e-30j,   1.64638076+3.20474743e-31j,
        -11.08111069+1.97215226e-31j,   0.19541758+0.00000000e+00j,
          1.96616419+4.48415509e-44j,   0.54752066-1.18329136e-30j,
         -0.56750495-1.57772181e-30j,  -7.43474479-3.94430453e-31j,
          0.56304712-1.57772181e-30j,   0.54752066+1.08468374e-30j,
          1.07252937+5.38098610e-43j],
       [  1.29893642+9.78011300e-36j,  -3.84386014+5.29395592e-23j,
         -1.86092176+9.92616735e-24j,  -4.21592632+1.32348898e-23j,
          1.03933803+6.61744490e-24j,  -3.84385976-6.61744490e-23j,
        -10.70816264+1.88079096e-36j,  31.76054462-5.29395592e-23j,
         -0.81206901+1.98523347e-23j,  -2.42506017-7.27918939e-23j,
         -1.8609219 -2.64697796e-23j,  31.76054419+2.64697796e-23j,
         -9.99217824+6.01853108e-36j,  -4.18589448-4.30133919e-23j,
         -2.25738823+0.00000000e+00j,  -4.21592646+0.00000000e+00j,
         -0.81206922-6.61744490e-24j,  -4.18589417+4.63221143e-23j,
          1.53827682+1.88079096e-37j,   1.02240603-1.45583788e-22j,
          1.03933832-6.61744490e-24j,  -2.42506066+7.27918939e-23j,
         -2.2573882 -1.32348898e-23j,   1.02240648+1.45583788e-22j,
          0.4993346 -1.20370622e-35j],
       [  1.31682879+9.02779661e-36j,  -4.65882716+3.30872245e-23j,
         -4.29572396+1.32348898e-23j,  -0.86792122+7.44462551e-24j,
          1.04359537+2.64697796e-23j,  -4.65882691-5.29395592e-23j,
        -11.08111069+3.00926554e-36j,  -0.81206926-5.29395592e-23j,
         32.00910946+1.32348898e-23j,  -2.29180333-6.61744490e-23j,
         -4.29572396-1.98523347e-23j,  -0.81206897+7.94093388e-23j,
          1.42042175+4.51389831e-36j,  -3.87247572-2.97785021e-23j,
          0.971801  -1.98523347e-23j,  -0.86792115-2.48154184e-24j,
         32.00910859-2.64697796e-23j,  -3.87247562+3.63959470e-23j,
        -10.69471254-9.40395481e-38j,  -2.38335511-7.94093388e-23j,
          1.04359555-1.32348898e-23j,  -2.29180342+7.27918939e-23j,
          0.97180114+0.00000000e+00j,  -2.38335456+1.19114008e-22j,
          0.51309298-3.00926554e-36j],
       [ -0.351662  -6.77084746e-36j,  -2.99673978-2.64697796e-23j,
          1.09027494-2.64697796e-23j,   0.99791231-3.30872245e-24j,
          0.38043531+6.61744490e-24j,  -2.99673969+2.64697796e-23j,
         -7.43474479-1.78675141e-36j,  -2.42506037+2.31610572e-23j,
         -2.29180337-4.96308368e-24j,   5.48456743+5.29395592e-23j,
          1.09027497+1.98523347e-23j,  -2.42506046-1.15805286e-23j,
          0.66133065-4.51389831e-36j,   2.08403436+3.63959470e-23j,
         -0.34767484-4.30133919e-23j,   0.99791231+4.96308368e-24j,
         -2.29180338+5.79026429e-24j,   2.08403451-1.32348898e-23j,
          0.53327358+1.03443503e-36j,  -0.33110204+7.23783036e-23j,
          0.38043522-6.61744490e-24j,   5.48456708-5.29395592e-23j,
         -0.34767495+3.97046694e-23j,  -0.33110231-7.63074115e-23j,
         -1.13338852+1.20370622e-35j],
       [ -9.15586255+1.50463277e-36j,  -4.54039273-4.13590306e-24j,
         32.72911818+4.96308368e-24j,  -1.85021376+1.65436123e-24j,
         -2.53702818-1.32348898e-23j,  -4.54039278-2.48154184e-24j,
          1.83872342+1.50463277e-36j,  -1.86092174+0.00000000e+00j,
         -4.29572395+0.00000000e+00j,   1.090275  +1.32348898e-23j,
         32.72911898-6.61744490e-24j,  -1.86092192+9.92616735e-24j,
         -9.24894513-5.64237288e-37j,  -4.93062949-9.92616735e-24j,
         -2.40960075-1.32348898e-23j,  -1.85021394-1.65436123e-24j,
         -4.29572397+0.00000000e+00j,  -4.93062942+6.61744490e-24j,
          1.40879764-3.76158192e-37j,   1.34936389-3.97046694e-23j,
         -2.53702773-1.65436123e-24j,   1.09027491-2.64697796e-23j,
         -2.40960071+6.61744490e-24j,   1.34936369+3.97046694e-23j,
          0.47563635-1.50463277e-35j],
       [  1.29893642-1.05324294e-35j,  -3.84385976-5.95570041e-23j,
         -1.8609219 -2.64697796e-23j,  -4.21592646-6.61744490e-24j,
          1.03933832+2.31610572e-23j,  -3.84386014+6.61744490e-23j,
        -10.70816264-3.38542373e-36j,  31.76054419+5.29395592e-23j,
         -0.81206922+1.98523347e-23j,  -2.42506066+5.95570041e-23j,
         -1.86092176+2.31610572e-23j,  31.76054462-7.94093388e-23j,
         -9.99217824-6.77084746e-36j,  -4.18589417+3.30872245e-23j,
         -2.2573882 +3.97046694e-23j,  -4.21592632+6.61744490e-24j,
         -0.81206901-1.32348898e-23j,  -4.18589448-4.63221143e-23j,
          1.53827682-1.12847458e-36j,   1.02240648+1.05879118e-22j,
          1.03933803+9.92616735e-24j,  -2.42506017-5.29395592e-23j,
         -2.25738823-1.32348898e-23j,   1.02240603-1.05879118e-22j,
          0.4993346 +0.00000000e+00j],
       [  2.36636534+8.96831017e-44j,  -0.51143448-1.57772181e-30j,
         -9.24894513-8.87468518e-31j,   1.17619748+3.94430453e-31j,
          0.42008941-7.95023881e-31j,  -0.51143448+2.36658272e-30j,
          1.86080059+1.79366203e-43j,  -9.99217824+1.77493704e-30j,
          1.42042175-7.88860905e-31j,   0.66133065+0.00000000e+00j,
         -9.24894513+5.91645679e-31j,  -9.99217824-1.18329136e-30j,
         64.80119411-1.79366203e-43j,  -5.84612899-1.08468374e-30j,
         -7.81047186+0.00000000e+00j,   1.17619748-5.91645679e-31j,
          1.42042175+0.00000000e+00j,  -5.84612899+1.77493704e-30j,
          3.7271156 -2.24207754e-44j,  -0.60817689+0.00000000e+00j,
          0.42008941+4.25245332e-31j,   0.66133065+0.00000000e+00j,
         -7.81047186+7.88860905e-31j,  -0.60817689+1.97215226e-31j,
          1.20175111+6.27781712e-43j],
       [ -0.57657211+1.50463277e-36j,  -6.96326969+2.64697796e-23j,
         -4.93062959+1.65436123e-24j,  -3.93042692-6.61744490e-24j,
          2.36685766+1.32348898e-23j,  -6.96326951-1.32348898e-23j,
          0.19541758+0.00000000e+00j,  -4.18589425+1.32348898e-23j,
         -3.87247569-1.98523347e-23j,   2.08403463-3.97046694e-23j,
         -4.93062931+3.30872245e-24j,  -4.1858944 -1.65436123e-23j,
         -5.84612899+3.00926554e-36j,  37.77920832+3.30872245e-24j,
         -3.01763347+0.00000000e+00j,  -3.93042706+9.92616735e-24j,
         -3.87247565+1.65436123e-23j,  37.77920777-1.65436123e-24j,
         -7.23213763+1.50463277e-36j,  -3.04026415+6.61744490e-24j,
          2.36685764-1.32348898e-23j,   2.08403425+3.97046694e-23j,
         -3.01763353+6.61744490e-24j,  -3.04026397-6.61744490e-24j,
          0.28030407+1.20370622e-35j],
       [  0.39917092+2.44502825e-36j,   2.14326495+1.07533480e-23j,
         -2.40960073+6.61744490e-24j,   1.17237044+1.34416850e-24j,
         -0.24619708+0.00000000e+00j,   2.14326504-8.27180613e-24j,
          0.56304712+1.59867232e-36j,  -2.25738823-1.65436123e-23j,
          0.97180111-6.61744490e-24j,  -0.34767488+9.92616735e-24j,
         -2.40960073+3.30872245e-24j,  -2.25738819+6.61744490e-24j,
         -7.81047186-4.70197740e-37j,  -3.01763354-4.96308368e-24j,
          5.63841366-1.32348898e-23j,   1.17237037-3.30872245e-24j,
          0.97180102+0.00000000e+00j,  -3.01763345+6.61744490e-24j,
         -0.46208872+1.88079096e-37j,   0.35404579-3.30872245e-23j,
         -0.24619711+0.00000000e+00j,  -0.34767491-6.61744490e-24j,
          5.6384134 +1.98523347e-23j,   0.35404595+4.30133919e-23j,
         -1.1998646 -3.00926554e-36j],
       [ -9.00198187-6.58276836e-36j,  -4.70636767-3.97046694e-23j,
         -1.85021388-2.15066959e-23j,  32.36796885+0.00000000e+00j,
         -2.62837615-3.30872245e-24j,  -4.70636737+2.64697796e-23j,
          1.64638076+7.52316385e-37j,  -4.21592618+2.15066959e-23j,
         -0.86792107-1.98523347e-23j,   0.99791236+6.61744490e-23j,
         -1.85021382+2.64697796e-23j,  -4.2159266 -2.64697796e-23j,
          1.17619748-6.01853108e-36j,  -3.93042699+1.98523347e-23j,
          1.17237044-2.31610572e-23j,  32.3679699 +0.00000000e+00j,
         -0.86792129-1.32348898e-23j,  -3.93042699-1.65436123e-23j,
        -10.37559738+3.76158192e-37j,  -2.60535074+6.61744490e-23j,
         -2.62837621+3.30872245e-24j,   0.99791227-6.61744490e-23j,
          1.17237038+1.65436123e-23j,  -2.60535084-6.61744490e-23j,
          0.59574761+1.50463277e-36j],
       [  1.31682879-9.78011300e-36j,  -4.65882691-5.95570041e-23j,
         -4.29572396-6.61744490e-24j,  -0.86792115+4.96308368e-24j,
          1.04359555+0.00000000e+00j,  -4.65882716+4.63221143e-23j,
        -11.08111069-1.78675141e-36j,  -0.81206897+7.94093388e-23j,
         32.00910859+6.61744490e-24j,  -2.29180342+6.61744490e-23j,
         -4.29572396+2.64697796e-23j,  -0.81206926-6.61744490e-23j,
          1.42042175-7.52316385e-36j,  -3.87247562+2.64697796e-23j,
          0.97180114+6.61744490e-24j,  -0.86792122-4.96308368e-24j,
         32.00910946+1.32348898e-23j,  -3.87247572-3.30872245e-23j,
        -10.69471254-5.64237288e-37j,  -2.38335456+1.32348898e-22j,
          1.04359537+1.32348898e-23j,  -2.29180333-6.61744490e-23j,
          0.971801  -6.61744490e-24j,  -2.38335511-1.19114008e-22j,
          0.51309298+7.52316385e-36j],
       [ -0.57657211+0.00000000e+00j,  -6.96326951-1.32348898e-23j,
         -4.93062931+1.32348898e-23j,  -3.93042706+1.32348898e-23j,
          2.36685764-2.64697796e-23j,  -6.96326969+0.00000000e+00j,
          0.19541758+3.00926554e-36j,  -4.1858944 +1.98523347e-23j,
         -3.87247565+1.65436123e-23j,   2.08403425-2.64697796e-23j,
         -4.93062959-2.15066959e-23j,  -4.18589425-2.31610572e-23j,
         -5.84612899+0.00000000e+00j,  37.77920777+4.96308368e-24j,
         -3.01763353+2.64697796e-23j,  -3.93042692-9.92616735e-24j,
         -3.87247569-1.81979735e-23j,  37.77920832-6.61744490e-24j,
         -7.23213763-1.50463277e-36j,  -3.04026397-1.32348898e-23j,
          2.36685766+2.64697796e-23j,   2.08403463+1.98523347e-23j,
         -3.01763347-1.98523347e-23j,  -3.04026415-6.61744490e-24j,
          0.28030407-3.00926554e-36j],
       [  2.36648045+2.24207754e-44j,   0.72851489+7.88860905e-31j,
          1.40879764+5.91645679e-31j, -10.37559738+0.00000000e+00j,
          0.34120424-1.97215226e-31j,   0.72851489+0.00000000e+00j,
          1.96616419+0.00000000e+00j,   1.53827682+7.88860905e-31j,
        -10.69471254+3.94430453e-31j,   0.53327358-7.88860905e-31j,
          1.40879764-7.39557099e-31j,   1.53827682-5.42341872e-31j,
          3.7271156 +8.96831017e-44j,  -7.23213763+9.86076132e-31j,
         -0.46208872+3.94430453e-31j, -10.37559738+1.97215226e-31j,
        -10.69471254+0.00000000e+00j,  -7.23213763-1.38050658e-30j,
         66.67576266+2.24207754e-44j,  -7.36545725+0.00000000e+00j,
          0.34120424+1.97215226e-31j,   0.53327358+9.86076132e-31j,
         -0.46208872+3.94430453e-31j,  -7.36545725-3.94430453e-31j,
          1.10890059+0.00000000e+00j],
       [  0.28162   +5.26621469e-36j,   2.15291775+2.64697796e-23j,
          1.34936389+9.92616735e-24j,  -2.6053508 +0.00000000e+00j,
         -0.26687909+8.27180613e-24j,   2.15291739-2.64697796e-23j,
          0.54752066+7.52316385e-37j,   1.02240618-2.64697796e-23j,
         -2.38335482+1.65436123e-23j,  -0.33110235-3.97046694e-23j,
          1.34936369-1.65436123e-23j,   1.02240633+2.64697796e-23j,
         -0.60817689+3.00926554e-36j,  -3.04026407-9.92616735e-24j,
          0.35404585+3.97046694e-23j,  -2.60535078-1.65436123e-24j,
         -2.38335485-3.30872245e-24j,  -3.04026404+6.61744490e-24j,
         -7.36545725+0.00000000e+00j,   5.62390673-6.28657266e-23j,
         -0.26687921+4.13590306e-24j,  -0.33110199+5.29395592e-23j,
          0.35404589+0.00000000e+00j,   5.62390689+6.28657266e-23j,
         -1.18245232-3.00926554e-36j],
       [ -7.71376342-6.01853108e-36j,  -2.75237175-1.32348898e-23j,
         -2.5370281 -9.92616735e-24j,  -2.62837631+0.00000000e+00j,
          5.6962513 -1.32348898e-23j,  -2.75237237+1.98523347e-23j,
         -0.56750495-3.57350283e-36j,   1.03933817+2.64697796e-23j,
          1.04359542+1.32348898e-23j,   0.38043513+6.61744490e-24j,
         -2.53702781+9.92616735e-24j,   1.03933818+0.00000000e+00j,
          0.42008941-3.00926554e-36j,   2.3668577 +6.61744490e-24j,
         -0.24619695+5.29395592e-23j,  -2.62837605+3.30872245e-24j,
          1.0435955 +0.00000000e+00j,   2.3668576 -1.15805286e-23j,
          0.34120424-7.52316385e-37j,  -0.2668791 +8.93355062e-23j,
          5.69625179+6.61744490e-24j,   0.3804354 -1.32348898e-23j,
         -0.24619723-3.97046694e-23j,  -0.2668792 -8.27180613e-23j,
         -1.24019775+3.00926554e-36j],
       [ -0.351662  +6.77084746e-36j,  -2.99673969+3.97046694e-23j,
          1.09027497+1.32348898e-23j,   0.99791231+4.13590306e-24j,
          0.38043522-3.30872245e-24j,  -2.99673978-2.64697796e-23j,
         -7.43474479+1.12847458e-36j,  -2.42506046-2.81241408e-23j,
         -2.29180338+8.27180613e-25j,   5.48456708-5.29395592e-23j,
          1.09027494-1.32348898e-23j,  -2.42506037+2.31610572e-23j,
          0.66133065+4.51389831e-36j,   2.08403451-2.64697796e-23j,
         -0.34767495+4.63221143e-23j,   0.99791231-1.65436123e-24j,
         -2.29180337-8.27180613e-25j,   2.08403436+2.31610572e-23j,
          0.53327358-7.52316385e-37j,  -0.33110231-6.90695811e-23j,
          0.38043531+1.65436123e-24j,   5.48456743+5.29395592e-23j,
         -0.34767484-4.63221143e-23j,  -0.33110204+6.47268829e-23j,
         -1.13338852-9.02779661e-36j],
       [  0.39917092-9.40395481e-37j,   2.14326504-5.79026429e-24j,
         -2.40960073-3.30872245e-24j,   1.17237037+8.27180613e-25j,
         -0.24619711+0.00000000e+00j,   2.14326495+7.44462551e-24j,
          0.56304712-5.64237288e-37j,  -2.25738819+1.65436123e-23j,
          0.97180102+3.30872245e-24j,  -0.34767491-1.32348898e-23j,
         -2.40960073+3.30872245e-24j,  -2.25738823-9.92616735e-24j,
         -7.81047186-9.40395481e-38j,  -3.01763345+3.30872245e-24j,
          5.6384134 +1.98523347e-23j,   1.17237044-2.06795153e-24j,
          0.97180111-3.30872245e-24j,  -3.01763354-1.65436123e-24j,
         -0.46208872-5.64237288e-37j,   0.35404595+2.64697796e-23j,
         -0.24619708+0.00000000e+00j,  -0.34767488+1.32348898e-23j,
          5.63841366-1.98523347e-23j,   0.35404579-2.97785021e-23j,
         -1.1998646 +3.00926554e-36j],
       [  0.28162   -4.51389831e-36j,   2.15291739-2.64697796e-23j,
          1.34936369-1.98523347e-23j,  -2.60535078+1.65436123e-24j,
         -0.26687921-5.79026429e-24j,   2.15291775+1.98523347e-23j,
          0.54752066+0.00000000e+00j,   1.02240633+3.30872245e-23j,
         -2.38335485-1.32348898e-23j,  -0.33110199+2.64697796e-23j,
          1.34936389+1.65436123e-23j,   1.02240618-3.97046694e-23j,
         -0.60817689-3.00926554e-36j,  -3.04026404+6.61744490e-24j,
          0.35404589-3.97046694e-23j,  -2.6053508 +3.30872245e-24j,
         -2.38335482+1.98523347e-23j,  -3.04026407+0.00000000e+00j,
         -7.36545725+3.76158192e-37j,   5.62390689+6.94831715e-23j,
         -0.26687909+5.79026429e-24j,  -0.33110235+0.00000000e+00j,
          0.35404585+1.32348898e-23j,   5.62390673-6.12113653e-23j,
         -1.18245232+2.40741243e-35j],
       [  1.21720917+4.93257059e-43j,   0.2575331 +7.88860905e-31j,
          0.47563635+0.00000000e+00j,   0.59574761+0.00000000e+00j,
         -1.24019775+0.00000000e+00j,   0.2575331 -5.52202634e-30j,
          1.07252937-3.58732407e-43j,   0.4993346 +0.00000000e+00j,
          0.51309298-1.26217745e-29j,  -1.13338852-6.31088724e-30j,
          0.47563635-1.57772181e-30j,   0.4993346 -6.31088724e-30j,
          1.20175111+8.96831017e-43j,   0.28030407+9.46633086e-30j,
         -1.1998646 +1.26217745e-29j,   0.59574761+3.15544362e-30j,
          0.51309298+6.31088724e-30j,   0.28030407-3.15544362e-30j,
          1.10890059+0.00000000e+00j,  -1.18245232-3.15544362e-30j,
         -1.24019775+9.46633086e-30j,  -1.13338852+7.88860905e-30j,
         -1.1998646 -1.89326617e-29j,  -1.18245232+0.00000000e+00j,
          1.80359473-3.22859166e-42j]])

How do I suppress complex numbers within the calculation of scipy.linalg.sqrtm() so that the output matrix does not contain any complex numbers?

I did some googling around and found the following Github PR from 6 years ago which suggest to include function features like funm_psd and structure="psd" to evaluate general matrix functions of positive semi-definite matrices but I can’t seem to find these functions features anywhere now.

https://github.com/scipy/scipy/pull/3556

Any help would really be appreciated!

Edit: I know I can use np.real() on the output matrix to get just the real part. My concern is including complex numbers during the calculation of the square root might affect the values in the output matrix, rather than just removing the complex numbers in the output matrix. Unless of course if including or excluding the complex numbers during the calculation of the square root won’t affect much on the values of the output matrix. Would anyone know if this is the case?

I should also clarify that why I think the complex numbers are appearing is because of numerical precision/rounding issues with the implementation of the scipy.linalg.sqrtm() function (for eg. see here https://math.stackexchange.com/questions/909814/how-to-avoid-complex-value-for-square-root-of-a-symmetric-matrix). For eg. values which are mathematically suppose to be zeros, have values like -7.52206733e-20 + 7.73635801e-19j where you can see has a complex part but the real part is very very close to zero.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ilayncommented, Oct 19, 2020

Yes indeed, you can check afterwards your result by forming the original matrix from the square root to verify.

1reaction
ilayncommented, Oct 19, 2020

These imaginary parts are, though annoying, numerically zero. Note that machine precision is around 2e-16. Hence it’s just noise. In your last sentence that entry is also zero. This is quite typical to have when the matrices grow large for many routines and not just sqrtm.

So you can actually clean up the noise afterwards which is what #3556 with a very numerically expensive eigh call. Or instead, you can check (np.abs(a.imag) < np.spacing(1.)).all() and then take the real part.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - How to suppress complex numbers in the calculation ...
How to suppress complex numbers in the calculation of the square root of a positive semi-definite matrix using scipy.linalg.sqrtm().
Read more >
scipy.linalg.sqrtm — SciPy v1.9.3 Manual
scipy.linalg.sqrtm(A, disp=True, blocksize=64)[source]#. Matrix square root. Parameters. A(N, N) array_like. Matrix whose square root to evaluate.
Read more >
How to avoid complex value for square root of a symmetric ...
This is a common problem. If you are certain that your matrix is supposed to be positive semidefinite, but is not so due...
Read more >
Python – scipy.linalg.sqrtm - Tutorialspoint
The sqrtm() function of scipy.linalg package can be used to find the square root of an input matrix. Syntax. scipy.linalg.sqrtm(x) ...
Read more >
jax._src.scipy.linalg - JAX documentation
_src.numpy import linalg as np_linalg from jax. ... A.shape[1]: raise ValueError('expected A to be a square matrix') A_L1 = np_linalg.norm(A,1) n_squarings: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found