Extensionality
A set is a collection of objects. The objects in the set are called elements of the set.
- If is an element of a set , we write ;
- If not, we write .
- The set which has no elements is called empty set and denoted .
It does not matter how we specify the set, or how we order its elements or how many times we count its elements. All that matters are what its elements are.
If and are sets, then if and only if every element of is also an element of , and vice versa.
In general, when we have objects , then is the set whose elements are . We emphasise the word the, since extensionality tells us that there can be only one such set. Indeed, extensionality also licenses the following:
We usually don't write out duplicate elements since they have no meaning as extensionality reduces them to same set. In the symbols related to sets are:
Symbol | LaTex |
---|---|
\{ | |
\} | |
\in | |
\notin | |
\emptyset |
Colors used in a website design can be thought as a set. Choosing two blues of the same tone would result in one blue. If two websites use same colors, they use same set of colors.
The color palettes and are same because the elements (colors) are same and the order does not matter. The set is different as only one color matches.
We will later use the sets in relational algebra which is the foundation of SQL databases. For now, we use sets to handle data that are not quantified. One such case is product labels which lists the ingredients used e.g. the amount of milk does not change the fact that a product indeed contains dairy.
def contains_dairy(ingredients: set[str]) -> bool:
return "milk" in ingredients or "butter" in ingredients
print(contains_dairy({ "flour", "milk", "butter" }))
# True
Prove that there is at most one empty set.
Authentication Required
Set Builder
Frequently, we’ll specify a set by some property that its elements share. We’ll use the following shorthand notation for that:
where the stands for the property that has to have in order to be counted among
the elements of the set. The property can be thought as a function that generate the elements.
The symbol is written as \mid
.
We want to serve potatoes to our guests. The head cook decided to serve them in multiples of where base amount is . The maximum serving is no more than . To generate all sizes we write
In programming, it is useful to generate values that follow certain rules. Were we writing a program of the previous example to make it possible to calculate servings for any other foods, the program would look like
def calculate_serving_sizes(base_amount: float, multiplier: float, max_amount: float) -> set[float]:
return { base_amount * multiplier ** i for i in range(1, 10) if base_amount * multiplier ** i <= max_amount}
print(calculate_serving_sizes(100, 1.5, 500))
# {337.5, 225.0, 150.0}
This kind of notation is called set comprehension. Typical use cases may be related to business logic where certain values need to be generated e.g. price classes. The notation in programming languages tend to get longer and more complex than in math so use it only for simple cases.