Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science,...

22
Intro to Maths for CS: Logarithms Joshua Knowles School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden)

Transcript of Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science,...

Page 1: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Intro to Maths for CS:Logarithms

Joshua Knowles

School of Computer Science, University of Birmingham

Term 1, 2015-16

(Slides by John Barnden)

Page 2: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Textbook Segment

Programme F.2, segment on Logarithms

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 2 / 22

Page 3: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Introduction

! From the point of view of CS, logarithms are one of the most important things tolearn from this module!

! Logs are important in measures of information and entropy (disorder) heavily used insome areas of CS.

! Logarithms crop up a lot in the efficiency analysis of algorithms. For instance ...! Remember Fun Quiz: finding a position in a book took a max time that was roughly

proportional to a logarithm of the number of pages.

! A sorting algorithm that takes n log n time units (n = number of itmes to be sorted) ismuch better than one that takes n2 time units.

! Is it a coincidence that logarithm is an anagram of algorithm ??!!

! A logarithm of n rises much more slowly than n.

See the logarithm graph in textbook (7th Ed: Prog F.11, 6th Ed: Prog F.10).

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 3 / 22

Page 4: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

What is a Logarithm?

Given some POSITIVE real numbers x and b where b ̸= 1,

The logarithm of x to the base b is

the power that you have to raise b to in order to get x .

So if the logarithm is L, then bL = x .

We write L = logb x .

So blogb x = x .

NOTES

! The textbook requires b > 1, but this is not necessary for the definition. However, inpractice, b > 1 almost always.

! In Computer Science (but not in engineering/science more generally), b is typically awhole number ... and 2 is the usual case.

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 4 / 22

Page 5: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Examples 1

log10 100 = 2

log10 10, 000 = 4

log10 10, 000, 000 = 7

log10 10 = 1

log10 1 = 0

log10 0.1 = −1 because 0.1 = 1/10 = 10−1

log10 0.001 = −3 because 0.001 = 1/1000 = 10−3

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 5 / 22

Page 6: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Examples 2

log100 10, 000 = 2

log100 100 = 1

log100 1 = 0

log100 10 = 0.5 because 10 =√

100 = 1001/2

log100 0.1 = −0.5 because 0.1 = 1/√

100 = 100−1/2

log100 0.000001 = −3

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 6 / 22

Page 7: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Examples 3

Those were all relatively easy because the x was a power of 10.

Other cases:

log10 2 ≈ 0.30103 (that is, = 0.30103 approx.)

log10 3 ≈ 0.47712

log10 4 ≈ 0.60206

log10 6 ≈ 0.77815

log10 20 ≈ 1.30103

log10 300 ≈ 2.47712

log10 0.05 ≈ −1.30103

The similarities amongst the above numbers is not coincidental! See later.Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 7 / 22

Page 8: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Examples 4

log2 1 = 0

log2 2 = 1

log2 8 = 3

log2 32 = 5

log2 128 = 7

log2 0.125 = log2 1/8 = −3

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 8 / 22

Page 9: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Examples 5

log4 1 = 0

log4 4 = 1

log4 16 = 2

log4 64 = 3

log4 128 = 3.5

log4 1/128 = −3.5

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 9 / 22

Page 10: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Examples 6

The base doesn’t have to be a whole number.

It doesn’t have to be above 1 for the definition to work, though it almost always is (andthe textbook requires it to be).

For example, let’s use 1/4 as a base.

log 141 = 0

log 14

14 = 1

log 1416 = −2

log 1464 = −3

log 14128 = −3.5

log 14

1128 = 3.5

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 10 / 22

Page 11: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Manipulation of Logarithms, and Some Rules

logb 1 = 0 whatever b is

logb b = 1 whatever b is

logb xy = logb x + logb y

logb x/y = logb x − logb y

logb xy = y logb x

logb 1/y = − logb y

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 11 / 22

Page 12: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Manipulation Examples

log10 5 = log10 10/2 = log10 10 − log10 2

≈ 1 − 0.30103 = 0.69897.

log10 6000 = log10 6.103 = log10 2 + log10 3 + log10 103

≈ 0.30102 + 0.47712 + 3 = 3.77815.

logb [(1 − x)3/y (p2+5)] = 3 logb (1 − x) − (p2 + 5) logb y

43 log2 (n−1) = (22)3 log2 (n−1) = 26 log2 (n−1) =

(2log2 (n−1))6 = (n − 1)6.Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 12 / 22

Page 13: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Reasons for the Manipulation Rules

1 = b0 whatever b is (when b ̸= 0).

b = b1 whatever b is.

blogb x+logb y = blogb x × blogb y (because bp+q = bpbq),but that is just xy .

Similarly,blogb x−logb y = blogb x / blogb y (because bp−q = bp/bq),

but that is just x/y .

by logb x = (blogb x)y (because bpq = bqp = (bq)p),but that is just xy .

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 13 / 22

Page 14: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Changing the Base

logc x = logb xlogb c

(it’s sort of as if the b cancels out)

or: logc x = logc b × logb x .

For example:

log2 x = log10 xlog10 2 ≈ log10 x

0.30103 ≈ 3.3219 log10 x

Special case:

logc b = 1logb c

Example:

log2 10 = 1log10 2

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 14 / 22

Page 15: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Reason for the Base-Change Rule

c logc b × logb x =

(c logc b)logb x =

blogb x = x .

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 15 / 22

Page 16: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Logs of Different Bases Are Proportional to Each Other

If you have two fixed bases b and c, and x is a varying quantity, then the logarithm of xto one of the bases is proportional to the logarithm of x to the other base.

That is, logc x = K logb x .

for some constant K (which is in fact logc b).

Much of the time in Computer Science, when measuring say the efficiency of analgorithm, we’re not interested in K . What’s of main interest are the x-dependentexpressions.

One algorithm might take time proportional to n logb n, while another takes timeproportional to n2. The two constants of proportionality don’t matter much,because n2 will get worse eventually anyway. So the base b doesn’t matter as it onlyaffects the first constant of proportionality.

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 16 / 22

Page 17: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Particular Bases and Abbreviations

In non-CS scientific contexts, log x is an abbreviation for log10 x .

In CS, log x is used either to abbreviate log2 x , or when it doesn’t matter what the base is.Base 2 crops up a lot because of so many algorithms proceeding by splitting possibilitiesinto two groups, as in binary search (and recall again the book-page search problem).

Another important special case are natural logarithms, notated ln x . These have base e,where e is a special important constant in mathematics, expressible as

e =∞!

k=0

1k! = 1

0! + 11! + 1

2! + 13! + 1

4! + . . .

(see Binomials programme of textbook — 7th Ed: F.7, 6th Ed: F.9)

[This formula for e is OPTIONAL material—you do not need it for the examination.]

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 17 / 22

Page 18: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Logarithms and Exponentials

Taking the logarithm of something is the inverse of exponentiation.

x is an exponential function of log x :

x = blogb x

by grows very fast with respect to y , so x grows very fast with respect to logb x ,

and so logb x grows very slowly with respect to x .

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 18 / 22

Page 19: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Changing the Base in an Exponentiation Expression

Consider the exponentiations 125 and 78. It may be convenient to express them both aspowers of the same base, e.g. in order to compare them or combine them to form oneexponentiation.

Note that 12 = 7log7 12. This just follows immediately from the definition of what alogarithm is. So

125 = (7log7 12)5 = 75 log7 12.

So we can, e.g., multiply this with 78 to get 78+5 log7 12.

Or we can change both exponentiations to be powers of, e.g., 10 to get

105 log10 12, 108 log10 7.

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 19 / 22

Page 20: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Changing the Base in an Exponentiation Expression

In CS, one often ends up considering large powers of 2. Consider 240.

It can help to get an intuitive feel for the size of this by converting to base 10, to get

1040 log10 2.

If you happen to know that log10 2 is about 0.301,

then our number is about

1012.04 = 100.04.1012 = 25√

10 × 1012.

Here the first component can only be a bit above 1, so our number is about 1012, whichis a million squared.

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 20 / 22

Page 21: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

log log

We saw that double exponentials such as

2(2y )

grow incredibly fast.

Correspondingly, double logarithms such as

log2 log2 x

grow incredibly slowly.

Double logarithms occasionally crop up in CS, and when they do it can be greatnews!

Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 21 / 22

Page 22: Intro to Maths for CS: Logarithmsjdk/Modules/LecSlides/logs.pdf · School of Computer Science, University of Birmingham Term 1, 2015-16 (Slides by John Barnden) Textbook Segment Programme

Examples of log log growth

log10 log10 10 = log10 1 = 0

log10 log10 100 = log10 2 ≈ 0.30103

log10 log10 1, 000 = log10 3 ≈ 0.47712

log10 log10 1, 000, 000 = log10 6 ≈ 0.77815

log10 log10 1, 000, 000, 000 = log10 9 ≈ 0.95424

log10 log10 10, 000, 000, 000 = log10 10 = 1

log10 log10 100, 000, 000, 000, 000, 000, 000 = log10 20 ≈ 1.30103

To get logb logb x to increase by 1, you have to go from x to xb.

E.g., when b = 2, to get an increase of 1 you have to square x .Barnden (SoCS) Intro to Maths for CS: Logarithms Term 1, 2014–15 22 / 22