52
snick snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Snick snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

snick

snack

CPSC 121: Models of Computation2008/9 Winter Term 2

Functions

Steve Wolfman, based on notes by Patrice Belleville and others

Page 2: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Lecture Prerequisites

Read Section 7.1.

Solve problems like Exercise Set 7.1, #1-4, 13-14, 23-24.

Page 3: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Learning Goals: Pre-Class

By the start of class, you should be able to:– Define the terms domain, co-domain, range,

image, and pre-image– Use appropriate function syntax to relate

these terms (e.g., f : A B indicates that f is a function mapping domain A to co-domain B).

– Determine whether f : A B is a function given a definition for f as an equation or arrow diagram.

Page 4: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Learning Goals: In-Class

By the end of this unit, you should be able to:– Define the terms injective (one-to-one),

surjective (onto), bijective (one-to-one correspondence), and inverse.

– Determine whether a given function is injective, surjective, and/or bijective.

– Apply your proof skills to proofs about the properties (e.g., injectiveness, surjectiveness, bijectiveness, and function-ness) of functions and their inverses.

Page 5: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Outline

• Functions as “Computation Abstractions”• Definition of Functions and

Function Terminology• Function Properties

– Injective– Surjective– Bijective

• Function Operations– Inverse– Composition Dropped from learning goals and exam.

Page 6: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

What is this?

control

data1

data2

output

Page 7: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

What is this?

control

data1

data2

output

Page 8: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

What is this?

control

data1

data2

output

Page 9: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

What is this?

control

data1

data2output

In the lab, you implemented a multiplexer and then used it as a piece in larger circuits.

You abstracted from the concrete implementation to a description of its function:

f(control, data1, data2) = output= (~control data1) (control data2) = data1 if control is 0, but data2 otherwise

Page 10: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Functions, Abstraction, and CPSC 121

Computer scientists use many abstraction levels, with reasoning and often execution tools at each level.

In 121, we learn tools for some key abstraction levels: wiring physical gates, computer-based hardware design techniques, propositional logic, predicate logic, sets, …

Functions are a new level that let us talk about how computations work and fit together.

Bonus: functions alone are enough for all of computation, using the “λ calculus”.

Page 11: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Outline

• Functions as “Computation Abstractions”• Definition of Functions and

Function Terminology• Function Properties

– Injective– Surjective– Bijective

• Function Operations– Inverse– Composition

Page 12: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

What is a Function?

Mostly, a function is what you learned it was all through K-12 mathematics, with strange vocabulary to make it more interesting…

A function f:A B maps values from its domain A to its co-domain B.

Domain Co-domainf(x) = x3

f(x) = x mod 4f(x) = x

Look, sets!

CORRESPONDS TO TEXTBOOK READING(NOT COVERED IN CLASS)

Page 13: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Plotting Functions

f(x) = x3

f(x) = x mod 4

f(x) = x

Not every function is easy to plot!

CORRESPONDS TO TEXTBOOK READING(NOT COVERED IN CLASS)

Page 14: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

What is a Function?

Not every function has to do with numbers…

A function f:A B maps values from its domain A to its co-domain B.

Domain Co-domainf(x) = ~x

f(x,y) = x yf(x) = x’s phone #

CORRESPONDS TO TEXTBOOK READING(NOT COVERED IN CLASS)

Page 15: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

What is a Function?

A function f:A B maps values from its domain A to its co-domain B.

f(control, data1, data2)= (~control data1) (control data2)

Domain?Co-domain?

CORRESPONDS TO TEXTBOOK READING(NOT COVERED IN CLASS)

Page 16: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

What is a Function?

A function f:A B maps values from its domain A to its co-domain B.

Domain?Co-domain? Other examples?

Alan

Steve

Paul

Patrice

Karon

George

111

121

211

CORRESPONDS TO TEXTBOOK READING(NOT COVERED IN CLASS)

Page 17: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

What is a Function?

A function f:A B maps values from its domain A to its co-domain B.

f can’t map one element of its domain to more than one element of its co-domain:

x A, y1,y2 B, [(f(x) = y1) (f(x) = y2)] (y1 = y2).

Why insist on this?A B

f

CORRESPONDS TO TEXTBOOK READING(NOT COVERED IN CLASS)

Page 18: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Not a Function

Why isn’t this a function?

(The Laffer Curve: a non-functional tax policy.)

CORRESPONDS TO TEXTBOOK READING(NOT COVERED IN CLASS)

Page 19: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Function Terminology

A function f:A B maps values from its domain A to its co-domain B.

For f to be a function, it must map every element in its domain:

x A, y B, f(x) = y.

Why insist on this?

A B

f

Warning: some mathematicians would say that makes f “total”.

CORRESPONDS TO TEXTBOOK READING(NOT COVERED IN CLASS)

Page 20: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Not a Function

Foiled by sabbatical.

Alan

Steve

Paul

Patrice

Karon

George

111

121

211

Anne

CORRESPONDS TO TEXTBOOK READING(NOT COVERED IN CLASS)

Page 21: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Function Terminology

A function f:A B maps values from its domain A to its co-domain B.

f(x) is called the image of x (under f).

x is called the pre-image of f(x) (under f).

A B

f

x

y

CORRESPONDS TO TEXTBOOK READING(NOT COVERED IN CLASS)

Page 22: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Trying out Terminology

f(x) = x2

What is the image of 16?What is the range of f?

x

f(x)

CORRESPONDS TO TEXTBOOK READING(NOT COVERED IN CLASS)

Page 23: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Functions We’ve Used

• Any combinational logic circuit

• The “inverse” and “zip” operations over algorithms from the midterm

• The operation that gives you a student’s grade given the student: G(s) = s’s grade

• Arrays, as long as they’re immutable (unchangeable): A(i) = the ith value in array A.

Sequential circuits and mutable arrays aren’t functions. Why not?(Actually we can model them as functions by making time explicit)

Page 24: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Outline

• Functions as “Computation Abstractions”• Definition of Functions and

Function Terminology• Function Properties

– Injective– Surjective– Bijective

• Function Operations– Inverse– Composition

Page 25: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Function Properties: Injective

A function f:A B is injective (also one-to-one) if each image is associated with at most one pre-image: x,y A, xy f(x) f(y).

AlanStevePaul

PatriceKaron

George

111121211

AlanStevePaul

PatriceKaron

George

211/201211/202211/BCS

111121/202121/203121/BCS

Injective? Injective?

Page 26: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Trying out Terminology

f(x) = x2

Injective?What if f:R+ R+?

x

f(x)

Page 27: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Trying out Terminology

f:{s|s is a 121 student} {A+, A, …, D, F}f(s) = s’s mark in 121

Is f injective?

What if we didn’t know what f represented, only its “type” and the fact that there are lots of 121 students:f:{s|s is a 121 student} {A+, A, …, D, F}

Page 28: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Nifty Injective Function: Error-Correcting Codes

Error-correcting codes must be injective. Why?

Page 29: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Outline

• Functions as “Computation Abstractions”• Definition of Functions and

Function Terminology• Function Properties

– Injective– Surjective– Bijective

• Function Operations– Inverse– Composition

Page 30: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Function Properties: Surjective

f:A B is surjective (also onto) if every element of the co-domain has a pre-image: y B, x A, y = f(x).

AlanStevePaul

PatriceKaron

George

121211

AlanStevePaul

PatriceKaron

George

211/201211/202211/BCS

111121/202121/203121/BCS

Surjective? Surjective?

Can we define “surjective” in terms of “range” and “co-domain”?

Page 31: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Trying out Terminology

f(x) = x2

f:R R0?

Surjective?What if f:R R?What if f:Z Z0?

How about f(x) = x?For what types is it surjective?

x

f(x)

Page 32: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Trying out Terminology

f:{s|s is a 121 student} {A+, A, …, D, F}f(s) = s’s mark in 121

Is f surjective?

Could we ever know that f was surjective just by knowing f’s domain and co-domain?

Page 33: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Nifty Surjective Function: “Lossy” Compression

WARNING: Lossy compression functions are not always surjective… But it’s valuable if they are. Why?

Page 34: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Surjective Functions So Far

Under what circumstances is a combinational circuit with one ouput surjective?

Under what circumstances is it not surjective?

So, are our circuits usually surjective or not?

Page 35: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Outline

• Functions as “Computation Abstractions”• Definition of Functions and

Function Terminology• Function Properties

– Injective– Surjective– Bijective

• Function Operations– Inverse– Composition

Page 36: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Function Properties: Bijective

A function f:A B is bijective (also one-to-one correspondence) if it is both one-to-one and onto.

Every element in the domain has exactly one unique image. Every element in the co-domain has exactly one unique pre-image.

Page 37: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Function Properties: Bijective

A function f:A B is bijective (also one-to-one correspondence) if it is both one-to-one and onto.

AlanStevePaul

PatriceKaron

George

121211

AlanStevePaul

PatriceKaron

George

211/201211/202211/BCS

121/202121/203121/BCS

Bijective? Bijective?

111

Page 38: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Trying out Terminology

f(x) = x2

f:? ?

Bijective for what domain/co-domain?

x

f(x)

Page 39: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Nifty Bijective Function: Encryption/Lossless Compression

Two sets have the same cardinality if we can put them in a bijection. What does that say about lossless compression?

Page 40: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Outline

• Functions as “Computation Abstractions”• Definition of Functions and

Function Terminology• Function Properties

– Injective– Surjective– Bijective

• Function Operations– Inverse– Composition

Page 41: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Function Operations: Inverse

The inverse of a function f:A B is f-1:B A. f(x) = y f-1(y) = x.

How can we tell whether f-1 is a function?

(Hint: what would make f-1 not a function?)

Can we prove it?

Page 42: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Trying out Terminology

What’s the inverse of each of these fs?

AlanStevePaul

PatriceKaron

George

121211

AlanStevePaul

PatriceKaron

George

211/201211/202211/BCS

121/202121/203121/BCS

111

Page 43: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Trying out Terminology

f(x) = x2

What’s the inverse of f?

What should the domain/co-domain be?

x

f(x)

Page 44: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Proving a function injective

Recall: A function f : A B is injective exactly when: x,y A, xy f(x) f(y).

A typical approach is to prove the contrapositive by antecedent assumption: assume f(x) = f(y) and show that x = y.

Page 45: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Problem: x3 + 5 is injective

Theorem: f(x) = x3 + 5 is injective, where f : Z Z.

Page 46: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Proving a function surjective

Recall: A function f : A B is surjective exactly when: y B, x A, y = f(x).

That existential gives us a lot of freedom to pick a witness!

A typical approach is to “solve for” the necessary x given a y.

Page 47: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Problem: w + 2z is surjective

Theorem: f(w,z) = w + 2z is surjective, where f : (Z Z) Z.

Page 48: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Proving a function bijective

Prove that it’s injective.

Prove that it’s surjective.

Done.

Page 49: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Proving a function has an inverse

Prove that it’s bijective.

Done.

Page 50: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

An Inverse Proof

Theorem: If f:A B is bijective, then f-1 : B A is a function.

Recall that f-1(f(x)) = x.

Page 51: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Aside: Functions are Just Sets

We can define functions in terms of sets and tuples (and we can define tuples in terms of sets!).

The function f:A B is the set {(x, f(x)) | x A}.

f is a subset of A B!

Page 52: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Functions Steve Wolfman, based on notes by Patrice Belleville and others

Learning Goals: In-Class

By the end of this unit, you should be able to:– Define the terms injective (one-to-one),

surjective (onto), bijective (one-to-one correspondence), and inverse.

– Determine whether a given function is injective, surjective, and/or bijective.

– Apply your proof skills to proofs about the properties (e.g., injectiveness, surjectiveness, bijectiveness, and function-ness) of functions and their inverses.