Logarithms#

Just as addition and multiplication have inverses (subtraction and division), so we have inverses for exponentials. These are called logarithms. A logarithm answers the question “how many of one number do we multiply to get another number?”.

Example

How many 2s do we multiply to get 8?

Solution: 2 &times 2 &times 2 = 8, so we need to multiply 3 of the 2s to get 8. So the logarithm is 3.

We have a notation for logarithms, for example, the last example would be written as:

\[ \log_2(8) = 3 \]

where this tells us we need to multiply 2 by itself 3 times to get 8.

So more generally, we can write:

\[ \log_a(x) = y \]
  • This tells use that we need to multiply \(a\) by itself \(y\) times to get \(x\) (in other words \(x = a ^ y\)).

  • We say that \(a\) is the base of the logarithm.

We can see clearly that as logarithms are the inverse of exponents, there exists a relationship between the two, given by:

\[ \textrm{If }y = a^x\textrm{ then }x=\log_a(y) \]

Example

Write \(81^{0.5}=9\), as a logarithm.

Solution: So we need to multiply 81 by itself 0.5 times to get 9, so we have \(\log_{81}(9) = 0.5\).

Example

If \(10^x = 3\), then find \(x\).

Solution: We need to multiply 10 by itself \(x\) times to get 3, so we have:

\[ x = \log_{10}(3) = 0.447\textrm{ to 3 s.f.} \]

Python is capable of performing logarithms, for a logarithm to base ten, any of the following will work.

from math import log10

log10(3)
0.47712125471966244
from math import log 

log(3, 10)
0.47712125471966244
import numpy as np

np.log10(3)
0.47712125471966244

Logarithms are useful for expressing quantities that span several orders of magnitude. For example, the pH equation \(\textrm{pH} = -\log_{10}{[ \textrm{H}^+]}\) as small change in pH results in a very large change in [H+].

Logarithms: The Inverses of Exponentials#

As taking a logarithm is the inverse of an exponential, to the same base, we can cancel them using the rules below:

\[ \log_a(a^x) = x \;\;\;\;\;\; a^{\log_a(x)} = x \]

Important

  1. When cancelling in the first case, all the quantities in the logarithm must be contained in the ‘power’ of the exponential. For example, \(\log_a(a^x+3) \neq x + 3\), as the 3 is not part of the power of the exponential.

  2. When cancelling in the second case, all of the power must be contained in the logarithm. For example, \(a^{\log_a(x) + 4} \neq x+4\), as the 4 is not in the logarithm.

Example

Simplifying the following:

  1. \(\log_{10}(10^{5x^3 + 3x})\)

  2. \(3^{\log_3(x^7+1)}\)

  3. \(\log_3(10^{4x})\)

  4. \(a^{\log_a(4x^3) + 2x}\)

  5. \(\log_8(8^{2x^4} + 7)\)

Solution:

  1. \(\log_{10}(10^{5x^3 + 3x}) = 5x^3 + 3x\)

  2. \(3^{\log_3(x^7+1)} = x^7+1\)

  3. We cannot simplify this as the logarithm has the base 3, and the exponential is to the base 10.

  4. First, note that \(a^{\log_a(4x^3) + 2x} \neq 4x^3 + 2x\). We do this as follows: $\( a^{\log_a(4x^3) + 2x} = a^{\log_a(4x^3)} a^{2x} = 4x^3a^{2x} \)

  5. We cannot simplify this case, as the 7 is not part of the exponential.


Let’s see how sympy.simplify gets on with these.

from sympy import symbols, simplify
from sympy import log as slog

x = symbols('x', positive=True, real=True)

simplify(slog(10 ** (5 * x ** 3 + 3 * x), 10))
\[\displaystyle x \left(5 x^{2} + 3\right)\]
3 ** (slog(x ** 7 + 1, 3))
\[\displaystyle x^{7} + 1\]
slog(10 ** (4 * x), 3)
\[\displaystyle \frac{\log{\left(10^{4 x} \right)}}{\log{\left(3 \right)}}\]
a = symbols('a', positive=True, real=True)

simplify(a ** (slog(4 * x ** 3, a) + 2 * x))
\[\displaystyle 4 a^{2 x} x^{3}\]
slog(8 ** (2 * x ** 4) + 7, 8)
\[\displaystyle \frac{\log{\left(8^{2 x^{4}} + 7 \right)}}{\log{\left(8 \right)}}\]

For some of the equations, it is necessary to use the simplify function, while for others, it is not.


Logarithms to the Base 10#

Many equations in chemistry include logarithms, and we tend to use only two logarithms to different bases. The first one is to the base 10, such as in the pH equation, \(\textrm{pH} = -\log_{10}[\textrm{H}^+]\). In the usual notation, we would write \(\log_{10}\), however we tend to write logs to the base 10 as \(\log\), so \(\textrm{pH} = -\log[\textrm{H}^+]\). All the rules we have looked at are the same, just when the base \(a=10\), so in particular:

\[ \textrm{If }y=10^x\textrm{ then }x = \log{(y)} \]

Logarithms to the Base \(e\)#

The second common logarithm found in chemistry is the logarithm to the base \(e\), known as the ‘natural logarithm’. We use the notation \(\ln{(x)}\) but this means the same as \(\log_e{(x)}\). The natural logarithm is the inverse operation for the exponential, \(y=e^x\), so we have the relationship as before:

\[ \textrm{If }y=e^x\textrm{ then }x = \ln{(y)} \]

Example

If \(3=e^x\), then \(x = \ln{(3)} \approx 1.09861\)


In Python, math.log or np.log perform the natural logarithm by default.

log(3)
1.0986122886681098
np.log(3)
1.0986122886681098

As natural logarithms are the inverse operation to exponentials, we have the following rules:

\[ \ln(e^x) = x \;\;\;\;\; e^{\ln(x)} = x \]

Example

We can simplify \(e^{\ln(x^4+2)} = x^4 + 2\).


Using now the exp method from sympy.

from sympy import exp

exp(slog(x ** 4 + 2))
\[\displaystyle x^{4} + 2\]

Example

We can simplify \(\ln{\left[e^{(5x^4 + 2^x)}\right]} = 5x^4 + 2^x\).


slog(exp(5 * x ** 4 + 2 ** x))
\[\displaystyle 2^{x} + 5 x^{4}\]

Laws of Logarithms#

There are 3 laws of logarithms that we use to help algebraically manipulate equations involving logarithms:

  1. \(\log_a(x\times y) = \log_a(x) + \log_a(y)\)

  2. \(\log_a\left(\frac{x}{y}\right) = \log_a(x) - \log_a(y)\)

  3. \(\log_a(x^b) = b \times \log_a(x)\).

Example

Simplify each of the following into a single logarithm:

  1. \(\log(5) + \log(2)\)

  2. \(\log_3(2) + \log_3(2) + \log_3(2)\)

  3. \(3\log(3) - 2\log(2)\)

  4. \(\ln(8) - \ln(4)\)

Solution:

  1. Using the first law, we can see that:

    \[ \log(5) + \log(2) = \log(5 \times 2) = \log(10) \]

    Since \(\log\) is used to denote the base 10 logarithm, \(\log(10) = 1\).

  2. this can be done using law 1 or by using law 3:

    \[ \log_3(2) + \log_3(2) + \log_3(2) = 3\log_3(2) = \log_3(2^3) = \log_3(8) \]
  3. Law 3 need to be used to remove the co-efficient in front of the logs:

    \[ 3\log(3) - 2\log(2) = \log(3^2) - \log(2^3) = \log(27)-\log(4) \]

    Now law 2 is applied to produce, \(\log\left(\frac{27}{4}\right)\), which can be simplified further.

  4. Law 2 is used to give:

    \[ \ln(8)-\ln(4) = \ln\left(\frac{8}{4}\right) = \ln(2) \]

We can use sympy.logcombine to simplify these logarithms.

from sympy import logcombine, expand_log

logcombine(slog(5) + slog(2))
\[\displaystyle \log{\left(10 \right)}\]
logcombine(slog(2) + slog(2) + slog(2))
\[\displaystyle \log{\left(8 \right)}\]
logcombine(3 * slog(3) - 2 * slog(2))
\[\displaystyle \log{\left(\frac{27}{4} \right)}\]
logcombine(slog(8) - slog(4))
\[\displaystyle \log{\left(2 \right)}\]

We use \(\log\) without any explicit base as the rules of logarithms are the same for all bases.


Equilibrium Constants

A general reaction takes place of the form:

\[ a\textrm{A} + b\textrm{B} \rightarrow c\textrm{C} + d\textrm{D} \]

where the equilibrium constants \(K\) is defined as \(K = [\textrm{A}]^{-a}\times[\textrm{B}]^{-b}\times[\textrm{C}]^{-c}\times[\textrm{D}]^{d}\). Find the logarithm of \(K\), as a sum of logarithms.

Solution: First, we take the log of both sides of the equation.

\[ \log{(K)} = \log{\left([\textrm{A}]^{-a}\times[\textrm{B}]^{-b}\times[\textrm{C}]^{-c}\times[\textrm{D}]^{d}\right)} \]

Then using law 1, we can expand this to be a sum of several logs.

\[ \log{(K)} = \log([\textrm{A}]^{-a}) + \log([\textrm{B}]^{-b}) + \log([\textrm{C}]^{-c}) + \log([\textrm{D}]^{-d}) \]

Now, we can use law 3 to bring the powers down.

\[ \log{(K)} = -a\log([\textrm{A}]) -b\log([\textrm{B}]) + c\log([\textrm{C}]) + d\log([\textrm{D}]) \]

The sympy.expand_log function can be used to perform the opposite operation from logcombine, as we show below.

A, B, C, D = symbols('A B C D', positive=True, real=True)
a, b, c, d = symbols('a b c d', positive=True, real=True)

expand_log(slog(A ** -a * B ** -b * C ** c * D ** d))
\[\displaystyle - a \log{\left(A \right)} - b \log{\left(B \right)} + c \log{\left(C \right)} + d \log{\left(D \right)}\]

Converting Between Logarithms and Different Bases#

We may wish to convert between the different logarithms of different bases, so for example, we might want to convert \(\log(x)\) to \(\ln(x)\). We do this using the following formula:

\[ \log_a(x) = \frac{1}{\log_b(a)} \times \log_b(x) \]

Example

Convert \(\log(x)\) to be in the form \(\ln(x)\)..

Solution: Remember that \(\log(x) = \log_{10}(x)\) and \(\ln(x) = \log_e(x)\). So using the formula above, we have:

\[ \log_{10}(x) = \frac{1}{\log_e(10)}\times \log_e(x) = \frac{\ln(x)}{\ln(10)} \]