<jupyter_start><jupyter_text>3 Counting 3.1 Disjoint Unions<jupyter_code># Size of a set
NewSet = {1, 2}
print(len(NewSet))
# Smallest element in a set
NewSet = {2, 1, 3}
print(min(NewSet))
# Largest element in a set
NewSet = {-9, -1, -7}
print(max(NewSet))
# Print the sum of elements of a set
A = {1, 5, 2, -10, 19}
print(sum(A))
# Verify the above using a 'for' loop
total=0
for i in A:
    total += i
print(total)
A = {1, 2, 3}
B = {1, 3, 5}
print(len(A),len(B))
# Intersection
C = A & B
print(C, "\n", len(C))
# Difference
E = A-B
print(E, "\n", len(E))<jupyter_output>(set([2]), '\n', 1)<jupyter_text>3.2 General UnionsCalculate size of union in two ways: directly and using inclusion exclusion<jupyter_code># Union
D = A | B
print(D, "\n", len(D) ,"\n", len(A)+len(B)-len(C))<jupyter_output>(set([1, 2, 3, 5]), '\n', 4, '\n', 4)<jupyter_text>3.3 Cartesian ProductsWe'll find the cartesian product of two sets $A$ and $B$ and determine $|A\times B|$ in two ways. First, by counting the number of elements in $A\times B$, then by simply multiplying $|A|$ by $|B|$. We begin by importing the **itertools** library.<jupyter_code>import itertools
A = {1, 2, 3}
B = {4, 5}
# Find cartesian product A X B and its size
cartesian_product = set([i for i in itertools.product(A, B)])
print("Ordered pairs in %s x %s:  " %(A,B))
for i in cartesian_product:
    print(i)
print;print("Size = %i" %len(cartesian_product))
# |A X B| directly
print(len(cartesian_product))
# |A X B| using product rule
print(len(A)*len(B))<jupyter_output>6<jupyter_text>3.3 Cartesian Powers We determine the size of $A^k$, the $k$-th cartesian power of $A$ in two ways: calculating $A^k$ and its size, and then via the formula $|A|^k$.<jupyter_code>A = {1, 2, 3}
k = 2
# Print k'th cartesian power of A
print(set(itertools.product(A, repeat = k)))
# Specilized printing
cartesian_power = set([i for i in itertools.product(A, repeat = k)])
print("Tuples in %s^%i: " %(A,k))
for i in cartesian_power:
    print(i)
print;print("Size = ", len(cartesian_power))
# Find |A|^k directly
print(len(A)**k)<jupyter_output>9<jupyter_text>Next we calculate and graph the exponential $2^x$ and the polynomials $x$ and $x^2$. Move the slider to see that the exponential grows much faster than the polynomials. Please experiment with different functions to see how fast they grow.<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import ipywidgets as widgets
def f(x_max):
    x = np.arange(0, x_max, 0.01)
    plt.plot(x, x, 'b', linewidth = 3, label = '$x$')
    plt.plot(x, x**2, 'r', linewidth = 3, label = '$x^2$')
    plt.plot(x, 2**x, 'm', linewidth = 3, label = '$2^x$')
    plt.xlabel('x', fontsize = 20)
    plt.xticks(fontsize = 18)
    plt.yticks(np.linspace(max(2**x_max, x_max**2)/10, max(2**x_max, x_max**2), 10), fontsize = 18)
    plt.gca().set_xlim([0, x_max])
    plt.gca().set_ylim([0, max(2**x_max, x_max**2)])
    plt.gcf().set_size_inches(20, 10)
    plt.legend(fontsize = 20)
    plt.show()
    
widgets.interact(f, x_max=widgets.FloatSlider(min = 0.5, max = 20, step = 0.5))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1689777491131893, 'fraction_numerical': 0.02999032570138665, 'mean_word_length': 4.619565217391305, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '549856', 'license': 'MIT', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1227, 'n_words': 382, 'path': 'week3/3_Counting.ipynb', 'repo_name': 'duttk/probstats', 'size': '3101'}