Int 3 for Programmers

Functions, transformations, composition and inverses

1 / 396
Contents

Contents

Int 3 for Programmers

0 of 396 complete

10 min read

What a function promises

Notation, domain and range, read from an equation, a table and a graph.

A vending machine takes a code and hands back one item. Press B4 today and you get a bag of crisps. Press B4 tomorrow and you get a bag of crisps. There is never a moment where B4 gives crisps to one person and chocolate to the next, and there is never a moment where it gives out two things at once.

That reliability is the whole idea behind the word function in mathematics. A function is a rule that takes an input and gives back exactly one output. Not two, not sometimes none. Exactly one, every time you ask.

Here is a rule written as code. Run it and watch six inputs come back paired with their outputs.

Try CodePython

Change the rule to 3 * x - 4 and run it again. Which input now comes back as 2?

Six inputs went in and six outputs came out, one apiece. Nothing was skipped and nothing produced a pair of answers. That is what you were promised.

The notation

Mathematics writes that rule almost exactly as the code does.

f(x)=2x+1f(x) = 2x + 1
Read it as: the function called f, given an input called x, produces 2x + 1.

The letter ff is the name of the function. The letter xx inside the brackets is a placeholder for whatever you put in, and it has no value of its own. To use the rule you replace every xx on the right by the input. So f(3)=2×3+1=7f(3) = 2 \times 3 + 1 = 7, and f(−2)=2×(−2)+1=−3f(-2) = 2 \times (-2) + 1 = -3.

You will also see yy used for the output, as in y=2x+1y = 2x + 1. That is the same rule with the output named instead of the function. When a question says find f(3)f(3) and you have been given y=2x+1y = 2x+1, it is asking for the yy that goes with x=3x = 3.

The reverse question comes up as often. Given the output, which input produced it? Run this and watch the search.

Try CodePython

Change 11 to 1, then to 12. One of those prints nothing at all. Why can whole numbers alone miss the answer?

Finding xx from f(x)=11f(x) = 11 is solving an equation: 2x+1=112x + 1 = 11, so 2x=102x = 10 and x=5x = 5. Finding f(5)f(5) is substituting. Exams ask for both and the words look similar, so read carefully: a value in the brackets means substitute, a value after the equals sign means solve.

Which inputs are allowed

Not every rule accepts every number. Run this one and look at what happens on the last line.

Try CodePython

Change the 2 inside x - 2 to 5, and change the last two lines to use 5. A different input becomes the forbidden one.

Every input except 2 gave an ordinary number. The input 2 did not, because it asks the machine to divide by zero, and there is no number that answers that. The rule has nothing to say about 2.

So a function comes with a list of inputs it accepts. The domain of a function is the set of all inputs it is allowed to take. The range is the set of all outputs it actually produces. For f(x)=1x−2f(x) = \frac{1}{x-2} the domain is every real number except 2.

There are only two ways an ordinary formula throws an input out at this stage of the course. Dividing by zero is one. Taking the square root of a negative number is the other, because no real number squares to give something negative. Watch both refuse.

Try CodePython

Change the test x < 0 to x < -100 and run it again. The program now tries the impossible one, and you can see exactly which inputs a rule with a square root has to refuse.

The range takes a little more looking at, because it is about what comes out rather than what goes in. Run this and read the smallest output.

Try CodePython

Widen the sweep to run from -40 to 40. The largest output grows without limit, but the smallest one does not move. What number is it stuck on, and which input produces it?

Squaring can never give a negative result, so x2x^2 is at least 0 and x2+3x^2 + 3 is at least 3. The range of gg is every number from 3 upwards. The value 3 is reached, at x=0x = 0, so it belongs in the range rather than being merely approached.

The same function, three ways

A function can arrive as a formula, as a table of pairs, or as a graph. All three say the same thing and an exam moves between them without warning.

FormWhat you are givenHow to read f(3)
Equationf(x)=2x+1f(x) = 2x + 1Substitute 3 for x and work it out
TableRows of inputs beside outputsFind the row with 3 in the input column
GraphA curve on axesGo to 3 on the horizontal axis, then read the height
One function, f(x) = 2x + 1, in the three forms a question can use.

On a graph the input is the horizontal coordinate and the output is the height, so the point (3,7)(3, 7) is exactly the statement f(3)=7f(3) = 7. Reading a domain off a graph means asking how far left and right the curve goes. Reading a range means asking how far down and up it goes.

A pairing that gives one input two different outputs is not a function. You have already met the machine version of this promise: a dictionary or map keyed by the input cannot hold two values for one key. Run it.

Try CodePython

Add table[5] = 12 a second time and run it again. Adding a repeat that agrees changes nothing; it was the disagreement at 3 that destroyed the promise.

The second instruction for 3 overwrote the first, because the structure refuses to hold both. On a graph the same refusal has a picture: if a vertical line ever crosses a curve twice, then one input has two outputs and the curve is not a function. That check is called the vertical line test.

A table lists the pairs (1, 4), (2, 9), (3, 4) and (2, 11). Is it a function?

No. The input 2 appears twice with different outputs, 9 and 11, so the promise is broken. The repeated output 4 for the inputs 1 and 3 is fine: two inputs sharing an output is allowed, and only one input having two outputs is not.

Check yourself

If h(x)=x+1x−4h(x) = \frac{x+1}{x-4}, which input is not in the domain?

All lessons in Int 3 for Programmers