Search code examples
pythontypeerrornatural-logarithm

TypeError: can't multiply sequence by non-int of type 'float' when using numpy.linalg.slogdet()


I am trying to calulate val but I get a type error.

This is a small part of matrix used in the calculation to show the data type it contains, original matrix is 30x30.

covM = [[ 9.81431930e-02, -6.94931008e-03, -1.28573646e-02],
       [-6.94931008e-03,  5.28292692e-02,  6.23429384e-03],
       [-7.04098766e-03,  2.64439715e-04, -8.66008123e-04]]

To find val I calculate:

val = math.sqrt((2*pi)**30*np.linalg.slogdet(covM))

But get this:

TypeError: can't multiply sequence by non-int of type 'float'

When I try using the normal determinant instead of the log it works fine:

val = math.sqrt((2*pi)**30*np.linalg.det(covM))

Why does this error occur with slogdet() but not det() and how can I make it work for the log determinant?


Solution

  • Since np.linalg.slogdet() returns value two values, the sign of the determinant and the log determinant. I needed to specify the sign. So to get absolute values I did:

    val = math.sqrt((2*pi)**30*np.linalg.slogdet(covM)[1])