Skip to main content

Discrete Math with SageMath: Learn math with open-source software

Section 2.3 Operations on Sets

Subsection 2.3.1 Union of Sets

There are two distinct methods available in Sage for calculating unions.
Suppose \(A = \{1, 2, 3, 4, 5\}\) and \(B = \{3, 4, 5, 6\}\text{.}\) We can use the union() function to calculate \(A \cup B\text{.}\)
Alternatively, we can use the | operator to perform the union operation.

Subsection 2.3.2 Intersection of Sets

Similar to union, there are two methods of using the intersection function in Sage.
Suppose \(A = \{1, 2, 3, 4, 5\}\) and \(B = \{3, 4, 5, 6\}\text{.}\) We can use the intersection() function to calculate \(A \cap B\text{.}\)
Alternatively, we can use the & operator to perform the intersection operation.

Subsection 2.3.3 Difference of Sets

Suppose \(A = \{1, 2, 3, 4, 5\}\) and \(B = \{3, 4, 5, 6\}\text{.}\) We can use the difference() function to calculate the difference between sets.
Alternatively, we can use the - operator to perform the difference operation.

Subsection 2.3.4 Multiple Sets

When performing operations involving multiple sets, we can repeat the operations to get our results. Here is an example:
Suppose \(A = \{1, 2, 3, 4, 5\}\text{,}\) \(B = \{3, 4, 5, 6\}\) and \(C = \{5, 6, 7\}\text{.}\) To find the union of all three sets, we repeat the union() function.
Alternatively, we can repeat the | operator to perform the union operation.
The intersection() and difference() functions can perform similar chained operations on multiple sets.

Subsection 2.3.5 Complement of Sets

Let \(U = \{1, 2, 3, 4, 5, 6, 7, 8, 9\}\) be the universal set. Given the set \(A = \{1, 2, 3, 4, 5\}\text{.}\) We can use the difference() function to find the complement of \(A\text{.}\)
Alternatively, we can use the - operator.

Subsection 2.3.6 Cartesian Product of Sets

Suppose \(A = \{1, 2, 3, 4, 5\}\) and \(D = \{x, y\}\text{.}\) We can use the cartesian_product() and Set() functions to display the Cartesian product \(A \times D\text{.}\)
Alternatively, we can use the . notation to find the Cartesian product.

Subsection 2.3.7 Power Sets

The power set of the set \(V\) is the set of all subsets, including the empty set \(\{ \emptyset \} \) and the set \(V\) itself. Sage offers several ways to create a power set, including the Subsets() and powerset() functions. First, we will explore the Subsets() function. The Subsets() function is more user-friendly due to the built-in Set methods. Next, we will examine some limitations of the Subsets() function. We introduce the powerset() function as an alternative for working with advanced sets not supported by Subsets().
The Subsets() function returns all subsets of a finite set in no particular order. Here, we find the power set of the set of vowels and view the subsets as a list where each element is a Set.
We can confirm that the power set includes the empty set.
We can also confirm that the power set includes the original set.
The cardinality() method returns the total number of subsets.
There are limitations to the Subsets() function. For example, the Subsets() function does not support non-hashable objects.
About hashable objects:
  • A hashable object has a hash value that never changes during its lifetime.
  • A hashable object can be compared to other objects.
  • Most of Python’s immutable built-in objects are hashable.
  • Mutable containers (lists or dictionaries) are not hashable.
  • Immutable containers (tuples) are only hashable if their elements are hashable.
You will see an unhashable type error message when trying to create Subsets of a list containing a list. The powerset() function returns an iterator over the list of all subsets in no particular order. The powerset() function is ideal when working with non-hashable objects.
The powerset() function supports infinite sets. Let’s generate the first 7 subsets from the power set of integers.
While the Subsets() function can represent infinite sets symbolically, it is not practical.
Observe the TypeError message when trying to retrieve a random element from Subsets(ZZ)
Pay close attention to the capitalization of function names. There is a difference between the functions Subsets() and subsets(). Notice the lowercase s in subsets(), which is an alias for powerset().

Subsection 2.3.8 Viewing Power Sets

Power sets can contain many elements. The powerset of the set \(R\) contains elements \(128\) elements.
If we only want to view part of the power set, we can specify a range of elements with a technique called slicing. For example, here are the first 5 elements of the power set.
Now, let’s retrieve the following 5 elements of the power set.