# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: DS-ENV
#     language: python
#     name: ds-env
# ---

# + [markdown] tags=[]
# # Set theory
# > Putting things in mathematical boxes
#
# - toc: true
# - badges: true
# - author: <NAME>
# - hide: true
# - categories: [mathematics]
# -

# # Overview
#
# In mathematics, we are frequently interested in characterizing a group of numbers, such as the natural numbers or all the positive integers. Sets are abstract definitions for these collections of numbers. 
#
# In a nutshell, a set is a collection of mathematical objects, also called elements. These objects can be  numbers, symbols, variables, and even functions. We can use set algebra to describe set properties and laws, such as set-theoretic operations like union, intersection, complementation, set equality and set inclusion relations. These systemic procedures help us to evaluate expressions and perform calculations. 

# ## Basic concepts of sets
#
# > A set is a collection of elements. We denote $A = \{ξ_1,ξ_2,...,ξ_n\}$ as a set, where $ξ_i$ is the $i$th element in the set.
#
# Examples of sets and their elements:
#
# $A = \{apple, orange, banana\}$ is a finite set of fruits
#
# $A = \{1, 2, 3, 4, 5, 6\}$ is a finite set of natural numbers
#
# $A = \{2, 4, 6, 8, . . .\}$ is a countable but infinite set of even natural numbers

A = {1, 2, 3, 4, 5, 6}
B = {'apple', 'orange', 'banana'}

# + [markdown] tags=[]
# ###  Membership and cardinality
#
# Sometimes we want to know if a certain element is in a set. 
#
# If a certain $\xi $ is a member (or element) of A, the notation $\xi \in A$ is used.
# For example, we can write $apple \in \{apple, orange, banana\}$. Read in english, apple is in the set of apple, orange and banana. 
#
# The notion of `cardinality` is how big a set is, or how many elements there are in a set. In the above example, the cardinality of the set of fruits is 3.
#
# A set can also be used to describe a collection of sets. Let A and B be two sets. Then C = {A, B} is a set of sets.
# For example, Let $A = \{1, 2\}$ and $B = \{apple, orange\}$. 
# Then $C = \{A, B\} = \{\{1, 2\}, \{apple, orange\}\}$
# -

A = {1, 2}
B = {'apple', 'orange', 'banana'}

1 in A

len(A), len(B)

# To represent sets of sets in Python, the inner sets must be frozenset objects.

# +
A = frozenset([1, 2])
B = frozenset(['apple', 'orange'])

C = {A, B}
# -

# ### Subsets
#
# Given a set, we often want to specify a portion of the set, which is called a `subset`.
#
# suppose B is a subset of A if for any $ξ ∈ B, ξ$ is also in A. We write
# $B ⊆ A$ to denote that B is a subset of A.
# For example, if we have two sets $A$ and $B$ in which $A = \{1, 2\}$ and $B = \{2, 1, 4, 3, 5 \}$ then we write $A \subseteq B$. Read in English, A is a subset of B.
#
# Theres also the notion of a `proper subset` in which states that if A is a subset of B, but A is not equal to B. 
# For example, if we have two sets, $A = \{1,2,3,4,5,6\}$, then $B = \{1,3,5\}$ is a proper subset of A.

# +
A = {1, 2}
B = {2, 1, 4, 3, 5}

A.issubset(B)
# -

# ### Empty set and Universal set
#
# A set is empty if it contains no element. We denote an empty set as $A = \{\emptyset \}$. We also note its size or cardinality is zero.
#
# The universal set is the set containing all elements under consideration. We denote a universal set as
# $A = \Omega$. Note that there is no universtal symbol for this set, here I choose omega. 
#
#
#

# ## Set Operations
#
# ### Union
#
# The union of two sets A and B contains all elements in A or in B. That is, $A ∪ B = \{ξ | ξ ∈ A or ξ ∈ B\}.$ The union of two sets connects the sets using the logical operator `or`.  
#
# For example, If $A = \{1, 2\}, B = \{1, 5\}$, then $A ∪ B = \{1, 2, 5\}$

# +
A = {1, 2}
B = {1, 5}

A.union(B)
# -

# ### Intersection
#
# The `intersection` of two sets A and B contains all elements in A and in B. That is, $A ∩ B = \{ξ | ξ ∈ A$ and $ξ ∈ B\}$.
#
# For example, If $A = \{1, 2, 3, 4\}$, $B = \{1, 5, 6\}$, then $A ∩ B = \{1\}$.

# +
A = {1, 2, 3, 4}
B = {1, 5, 6}

A.intersection(B)
# -

# ### Complement and difference
#
# The complement of a set A is the set containing all elements that are in $\Omega$ but not in A. That is, $A^c = \{ \xi$ | $\xi \in \Omega$ and $\xi \notin A \}$.
#
# For example, Let $A = \{1, 2, 3\}$ and $\Omega = \{1, 2, 3, 4, 5, 6\}$. Then $A^c = \{4, 5, 6 \}$.
#
# The concept of the complement will help us understand the concept of `difference`.
#
# The difference A\B is the set containing all elements in A but not in B. $A \setminus B$ = $\{\xi$ | $\xi \in A$ and $\xi \in B\}$.

# +
A = {1, 2, 3}
B = {1, 2, 3, 4, 5, 6}

B.difference(A)
# -

# ## Closing remarks
#
#
# An axiom is a proposition that serves as a premise or starting point in a logical system. Axioms are not definitions, nor are they theorems. They are believed to be true or true within a certain context.
#
#
# <NAME> noticed that “pure” mathematics, which had completely dis􏱚 associated from the inquiries of natural sciences, was never the center of at􏱚 tention and interest of the great mathematicians of the past. They considered “pure” mathematics as some kind of “entertainment,” a rest from much more important and fascinating problems, which were put forward by natural sci􏱚 ences
#
#
#
#
# Set theory is commonly employed as a foundational system for the whole of mathematics, particularly in the form of Zermelo–Fraenkel set theory with the axiom of choice. 
#
# I don't hold these views. 
#
# Set theory is an amazing piece of mathematical theory that has many usefullness in many areas of mathematics. I just don't think set theory belongs at her foundations. 
#
# You'll see set theory employed in numerical analysis when formulating the number system from scratch. 

# Resources:
#     
# - https://docs.python.org/3/library/stdtypes.html#set or https://realpython.com/python-sets/
# - https://en.wikipedia.org/wiki/Set_theory

StarcoderdataJupyterScriptsDedupFiltered