Download presentation
Presentation is loading. Please wait.
Published bySuharto Susman Modified over 6 years ago
1
Functions CSRU1400, Spring 2008 Ellen Zhang CSRU1400 Spring 2008
2
Outline What is a function ? Web project: part 1 and 2
Property of functions: onto, one-to-one Pigeonhole principle Inverse function & function composition CSRU1400 Spring 2008 Ellen Zhang
3
Remember binary relations ?
A binary relation is a mapping between two sets as defined by a rule. CSRU1400 Spring 2008 Ellen Zhang
4
Some relations are special
We call these special relations “functions” and you have probably met them before. A function is a way of transforming one set of things (usually numbers) into another set of things (also usually numbers). For example: Fahrenheit to Celsius Conversion (link) [℃] = ([°F] − 32) × 5⁄9 CSRU1400 Spring 2008 Ellen Zhang
5
More examples of functions
A function that maps a student to his/her major (or age, height, …) GDP of a country: maps the country to GDP (Gross Domestic Production) CSRU1400 Spring 2008 Ellen Zhang
6
Components of All Functions
Every functions must have Name, typically we use a letter like f, g, or h to name a function Domain, a set of values Codomain, a set of values Rule: maps values in the domain to values in the codomain CSRU1400 Spring 2008 Ellen Zhang
7
Terminology f : A B This means that the function f maps values in the set A (the domain) to the values in the set B (the codomain) CSRU1400 Spring 2008 Ellen Zhang
8
Simple rules We can specify the rules of functions by enumerating ordered pairs that represents the mapping. Name: AddFour Domain: {1, 2, 3} Codomain: {5, 6, 7, 8} Rule: {(1, 5), (2, 6), (3, 7)} CSRU1400 Spring 2008 Ellen Zhang
9
Using mathematic formula
Rules can also be specified using formula Functions mapping numbers to numbers f(a) = a + 4, “f of a equals a plus 4” g(b) = b * b + 2, “g of b equals b times b plus 2” h(c) = 5, “h of c equals 5” CSRU1400 Spring 2008 Ellen Zhang
10
Rules continued When we see rules we often ask what their value might be when given concrete values. Take the formula from the previous page f(a) = a+4 What is its value when a equals 7? Answer: 11 CSRU1400 Spring 2008 Ellen Zhang
11
More Rules To abbreviate this questions I might just ask what is the f(7)? This means substitute the 7 for the a and solve the equation on the right hand side. Try out the following formulas and related questions on the next slide. CSRU1400 Spring 2008 Ellen Zhang
12
Formula Problems f(x) = 2x + 3 g(x) = 7 What is the f(5)?
What is the g(3)? What is the g(7)? What is the g(-10)? 13 19 -5 7 7 7 CSRU1400 Spring 2008 Ellen Zhang
13
So what is a function really?
A function has three components: domain, codomain and rule; and you can take every value in the domain, use the rule to map the value into a single value that is in the Codomain CSRU1400 Spring 2008 Ellen Zhang
14
Definition of function
A function F from A to B is a binary relation with domain A and codomain B, where for every , there is exactly one element for which CSRU1400 Spring 2008 Ellen Zhang
15
But that makes no sense? Let’s look at an example. Is this a function?
Suppose I define my domain to be {1, 2, 3} And I define my Codomain to be {5, 6, 7, 8} And my formula is f(x) =x + 5 Is this a function? To find out, be very meticulous and walk through each value of the domain CSRU1400 Spring 2008 Ellen Zhang
16
Let me try the value 1. f(1) = 1+ 5 = 6
Domain = {1, 2, 3} Codomain = {5, 6, 7, 8} f(x) = x + 5 Let me try the value 1. f(1) = 1+ 5 = 6 6 is in my Codomain. So far it’s working Let me try the value 2. f(2) = = 7 7 is in my Codomain. It is still working Let me try the value 3. f(3) = = 8 8 is in my Codomain. It is still working I have tried all values in my domain, and they are all mapped to a value in codomain. Therefore this is a function CSRU1400 Spring 2008 Ellen Zhang
17
Taking 1 from the domain, works Taking 2 from the domain, works
Domain = {1, 2, 3}, Codomain = {5, 6, 7}, f(x) = x + 5 Almost identical. Taking 1 from the domain, works Taking 2 from the domain, works Taking 3 from the domain, fails. Why? The rule f(x) maps 3 to value 8, which is not in my Codomain. Therefore this example is not a function. CSRU1400 Spring 2008 Ellen Zhang
18
Choose 0. f(x) = 5 … it’s in the Codomain
Domain = Z (all integers) Codomain = Z (all integers) f(x) = x + 5 Ok, begin the same way (take values from the domain and put them in the formula) Choose 0. f(x) = 5 … it’s in the Codomain Choose 1. f(1) = 6 … it’s in the Codomain Choose -1. f(-1) = it’s in the Codomain But we can’t do this forever CSRU1400 Spring 2008 Ellen Zhang
19
Dealing with infinite sets
If the domain is infinite, you can’t try all values in the domain So you need to look for values that might not work and try those. If you can’t find any domain values that don’t work, can you make an argument that all the domain values do work ? CSRU1400 Spring 2008 Ellen Zhang
20
Domain = Z (all integers) Codomain = Z (all integers)
f(x) = x + 5 Hand-waving argument “Regardless of what integer I take from the domain, I can add 5 to that number and still have a value in the Codomain.” Once you convince yourself of this argument then you know it is a function CSRU1400 Spring 2008 Ellen Zhang
21
It always works… so it is a function
Domain = Z (all integers) Codomain = {4, 5, 6} f(x) = 6 Ok choose some values Choose 0: f(0) = 6 … it works Choose 1: f(1) = 6 … it works Choose -1: f(-1) = 6 … it works It always works… so it is a function CSRU1400 Spring 2008 Ellen Zhang
22
Choose some values so it’s not a function
Domain = N (natural numbers, i.e., 0,1,2,…) Codomain = N f(x) = x-1 Choose some values Choose 0: f(0) = -1 … -1 is not in the codomain, it doesn’t work so it’s not a function CSRU1400 Spring 2008 Ellen Zhang
23
We have seen many functions so far …
Are the following functions ? What are their domains and codomains? f(A)=#A-1 f(x) = (x>0) And CSRU1400 Spring 2008 Ellen Zhang
24
Functions with multiple variables
Example: f(x,y)=x-y, where x takes integer value, and y takes integer value f maps an ordered pair of integers, i.e., x and y, to their difference (x-y), which is also an integer What’s the domain ? The set of ordered pairs of integers … In mathematical notation: , the Cartesian product of Z CSRU1400 Spring 2008 Ellen Zhang
25
More examples What are the domain, codomains ? f(x,y)=x>y 2.
3. Union(A,B)= 4. SizeofProduct(A,B) = #A * #B 5. NumOfSubsets (A) = 2#A CSRU1400 Spring 2008 Ellen Zhang
26
Web Projects: develop your own web pages and make them interesting/useful …
CSRU1400 Spring 2008 Ellen Zhang
27
What is a Computer ? Computer is a device capable of hardware devices
Performing computations Making logical decisions Works billions of times faster than human beings hardware devices Keyboard, screen (monitor) Disks, Memory Processing Units CSRU1400 Spring 2008 Ellen Zhang
28
What is Software ? Computer Programs are called software
sets of instructions that process data, e.g., Add two numbers If some condition is true, then go to execute a certain instruction guide computer through orderly sets of actions specified by computer programmers Programmers write instructions that comprise software in various programming languages Java, C ++(CS1) CSRU1400 Spring 2008 Ellen Zhang
29
The Internet A "network of networks" that consists of millions of smaller domestic, academic, business, and government networks. Worldwide, publicly accessible Mixing computing and communications technologies Carrying information and services, such as electronic mail, online chat, file transfer, and the interlinked Web pages and other documents of the World Wide Web. CSRU1400 Spring 2008 Ellen Zhang
30
The World Wide Web Introduced in 1990 by Tim Berners-Lee
A system of interlinked, hypertext documents accessed via the Internet. With a web browser, a user views web pages that may contain text, images, video, audio, and other multimedia, and navigates between them using hyperlinks. CSRU1400 Spring 2008 Ellen Zhang
31
What happens behind the scene ?
A web browser (client) lets a user request a resource. A web server takes the client request and sends something back to the client. Clients and servers know HTML (HyperText Markup Language), the agreed-upon standard for web pages Client Request Response CSRU1400 Spring 2008 Ellen Zhang
32
What is Web Browser ? A Web browser is a software that
enables a user to display and interact with the Web’s rich multimedia content such as text, images, and other information. The Web could be the World Wide Web, a local area network, or a web page on your own machine. Microsoft Internet Explorer, Mozilla Firefox, Safari or Netscape Navigator The appearance of a Web page may differ between browsers. Netscape Browser will Die in February 2008 CSRU1400 Spring 2008 Ellen Zhang
33
URL & Hyperlinks URL (Uniform/Universal Resource Locator) Hyperlinks
Web page address – typing in Address field HTTP (HyperText Transfer Protocol) Protocol for transferring data over the Internet HTTPS (Secure HyperText Transfer Protocol) Protocol for transferring encrypted data over the Internet. Hyperlinks Graphical or textual elements Click to link to another Web page Loads new page into browser window CSRU1400 Spring 2008 Ellen Zhang
34
Our computing environment: server
The server is storm.cis.fordham.edu An account with the server has been created for you (if you have requested for it) You have an account name and password, also a file directory, account, a URL CSRU1400 Spring 2008 Ellen Zhang
35
The client: OpenMail Our client software is OpenMail: Using OpenMail, you can Check s (WebMail button) View/Manipulate your directory (WebDisk) Configure your account (e.g., change passwd, Pref) Detailed instruction at the course web site… Create, edit a file, upload a file … CSRU1400 Spring 2008 Ellen Zhang
36
Steps to build your web pages
URL for your web page with storm is: When someone (anyone) enters above URL into web browser, browser will try to fetch & display index.html file located under ~<your_account>/public_html/, at storm Therefore, you need to create a file named index.html under public_html directory CSRU1400 Spring 2008 Ellen Zhang
37
Build you home page … Your index.html have to follow certain standard, HTML or XHTML Start with the simple example copy and paste it to your editor save and preview it Experiment by adding one thing a time Borrow someone else’s good ideas When you see a cool web page, select to view its source in the browser Copy and Paste to your editor CSRU1400 Spring 2008 Ellen Zhang
38
First HTML Example (1) <html> Basic HTML Elements: <head>
<title>My first Web Page</title> </head> <body bgcolor = "white"> <h2> My first Web Page</h2> <p>I am so happy to be at Fordham! </body> </html> head element Head section: Title of the document and Style sheets and scripts body element Body section: Page’s content the browser displays CSRU1400 Spring 2008 Ellen Zhang
39
What is HTML Element ? A name and value pair Example:
Every element starts with Start tag and ends with End tag, with the displayed content in between them. Example: <html>. . .</html>, <head>. . .</head>. Start tag may have attributes (provide additional information about an element) A name and value pair Example: <body bgcolor = "white"> CSRU1400 Spring 2008 Ellen Zhang
40
Headers Elements Six headers ( header elements): h1 ~ h6
CSRU1400 Spring 2008 Ellen Zhang
41
CSRU1400 Spring 2008 Ellen Zhang
42
Adding HyperLink References other sources such as HTML documents and images Both text and images can act as hyperlinks Created using the a (anchor) element: <a> </a> Attribute href specifies the location of a linked resource : href = “ Link to addresses: href = CSRU1400 Spring 2008 Ellen Zhang
43
links.html (1 of 2) CSRU1400 Spring 2008 Ellen Zhang
44
Adding Images Element (1)
img element with attributes: src attribute : Specifies the location of the image file width and height attributes: Pixels (“picture elements”) alt attribute : the text will be displayed if the browser could not display the image. CSRU1400 Spring 2008 Ellen Zhang
45
Adding Images Element (2)
Image elements are empty, i.e., nothing goes between start tag and end tag <img src=“1.jpg” height=“238” width=“183”></img> Shorthand: terminated by character / inside the closing right angle bracket (>) <img src=“1.jpg” height=“238” width=“183” /> CSRU1400 Spring 2008 Ellen Zhang
46
picture.html (1 of 1) Ellen Zhang CSRU1400 Spring 2008
47
CSRU1400 Spring 2008 Ellen Zhang
48
Image as Link Use an image as a link
<a href= <img src= "yahoo.gif" width="232" height = "44" /> </a> CSRU1400 Spring 2008 Ellen Zhang
49
Internal Linking Enables user to jump between locations in the same document First, create an internal hyperlink destination by setting attribute id of an element Second, create an internal link to this element. Example: <h1 id=“bugs”>Bugs </h1> : <a href=“#bugs”>Go to Bugs</a> CSRU1400 Spring 2008 Ellen Zhang
50
links.html (1 of 3) CSRU1400 Spring 2008 Ellen Zhang
51
links.html (3 of 3) CSRU1400 Spring 2008 Ellen Zhang
52
Unordered Lists Unordered list element ul
Creates a list in which each item begins with a bullet symbol (called a disc) li (list item): Entry in an unordered list Format: <ul> <li> first item </li> <li> second item </li> </ul> CSRU1400 Spring 2008 Ellen Zhang
53
Nested and Ordered Lists
Nested lists: Represent hierarchical relationships Ordered lists (ol) Creates a list in which each item begins with a number Format <ol> <li> first item </li> <li> second item </li> </ol> CSRU1400 Spring 2008 Ellen Zhang
54
list.html (1 of 3) CSRU1400 Spring 2008 Ellen Zhang
55
list.html (2 of 3) CSRU1400 Spring 2008 Ellen Zhang
56
list.html (3 of 3) CSRU1400 Spring 2008 Ellen Zhang
57
CSRU1400 Spring 2008 Ellen Zhang
58
We need to do some simple programming …
So far, everything is static, i.e., the contents of the web page is fixed… How to make the content adapts to the viewer, and the time ? Or make your web page interact with viewer ? We need to do some simple programming … CSRU1400 Spring 2008 Ellen Zhang
59
JavaScript JavaScript scripting language
Enhances functionality and appearance Web browser contains a JavaScript interpreter. JavaScript is an Object Oriented Programming (OOP) language. Everything is an object Each object is associated with some attributes (data) and behaviors (methods). A statement should end with a semicolon (;). JavaScript is case sensitive. CSRU1400 Spring 2008 Ellen Zhang
60
Simple JavaScript Program
Inline scripting Written in the <body> or <head> of a document <script> tag JavaScript Single-line comment symbol: // Example: <script type=“text/javascript”> <! - - script code here // - - > </script> CSRU1400 Spring 2008 Ellen Zhang
61
Programming with JavaScript (1)
document object: the HTML document the browser is currently displaying writeln method writes a line in the document document.writeln(“<h1>Welcome</h1>”); CSRU1400 Spring 2008 Ellen Zhang
62
Example One <script language="javascript“
type="text/javascript"> <!-- document.write(“<p>Last updated on " + document.lastModified + “</p>”); --> </script> CSRU1400 Spring 2008 Ellen Zhang
63
Programming with JavaScript (2)
window object : a browser window. A Window object is created automatically with every instance of a <body> or <frameset> tag alert method creates a Dialog box window.alert(“welcome”); CSRU1400 Spring 2008 Ellen Zhang
64
welcome.html (1 of 1) CSRU1400 Spring 2008 Ellen Zhang
65
welcome4.html 1 of 1 CSRU1400 Spring 2008 Ellen Zhang
66
Variables Variables are used to store data.
Keyword : var A variable's value can change during the script. You can refer to a variable by name to see its value or to change its value. Name of a variable : a series of characters letters, digits, underscores( _ ) and dollar signs ($) no white space not begin with a digit not a keyword. CSRU1400 Spring 2008 Ellen Zhang
67
Use Variables Declare a variable: Assign a value to a variable:
var name; var size; var name, size; Assign a value to a variable: name = “Lisa”; size = 20; Combine two steps: var name = “Lisa”; CSRU1400 Spring 2008 Ellen Zhang
68
Dynamic Welcome Page A script can adapt the content based on input from the user or other variables prompt method of window object window.prompt(“Please enter your name”, “Lisa”); CSRU1400 Spring 2008 Ellen Zhang
69
welcome5.html (1 of 2) CSRU1400 Spring 2008 Ellen Zhang
70
CSRU1400 Spring 2008 Ellen Zhang
71
This is the prompt to the user.
When the user clicks OK, the value typed by the user is returned to the program as a string. This is the prompt to the user. This is the default value that appears when the dialog opens. This is the text field in which the user types the value. Fig. 7.7 Prompt dialog displayed by the window object’s prompt method. CSRU1400 Spring 2008 Ellen Zhang
72
Arithmetic JavaScript performs arithmetic calculations.
Arithmetic operators: Addition: numberOne + numberTwo Subtraction: numberOne – 5 Multiplication: numberOne * numberTwo Division: numberTwo / 6 Rules of operator precedence a * ( b – c ) CSRU1400 Spring 2008 Ellen Zhang
73
Adding Integers Prompt user for two integers and calculate the sum
parseInt method Converts its string argument to an integer parseFloat method Converts its string argument to a float Assignment statement sum = number1 + number2; CSRU1400 Spring 2008 Ellen Zhang
74
Addition.html (1 of 2) CSRU1400 Spring 2008 Ellen Zhang
75
Addition.html (2 of 2) CSRU1400 Spring 2008 Ellen Zhang
76
CSRU1400 Spring 2008 Ellen Zhang
77
Part II of web project: a degree converter
After finish part I , extend your website Create a converter.html file Prompt viewer to input temperature in Fahrenheit Show the degree in Celsius (see formula in slide 4) Add a hyperlink in your main file, i.e., index.html, to the converter.html Here are useful links for doing arithmetic in JavaScript You are welcomed to experiment more… CSRU1400 Spring 2008 Ellen Zhang
78
Outline Definition of function Property of functions Inverse function
Onto One-to-one Pigeonhole principle Inverse function Function composition CSRU1400 Spring 2008 Ellen Zhang
79
Function f: S→T s t f(s)=t f maps s to t t is called the image of s.
Codomain T Domain S s t f f(s)=t f maps s to t t is called the image of s. CSRU1400 Spring 2008 Ellen Zhang
80
Properties of Functions
Two interesting properties of functions A function can be onto A function can be one-to-one Note: If the relation is not a function than these properties are irrelevant CSRU1400 Spring 2008 Ellen Zhang
81
Onto Functions A function is onto if every value in the codomain is the image of some value in the domain. Formally, function f: S→T is onto if CSRU1400 Spring 2008 Ellen Zhang
82
Onto example First you have to figure out if it is a function or not.
Domain = {1, 2, 3, 4} Codomain = {11, 12, 13, 14} f(x) = x + 10 First you have to figure out if it is a function or not. Choose 1: f(1) = 11 Choose 2: f(2) = 12 Choose 3: f(3) = 13 Choose 4: f(4) = 14 So it is a function Is it onto? Yes. Because we covered all of the Codomain values, i.e., every value of codomain is an image of some values in the domain. CSRU1400 Spring 2008 Ellen Zhang
83
Determine whether it is a function
Domain = {1, 2, 3,} Codomain = {0, 1, 2, 3} f(x) = x -1 Determine whether it is a function Choose 1: f(1) = 0 Choose 2: f(2) = 1 Choose 3: f(3) = 2 So it is a function. Is it onto? No, we never arrived at the value 3 which is in the Codomain CSRU1400 Spring 2008 Ellen Zhang
84
Dealing with infinite sets (1)
f: R→R with the rule f(x)=x2+1 Is it a function? Is it an onto function ? For every CSRU1400 Spring 2008 Ellen Zhang
85
Dealing with infinite sets (2)
g: Q+→Q+ with the rule f: R→R with the rule f(x)=x2+4x+1 CSRU1400 Spring 2008 Ellen Zhang
86
One-to-One A function is one-to-one if each value in the Codomain that is reached can only be reached by one value in the domain Each value in the codomain can be the image of at most one value in the domain s t CSRU1400 Spring 2008 Ellen Zhang
87
Example So we must ask if it is a function? Is it one-to-one? Well
Domain = {1, 2, 3,} Codomain = {1, 2, 3, 4} f(x) = x + 1 So we must ask if it is a function? Choose 1: f(1) = 2 Choose 2: f(2) = 3 Choose 3: f(3) = 4 So it is a function. Is it one-to-one? Well we only reached the value 2 by using x = 1. we only reached the value 3 by using x = 2. we only reached the value 4 by using x = 3. So it is one-to-one CSRU1400 Spring 2008 Ellen Zhang
88
Is it a function? Is it one-to-one?
Domain = {-2, -1, 0, 1, 2} Codomain = {0, 1, 2, 3, 4, 5, 6} f(x) = x*x Is it a function? f(-2) = 4, f(-1) = 1, f(0) = 0, f(1) = 1, f(2)=4 So it is a function Is it one-to-one? No. We can reach the value 4 in two ways. CSRU1400 Spring 2008 Ellen Zhang
89
Dealing with infinite sets
f: R->R with the rule f(x)=x2+4x+1 Is the function one-to-one ? CSRU1400 Spring 2008 Ellen Zhang
90
Dealing with infinite sets (cont’d)
g: Q+->Q+ with the rule CSRU1400 Spring 2008 Ellen Zhang
91
Dealing with muti-variate function
Is the following function one-to-one, onto, invertible ? 1. 2. CSRU1400 Spring 2008 Ellen Zhang
92
Outline Definition of function
How to decide if a relation is a function or not Property of functions Onto One-to-one Pigeonhole principle Function composition Inverse function CSRU1400 Spring 2008 Ellen Zhang
93
Function Composition We can also put functions together – this is called composition f ◦ g which reads “f compose g” This means I insert the value of the function g into the function f. f(x) = x+5, g(x) = 2x + 3 The composition says put the value of g into f f ◦ g (x)=f(g(x))=f(2x+3) = (2x+3) + 5 = 2x + 8 CSRU1400 Spring 2008 Ellen Zhang
94
Assume we have two functions with Domains and Codomains over all integers
f(x) = 3x – 2 g(x) = x * x What is f ◦ g ? What is g ◦ f? What is f ◦ f? What is g ◦ g ? What is f ◦ g for g(2)? f(x*x) = 3(x*x)-2 = 3x2-2 g(3x-2) = (3x-2)*(3x-2)=9x2-12x+4 f(3x-2) = 3(3x-2)-2 = 9x-8 g(x*x) = (x*x) * (x*x) = x4 f(g(2)) = f(4) = 3(4) – 2 = 10 CSRU1400 Spring 2008 Ellen Zhang
95
Finding missing functions
If we know f(x), and (g ◦ f)(z), how to find out g(y) ? ex. f(x)=3x-2, (g ◦ f)(z)=9z2-9z, what is g(y) ? CSRU1400 Spring 2008 Ellen Zhang
96
Finding missing functions
If we know g(y), and (g◦ f)(z), how to find out f(x) ? ex. g(y)=2y-1, (g◦ f)(z)=4z2-1, what is f(x) ? CSRU1400 Spring 2008 Ellen Zhang
97
Bijection A function that is both onto and one-to-one is called a bijection, or we say the function is bijective. Illustration of functions and properties using figures CSRU1400 Spring 2008 Ellen Zhang
98
Bijection and Inverse For any bijective function
Every value in the codomain has some value in domain mapped to it (because it’s onto) Every value in the codomain has no more than one value mapped to it (because it’s one-to-one) Therefore, every value in the codomain has one and only one value in the domain mapped to it This means if we reverse the mapping, we get a function too. CSRU1400 Spring 2008 Ellen Zhang
99
What is an inverse? An inverse of a function is another function that reverses the process of the original function. To create an inverse Make the old Codomain the new domain Make the old domain the new Codomain Swap the f(x) and the x in the formula Use algebra to get the f(x) back by itself CSRU1400 Spring 2008 Ellen Zhang
100
Inverse Example New domain = {4, 8, 12, 16}
Domain = {2, 4, 6, 8} Codomain = {4, 8, 12, 16} f(x) = 2x New domain = {4, 8, 12, 16} New Codomain = {2, 4, 6, 8} To compute the new formula reverse the f(x) and the x x = 2f(x) Then solve for f(x) f(x) = x / 2 … so that is our inverse CSRU1400 Spring 2008 Ellen Zhang
101
How to find inverse of a function ?
If the function is given by the set If the function is given by a diagram If the function is given by a formula CSRU1400 Spring 2008 Ellen Zhang
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.