Are NumPy's choices for log of identity matrix and exponent of zero matrix correct?

104 Views Asked by At

Python's NumPy gives

$$\log{\Big( \begin{matrix} 1 & 0 \\ 0 & 1 \end{matrix} \Big)} = \Big( \begin{matrix} 0 & -\infty \\ -\infty & 0 \end{matrix} \Big)$$

and

$$\exp{\Big( \begin{matrix} 0 & 0 \\ 0 & 0\end{matrix} \Big)} = \Big( \begin{matrix} 1 & 1 \\ 1 & 1 \end{matrix} \Big)$$

Are these choices consistent and correct? Are there multiple choices/solutions for these operations on matrices?

The code is:

np.log([[1,0],[0,1]])
array([[  0., -inf],
       [-inf,   0.]])

np.exp([[0,0], [0,0]])
array([[ 1.,  1.],
       [ 1.,  1.]])

NOTE: It is pointed out in comments below that NumPy is obviously doing element-wise operations, which are incorrect in this case. I should have been using expm and logm. I will close with answer below to this effect.

2

There are 2 best solutions below

1
On BEST ANSWER

It is pointed out in comments above that NumPy exp and log are doing element-wise operations, which are unintended in this case. I should have been using expm and logm.

0
On

Numpy (and most other programming languages which allow you to do similar things, such as Matlab) applies the exp and log functions to each element of an array, i.e.

exp(np.array([[a, b], [c, d]])) = np.array([[exp(a), exp(b)], [exp(c), exp(d)]])
log(np.array([[a, b], [c, d]])) = np.array([[log(a), log(b)], [log(c), log(d)]])

This, along with the IEEE standard that log(0)=-Inf and exp(-Inf)=0, fits perfectly with your result.


Note that I wrote log(0)=-Inf, not $\log(0)=-\infty$. The two statements are not identical.

  • The first statement, log(0)=-Inf, is a statements about values of variables in a computer program, and is correct.
  • The second statement, $\log(0)=-\infty$, is a statement about real numbers, and is incorrect. $\log(0)$ is undefined.