Functions

Functions Recap

five properties fun-tot-inj-sur-bij

binary relation’s five properties: fun-tot-inj-sur-bij

Definition of function

A function, f : S → T, is a binary relation f ⊆ S ×T that satisfies (Fun) and (Tot). That is, for all s ∈ S there is exactly one t ∈ T such that (s,t) ∈ f.

We write f(s) for the unique element related to s.

We write $T^S$ for the set of all functions from S to T.

f : S →T describes pairing of the sets: it means that f assigns to every element s ∈ S a unique element t ∈ T. To emphasise where a specific element is sent, we can write f : x → y, which means the same as f(x) = y

Symbol Symbol Symbol Description
S domain of f Dom(f) (inputs)
T co-domain of f Codom(f) (possible outputs)
f(S) image of f Im(f) (actual outputs)
S domain of f Dom(f) (inputs)

example

The identity function on S

Important!

The domain and co-domain are critical aspects of a function’s definition.

$f: \mathbb{N} \rightarrow \mathbb{Z}$ given by $f(x)=x^2$

and

$g: \mathbb{N} \rightarrow \mathbb{N}$ given by $g(x)=x^2$

are different functions even though they have the same behaviour!

Injective functions

Function $f : S → T$ is called an injection or 1-1 (one-to-one) if it satisfies (Inj)

Examples

functions that are injective

$f : N→N$ with $f(x) → x$

set complement (for a fixed universe)

functions that are not injective

absolute value, floor, ceiling

length of a word

Surjective functions

Function $f : S → T$ is called a surjection or onto if it satisfies (Sur). That is, if

Examples

functions that are surjective

$f : \mathbb{N} → \mathbb{N} \quad with \quad f(x) → x$

Floor, ceiling

functions that are not surjective

$f : \mathbb{N} → \mathbb{N} \quad with \quad f(x) → x^2$

Bijection functions

Both (Inj) and (Suj) functions

Functions on finite sets

Take Notice

For a finite set S and $f : S → S$ the properties surjective and injective are equivalent.

Functional Composition

Q: If $f : S →T$ and $g :T →U$ are functions, then $f;g$ is a relation. When is it a function?

A: In the lecture, I said Injective. But Jiaojiao smiled and questioned ‘Is it sufficient?’. The correct answer is ‘Bijective’.

Definition

If $f : S →T$ and $g :T →U$ then the composition of f and g, written $g◦f$ , is the function given by

That is, g ◦ f = f;g.

Take note of the order between f and g. f;g means f first and then g, and g ◦ f is the same of g(f(x)).

Facts

Composition is associative

h ◦(g ◦f) = (h◦g)◦f

For $g : S →T$ (note the g is different from the g above)

S->S->T and S->T->T

Iteration of Functions

If a function maps a set into itself, i.e. when Dom(f) = Codom(f), the function can be composed with itself — iterated

exercise

Let $f ,g : \mathbb{Z} → \mathbb{Z}$ be given by $f(n) = n^2 +3$ and $g(n) = 5n−11$. What is:

Inverse Functions

Converse of a function

Q: If $f : S →T$, then $f^\leftarrow$ is a relation; when is it a function?

A: bijective

If $f^\leftarrow$ is a function then it is called the inverse function; denoted $f^{−1}$.

Take Notice

$f^{−1}$ only exists if f is a bijection.

$f^←$ always exists.

$f^{−1}$ is the procedure of “undoing” f .

Properties of the inverse

If $f : S →T$ and $f^{−1}$ exists then:

Conversely, if $f : S → T \quad and \quad g : T → S$ and

then $f^{−1}$ exists and is equal to g.

Exercises

Q:
f and g are ‘shift’ functions $\mathbb{N}→\mathbb{N}$ defined by f(n)=n+1, and g(n)=max(0,n−1)

(c) Is f injective? surjective?

A:
f(n)=n+1 (Inj) but 0 belongs to the co-domain, and f(n) cannot point 0, so it is not (Sur)

(d) Is g injective? surjective?

g(n)=max{0,n−1}, g(0)=g(1)=0, so g(n) is not (Inj), it is (Suj)

(e) Do f and g commute, i.e. ∀n((f◦g)(n) = (g◦f)(n))?

f(g(n))=g(n)+1=max{0, n-1}+1=max{1,n}, n belongs to Natural Number. The function’s result = 1 if n=0; =n if n>=1

g(f(n))=max{0,n+1-1}=max{0,n}

Thus f and g don’t commute.


Q:
Σ={a,b,c}

(c) Is length:$Σ^∗→\mathbb{N} \quad surjective$?

A:Yes, it is (Surj), $\lambda, a, aa, aaa, aaaa, \dots$.

(d) $length^←(2)\overset{?}{=}$

A:{aa,ab,ac,ba,bb,bc,ca,cb,cc}


Q:
Verifythat $f:\mathbb{R} \times \mathbb{R}→\mathbb{R} \times \mathbb{R}$ defined by $f(x,y)=(x+y,x−y)$ is invertible.

A: to prove the function is bijective.

$f(x,y)=(x+y,x−y) invertible \iff bijective (Inj)(Sur)$

$(Inj)$ suppose f(x,y)=(x+y,x−y),f(w,v)=(w+v,w−v)

$(Sur)$ for any $(m,n) \in \mathbb{R} \times \mathbb{R}$, we need to find (x,y), s.t. f(x,y)=(m,n)

f(x,y)=(x+y,x−y)=(m,n)

Matrices

An m×n matrix is a rectangular array with m horizontal rows and n vertical columns.

Take Notice

Matrices are important objects in Computer Science, e.g. for

  • optimisation
  • graphics and computer vision
  • cryptography
  • information retrieval and web search
  • machine learning

Matrix Motivation

Solvinglinearequations:

5x=15


5x+3y = 15 and 4x−2y = 12

hence


Then how to simultaneous equations?

Basic Matrix Operations

The $transpose A^T$ of an $m \times n$ matrix $A = [a{ij}]$ is the $n \times m$ matrix whose entry in the ith row and jth column is $a{ji}$.

Take notice

A matrix M is called symmetric if M^T = M

Matrix Sum

The sum of two $m \times n$ matrices $A = [a{ij}]$ and $B = [b{ij}]$ is the $m \times n$ matrix whose entry in the ith row and jth column is $a{ij} +b{ij}$.

Fact:

Scalar Product

Given $m \times n$ matrix $A = [a{ij}]$ and $c ∈ \mathbb{R}$, the scalar product cA is the $m \times n$ matrix whose entry in the ith row and jth column is $c · a{ij}$.

Matrix Product

The product of an $m \times n$ matrix $A = [a{ij}]$ and an n×p matrix $B=[b{jk}]$ is the $m \times p$ matrix $C = [c_{ik}]$ defined by

Take Notice

The number of columns of A must be the same as the number of rows of B. (口诀“一行二列反相等”)

The product of a $1 \times n$ matrix and an $n \times 1$ matrix is usually called the inner product of two n-dimensional vectors. One line matrix multiples one column matrix, we could obtain only a number. Note, the 1 means only number 1, it is only a scalar, and n means 1-dimensional vector, just like.

matrix,
and

to be the $n \times 1$ matrix.

to be the $1 \times n$ matrix.

Then

$\cdot$

= $a{11}b{11} + a{12}b{21}$

It is the inner product, which is also resulting in a scalar.

Consider

Calculate AB, BA

Take Notice

In general, $A · B \neq B·A$

Computer Graphics

Rotating an object w.r.t. the x axis by degree α:


Introduction to Big-O Notation

Motivation

Want to compare functions, particularly functions from $\mathbb{N}$ to $\mathbb{R}$

Options

  • Equality: $f(n) = g(n)$ for all n
  • (Pointwise) comparison: $f(n) ≤ g(n)$ for all n
  • (Almost all) comparison: $f(n) ≤ g(n)$ for all but finitely many n
  • Asymptotic growth: $lim_{n->\infin} \frac{f(n)}{g(n)}$

example: Algorithmic analysis

Want to compare algorithms– particularly ones that can solve arbitrarily large instances.

We would like to be able to talk about the resources (running time, memory, energy consumption) required by a program/algorithm as a function f (n) of some parameter n (e.g. the size) of its input.

e.g. How long does a given sorting algorithm take to run on a listof n elements?

Issues

The exact resources required for an algorithm are difficult to pin down. Heavily dependent on:

  • Environment the program is run in (hardware, choice of language, external factors, etc)

  • Choice of inputs used

Cost functions can be complex, e.g.

Need to identify the “important” aspects of the function.

Look at the asymptotic growth: how do the costs scale as n gets large?

“Big-O” Asymptotic Upper Bounds

Definition:

Let $f,g:\mathbb{N} → \mathbb{R}_{≥0}$. We say that g is asymptotically less than f (or: f is an upper bound of g) if there exists $n_0 \in \mathbb{N}$ and a real constant $c > 0$ such that for all $n ≥ n_0$,

Write O(f(n)) for the class of all functions g that are asymptotically less than f .

example

g(n) = 3n+1 ⇒ g(n)≤4n, for all n ≥1

Therefore, 3n + 1 ∈ O(n)

$O(n log n)$ is different from $O(n^2)$

Note! The traditional notation has been

g(n) = O(f(n))

instead of g(n) ∈ O(f(n)).

It allows one to use O(f(n)) or similar expressions as part of an equation; of course these ‘equations’ express only an approximate equality. Thus,

means “There exists a function $f (n) ∈ O(n)$ such that $T(n) = 2T(\frac{n}{2}) + f(n)$.”

properties

Suppose f (n) ∈ O(g(n)), g(n) ∈ O(h(n)) and j(n) ∈ O(k(n)).

Then:

  • f(n) ∈ O(h(n))
  • f(n) +j(n) ∈ O(g(n)+k(n))
  • f(n) · j(n) ∈ O(g(n) · k(n))

example

Generally, for constants $a_k … a_0$,

“Big-Omega” Asymptotic Lower Bounds

Definition

Let $f,g : \mathbb{N} → \mathbb{R}$. We say that g is asymptotically greater than f (or: f is an lower bound of g) if there exists $n_0 ∈ \mathbb{N}$ and a real constant c > 0 such that for all $n ≥ n_0$

Write $\Omega(f(n))$ for the class of all functions g that are asymptotically greater than f.

Example

g(n) = 3n+1 ⇒ g(n)≥3n, for all n ≥1

Therefore, 3n + 1 ∈ Ω(n)

“Big-Theta” Notation

Definition

Two functions f,g have the same order of growth, or are asymptotically equivalent, if they scale up in the same way:
There exists $n_0 ∈ \mathbb{N}$ and real constants c > 0, d > 0 such that for all $n ≥ n_0$,

Write Θ(f(n)) for the class of all functions g that have the same order of growth as f .

If g ∈ O(f) (or Ω(f)) we say that f is an upper bound (lower bound) on the order of growth of g; if g ∈ Θ(f) we call it a tight bound.

Properties

Observe that, somewhat symmetrically

g∈Θ(f) ⇐⇒ f ∈Θ(g)

We obviously have

Θ(f(n)) ⊆ O(f(n)) and Θ(f(n)) ⊆ Ω(f(n)),

in fact

Θ(f(n)) = O(f(n)) ∩ Ω(f(n)).

At the same time the ‘Big-Oh’ is not a symmetric relation

g ∈O(f) ̸⇒ f ∈O(g),

but

g ∈O(f) ⇔f ∈Ω(g)

Observations

For all k,ϵ > 0:

All logarithms have the same order, irrespective of base:

Exponentials to different bases have different orders:

Similarly for polynomials

Examples

Here are some of the most common functions occurring in the analysis of the performance of programs (algorithm complexity), arranged in increasing asymptotic growth:

Take Notice

$O(1) ≡ const$, although technically it could be any function that varies between two constants c and d.

Exercises

Q: True or false?

A:
TTF->$O(4^n)$
TTFT


Welcome to point out the mistakes and faults!