ENع
→Functions and relations
الدرس 08 · المنطق والمجموعات

Functions and relations

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة9 خطوات5 أسئلةBeginner
عن هذا الدرس

A function takes each input to exactly one output. Follow one small example, f(x) = x mod 3 from A = {1, 2, 3, 4} to B = {0, 1, 2}: feed every input through the rule, draw each result as an arrow, and see that the arrows are just ordered pairs. Then learn what makes a relation a function, what the range is, and what onto and one-to-one mean, in maths and in Python.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 8 · تفاعلي
01 / 09

Inputs and outputs

A function needs two sets. A = {1, 2, 3, 4} holds the inputs; it is called the domain. B = {0, 1, 2} holds the allowed outputs; it is the codomain. We write f: A → B, read 'f maps A to B'.

>>> A = {1, 2, 3, 4}
>>> B = {0, 1, 2}
>>> len(A), len(B)
(4, 3)
>>> 3 in A, 3 in B
(True, False)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Inputs and outputs

    A function needs two sets. A = {1, 2, 3, 4} holds the inputs; it is called the domain. B = {0, 1, 2} holds the allowed outputs; it is the codomain. We write f: A → B, read 'f maps A to B'.

    >>> A = {1, 2, 3, 4}
    >>> B = {0, 1, 2}
    >>> len(A), len(B)
    (4, 3)
    >>> 3 in A, 3 in B
    (True, False)
  2. A rule for each input

    Our rule is f(x) = x mod 3: divide x by 3 and keep the remainder. Feed in 4: 4 = 1 × 3 + 1, so f(4) = 1. Python writes mod as %. We draw it as an arrow from 4 to 1.

    >>> def f(x):
    ...     return x % 3
    ...
    >>> f(4)
    1
    >>> divmod(4, 3)          # (quotient, remainder)
    (1, 1)
  3. Map every input

    Do the same for every input: f(1) = 1, f(2) = 2 and f(3) = 0. Write each arrow as an ordered pair (input, output), input first. The four pairs are (1, 1), (2, 2), (3, 0) and (4, 1).

    >>> def f(x): return x % 3
    ...
    >>> [f(x) for x in [1, 2, 3, 4]]
    [1, 2, 0, 1]
    >>> [(x, f(x)) for x in [1, 2, 3, 4]]
    [(1, 1), (2, 2), (3, 0), (4, 1)]
  4. Relations are sets of pairs

    Any set of ordered pairs (a, b), with a from A and b from B, is a relation from A to B. Each pair is one arrow. Order matters: (3, 0) is in our relation, but (0, 3) is not.

    >>> R = {(1, 1), (2, 2), (3, 0), (4, 1)}
    >>> (3, 0) in R
    True
    >>> (0, 3) in R            # order matters
    False
  5. Exactly one arrow out

    A relation is a function when every input has exactly one arrow leaving it: not zero, not two. Here 1, 2, 3 and 4 each have one. Two inputs may share an output: 1 and 4 both go to 1.

    >>> R = {(1, 1), (2, 2), (3, 0), (4, 1)}
    >>> [len([b for (a, b) in R if a == x]) for x in [1, 2, 3, 4]]
    [1, 1, 1, 1]
  6. Not a function

    Now add the pair (1, 2). The input 1 has two arrows out, so f(1) has no single answer: this relation is not a function. A relation that leaves some input with no arrow is not a function either.

    >>> R = {(1, 1), (2, 2), (3, 0), (4, 1), (1, 2)}
    >>> sorted(b for (a, b) in R if a == 1)
    [1, 2]
  7. Range and onto

    The range is the set of outputs that are actually hit. {f(x) for x in A} gives {0, 1, 2}, all of B, so f is onto (surjective). The codomain only lists the allowed outputs; a range can be smaller.

    >>> A = {1, 2, 3, 4}
    >>> {x % 3 for x in A}       # the range
    {0, 1, 2}
    >>> {x % 3 for x in A} == {0, 1, 2}
    True
  8. Not one-to-one

    f is one-to-one (injective) when different inputs always give different outputs. Ours is not: f(1) = 1 and f(4) = 1. Seeing the output 1, you cannot tell which input made it, so f has no inverse.

    >>> outs = [x % 3 for x in [1, 2, 3, 4]]
    >>> outs
    [1, 2, 0, 1]
    >>> len(set(outs)) == len(outs)    # one-to-one?
    False
  9. A dict is a function

    A Python dict stores a function as pairs: d = {x: f(x) for x in A} gives {1: 1, 2: 2, 3: 0, 4: 1}. A key can appear only once, so each input keeps exactly one output. d[4] is 1.

    >>> d = {x: x % 3 for x in [1, 2, 3, 4]}
    >>> d
    {1: 1, 2: 2, 3: 0, 4: 1}
    >>> d[4]
    1
    >>> {1: 1, 1: 2}          # a key cannot appear twice
    {1: 2}

اختبار يتاح بعد الخطوة الأخيرة.