# Sets

### From www.norsemathology.org

## Contents |

# Sets

A set is an unordered collection of unique elements. Both Sage and Mathematica provide a means for working with sets, creating sets and performing various operations on them.

## Creating a Set

### Sage

Sets are a built-in type in Python and as such Sage provides built-in support for sets. To create a Set in Sage you use the syntax **set(list)**.

Example - Creating a Set

mySet = set( [1,2,3,4,5] )

### Mathematica

Mathematica doesn't have a separate data-type, instead sets are represented in Mathematica using lists. Mathematica then provides special functions for performing set operations on lists.

Example - Creating a Set

mySet = {1,2,3,4,5}

## Adding Values to a Set

### Sage

The set class in Python provides a method called **add** for adding an element to a set. Note if add is passed an element already in the set then the set remains unmodified.

Example - Add an element to a set

mySet = set([1,2,3,4]) mySet.add(5)

### Mathematica

## Removing a Value from a Set

### Sage

The set class in Python provides a method called **remove** for removing an element from a set. Note that if remove is passed an element not in the set a *KeyError* will be raised.

Example - Remove an element from a set

mySet = set([1,2,3,4]) mySet.remove(2)

### Mathematica

## Union

The union of two sets a,b is the set of all the elements that are in either a or b.

### Sage

Python provides to ways for getting the union of two sets. One is with the *|* operator, and the other is with the method **union**.

Example - Union of a and b

a = set([1,2,3]) b = set([4,5,6]) #all the statements below are equivalent a|b a.union(b) b.union(a) #output set([1, 2, 3, 4, 5, 6])

### Mathematica

Mathematica provides two ways for getting the union of two sets. One is to use the mathematical notation . The other is to use the **Union** function.

Example - Union of a and b

a = {1,2,3} b = {4,5,6} Union[a,b] (* Output: {1, 2, 3, 4, 5, 6} *)

## Intersection

The intersection of two sets a,b is the set of all elements that are in both a and b.

### Sage

Python provides two ways for getting the intersection of two sets. One is to use the *&* operator and the other is to use the method **intersection**.

Example - Intersection of a and b

a = set([1,2,3,9]) b = set([1,2,5,6,7,9]) #The statements below are equivalent a & b a.intersection(b) b.intersection(a) #Output set([1,2,9])

### Mathematica

Mathematica provides two ways for getting the intersection of two sets. One is to use the mathematical notation . The other is to use the function **Intersection**.

Example - Intersection of a and b

a = {1,2,3,9} b = {1,2,5,6,7,9} Intersection[a,b] (* Output: {1,2,9} *)

## Complement

The complement of a set *b* in a set *a* is a set of all elements in *a* but not in *b*.

### Sage

Python provides two ways for determining the complement of a set in another set. One is to use the *-* operator and the other is to use the method *difference*.

Example

a = set([1,2,3]) b = set([1,4,5]) a - b #output set([2,3]) b - a #output set([4,5]) a.difference(b) #output set([2,3])

### Mathematica

In Mathematica the complement of a set *a* in the set *b* is determined using the function **Complement**.

Example

a = {1,2,3} b = {1,4,5} Complement[a,b] #output {2,3} Complement[b,a] #output {4,5}

## Symmetric Difference

Python also provides an operation on sets called the symmetric difference. The symmetric difference of a set *a* and a set *b* is the set of all elements that are in either *a* or *b*, but not both.

### Sage

Python provides two ways for getting the symmetric difference of two sets *a* and *b*. One is to use the method **symmetric_difference**. The other is to use the *^* operator. **Important: The ^ operator only works in Python it does not work in Sage. The reason for this is that Sage's preprocessor considers the ^ operator to mean a raised to the power b, and converts it to **.**

Example

a = set([1,2,3]) b = set([1,4,5]) a.symmetric_difference(b) #output set([2,3,4,5])

## Subsets

A set *a* is a subset of *b* if every element of *a* is also an element of *b*.

### Determining Subsets

#### Sage

To determine if a set *a* is a subset of the set *b* you use the method **issubset**.

Example:

a = set([2,3]) b = set([1,2,3]) a.issubset(b) #Output True b.issubset(a) #Output False

### Getting a list of all Subsets

#### Sage

Python doesn't natively provide a means of getting a list of all subsets of a set. However Sage provides a class called *Subsets* that can be used to get a list of all subsets of a set.

Example:

a = set([1,2,3,4]) ls = Subsets(a) ls.list() #Output: [{}, {1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}, #{1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}] ls = Subsets(a,3) ls.list() #Output: [{1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}]

#### Mathematica

Mathematica provides a function **Subsets** that returns a list of all the subsets of a set.

Example:

a = {1,2,3,4} Subsets[a] #Output: {{}, {1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, # 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}} Subsets[a,{3}] #Output: {{1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}}