* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download WEEK 6: FUNCTIONS 1. Motivation Programs can be quite large. In
Mathematics of radio engineering wikipedia , lookup
Abuse of notation wikipedia , lookup
Functional decomposition wikipedia , lookup
Elementary mathematics wikipedia , lookup
Big O notation wikipedia , lookup
Continuous function wikipedia , lookup
Principia Mathematica wikipedia , lookup
Non-standard calculus wikipedia , lookup
Dirac delta function wikipedia , lookup
Function (mathematics) wikipedia , lookup
WEEK 6: FUNCTIONS 1. Motivation Programs can be quite large. In order to design and maintain a large program, it is desirable to break it down into more manageable parts. A well-designed program will consist of several parts which can — to a large extent— be written and tested separately. The concept of function is extremely useful in this respect. 2. The mathematical concept of function Definition 2.1. A function consists of (1) a set, say A, called the domain of the function, (2) a set, say B, called the codomain of the function, (3) a rule, say f , that associates with every element of A a unique element of B. Notation . We shall use the notation f : A → B for a function. Let x ∈ A, and let y ∈ B be the element that the rule f associates with x. We shall write y = f (x) . The mathematical concept of function will be explored in some detail in the Analysis part of the Core Mathematics B unit. For the present, it should suffice to give a few examples. Example 2.1. Let A be the set of all the students registered for this unit. Let B be the set of the genders, i.e. B = {MALE, FEMALE} Define the rule by f (x) = Gender of x . Example 2.2. Let A = N and B = Q. Define the rule by f (x) = x/2 . Example 2.3. Let A = Z and B = Q. Define the rule by f (x) = x/2 . Example 2.4. Let A = N and B = R. Define the rule by f (x) = x/2 . 1 2 WEEK 6: FUNCTIONS Let us consider the last three examples in greater depth. In a way, the rule f appears to be the same in all three cases. Yet, according to Definition 2.1, each example defines a different function. • The function in Example 2.3 differs from that in Example 2.4 because the domain is not the same. • The function in Example 2.4 differs from those in Examples 2.2 and 2.3 because the codomain is not the same. 3. The function as a programming concept Example 3.1. Consider the function sign : R → R defined by ( 1 if x ≥ 0 sign(x) = −1 if x < 0 Implement this function in PYTHON. Example 3.2. Let b ∈ {2, 3, . . .}. Every real number x ∈ (0, 1) can be expressed in the form ∞ X (E) x= an b−(n+1) , n=0 where the coefficients an take values in the set {0, 1, . . . , b − 1}. The series on the right-hand side is called the base-b expansion of x. Write a function that takes a floating-point number x and integers b and n as arguments, and returns a list holding the coefficients a0 , . . . , an−1 in the expansion (E). In summary, we note that the PYTHON concept of function is similar to the mathematical concept. The main difference is that the type of the argument is not specified explicitly in the interface of a PYTHON function. Hence the same function may cater for arguments of many different types. Other programming languages such as C++ are not so permissive. 4. Functions that return nothing! Let a and b be two numbers. Suppose x = a and y = b. It is often useful in programming to have a procedure for swapping the values of x and y. (For instance, when sorting a set of numbers by size.) In PYTHON, the following code does the job temp = y y = x x = temp After these three statements have been executed, the value of the variable x is b and that of the variable y is a. If this procedure is to be used repeatedly in a program, it is desirable to implement it as a “function”. This “function” would return nothing; instead, it would carry out a specific task. Example 4.1. Here’s a failed attempt to write a function to do this: def swap(x,y): temp = y y = x x = temp WEEK 6: FUNCTIONS 3 and here’s a PYTHON session that makes use of this function: >>> u=-1 >>> v=1 >>> swap(u,v) >>> u -1 >>> v 1 >>> Nothing happens! u and v have not been swapped. Read Cogliati, Chapter 7, for the explanation. In brief, the variables x and y inside the function swap are copies of the variables u and v respectively. Making changes to a copy does not affect the original. How, then, does one get hold of the modified copies? One can’t; x and y do not exist outside the function swap. So the function, as defined, is useless. Example 4.2. Here’s a function that works: def swap(listOfTwo): temp = listOfTwo[1] listOfTwo[1] = listOfTwo[0] listOfTwo[0] = temp This gives >>> pair = [-1,1] >>> pair [-1, 1] >>> swap(pair) >>> pair [1, -1] >>> The essential difference is that the argument of the function swap is now a list. Lists are mutable objects, i.e. when a list is used as argument in a function, it is (in some sense) the original that is passed— not a copy. So, although the name used for the list inside the function (e.g. listOfTwo) may differ from the name used outside (e.g. pair), both names refer to the same “address” in the computer memory. The statements inside the function affect the list passed as argument. 5. Built-in functions, modules Last week, we reviewed some of PYTHON’s built-in data types. Each built-in data type comes with some functions that are available to the user. Example 5.1. For the numeric types, there are a number of built-in functions, e.g. abs,pow etc. To find all the functions that are defined for, say, the floating-point type, we may proceed as follows. >>> x =1.0 >>> dir(x) [’__abs__’, ’__add__’, ’__class__’, ’__cmp__’, ’__coerce__’, ’__delattr__’, ’__div__’, ’__divmod__’, ’__doc__’, ’__float__’, ’__floordiv__’, ’__getattribute__’, 4 WEEK 6: FUNCTIONS Name syntax abs abs(x) add x+y cmp x<=y etc. truediv x/y mul x*y sub x-y Table 1. Some built-in result absolute value of x sum of x and y enables comparison of x and y quotient of x and y product of x and y difference of x and y functions for the floating-point type ’__getnewargs__’, ’__hash__’, ’__init__’, ’__int__’, ’__long__’, ’__mod__’, ’__mul__’, ’__neg__’, ’__new__’, ’__nonzero__’, ’__pos__’, ’__pow__’, ’__radd__’, ’__rdiv__’, ’__rdivmod__’, ’__reduce__’, ’__reduce_ex__’, ’__repr__’, ’__rfloordiv__’, ’__rmod__’, ’__rmul__’, ’__rpow__’, ’__rsub__’, ’__rtruediv__’, ’__setattr__’, ’__str__’, ’__sub__’, ’__truediv__’] The functions listed are of the form __nameOfFunction__ The underscores mean that the function is private; the user cannot view the definition of the function. The details of each function’s implementation are hidden from the user, who must rely on the PYTHON documentation to learn what the function does, and how it can be used. On the other hand, functions such as class simply cannot be accessed by the user. We shall discuss that and other functions next week.