Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 1261
Image: ubuntu2004
# Sage Project # Ashley Garvey # Set Operations # 1) How do you formulate a set in sage?
x = set({1, 2, 5 ,6})
x
{1, 2, 5, 6}
y = set({0, 3, 7, 8})
y
{0, 8, 3, 7}
# The first step is to name the set. # Then you must use the equal sign (=).
# Lastly you must write set{()} and the elements of the set go within the parenthesis
# 2) How do you find the intersection of two sets?
x & y
set()
# The intersection of two sets is all the elements that are included in both sets # To fint the intersection on sage we use the & sign # If we calculate the intersection of x & y we get the empty set since no elements of the set x are in the set y.
# 3) How do you find the union of two sets?
x | y
{0, 1, 2, 3, 5, 6, 7, 8}
# The union of two sets are the elements from both sets combined into a larger set # To find the union of a set, we use the | symbol. # The union of the sets x | y is {0, 1, 2, 3, 5, 6, 7, 8}
# 4) How do you find the complete set of subsets of a set?
subsets(x)
<generator object powerset at 0x7fc03be6a9e0>
list(subsets(x))
[[], [1], [2], [1, 2], [5], [1, 5], [2, 5], [1, 2, 5], [6], [1, 6], [2, 6], [1, 2, 6], [5, 6], [1, 5, 6], [2, 5, 6], [1, 2, 5, 6]]
subsets(y)
<generator object powerset at 0x7fc03be6a9e0>
list(subsets(y))
[[], [0], [8], [0, 8], [3], [0, 3], [8, 3], [0, 8, 3], [7], [0, 7], [8, 7], [0, 8, 7], [3, 7], [0, 3, 7], [8, 3, 7], [0, 8, 3, 7]]
# To find the complete list of subsets of two sets you first have the generate the sets. To do this you have to do subsets(x). # Then you have to list the set. to do this you have to type list(subsets(x)).
# This will list out all the subsets of the set x.
# 5) How do you check the identity of a set? how about multiple sets
3 in x
False
8 in x
False
1 in x
True
1 in x, 3 in y
(True, True)
0 in x, 4 in y
(False, False)
# You can cehck the identity of each set by asking sage if an element is in the set. For example 0 in x, sage will return false. # This is the case for asking mulitple sets as well but we would jsut place a comma between the sets.
# from class
# Find all the elements of x that are not in y. [LKG]
x.difference(y)
{1, 2, 5, 6}