Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to calculate the following

$K^{-1}DK^{-1} * p$

Where $K$ is symmetric positive definite and $D$ is positive diagonal and $p$ is a vector. The problem is that p is very, very small so its rounded to zero by Matlab so I want to take the log of of the calculations of $p$ so that I can actually calculate the map above. However if I take the log I need to transform it back somehow.

Can this be done? $K^{-1}$ will naturally contain negative entries.

share|cite|improve this question
Why not considering a suitable scalar multiple of $p$ instead of the logarithm of the components? – Hagen Knaf Mar 30 '11 at 11:51
Yea. That seems to be it. P in my case is a probability so I do negative log transform and then add the min value to exponential transform. At least I get some non-zeros. – Reed Richards Apr 18 '11 at 16:11

Short answer: using logs won't really help. Instead, you should multiply $p$ by something large so that matlab can deal with it, and then divide out in the end.

Long answer:There is a $\log$ for matrices, but it doesn't behave quite the same as $\log$ for numbers, and so it's not quite suitable for computations in the way you think it is.

From Taylor series, we have that $e^x=\sum x^n/n!$ and $\log(1+x)=\sum (-1)^n x^{n+1}/(n+1)$. The fist formula holds for all $x$, the second for $x$ that are close to $0$, and they define inverse functions where they are defined.

Similarly, we can define a matrix $\log$ and matrix exponential by means of the exact same power series. $e^A$ will converge for all $A$, and $\ln(I+A)$ will converge for small matrices (take any linear norm on matrices such that $||AB||\leq ||A|| ||B||$, and the condition $||A||<1$ will work).

Unfortunately, these do not obey all the properties that you want. For example, $e^{A+B}=e^A e^B$ only when $A$ and $B$ commute with each other. When they don't commute, the Campbell-Baker-Hausdorff formula says what $\log(e^A e^B)$ equals. Because of this, we do not in general have that $\log AB=\log A + \log B$, even when everything is defined.

Even ignoring this problem, we would still need to define the $\log$ of a vector. I honestly don't know where one would begin, other than to take $\log$ of the individual entries, which wouldn't have any immediately useful properties as far as matrix actions are concerned.

If the problem is that Matlab has rounding errors, then you can exploit the fact that matrix multiplication is a linear operator and just multiply $p$ by some large constant $c$, do the calculation, and then divide by $c$ again. Additionally, if you have problems with $D$ or $K$ being too small/large you can do the same to them.

share|cite|improve this answer

It seems like it can't be done. If the $K^{-1}DK^{-1}$ is a m-by-n matrix then the map can be seen as just $m$ scalar products. However the log of a scalar product can't be tampered with since $\log(a \cdot b) = \log(a_{1} b_{1} + a_{2} b_{2} +...)$. Further on if we take the log of the individual elements we get $\log a_{1} \log b_{1} + \log a_{2} \log b_{2} +...$ which in this case is not feasible since $b$ in our case contains negative elements.

Even if $b$ just contained positive element there's no easy way of transforming it back since in general

$\log(a_{1} b_{1} + a_{2} b_{2} +...) \neq \log a_{1} \log b_{1} + \log a_{2} \log b_{2} +...$

and we thus can't take the exp to transform back.

share|cite|improve this answer
Use $\backslash\!\!\log$. – Did Apr 4 '11 at 15:17
When, where and how? – Reed Richards Apr 4 '11 at 16:00
When: Each time you use log in a formula. How: Type backslash-l-o-g instead of l-o-g. I did it for you at the first occurrence in your answer. – Did Apr 4 '11 at 16:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.