Using solve() to find inverse of a matrix containing real and complex numbers


I have a square matrix that contains real and complex numbers. When I used inv() and Inverse() function to compute the inverse, R returned an error saying that X must be a square numeric matrix. When I use solve(), it returns a result and no error is thrown. But when I use Det() which is from the complexplus library, the determinant is returned as zero which suggests that the matrix should not be invertible. How is the solve function returning an inverse then? Can someone please help me out? Thanks.

Have you got an example? Can you post some code?

I am not sure how much would pasting the code help in this case but in case it does:

#Code to calculate the inverse of a matrix that has both real and complex values


The inv() function returns an error saying that "X must be a square numeric matrix". The Det() function from the complexplus library returns a value of 0 suggesting that the matrix is not invertible but the solve() function from R returns a square matrix which is the inverse I guess. Is there a way to judge the relative accuracy of solve() versus Det() ? Any leads would be appreciated. Thanks

Can you show us your matrix?

To combine the requests of @williaml and @DPaul_007 : please send us a reprex
In that way we know exactly which version of the functions you use and for which matrix you see the behaviour that you report.

1 Like

@DPaul_007, @williaml and @HanOostdijk , this is what my matrix looks like:

df1 <- tibble::tribble(
                        ~V1,        ~V2,        ~V3,        ~V4,        ~V5,        ~V6,        ~V7,        ~V8,           ~V9,          ~V10,          ~V11,          ~V12,          ~V13,          ~V14,          ~V15,          ~V16,          ~V17,          ~V18,          ~V19,          ~V20,          ~V21,          ~V22,          ~V23,          ~V24,       ~V25,       ~V26,       ~V27,          ~V28,          ~V29,          ~V30,          ~V31,   ~V32,   ~V33,    ~V34,    ~V35,    ~V36,
                  "0.78+0i",  "0.79+0i",  "0.72+0i",   "0.8+0i", "-0.52+0i",  "0.09+0i", "-0.17+0i", "-0.09+0i", "-0.01+0.07i", "-0.01-0.07i",     "-0.4+0i",     "-0.4+0i",  "0.56+0.03i",  "0.56-0.03i",  "0.04-0.14i",  "0.04+0.14i",   "-0.1-0.2i",   "-0.1+0.2i",  "0.21+0.09i",  "0.21-0.09i",  "-0.09+0.2i",  "-0.09-0.2i",   "0.3+0.31i",   "0.3-0.31i",  "0.47+0i", "-0.46+0i",  "0.45+0i",  "0.35+0.03i",  "0.35-0.03i", "-0.04-0.23i", "-0.04+0.23i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.02+0i",     "0+0i",  "0.02+0i", "-0.01+0i",  "0.08+0i", "-0.01+0i",  "0.02+0i",  "0.07+0i",  "-0.02-0.1i",  "-0.02+0.1i",  "0.29-0.06i",  "0.29+0.06i",     "-0.6+0i",     "-0.6+0i",   "0.16+0.3i",   "0.16-0.3i",  "-0.1+0.15i",  "-0.1-0.15i",    "-0.55+0i",    "-0.55+0i",      "0.5+0i",      "0.5+0i", "-0.13-0.33i", "-0.13+0.33i", "-0.45+0i",  "0.73+0i", "-0.72+0i",    "-0.51+0i",    "-0.51+0i",  "0.18+0.38i",  "0.18-0.38i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.45+0i",  "0.46+0i",  "0.51+0i",  "-0.6+0i", "-0.33+0i", "-0.07+0i", "-0.13+0i", "-0.27+0i", "-0.03-0.07i", "-0.03+0.07i", "-0.35-0.04i", "-0.35+0.04i",   "0.14+0.1i",   "0.14-0.1i",  "0.14-0.25i",  "0.14+0.25i",  "0.06+0.14i",  "0.06-0.14i",   "0.2+0.23i",   "0.2-0.23i", "-0.09-0.16i", "-0.09+0.16i",  "0.09-0.11i",  "0.09+0.11i",  "0.05+0i", "-0.11+0i",  "0.12+0i",  "0.06-0.04i",  "0.06+0.04i", "-0.03-0.08i", "-0.03+0.08i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.08+0i", "-0.09+0i", "-0.05+0i",     "0+0i",  "0.12+0i",  "0.27+0i", "-0.26+0i", "-0.25+0i",  "0.02+0.38i",  "0.02-0.38i", "-0.38+0.01i", "-0.38-0.01i",  "0.06+0.04i",  "0.06-0.04i",  "0.08+0.19i",  "0.08-0.19i",  "0.08+0.06i",  "0.08-0.06i",  "-0.2+0.04i",  "-0.2-0.04i", "-0.01-0.18i", "-0.01+0.18i",    "-0.44+0i",    "-0.44+0i",  "-0.6+0i",  "0.33+0i", "-0.35+0i", "-0.24+0.31i", "-0.24-0.31i",  "0.31+0.22i",  "0.31-0.22i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.18+0i", "-0.22+0i",  "0.02+0i",     "0+0i", "-0.31+0i", "-0.45+0i",  "0.46+0i",  "0.37+0i",  "0.24+0.27i",  "0.24-0.27i",  "0.19+0.07i",  "0.19-0.07i",  "0.05+0.03i",  "0.05-0.03i",     "0-0.03i",     "0+0.03i",   "0.04+0.2i",   "0.04-0.2i",   "0.02+0.1i",   "0.02-0.1i",  "0.15+0.17i",  "0.15-0.17i", "-0.03+0.34i", "-0.03-0.34i",  "0.09+0i", "-0.09+0i",  "0.09+0i",  "0.05+0.01i",  "0.05-0.01i", "-0.04-0.04i", "-0.04+0.04i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.16+0i", "-0.22+0i", "-0.03+0i",     "0+0i",  "0.09+0i",   "0.5+0i", "-0.55+0i",  "0.17+0i",    "-0.61+0i",    "-0.61+0i",  "-0.1-0.14i",  "-0.1+0.14i",  "0.11+0.03i",  "0.11-0.03i", "-0.14-0.15i", "-0.14+0.15i", "-0.19-0.09i", "-0.19+0.09i",  "0.08-0.11i",  "0.08+0.11i",  "0.05-0.08i",  "0.05+0.08i",  "0.11-0.01i",  "0.11+0.01i",  "0.12+0i",   "0.1+0i", "-0.05+0i",     "0-0.16i",     "0+0.16i",  "-0.1+0.01i",  "-0.1-0.01i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.14+0i", "-0.18+0i",  "0.04+0i",     "0+0i", "-0.39+0i", "-0.45+0i",  "0.42+0i",  "0.05+0i", "-0.02+0.07i", "-0.02-0.07i",  "0.03+0.02i",  "0.03-0.02i",     "0.01+0i",     "0.01+0i",  "0.06-0.08i",  "0.06+0.08i",  "-0.09-0.1i",  "-0.09+0.1i",     "0+0.04i",     "0-0.04i",  "0.23+0.01i",  "0.23-0.01i", "-0.05+0.11i", "-0.05-0.11i", "-0.05+0i",  "0.02+0i", "-0.07+0i", "-0.04+0.15i", "-0.04-0.15i",  "0.13+0.07i",  "0.13-0.07i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.06+0i", "-0.05+0i", "-0.02+0i",     "0+0i",  "0.06+0i", "-0.01+0i",  "0.05+0i",  "-0.1+0i",  "0.46-0.22i",  "0.46+0.22i",   "0.04+0.1i",   "0.04-0.1i", "-0.05-0.01i", "-0.05+0.01i",  "0.24-0.02i",  "0.24+0.02i",  "0.15-0.03i",  "0.15+0.03i",     "0+0.21i",     "0-0.21i",   "0.1+0.12i",   "0.1-0.12i", "-0.14+0.13i", "-0.14-0.13i", "-0.14+0i", "-0.09+0i",  "0.01+0i",  "0.03+0.24i",  "0.03-0.24i",  "0.15+0.03i",  "0.15-0.03i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.12+0i",  "0.04+0i",  "0.07+0i",  "0.01+0i",  "0.51+0i",  "0.18+0i", "-0.07+0i",  "0.36+0i", "-0.09+0.06i", "-0.09-0.06i",  "0.03+0.16i",  "0.03-0.16i",  "0.16-0.01i",  "0.16+0.01i", "-0.15-0.29i", "-0.15+0.29i",  "0.02-0.03i",  "0.02+0.03i",  "0.27+0.03i",  "0.27-0.03i", "-0.08+0.06i", "-0.08-0.06i",  "0.15-0.08i",  "0.15+0.08i",  "0.08+0i", "-0.19+0i",   "0.2+0i",     "0.11+0i",     "0.11+0i", "-0.01-0.12i", "-0.01+0.12i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0-0.01i",     "0+0.01i",     "0.01+0i",     "0.01+0i",    "-0.01+0i",    "-0.01+0i",     "0.01+0i",     "0.01+0i",        "0+0i",        "0+0i",     "0+0.01i",     "0-0.01i",  "0.03-0.04i",  "0.03+0.04i",     "0-0.01i",     "0+0.01i",     "0+0i",     "0+0i",     "0+0i",     "0-0.02i",     "0+0.02i",    "-0.01+0i",    "-0.01+0i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.01+0i",     "0+0i",     "0+0i",     "0+0i",  "0.03+0i",     "0+0i",     "0+0i",  "0.02+0i",        "0+0i",        "0+0i",  "0.05-0.01i",  "0.05+0.01i", "-0.05-0.02i", "-0.05+0.02i",  "0.02+0.13i",  "0.02-0.13i",  "0.03+0.01i",  "0.03-0.01i", "-0.13-0.02i", "-0.13+0.02i", "-0.07+0.08i", "-0.07-0.08i",    "-0.05+0i",    "-0.05+0i", "-0.02+0i",     "0+0i",  "0.04+0i",  "0.07-0.02i",  "0.07+0.02i", "-0.06-0.09i", "-0.06+0.09i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.07+0i",  "0.03+0i",  "0.07+0i",  "0.02+0i",  "0.17+0i",     "0+0i",  "0.02+0i",  "0.16+0i",  "0.01+0.04i",  "0.01-0.04i",  "0.26+0.02i",  "0.26-0.02i", "-0.16-0.07i", "-0.16+0.07i", "-0.17+0.31i", "-0.17-0.31i",     "0-0.06i",     "0+0.06i", "-0.29-0.28i", "-0.29+0.28i",  "0.22+0.19i",  "0.22-0.19i",  "-0.1+0.04i",  "-0.1-0.04i", "-0.05+0i",  "0.16+0i", "-0.13+0i", "-0.07-0.03i", "-0.07+0.03i",  "0.01+0.07i",  "0.01-0.07i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.02+0i", "-0.01+0i", "-0.41+0i",     "0+0i", "-0.09+0i",  "0.22+0i",  "0.03+0i",  "0.11+0i", "-0.04+0.04i", "-0.04-0.04i", "-0.02+0.02i", "-0.02-0.02i",  "0.13-0.01i",  "0.13+0.01i",  "0.03-0.03i",  "0.03+0.03i", "-0.04-0.05i", "-0.04+0.05i",     "0+0.01i",     "0-0.01i",  "0.16+0.06i",  "0.16-0.06i", "-0.08+0.04i", "-0.08-0.04i", "-0.11+0i", "-0.05+0i",  "0.03+0i", "-0.03+0.05i", "-0.03-0.05i",  "0.04+0.02i",  "0.04-0.02i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                     "0+0i",     "0+0i",  "-0.1+0i",     "0+0i", "-0.02+0i", "-0.15+0i", "-0.05+0i", "-0.03+0i",        "0+0i",        "0+0i",    "-0.02+0i",    "-0.02+0i",  "0.02+0.01i",  "0.02-0.01i",  "0.01-0.03i",  "0.01+0.03i", "-0.02-0.02i", "-0.02+0.02i",  "0.01+0.01i",  "0.01-0.01i",  "0.04+0.01i",  "0.04-0.01i",  "0.01+0.02i",  "0.01-0.02i",     "0+0i", "-0.02+0i",  "0.03+0i",  "0.01-0.01i",  "0.01+0.01i", "-0.02-0.01i", "-0.02+0.01i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.02+0i", "-0.01+0i",     "0+0i",     "0+0i",  "0.04+0i", "-0.01+0i",  "0.04+0i",  "0.08+0i",  "0.03+0.01i",  "0.03-0.01i",  "0.14-0.02i",  "0.14+0.02i", "-0.11-0.04i", "-0.11+0.04i",  "0.04+0.07i",  "0.04-0.07i", "-0.01-0.03i", "-0.01+0.03i", "-0.03+0.03i", "-0.03-0.03i", "-0.22-0.01i", "-0.22+0.01i", "-0.01-0.02i", "-0.01+0.02i", "-0.02+0i",  "0.08+0i", "-0.08+0i", "-0.04+0.04i", "-0.04-0.04i",  "0.03+0.03i",  "0.03-0.03i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                     "0+0i",     "0+0i",     "0+0i",     "0+0i",  "0.01+0i",     "0+0i",     "0+0i",     "0+0i",     "0-0.01i",     "0+0.01i",  "0.02-0.01i",  "0.02+0.01i",    "-0.04+0i",    "-0.04+0i",  "-0.03+0.1i",  "-0.03-0.1i",  "0.08+0.06i",  "0.08-0.06i", "-0.05-0.05i", "-0.05+0.05i", "-0.19-0.01i", "-0.19+0.01i",     "0-0.05i",     "0+0.05i",  "0.02+0i", "-0.04+0i",  "0.03+0i",     "0-0.02i",     "0+0.02i", "-0.03-0.01i", "-0.03+0.01i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.01+0i",     "0+0i",     "0+0i",     "0+0i",  "0.02+0i",     "0+0i",  "0.01+0i", "-0.02+0i",  "0.01-0.08i",  "0.01+0.08i",  "0.04-0.01i",  "0.04+0.01i", "-0.16+0.02i", "-0.16-0.02i",  "0.01-0.08i",  "0.01+0.08i", "-0.02-0.03i", "-0.02+0.03i",  "0.07+0.03i",  "0.07-0.03i",  "-0.04-0.1i",  "-0.04+0.1i",  "0.11+0.01i",  "0.11-0.01i",  "0.08+0i", "-0.03+0i",  "0.03+0i",  "0.03-0.02i",  "0.03+0.02i", "-0.02-0.03i", "-0.02+0.03i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.01+0i",     "0+0i",     "0+0i",     "0+0i",  "0.06+0i", "-0.03+0i",  "0.03+0i", "-0.66+0i", "-0.03-0.03i", "-0.03+0.03i", "-0.16-0.08i", "-0.16+0.08i",   "0.1+0.04i",   "0.1-0.04i",  "0.05+0.01i",  "0.05-0.01i", "-0.01+0.03i", "-0.01-0.03i",     "0+0.05i",     "0-0.05i",  "0.06+0.02i",  "0.06-0.02i",    "-0.01+0i",    "-0.01+0i", "-0.02+0i",     "0+0i", "-0.02+0i", "-0.02+0.02i", "-0.02-0.02i",  "0.03+0.03i",  "0.03-0.03i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.03+0i", "-0.03+0i", "-0.04+0i",     "0+0i",  "0.04+0i",  "0.07+0i", "-0.05+0i", "-0.12+0i",  "0.09+0.07i",  "0.09-0.07i", "-0.11+0.03i", "-0.11-0.03i",     "0+0.02i",     "0-0.02i",  "0.04-0.02i",  "0.04+0.02i",  "0.02-0.02i",  "0.02+0.02i",  "0.05+0.05i",  "0.05-0.05i",  "0.02+0.09i",  "0.02-0.09i",  "0.11-0.02i",  "0.11+0.02i",  "0.16+0i", "-0.12+0i",  "0.12+0i",   "0.09-0.1i",   "0.09+0.1i",  "-0.1-0.08i",  "-0.1+0.08i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.01+0i",  "0.01+0i",  "0.01+0i", "-0.01+0i", "-0.01+0i",     "0+0i",     "0+0i", "-0.01+0i",        "0+0i",        "0+0i",     "0-0.02i",     "0+0.02i", "-0.04-0.01i", "-0.04+0.01i", "-0.08+0.15i", "-0.08-0.15i",  "0.16+0.16i",  "0.16-0.16i",  "-0.05-0.1i",  "-0.05+0.1i",  "-0.2-0.06i",  "-0.2+0.06i",     "0-0.16i",     "0+0.16i", "-0.02+0i", "-0.02+0i",  "0.04+0i",  "-0.04-0.1i",  "-0.04+0.1i", "-0.07-0.04i", "-0.07+0.04i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.02+0i",  "0.01+0i",  "0.02+0i", "-0.02+0i", "-0.03+0i", "-0.03+0i",  "0.02+0i",  "0.03+0i",     "0.03+0i",     "0.03+0i",  "0.02-0.01i",  "0.02+0.01i",    "-0.01+0i",    "-0.01+0i", "-0.03+0.05i", "-0.03-0.05i",  "0.11+0.04i",  "0.11-0.04i",    "-0.01+0i",    "-0.01+0i",  "0.05+0.05i",  "0.05-0.05i",   "0.05-0.2i",   "0.05+0.2i", "-0.06+0i",  "0.02+0i",     "0+0i",    "-0.02+0i",    "-0.02+0i",  "0.02-0.01i",  "0.02+0.01i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",        "0+0i",        "0+0i",  "0.01-0.01i",  "0.01+0.01i",    "-0.03+0i",    "-0.03+0i", "-0.03+0.07i", "-0.03-0.07i",  "0.07+0.07i",  "0.07-0.07i", "-0.03-0.04i", "-0.03+0.04i", "-0.08-0.03i", "-0.08+0.03i",     "0-0.05i",     "0+0.05i",  "0.03+0i",  "0.04+0i", "-0.05+0i", "-0.02+0.02i", "-0.02-0.02i",  "0.03+0.01i",  "0.03-0.01i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.02+0i",  "0.02+0i",  "0.03+0i",  "0.02+0i", "-0.02+0i",     "0+0i", "-0.01+0i", "-0.03+0i",    "-0.01+0i",    "-0.01+0i",    "-0.05+0i",    "-0.05+0i",  "0.02+0.02i",  "0.02-0.02i", "-0.02-0.06i", "-0.02+0.06i",  "0.01-0.04i",  "0.01+0.04i",  "0.08+0.01i",  "0.08-0.01i", "-0.07-0.11i", "-0.07+0.11i", "-0.03+0.03i", "-0.03-0.03i", "-0.02+0i", "-0.01+0i", "-0.03+0i", "-0.02+0.02i", "-0.02-0.02i", "-0.01+0.02i", "-0.01-0.02i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",     "0+0i",     "0+0i",     "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",     "0+0i",     "0+0i",     "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.03+0i",  "0.03+0i",  "0.04+0i", "-0.02+0i", "-0.04+0i", "-0.02+0i",  "0.01+0i",     "0+0i",  "0.01-0.03i",  "0.01+0.03i", "-0.04-0.04i", "-0.04+0.04i",     "0.02+0i",     "0.02+0i",  "0.13+0.09i",  "0.13-0.09i",     "0.03+0i",     "0.03+0i", "-0.08+0.01i", "-0.08-0.01i",  "0.04-0.03i",  "0.04+0.03i", "-0.09+0.11i", "-0.09-0.11i",  "0.02+0i",  "0.02+0i", "-0.04+0i",    "-0.01+0i",    "-0.01+0i", "-0.02+0.03i", "-0.02-0.03i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.06+0i", "-0.05+0i", "-0.02+0i",     "0+0i",  "0.04+0i", "-0.05+0i",  "0.07+0i", "-0.06+0i",    "-0.01+0i",    "-0.01+0i",  "0.14-0.06i",  "0.14+0.06i", "-0.08-0.06i", "-0.08+0.06i", "-0.09+0.09i", "-0.09-0.09i",  "-0.1+0.04i",  "-0.1-0.04i", "-0.13-0.11i", "-0.13+0.11i",  "0.08-0.04i",  "0.08+0.04i",  "0.09+0.07i",  "0.09-0.07i",  "0.08+0i",  "0.07+0i", "-0.08+0i", "-0.07+0.12i", "-0.07-0.12i",  "0.09+0.06i",  "0.09-0.06i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.03+0i",     "0+0i", "-0.01+0i",     "0+0i",  "0.06+0i", "-0.01+0i",  "0.01+0i",  "0.03+0i",    "-0.01+0i",    "-0.01+0i",     "0.07+0i",     "0.07+0i", "-0.08-0.01i", "-0.08+0.01i",    "-0.05+0i",    "-0.05+0i",  "0.01+0.06i",  "0.01-0.06i",  "0.01-0.04i",  "0.01+0.04i", "-0.04-0.02i", "-0.04+0.02i", "-0.11-0.07i", "-0.11+0.07i",  "-0.1+0i",  "0.03+0i", "-0.01+0i", "-0.03-0.03i", "-0.03+0.03i", "-0.01+0.01i", "-0.01-0.01i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.26+0i", "-0.15+0i", "-0.09+0i",     "0+0i",  "0.12+0i", "-0.38+0i",   "0.4+0i", "-0.16+0i",  "0.11+0.01i",  "0.11-0.01i",  "0.38-0.04i",  "0.38+0.04i",  "-0.2-0.09i",  "-0.2+0.09i",  "0.06-0.13i",  "0.06+0.13i",  "0.02+0.43i",  "0.02-0.43i",  "0.06-0.06i",  "0.06+0.06i",     "0.01+0i",     "0.01+0i",  "0.05-0.02i",  "0.05+0.02i",  "0.02+0i",     "0+0i",     "0+0i",     "0.01+0i",     "0.01+0i",        "0+0i",        "0+0i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.02+0i", "-0.02+0i", "-0.01+0i",     "0+0i",  "0.02+0i", "-0.07+0i",  "0.08+0i", "-0.05+0i",  "0.03-0.02i",  "0.03+0.02i",  "0.15-0.06i",  "0.15+0.06i", "-0.14-0.05i", "-0.14+0.05i", "-0.08-0.06i", "-0.08+0.06i",  "0.16-0.23i",  "0.16+0.23i", "-0.03-0.09i", "-0.03+0.09i",        "0+0i",        "0+0i", "-0.04+0.03i", "-0.04-0.03i", "-0.01+0i",  "0.06+0i", "-0.07+0i", "-0.03+0.01i", "-0.03-0.01i",  "0.01+0.04i",  "0.01-0.04i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.05+0i",  "0.04+0i",  "0.06+0i",  "0.07+0i",  "0.02+0i", "-0.05+0i",  "0.03+0i", "-0.06+0i", "-0.01-0.03i", "-0.01+0.03i",  "0.11-0.09i",  "0.11+0.09i", "-0.21-0.04i", "-0.21+0.04i",    "-0.47+0i",    "-0.47+0i",    "-0.62+0i",    "-0.62+0i",  "0.05-0.24i",  "0.05+0.24i", "-0.23-0.15i", "-0.23+0.15i",  "0.04+0.14i",  "0.04-0.14i", "-0.05+0i",  "0.06+0i", "-0.06+0i", "-0.02-0.02i", "-0.02+0.02i",  "0.01+0.02i",  "0.01-0.02i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",     "0+0i",     "0+0i",     "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",     "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i",     "0+0i",     "0+0i",     "0+0i",        "0+0i",        "0+0i",        "0+0i",        "0+0i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.01+0i", "-0.01+0i",     "0+0i",     "0+0i",  "0.01+0i",  "0.01+0i", "-0.01+0i",  "0.02+0i", "-0.02+0.02i", "-0.02-0.02i",  "0.01-0.01i",  "0.01+0.01i", "-0.02-0.01i", "-0.02+0.01i",  "0.06+0.04i",  "0.06-0.04i",    "-0.01+0i",    "-0.01+0i", "-0.09+0.05i", "-0.09-0.05i",  "-0.05+0.1i",  "-0.05-0.1i",     "0.06+0i",     "0.06+0i",  "0.03+0i",  "0.03+0i",  "0.11+0i", "-0.09-0.27i", "-0.09+0.27i",    "-0.59+0i",    "-0.59+0i", "1+0i", "1+0i", "-1+0i", "-1+0i", "-1+0i",
                  "0.01+0i", "-0.01+0i",     "0+0i",     "0+0i", "-0.01+0i", "-0.01+0i",  "0.01+0i",  "0.01+0i",  "0.08+0.01i",  "0.08-0.01i", "-0.03+0.03i", "-0.03-0.03i",  "0.08+0.01i",  "0.08-0.01i",  "0.02-0.03i",  "0.02+0.03i",     "0-0.03i",     "0+0.03i",  "0.05+0.02i",  "0.05-0.02i",    "-0.06+0i",    "-0.06+0i",  "0.07-0.04i",  "0.07+0.04i",  "0.08+0i", "-0.07+0i",  "0.07+0i",  "0.05-0.06i",  "0.05+0.06i", "-0.06-0.05i", "-0.06+0.05i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i",
                  "0.02+0i", "-0.01+0i",     "0+0i",     "0+0i",  "0.05+0i",     "0+0i",     "0+0i", "-0.14+0i",     "0.04+0i",     "0.04+0i", "-0.05+0.01i", "-0.05-0.01i",     "0.02+0i",     "0.02+0i",  "0.07-0.01i",  "0.07+0.01i", "-0.05-0.05i", "-0.05+0.05i",     "0+0.11i",     "0-0.11i", "-0.21+0.16i", "-0.21-0.16i",     "0.22+0i",     "0.22+0i",   "0.3+0i",  "0.05+0i",  "0.08+0i",  "0.24-0.34i",  "0.24+0.34i", "-0.32-0.12i", "-0.32+0.12i", "0+0i", "0+0i",  "0+0i",  "0+0i",  "0+0i"

This dataframe when converted to a matrix gives Det() = 0 while solve() returns an inverse. I understand that this is not a minimum reproducible example. I am new to reprex package and learning it. If this is not helpful either, I will think about another way of presenting the matrix so that my question can be addressed. Thanks.

Hello @mohitarora ,
can you also include the code (in reprex if possible :grinning: ).
You realize that you show us a character vector with contents looking like a complex numbers?
I have no experience with complex numbers in R so I am interested to know how you convert that to numeric.
Which functions/package do you use?

@HanOostdijk , Thanks for your reply. I re-checked my computation after seeing your comment and I realized that when I round off the values in the matrix to 2 places using the round() function, then the results given by Det() and solve() are consistent with each other - Det(mat1) = 0 and solve(mat1) returns an error saying that the system is exactly singular. However, if I round off the values in the matrix using signif() or use the original non-rounded off values, then Det(mat1) = 0 and solve(mat1) returns an inverse (hence, a discrepancy arises). So, the whole question boils down to the relative accuracy of Det() versus solve(). Note that Det() is returning a value of 0 no matter whether I round off the values or do not round them off. If you have any thoughts about which is more precise solve() or Det(), please let me know.

As far as your question about converting them to numeric is concerned, I do not convert them to numeric. They retain the "complex" class - one of the atomic data types in R.

I use the complexplus package for the Det() function. For conversion of classes, I generally use this code:

#To check the class of each column
lapply(df, class)
#To convert the classes of all columns into numeric
df[1:"number_of_cols"] <- sapply(df[1:"number_of_cols"] ,as.numeric)
#Function used to compute the determinant of a matrix with complex numbers
Det(mat1) #mat1 is the matrix containing complex numbers

Hello @mohitarora,

I keep asking a reprex from you. This is not a hobby of mine but necessary to be able to see what is YOUR problem.

If you had tried to deliver a reprex you would have seen that the data.frame that you included is not a numeric matrix. The code and the data.frame just don't match:

  • the df data.frame you did not define
  • the df[1:"number_of_cols"] line would never execute
  • the mat1 matrix is not defined

A suggestion: remove some quotes in the specification of df1.
I think I have contributed what I could.

1 Like

I would guess that you didn't hand write this tribble, but perhaps used.something like datapasta tribble paste... given the complex number type involved its probably not good for the purpose. I would try again using traditional dput() for a code representation of example data, I would expect dput to faithfully reproduce what it's given.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.