\subsection{Integral domains}\label{subsec:integral_domains}

\begin{definition}\label{def:integral_domain}
  An \term{integral domain} is an \hyperref[def:entire_semiring]{entire} \hyperref[def:ring/commutative]{commutative (unital) ring}.
\end{definition}

\begin{proposition}\label{thm:def:integral_domain}
  \hyperref[def:integral_domain]{Integral domains} have the following basic properties:
  \begin{thmenum}
    \thmitem{thm:def:integral_domain/subring} Any \hyperref[def:ring/submodel]{subring} of an integral domain is also an integral domain.

    \thmitem{thm:def:integral_domain/polynomial_ring} A commutative ring \( R \) is an integral domain if and only if its \hyperref[def:polynomial_algebra]{polynomial ring} \( R[X] \) is.

    \thmitem{thm:def:integral_domain/root_limit} In an integral domain, the \hyperref[def:polynomial_root]{multiset of roots} of a univariate nonzero polynomial of \hyperref[def:polynomial_degree]{degree} \( n \) has \hyperref[def:weighted_set/multiset]{multiset cardinality} at most \( n \).

    In other words, a polynomial of degree cannot have more that \( n \) roots, counting multiple roots.
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:def:integral_domain/subring} Trivial.

  \SubProofOf{thm:def:integral_domain/polynomial_ring}

  \NecessitySubProof* If \( R[X] \) is an integral domain, by \fullref{thm:def:integral_domain/subring}, so is \( R \).

  \SufficiencySubProof* It is sufficient to prove the statement for one indeterminate. If \( p(X) \) and \( q(X) \) are nonzero polynomials, then so is \( p(X) q(X) \) by \fullref{thm:def:polynomial_degree/product}.

  \SubProofOf{thm:def:integral_domain/root_limit} We will use induction on the degree. Zero-degree polynomials clearly have zero roots. Suppose that the statement holds for polynomials of degree \( n - 1 \), and let \( p(X) \) have degree \( n \).

  If \( p(X) \) has a root \( u \), by the equivalence in \fullref{def:polynomial_root}, \( (X - u) \) divides \( p(X) \). Then \( \ifrac {p(X)} {(X - u)} \) has degree \( n - 1 \) by \fullref{thm:def:polynomial_degree/product}. Let \( M \) be the multiset of roots of \( \ifrac {p(X)} {(X - u)} \). After adding \( u \) to \( M \), we have incremented its total cardinality by \( 1 \), thus making it at most \( n \).
\end{proof}

\begin{proposition}\label{thm:quotient_by_prime_ideal}
  The ideal \( P \) of the \hyperref[def:ring/commutative]{commutative ring} \( R \) is \hyperref[def:semiring_ideal/prime]{prime} if and only if the \hyperref[def:ring/quotient]{quotient ring} \( R / P \) is an \hyperref[def:integral_domain]{integral domain}.

  See \fullref{thm:quotient_by_maximal_ideal} for the corresponding statement for \hyperref[def:semiring_ideal/maximal]{maximal ideals} in possibly noncommutative rings.
\end{proposition}
\begin{proof}
  \SufficiencySubProof Suppose that \( P \) is a prime ideal. Clearly \( R / P \) is a commutative ring. Since \( P \) is a proper ideal, \( R / P \) must be nontrivial. We will show that it is an \hyperref[def:entire_semiring]{entire ring}.

  Let \( [x] [y] = [0] = P \) (where \( [x] = x + P \) is the coset of \( x \) in \( R / P \)). By definition,
  \begin{equation*}
    [x] [y] = (x + P) (y + P) = (xy + P),
  \end{equation*}
  which implies \( xy + P = P \) and hence \( xy \in P \). Since \( P \) is prime, by \fullref{thm:def:semiring_ideal/prime_pointwise}, we have \( x \in P \) or \( y \in P \).

  Therefore, \( [x] = [0] \) or \( [y] = [0] \). Generalizing on \( x \) and \( y \), we can conclude that \( R / P \) is entire, and thus an integral domain.

  \NecessitySubProof Suppose that \( R / P \) is an integral domain. Since \( R / P \) is nontrivial, \( P \) must be a proper ideal. We will show that it satisfies \fullref{thm:def:semiring_ideal/prime_pointwise}.

  Let \( xy \in P \). We have
  \begin{equation*}
    P = [0] = [xy] = [x] [y],
  \end{equation*}
  hence \( [x] \) and \( [y] \) are zero divisors in \( R / P \). But \( R / P \) is entire, hence either \( [x] \) or \( [y] \) must be zero. That is, either \( x \in P \) or \( y \in P \).

  Generalizing on \( x \) and \( y \), we can conclude that \( P \) is a prime ideal.
\end{proof}

\begin{definition}\label{def:domain_divisibility}\mimprovised
  We will introduce several notions related to \hyperref[def:divisibility]{divisibility} in \hyperref[def:integral_domain]{integral domains}.

  \begin{thmenum}
    \thmitem{def:domain_divisibility/associates} We say that \( x \) and \( y \) are \term{associates} if any of the following conditions hold:
    \begin{thmenum}
      \thmitem{def:domain_divisibility/associates/direct} Both \( x \mid y \) and \( y \mid x \).
      \thmitem{def:domain_divisibility/associates/unit} There exists a \hyperref[def:divisibility/unit]{unit} \( u \) such that \( x = uy \).
      \thmitem{def:domain_divisibility/associates/ideals} The \hyperref[def:semiring_ideal/principal]{principal ideals} \( \braket{ x } \) and \( \braket{ y } \) are equal.
    \end{thmenum}

    \thmitem{def:domain_divisibility/irreducible} We say that the nonzero nonunit element \( x \) is \term{irreducible} if any of the following conditions hold:
    \begin{thmenum}
      \thmitem{def:domain_divisibility/irreducible/direct} Whenever \( x = yz \), then \( y \) or \( z \) is a unit.
      \thmitem{def:domain_divisibility/irreducible/ideals} \( \braket{ x } \) is maximal among all proper principal ideals. Maximality means that, if \( \braket{ x } \subseteq \braket{ y } \) for some nonzero nonunit \( y \), then \( \braket{ x } = \braket{ y } \).
    \end{thmenum}

    \thmitem{def:domain_divisibility/prime} We say that the nonzero element \( x \) is \term{prime} if any of the following equivalent conditions hold:
    \begin{thmenum}
      \thmitem{def:domain_divisibility/prime/direct} If \( x \mid yz \), then \( x \mid y \) or \( x \mid z \).
      \thmitem{def:domain_divisibility/prime/ideals} The ideal \( \braket{ x } \) is \hyperref[def:semiring_ideal/prime]{prime}.
    \end{thmenum}

    This definition is motivated by \fullref{thm:euclids_lemma}.
  \end{thmenum}
\end{definition}
\begin{defproof}
  \SubProofOf{def:domain_divisibility/associates}
  \ImplicationSubProof*{def:domain_divisibility/associates/direct}{def:domain_divisibility/associates/unit} If \( x \mid y \) and \( y \mid x \), then there exist \( a \) and \( b \) such that \( x = ay \) and \( y = bx \). Hence, \( x = abx \). Since we are working in an integral domain, we can cancel \( x \) to obtain \( ab = 1_R \). Therefore, both \( a \) and \( b \) are \hyperref[def:divisibility/unit]{units}.

  \ImplicationSubProof*{def:domain_divisibility/associates/unit}{def:domain_divisibility/associates/ideals} Suppose that \( x = uy \) for some unit \( u \). If \( z \braket{ x } \), then \( x = uy \) divides \( z \) and hence \( y \) also divides \( z \), implying that \( \braket{ x } \subseteq \braket{ y } \). We obtain the converse inclusion by noting that \( y = u^{-1} x \).

  \ImplicationSubProof*{def:domain_divisibility/associates/ideals}{def:domain_divisibility/associates/direct} If \( \braket{ x } = \braket{ y } \), then, by \fullref{thm:def:semiring_ideal/division}, \( x \mid y \) and \( y \mid x \).

  \SubProofOf{def:domain_divisibility/irreducible}
  \ImplicationSubProof*{def:domain_divisibility/irreducible/direct}{def:domain_divisibility/irreducible/ideals} Suppose that \( x \) is not a unit and that \( x = yz \) implies that \( y \) or \( z \) is a unit. Since we are working in an integral domain, \( x \) is necessarily nonzero.

  Let \( \braket{ x } \subseteq \braket{ w } \) for some nonunit \( w \). By \fullref{thm:def:semiring_ideal/division}, \( w \mid x \). Then there exists some element \( a \) such that \( x = aw \). Since \( w \) is not a unit by assumption, \( a \) must be a unit. By the equivalent definitions of associates in a domain, \( \braket{ x } = \braket{ w } \).

  \ImplicationSubProof*{def:domain_divisibility/irreducible/ideals}{def:domain_divisibility/irreducible/direct} Suppose that \( \braket{ x } \) is maximal among nonzero proper principal ideals.

  Let \( x = yz \). If, without loss of generality, \( \braket{ x } \subseteq \braket{ y } \), then \( \braket{ x } = \braket{ y } \) and, again by the equivalent conditions for associates, there exists some unit \( u \) such that \( x = uy \). Cancelling \( y \) in \( yu = yz \), we obtain \( u = z \). Hence, \( z \) is a unit.

  \SubProofOf{def:domain_divisibility/prime} Trivial.
\end{defproof}

\begin{proposition}\label{thm:def:domain_divisibility}
  The notions from \fullref{def:domain_divisibility} have the following basic properties:
  \begin{thmenum}
    \thmitem{thm:def:domain_divisibility/prime_is_irreducible} Every \hyperref[def:domain_divisibility/prime]{prime element} is \hyperref[def:domain_divisibility/irreducible]{irreducible}.

    \thmitem{thm:def:domain_divisibility/irreducible_in_polynomial_ring} An element of the domain \( D \) is \hyperref[def:domain_divisibility/irreducible]{irreducible} in \( D \) if and only if it is irreducible in \( D[X] \).

    \thmitem{thm:def:domain_divisibility/associates_and_isomorphisms} If \( \varphi: D \to E \) is an isomorphism, then \( x \) and \( y \) are \hyperref[def:domain_divisibility/associates]{associates} in \( D \) if and only if \( \varphi(x) \) and \( \varphi(y) \) are associates in \( E \).

    \thmitem{thm:def:domain_divisibility/primes_and_isomorphisms} If \( \varphi: D \to E \) is an isomorphism, then \( x \) is \hyperref[def:domain_divisibility/prime]{prime} (resp. \hyperref[def:domain_divisibility/irreducible]{irreducible}) in \( D \) if and only if \( \varphi(x) \) is prime (resp. irreducible) in \( E \).
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:def:domain_divisibility/prime_is_irreducible} Let \( x \) be a prime element and suppose that \( x = yz \). Then \( x \) divides \( y \) or \( z \). If, without loss of generality, \( x \) divides \( y \), then \( x \) and \( y \) are \hyperref[def:divisibility/associates]{associates}, and, by the equivalence of conditions in \fullref{def:domain_divisibility/associates}, \( z \) must be a unit.

  \SubProofOf{thm:def:domain_divisibility/irreducible_in_polynomial_ring}

  \SufficiencySubProof* Suppose that \( x \) is irreducible in \( D \) and let \( x = y(X) z(X) \). By \fullref{thm:def:polynomial_degree/product}, both \( y(X) \) and \( z(X) \) must be constant polynomials. Therefore, they are scalars, and since \( x \) is irreducible, \( y \) or \( z \) is a unit. By \fullref{thm:def:polynomial_algebra/units}, if \( y \) is a unit in \( D \), it is a unit in \( D[X] \).

  Generalizing on \( x \), it follows that every irreducible element in \( D \) is also irreducible in \( D[X] \).

  \NecessitySubProof* Suppose that \( x \) is irreducible in \( D[X] \) and let \( x = yz \). Then \( y \) or \( z \) is a unit of \( D[X] \), and thus again by \fullref{thm:def:polynomial_algebra/units}, it is a unit of \( D \).

  Generalizing on \( x \), it follows that every element of \( D \) that is irreducible in \( D[X] \) is also irreducible in \( D \).

  \SubProofOf{thm:def:domain_divisibility/associates_and_isomorphisms} Follows from \fullref{thm:divisibility_and_isomorphisms}.

  \SubProofOf{thm:def:domain_divisibility/primes_and_isomorphisms} Follows from \fullref{thm:divisibility_and_isomorphisms}.
\end{proof}

\begin{example}\label{ex:def:domain_divisibility}
  We list some examples of the notions from \fullref{def:domain_divisibility}:
  \begin{thmenum}
    \thmitem{ex:def:domain_divisibility/integers} \hyperref[def:prime_number]{Prime numbers} are irreducible integers by their definition. By \fullref{thm:euclids_lemma}, they are also prime.

    The inverse \( -p \) of the prime number \( p \) is also irreducible and prime in \( \BbbZ \), but convention requires \enquote{prime numbers} to be positive.

    \thmitem{ex:def:domain_divisibility/irreducible_not_prime} Consider the ring of univariate \hyperref[def:polynomial_algebra]{polynomials} over \( \BbbR \) whose constant coefficient is rational.

    The polynomial \( X \) is irreducible. Indeed, if \( X = p(X) q(X) \), by \fullref{thm:def:polynomial_degree/product}, one of \( p(X) \) or \( q(X) \) must be a constant polynomial, i.e. a unit.

    The polynomial \( X \) is not prime, however. We have \( X \mid (\sqrt 2 X)^2 \), but \( X \not\mid \sqrt 2 X \) because that would imply that \( \sqrt 2 \) is a polynomial in our ring, and it is not a rational number.
  \end{thmenum}
\end{example}

\begin{remark}\label{rem:choice_of_associates}
  If \( x \) and \( y \) are \hyperref[def:domain_divisibility/associates]{associates}, we generally have no reason to prefer \( x \) to \( y \). This leads to a non-uniqueness in certain contexts, e.g. choosing a \hyperref[def:gcd_and_lcm]{greatest common divisor} or, more generally, a generator for a principal ideal. In such cases, we often prefer working with ideals.

  Fortunately, in the majority of cases, we have good candidates for uniqueness:
  \begin{itemize}
    \item In the domain \( \BbbZ \) of integers, there are two units, \( 1 \) and \( -1 \). It is conventional to choose the positive greatest common divisor.

    \item In a polynomial ring over the integers \( \BbbZ \), by \fullref{thm:def:polynomial_algebra/units}, the units are again \( 1 \) and \( -1 \), and we can choose the leading coefficient to be positive.

    \item If \( \BbbK \) is any \hyperref[def:field]{field}, any polynomial is associated with a unique \hyperref[def:monic_polynomial]{monic polynomial}.
  \end{itemize}
\end{remark}

\begin{remark}\label{rem:lattice_of_principal_ideals}
  The lattice of ideals described in \fullref{thm:semiring_of_ideals/lattice} is sometimes too general for our needs. Rather than stating definitions and theorems \enquote{up to a multiplication by a unit}, it is often more convenient to state them in terms of principal ideals. For this reason, we sometimes restrict ourselves to a lattice consisting only of principal ideals.

  Unfortunately, as demonstrated in \fullref{ex:def:irreducible_factorization/nonunique}, there may not be a least upper bound of principal ideals, and thus set of all principal ideals may fail to be lattice. Whether the principal ideals form a lattice is intimately related to the existence of \hyperref[def:gcd_and_lcm]{greatest common divisors}. This motivates introducing the concept of greatest common divisor domains defined in \fullref{def:gcd_domain}.

  The extreme case are \hyperref[def:principal_ideal_domain]{principal ideal domains}, in which this lattice is the same as the general lattice of ideals.
\end{remark}

\begin{definition}\label{def:gcd_domain}
  We say that an \hyperref[def:integral_domain]{integral domain} is a \term{greatest common divisor domain} if the subset of all principal ideals forms a lattice. By \fullref{thm:gcd_and_lcm}, it is sufficient for only joins or meets to exist.

  This definition is discussed in \fullref{rem:lattice_of_principal_ideals} and its relation to the usual concept of a greatest common divisor is given in \fullref{def:gcd_and_lcm}.
\end{definition}

\begin{definition}\label{def:gcd_and_lcm}\mimprovised
  Fix arbitrary elements \( x \) and \( y \) of a \hyperref[def:gcd_domain]{GCD domain}.

  Their \term{greatest common divisor ideal} is the supremum of the principal ideals \( \braket{ x } \) and \( \braket{ y } \) in the \hyperref[rem:lattice_of_principal_ideals]{lattice of principal ideals}. As discussed in \fullref{rem:choice_of_associates}, we can often choose a canonical representative from this ideal, which we call \enquote{the} greatest common divisor and denote by \( \gcd(x, y) \). Even without making a choice, we may denote the ideal itself by \( \braket{ \gcd(x, y) } \).

  \hyperref[def:semilattice/duality]{Dually}, the infimum of this lattice is generated by \enquote{the} \term{least common multiple} \( \braket{ \lcm(x, y) } \).

  Due to \fullref{thm:def:gcd_domain/join}, we can extend GCDs and LCMs to finitely many elements rather than only two.
\end{definition}

\begin{remark}\label{rem:gcd_of_zeros}
  The greatest common divisor \( \gcd(0, 0) \) is often left undefined, but we see no problem with defining it as \( 0 \). We have \( \braket{ 0 } + \braket{ 0 } = \braket{ 0 } \), and hence the result is consistent with \fullref{def:gcd_and_lcm}.
\end{remark}

\begin{proposition}\label{thm:gcd_and_lcm}
  The \hyperref[def:gcd_and_lcm]{greatest common divisors} and \hyperref[def:gcd_and_lcm]{least common multiples} of \( x \) and \( y \) (have representative that) are related as follows:
  \begin{equation}\label{eq:thm:gcd_and_lcm}
    xy = \gcd(x, y) \lcm(x, y).
  \end{equation}

  Furthermore, if in some integral domain only one of them exists, the other one exists too.
\end{proposition}
\begin{proof}
  Let \( d \) be any common divisor of \( x \) and \( y \). Then \( d \) also divides the product \( xy \). Let \( m \), \( a \) and \( b \) be elements satisfying
  \begin{align*}
    xy &= d m, \\
     x &= d a, \\
     y &= d b.
  \end{align*}

  We have
  \begin{equation*}
    x (db) = d m.
  \end{equation*}

  Cancelling \( d \), we obtain that \( x \) divides \( m \). We similarly obtain that \( y \) divides \( m \).

  Now suppose that \( d \) is the least common divisor. Let \( m' \) be a common multiple. We have \( xy = m' d' \) for some \( d' \). By \hyperref[def:preordered_set/duality]{duality} to what we have just proved, \( d' \) must be a common divisor. We have \( d' \mid d \), which in turn implies \( m \mid m' \).

  Therefore, \( m \) is a least common multiple.
\end{proof}

\begin{proposition}\label{thm:def:gcd_domain}
  \hyperref[def:gcd_domain]{Greatest common divisor domains} have the following basic properties:
  \begin{thmenum}
    \thmitem{thm:def:gcd_domain/join} A a lattice join, GCD is \hyperref[def:magma/associative]{associative}, \hyperref[def:magma/commutative]{commutative} and \hyperref[def:magma/idempotent]{idempotent} when considered as a binary operation.

    \thmitem{thm:def:gcd_domain/polynomial_ring} If the \hyperref[def:polynomial_algebra]{polynomial ring} \( R[X] \) over a commutative ring \( R \) is a greatest common divisor domain, then \( R \) also is.

    The converse to this is true, but it is more difficult to prove. See \fullref{thm:polynomial_ring_over_gcd_domain}.

    \thmitem{thm:def:gcd_domain/irreducible_is_prime} Every \hyperref[def:domain_divisibility/irreducible]{irreducible element} is \hyperref[def:domain_divisibility/prime]{prime}.
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:def:gcd_domain/join} Follows from \fullref{thm:binary_lattice_operations/semilattices}.

  \SubProofOf{thm:def:gcd_domain/polynomial_ring} By \fullref{thm:def:integral_domain/subring}, \( R \) is an integral domain.

  Given elements \( x \) and \( y \) from \( D \), \( \gcd(x, y) \) is a polynomial in \( R[X] \) that divides each of the elements. Hence, by \fullref{thm:def:polynomial_degree/product}, the GCD is a constant polynomial. We may thus regard it as an element of \( R \).

  Therefore, \( R \) is also a greatest common divisor domain.

  \SubProofOf{thm:def:gcd_domain/irreducible_is_prime} Suppose that \( x \) is an irreducible element and let \( x \mid yz \). If \( x \) is a unit, it divides both \( y \) and \( z \), and hence it is prime.

  Suppose that \( x \) is not a unit. Putting \( d \coloneqq \gcd(y, z) \), we obtain
  \begin{equation*}
    x \mid x y = d \cdot \frac y d \cdot \frac z d,
  \end{equation*}
  where \( d \), \( \tfrac y d \) and \( \tfrac z d \) are coprime. Hence, \( x \) divides exactly one of the three.

  By transitivity of divisibility, \( x \) necessarily divides \( y \), \( z \) or both.
\end{proof}

\begin{proposition}\label{thm:bezouts_identity_converse}
  In a \hyperref[def:gcd_domain]{GCD domain}, if \( d \) divides both \( x \) and \( y \), and if there exist elements \( a \) and \( b \) such that \( ax + by = d \), then \( d \) is a \hyperref[def:gcd_and_lcm]{greatest common divisor} of \( x \) and \( y \).
\end{proposition}
\begin{proof}
  Suppose that \( e \) is a common divisor of \( x \) and \( y \). Then \( e \) divides both \( ax \) and \( by \), hence also \( ax + by = d \). Therefore, \( e \) divides \( d \), implying that \( d \) is the greatest common divisor of \( x \) and \( y \).
\end{proof}

\begin{definition}\label{def:coprime_elements}
  We say that the nonzero elements \( x \) and \( y \) in a \hyperref[def:gcd_domain]{GCD domain} are \term{coprime} if any the \hyperref[def:gcd_and_lcm]{greatest common divisor} of \( x \) and \( y \) is a unit.

  In this form, the definition is unfortunately inconsistent with \hyperref[def:semiring_ideal/coprime]{coprime ideals}. See \fullref{thm:def:principal_ideal_domain/coprime}.
\end{definition}

\begin{definition}\label{def:irreducible_factorization}\mimprovised
  An \term{irreducible factorization} or simply \term{factorization} of a nonzero element \( x \) in an arbitrary \hyperref[def:integral_domain]{integral domain} is a finite sequence \( p_1, \ldots, p_n \) of \hyperref[def:domain_divisibility/irreducible]{irreducible elements} such that, for some \hyperref[def:divisibility/unit]{unit} \( u \),
  \begin{equation*}
    x = u p_1 \cdots p_n.
  \end{equation*}

  By cancellation of multiplication, the unit \( u \) is uniquely determined by the irreducible factors. If \( x \) is itself a unit, then it is its own factorization (i.e. \( n = 0 \)) because \( p \mid x \) for an irreducible \( p \) would imply that \( p \) is a unit and hence not irreducible.

  We say that two factorizations
  \begin{equation*}
    x = u p_1 \cdots p_n = v q_1 \cdots q_m
  \end{equation*}
  are equivalent if \( n = m \) and if there exists a \hyperref[def:symmetric_group/permutation]{permutation} \( \pi \in S_n \) such that \( q_{\pi(k)} \) and \( p_k \) are \hyperref[def:domain_divisibility/associates]{associated} for every \( k = 1, \ldots, n \).

  Finally, if any two factorizations of \( x \) are unique, we say that \( x \) \term{factors uniquely} into a product of irreducible factors.
\end{definition}

\begin{example}\label{ex:def:irreducible_factorization}
  We list some examples of \hyperref[def:irreducible_factorization]{irreducible factorization}:
  \begin{thmenum}
    \thmitem{ex:def:irreducible_factorization/integers} By \fullref{def:irreducible_factorization}, every integer has a unique factorization.

    \thmitem{ex:def:irreducible_factorization/nonunique} Consider the ring of real polynomials with a rational constant term discussed in \fullref{ex:def:domain_divisibility/irreducible_not_prime}.

    We have
    \begin{equation*}
      2X^2 = (\sqrt 2 X) \cdot (\sqrt 2 X) = 2 \cdot X \cdot X.
    \end{equation*}

    We have shown in \fullref{ex:def:domain_divisibility/irreducible_not_prime} that \( X \) is irreducible, and we can similarly show that \( \sqrt 2 X \) is irreducible. Thus, the above are two irreducible factorizations. They are not equivalent, however, since \( X \not\mid \sqrt 2 X \).

    As a consequence, both \( X \) and \( \sqrt 2 X \) are maximal divisors of \( 2X^2 \), but they are not \hyperref[def:domain_divisibility/associates]{associates}.
  \end{thmenum}
\end{example}

\begin{proposition}\label{thm:def:irreducible_factorization}
  \hyperref[def:irreducible_factorization]{Irreducible factorizations} in integral domains have the following basic properties:
  \begin{thmenum}
    \thmitem{thm:def:irreducible_factorization/existence} If every ascending chain of \hi{principal} ideals stabilizes in the sense of \fullref{def:noetherian_semimodule/acc}, then every element has at least one irreducible factorization.

    This condition is sometimes abbreviated as ACCP (ascending chain condition on principal ideals).

    \thmitem{thm:def:irreducible_factorization/uniqueness} If every \hyperref[def:domain_divisibility/irreducible]{irreducible element} is \hyperref[def:domain_divisibility/prime]{prime}, then all factorizations of an element are equivalent (but there may be elements without a factorization).

    \thmitem{thm:def:irreducible_factorization/polynomial_ring} For any domain \( D \), \( x = u p_1 \cdots p_n \) is an irreducible factorization in \( D \) if and only if it is an irreducible factorization of \( x \) in \( D[X] \).
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:def:irreducible_factorization/existence} Suppose that every ascending chain of principal ideals stabilizes.

  Let \( x \) be an arbitrary element, and suppose that it does not have an irreducible factorization. In particular, \( x \) is not a unit and is not irreducible. Then there exist elements \( a_1 \) and \( b_1 \), which are not both irreducible and not both units, such that \( x = a_1 b_1 \). Via \hyperref[rem:natural_number_recursion]{natural number recursion}, we can build a sequence \( a_1, a_2, \ldots \) such that
  \begin{equation*}
    \cdots \mid a_2 \mid a_1 \mid x
  \end{equation*}
  and no two elements are associates. By \fullref{thm:def:semiring_ideal/division}, this implies
  \begin{equation*}
    \braket{ x } \subsetneq \braket{ a_1 } \subsetneq \braket{ a_2 } \cdots.
  \end{equation*}

  The existence of such a sequence contradicts the ACCP. Therefore, \( x \) must have at least one irreducible factorization.

  \SubProofOf{thm:def:irreducible_factorization/uniqueness} Suppose that every irreducible element is prime.

  We will prove by induction that, if \( x \) has a factorization of length \( n \), then any other factorization is equivalent to it.

  If \( n = 0 \), then \( x = u \) is a unit, and hence \( m = 0 \) and \( x = u = v \).

  Otherwise, suppose that factorizations of length \( n - 1 \) are unique and that we are given the factorizations
  \begin{equation}\label{eq:def:irreducible_factorization/uniqueness/proof/assumption}
    x = u p_1 \cdots p_n = v q_1 \cdots q_m.
  \end{equation}

  Since \( p_1 \) is prime, \( p_1 \) divides \( q_{k_0} \) for some \( k_0 = 1, \ldots, m \). Thus, \( p_1 = w q_k \) for some unit \( w \). We can thus cancel \( p_1 \) to obtain
  \begin{equation*}
    u p_2 \cdots p_n = (vw) q_1 \cdots q_{k_0-1} q_{k_0+1} \cdots q_m.
  \end{equation*}

  By the inductive hypothesis, this factorization is unique. Hence, \( n = m \), and there exists a permutation \( \pi \in S_n \) such that \( p_k = q_{\pi(k)} \) for \( k = 2, \ldots, n \). Then
  \begin{equation*}
    \widehat{\pi}(k) \coloneqq \begin{cases}
      k_0,    & k = 1 \\
      \pi(i), & k > 1
    \end{cases}
  \end{equation*}
  is a permutation witnessing the equivalence of the factorizations \eqref{eq:def:irreducible_factorization/uniqueness/proof/assumption}.

  \SubProofOf{thm:def:irreducible_factorization/polynomial_ring}

  \SufficiencySubProof* Suppose that
  \begin{equation}\label{eq:thm:def:irreducible_factorization/polynomial_ring/sufficiency_assumption}
    x = u(X) p_1(X) \cdots p_n(X)
  \end{equation}
  be an irreducible factorization of \( x \in D \) in \( D[X] \). By \fullref{thm:def:polynomial_degree/product}, all polynomials in this factorization are constants. By \fullref{thm:def:domain_divisibility/irreducible_in_polynomial_ring}, since they are irreducible in \( D[X] \), they are also irreducible in \( D \).

  Therefore, \eqref{eq:thm:def:irreducible_factorization/polynomial_ring/sufficiency_assumption} is an irreducible factorization of \( x \) in \( D \).

  \NecessitySubProof* Due to \fullref{thm:def:polynomial_algebra/units} and \fullref{thm:def:domain_divisibility/irreducible_in_polynomial_ring}, irreducible elements and units in \( D \) are also irreducibles and units in \( D[X] \). Hence, every irreducible factorization in \( D \) is also an irreducible factorization in \( D[X] \).
\end{proof}

\begin{definition}\label{def:unique_factorization_domain}
  We say that an \hyperref[def:integral_domain]{integral domain} is a \term{unique factorization domain} if any of the following equivalent conditions hold:
  \begin{thmenum}
    \thmitem{def:unique_factorization_domain/factorization} Every element \hyperref[def:irreducible_factorization]{factors uniquely} into a product of irreducible elements.

    \thmitem{def:unique_factorization_domain/primes_and_ideals} Every ascending chain of principal ideals stabilizes and every irreducible element is prime.
  \end{thmenum}
\end{definition}
\begin{defproof}
  \ImplicationSubProof{def:unique_factorization_domain/factorization}{def:unique_factorization_domain/primes_and_ideals}

  \SubProof*{Proof that irreducibles are prime} holds, hence we only need to prove that an irreducible element is prime.

  Let \( x \) be an irreducible element. Suppose that \( x \mid yz \), so that there exists some element \( a \) satisfying \( ax = yz \).
  Let \( y = u p_1 \cdots p_n \) and \( z = v q_1 \cdots q_m \) be irreducible factorizations. Then
  \begin{equation*}
    ax = (uv) p_1 \cdots p_n q_1 \cdots q_m.
  \end{equation*}

  The factorizations are equivalent, hence \( x \) must divide one of the other irreducible elements. If \( x \mid p_k \) for some \( k = 1, \ldots, n \), then \( x \mid y \). If \( x \mid q_k \) for some \( k = 1, \ldots, m \), then \( x \mid z \).

  \SubProof*{Proof of ascending chain condition} Suppose that every element has a unique factorization.

  Fix an ascending sequence of principal ideals
  \begin{equation*}
    \braket{ x_1 } \subseteq \braket{ x_2 } \subseteq \braket{ x_3 } \cdots.
  \end{equation*}

  By \fullref{thm:def:semiring_ideal/division}, this implies that \( x_{k+1} \) divides \( x_k \) for \( k = 1, 2, \ldots \).

  Let \( x_1 = y x_2 \) and fix \hyperref[def:irreducible_factorization]{irreducible factorizations}
  \begin{align*}
    x_1 &= u p_1 \cdots p_n \\
    x_2 &= v q_1 \cdots q_m \\
    y   &= w r_1 \cdots r_k.
  \end{align*}

  Since the factorizations are unique, we have \( n = m + k \). If \( k = 0 \), then \( x_1 \) and \( x_2 \) are associated and \( \braket{ x_1 } = \braket{ x_2 } \). If \( k > 0 \), then \( \braket{ x_1 } \subsetneq \braket{ x_2 } \), and \( x_2 \) has a strictly shorter irreducible factorization.

  Proceeding by induction on the length of the factorization, we conclude that there are at most \( n \) strict inclusions in the sequence of ideals.

  \ImplicationSubProof{def:unique_factorization_domain/primes_and_ideals}{def:unique_factorization_domain/factorization} Follows from \fullref{thm:def:irreducible_factorization/existence} and \fullref{thm:def:irreducible_factorization/uniqueness}.
\end{defproof}

\begin{proposition}\label{thm:def:unique_factorization_domain}
  \hyperref[def:unique_factorization_domain]{Unique factorization domains} have the following basic properties:
  \begin{thmenum}
    \thmitem{thm:def:unique_factorization_domain/polynomial_ring} If the \hyperref[def:polynomial_algebra]{polynomial ring} \( R[X] \) over a commutative ring \( R \) is a unique factorization domain, then \( R \) also is.

    The converse to this is true, but it is more difficult to prove. See \fullref{thm:polynomial_ring_over_ufd}.

    \thmitem{thm:def:unique_factorization_domain/gcd} Every unique factorization domain is a \hyperref[def:gcd_domain]{GCD domain}.
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:def:unique_factorization_domain/polynomial_ring} Suppose that \( D[X] \) is a unique factorization. By \fullref{thm:def:irreducible_factorization/polynomial_ring}, every irreducible factorization of \( x \in D \) in \( D[X] \) is also an irreducible factorization in \( D[X] \). This implies both existence and uniqueness.

  \SubProofOf{thm:def:unique_factorization_domain/gcd} Fix arbitrary elements \( x \) and \( y \). We will show that they have a greatest common divisor.

  Suppose that we have the decompositions
  \begin{align*}
    x &= u p_1 \cdots p_n \\
    y &= v q_1 \cdots q_m.
  \end{align*}

  Otherwise, let \( r_1, \ldots, r_k \) be a well-ordering on the set \( \set{ p_1, \ldots, p_n, q_1, \ldots, q_m } \). For every \( i = 1, \ldots, k \), let \( s_i \) be the minimum of the number of elements from \( p_1, \ldots, p_n \) associated with \( r_i \) and the number of elements of \( q_1, \ldots, q_m \) associated with \( r_i \). Finally, define
  \begin{equation*}
    r \coloneqq r_1^{s_1} \cdots r_k^{s_k}.
  \end{equation*}

  We can use nested induction on \( k \) and \( s_k \) to show that \( r \) is, up to a unit, the greatest common divisor of \( x \) and \( y \). Indeed, \( k = 0 \) implies that \( x \) and \( y \) have no common divisors. For the inductive step, note that dividing both \( x \) and \( y \) by \( r_k^{s_k} \) allows us to use the inductive hypothesis with \( r_1^{s_1} \cdots r_{k-1}^{s_{k-1}} \), and that multiplying back by \( r_k^{s_k} \) makes \( r \) the GCD of \( x \) and \( y \).
\end{proof}

\begin{definition}\label{def:principal_ideal_domain}
  We say that an \hyperref[def:integral_domain]{integral domain} is a \term{principal ideal domain} if every \hyperref[def:semiring_ideal]{ideal} is \hyperref[def:semiring_ideal/principal]{principal}.
\end{definition}

\begin{proposition}\label{thm:def:principal_ideal_domain}
  \hyperref[def:principal_ideal_domain]{Principal ideal domains} have the following basic properties:
  \begin{thmenum}
    \thmitem{thm:def:principal_ideal_domain/noetherian} Every principal ideal domain is \hyperref[def:noetherian_semiring]{noetherian}.

    \thmitem{thm:def:principal_ideal_domain/ufd} Every principal ideal domain is a \hyperref[def:unique_factorization_domain]{unique factorization domain}.

    \Fullref{ex:ufd_but_not_pid} demonstrates that the converse is not true.

    \thmitem{thm:def:principal_ideal_domain/prime_ideal_is_maximal} \hyperref[def:semiring_ideal/prime]{Prime ideals} in a principal ideal domains are \hyperref[def:semiring_ideal/maximal]{maximal}.

    \thmitem{thm:def:principal_ideal_domain/gcd} For the \hyperref[def:gcd_and_lcm]{greatest common divisor}, we have
    \begin{equation*}
      \braket{ \gcd(x, y) } = \braket{ x } + \braket{ y } = \braket{ x, y }
    \end{equation*}
    and for the \hyperref[def:gcd_and_lcm]{least common multiple},
    \begin{equation*}
      \braket{ \lcm(x, y) } = \braket{ x } \cap \braket{ y }.
    \end{equation*}

    \thmitem{thm:def:principal_ideal_domain/coprime} \( x \) and \( y \) are \hyperref[def:coprime_elements]{coprime elements} if and only if \( \braket{ x } \) and \( \braket{ y } \) are \hyperref[def:coprime ideal]{coprime ideals}.
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:def:principal_ideal_domain/noetherian} In a principal ideal domain every ideal is generated by a single element, hence the domain satisfies \fullref{def:noetherian_semiring/generated}. Thus, it is noetherian.

  \SubProofOf{thm:def:principal_ideal_domain/ufd} Suppose that \( x \) is an irreducible element of a principal ideal domain. Then \( x \) satisfies \fullref{def:domain_divisibility/irreducible/ideals}, and hence \( \braket{ x } \) is a maximal ideal. By \fullref{thm:def:semiring_ideal/maximal_is_prime}, maximal ideals are prime, and hence \( x \) is a prime element.

  Therefore, every irreducible element is prime. Combined with \fullref{thm:def:principal_ideal_domain/noetherian}, this implies that the domain satisfies \fullref{def:unique_factorization_domain/primes_and_ideals}, and is hence a unique factorization domain.

  \SubProofOf{thm:def:principal_ideal_domain/prime_ideal_is_maximal} Let \( P \) be a prime ideal in a principal ideal domain. Then \( P = \braket{ p } \) for some prime element \( p \). By \fullref{thm:def:domain_divisibility/prime_is_irreducible}, \( p \) is irreducible, and hence \( \braket{ p } \) is a maximal ideal.

  \SubProofOf{thm:def:principal_ideal_domain/gcd} Follows from \fullref{thm:semiring_of_ideals/lattice} by nothing that every ideal is principal.

  \SubProofOf{thm:def:principal_ideal_domain/coprime} Follows from \fullref{thm:def:principal_ideal_domain/gcd}.
\end{proof}

\begin{theorem}[Bezout's identity]\label{thm:bezouts_identity}
  In a \hyperref[def:principal_ideal_domain]{principal ideal domain}, for every pair of elements \( x \) and \( y \), there exist some elements \( a \) and \( b \) such that
  \begin{equation}\label{eq:thm:bezouts_identity}
    ax + by = \gcd(x, y).
  \end{equation}

  \Fullref{alg:extended_euclidean_algorithm} gives us an explicit construction for \( a \) and \( b \).

  The converse, \fullref{thm:bezouts_identity_converse}, holds more generally.
\end{theorem}
\begin{proof}
  Clearly \( \ifrac x {\gcd(x, y)} \) and \( \ifrac y {\gcd(x, y)} \) are \hyperref[def:coprime_elements]{coprime elements}. By \fullref{thm:def:principal_ideal_domain/coprime}, there exist elements \( a \) and \( b \) such that
  \begin{equation*}
    a \frac x {\gcd(x, y)} + b \frac y {\gcd(x, y)} = 1.
  \end{equation*}

  Multiplying by \( \gcd(x, y) \), we obtain \eqref{eq:thm:bezouts_identity}.
\end{proof}

\begin{corollary}\label{thm:multiplicative_group_of_integers_modulo}
  The \hyperref[def:semiring]{multiplicative group} \( \BbbZ_n^\times \) of the ring \hyperref[thm:ring_of_integers_modulo]{\( \BbbZ_n \)} of integers modulo \( n > 1 \) is the set of all positive integers \hyperref[def:coprime_elements]{coprime} to \( n \).

  In particular, \( \BbbZ_p \) is a \hyperref[def:field]{field} if and only if \( p \) is a \hyperref[def:prime_number]{prime number}.
\end{corollary}
\begin{proof}
  Note that \( x < n \) is invertible modulo \( n \) if and only if there exists an integer \( a \) such that \( ax = 1 \pmod n \). That is, if there exist integers \( a \) and \( b \) such that \( ax + bn = 1 \).

  The rest of the theorem follows from \fullref{thm:bezouts_identity} in one direction and \fullref{thm:bezouts_identity_converse} in the other direction.
\end{proof}

\begin{example}\label{ex:ufd_but_not_pid}\mcite{ProofWiki:polynomials_in_integers_is_not_principal_ideal_domain}
  The \hyperref[def:unique_factorization_domain]{unique factorization domain} \( \BbbZ[X] \) is not a \hyperref[def:principal_ideal_domain]{principal ideal domain}.

  Note that \( \BbbZ[X] \) is a unique factorization domain by \fullref{thm:def:unique_factorization_domain/polynomial_ring}.

  Consider the ideal \( I \) of polynomials with an even constant term. Assume that \( I \) is generated by the polynomial \( p(X) \in \BbbZ[X] \). Since \( 2 \in I \), then \( p(X) \) divides \( 2 \), so \( p(X) \in \set{ -2, -1, 1, 2 } \). But then \( p(X) \) is a unit, and hence, \( I = \braket{p(X)} = \BbbZ[X] \), which contradicts the definition of \( I \).

  The obtained contradiction proves that \( \BbbZ[X] \) is not a principal ideal domain.
\end{example}

\begin{definition}\label{def:euclidean_domain}\mcite{nLab:euclidean_domain}
  An \term{Euclidean domain} is an \hyperref[def:integral_domain]{integral domain} \( D \) endowed with a function \( \delta: D \to \BbbZ_{\geq 0} \), which we call the \term{Euclidean degree}, such that for every pair \( x \) and \( y \) of elements of \( D \) with \( y \neq 0_D \), there exists a pair \( q \) and \( r \) such that
  \begin{equation}\label{eq:def:euclidean_domain/division}
    x = yq + r
  \end{equation}
  holds and either \( r = 0_D \) or \( \delta(r) < \delta(q) \).

  We say that \( y \) \term{divides} \( x \) with \term{quotient} \( q \) and \term{remainder} \( r \).

  If the quotient and remainder are unique, as they usually are, we use the special notation
  \begin{align*}
    \quot(x, y) \coloneqq q, \\
    \rem(x, y) \coloneqq r = x - y \quot(x, y).
  \end{align*}
\end{definition}

\begin{proposition}\label{thm:def:euclidean_domain}
  \hyperref[def:euclidean_domain]{Euclidean domains} have the following basic properties:
  \begin{thmenum}
    \thmitem{thm:def:euclidean_domain/pid} Every Euclidean domain is a \hyperref[def:principal_ideal_domain]{principal ideal domain}.

    Counterexamples to the converse are discussed in \cite{Anderson1988}.

    \thmitem{thm:def:euclidean_domain/field} Every field is a \hyperref[def:euclidean_domain]{Euclidean domain}.

    \thmitem{thm:def:euclidean_domain/field_polynomials} A commutative ring \( R \) is a field if and only if its \hyperref[def:polynomial_algebra]{polynomial ring} \( R[X] \) is a \hyperref[def:principal_ideal_domain]{principal ideal domain}.

    Furthermore, if \( R \) is a field, the \hyperref[def:polynomial_degree]{polynomial degree} function \( \deg: R[X] \setminus \set{ 0 } \to \BbbZ_{\geq 0} \) makes \( R \) an \hyperref[def:euclidean_domain]{Euclidean domain}.

    We are free to define \( \deg \) to take any value for the zero polynomial.
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:def:euclidean_domain/pid} Fix an ideal \( I \) of the Euclidean domain \( D \). By \fullref{thm:natural_numbers_are_well_ordered}, the set \( \delta(I) \) has a minimum. Choose an element \( m \in I \) such that \( \delta(m) = \min \delta(I) \). Obviously \( \braket{m} \subseteq I \). We will prove that \( I \subseteq m \).

  Let \( x \in I \). We divide it by \( m \) to obtain
  \begin{equation*}
    x = mq + r,
  \end{equation*}
  such that either \( r \) is zero or \( \delta(r) < \delta(m) \). Since both \( x \) and \( m \) are in \( I \), we have \( r = mq - x \in I \). But \( m \) minimizes \( \delta \) over \( I \), thus \( \delta(m) \leq \delta(r) \), which contradicts \( \delta(r) < \delta(m) \).

  Therefore, \( r \) is zero and
  \begin{equation*}
    x = mq,
  \end{equation*}
  which implies that \( x \in \braket m \). This proves \( I \subseteq \braket m \).

  We have now obtained \( \braket m = I \). Since \( I \) was an arbitrary ideal, we conclude that every ideal in the domain is principal.

  \SubProofOf{thm:def:euclidean_domain/field} By \fullref{thm:division_ring_is_entire}, a field is necessarily an integral domain. Since every element of \( \BbbK \) is divisible (without remainder), the Euclidean function can be arbitrary; for definiteness, we take it to be canonically zero.

  \SubProofOf{thm:def:euclidean_domain/field_polynomials}
  \SufficiencySubProof* Suppose that \( R[X] \) is a principal ideal domain.

  By \fullref{thm:def:integral_domain/subring}, \( R \) is an integral domain. By \fullref{thm:quotient_algebra_universal_property}, \( R[X] / \braket{ X } \cong R \). By \fullref{thm:quotient_by_prime_ideal}, \( \braket{ X } \) is a prime ideal in \( R[X] \). By \fullref{thm:def:principal_ideal_domain/prime_ideal_is_maximal}, \( \braket{ X } \) is a maximal ideal. By \fullref{thm:quotient_by_maximal_ideal}, \( R[X] / \braket{ X } \cong R \) is a field.

  \NecessitySubProof* Suppose that \( R \) is a field. By \fullref{thm:def:integral_domain/polynomial_ring}, \( R[X] \) is a domain. By \fullref{alg:euclidean_division_of_polynomials}, we can divide two polynomials \( f(X) \) and \( g(X) \), where \( g(X) \) is monic, in a way that satisfies the definition of an Euclidean domain.

  Thus, for any \( f(X) \) and a nonzero \( g(X) \) with leading coefficients \( b_m \), \fullref{alg:euclidean_division_of_polynomials} gives us
  \begin{equation*}
    f(X) = \frac {g(X)} {b_m} q(X) + r(X).
  \end{equation*}
\end{proof}

\begin{algorithm}[Euclidean algorithm]\label{alg:euclidean_algorithm}
  In an \hyperref[def:euclidean_domain]{Euclidean domain}, we can explicitly construct the \hyperref[def:gcd_and_lcm]{greatest common divisor} of arbitrary elements \( x \) and \( y \) as follows:
  \begin{thmenum}
    \thmitem{alg:euclidean_algorithm/guard} If \( y \) is zero, halt the algorithm with \( \gcd(x, y) \coloneqq x \).
    \thmitem{alg:euclidean_algorithm/initialization} Define \( r_{-1} \coloneqq x \) and \( r_0 \coloneqq y \).
    \thmitem{alg:euclidean_algorithm/step} Starting with \( k = 1 \), obtain a quotient \( q_k \) and remainder \( r_k \) so that
    \begin{equation*}
      r_{k-2} = r_{k-1} q_k + r_k.
    \end{equation*}

    If \( r_k \) is not zero, repeat \fullref{alg:euclidean_algorithm/step} with \( k + 1 \).

    Otherwise, halt the algorithm with \( \gcd(x, y) \coloneqq r_{k-1} \).
  \end{thmenum}
\end{algorithm}
\begin{defproof}
  If \( y \) is zero, then \( \braket{ x, y } = \braket{ x } \), and hence the result is consistent with \fullref{def:gcd_and_lcm}.

  Otherwise, Euclidean division ensures that \( \delta(r_k) < \delta(r_{k-1}) \) on the \( k \)-th step. Thus, the algorithm halts. Denote by \( n \) the last step where \( r_n \) is not zero.

  We show by induction on \( k < n \) that \( r_n \) divides \( r_{n-k} \). The base case \( k = 0 \) is obvious since \( r_n \) divides itself.

  Assume that \( r_n \) divides \( r_{n-i} \) for \( 0 \leq i < k \). Now, since
  \begin{equation*}
    r_{n-k} = r_{n-(k-1)} q_{n-(k-2)} + r_{n-(k-2)}
  \end{equation*}
  and both of the terms on the right-hand side are multiples of \( r_n \), the left-hand side \( r_{n-k} \) is also a multiple.

  So, we conclude that \( r_n \) divides both \( r_{n-(n-1)} = r_1 = y \) and \( r_{n-n} = r_0 = x \).

  Finally, we must show that \( r_n \) is the greatest among all common divisors of \( x \) and \( y \). Let \( d \) be a common divisor. Then \( d \mid r_0 \) and \( d \mid r_1 \). Suppose that \( d \mid r_{k-1} \) and \( d \mid r_{k-2} \), for some \( k < n \). Then
  \begin{equation*}
    r_{k-2} = r_{k-1} q_k + r_k,
  \end{equation*}
  implying that \( d \) also divides \( r_k \). Hence, we obtain \( d \mid r_n \). Since our choice of common divisor \( d \) was arbitrary, we conclude that \( r_n \) is the greatest common divisor.
\end{defproof}

\begin{algorithm}[Extended Euclidean algorithm]\label{alg:extended_euclidean_algorithm}
  In an \hyperref[euclidean_domain]{Euclidean domain}, for \( x \) and \( y \) we can explicitly construct elements \( a \) and \( b \) so that \fullref{thm:bezouts_identity} holds, i.e.
  \begin{equation*}
    ax + by = \gcd(x, y).
  \end{equation*}

  \begin{thmenum}
    \thmitem{alg:extended_euclidean_algorithm/guard} If \( y \) is zero, then halt the algorithm with \( a = 1 \), \( b = 0 \).

    \thmitem{alg:extended_euclidean_algorithm/algorithm} Let \( r_{-1}, r_0, r_1, \ldots, r_n \) and \( q_1, q_2, \ldots, q_n \) be the sequences of quotients and remainders from \fullref{alg:euclidean_algorithm}. The \term{extended Euclidean algorithm} proceeds as follows:

    Define
    \begin{equation*}
      a_k \coloneqq \begin{cases}
        1,                     &k = 1, \\
        a_{k-2} - a_{k-1} q_k, &k > 1,
      \end{cases}
    \end{equation*}
    and
    \begin{equation*}
      b_k \coloneqq \begin{cases}
        -q_1,                  &k = 1, \\
        b_{k-2} - b_{k-1} q_k, &k > 1.
      \end{cases}
    \end{equation*}

    Halt the algorithm with \( a \coloneqq a_n \) and \( b \coloneqq b_n \).
  \end{thmenum}
\end{algorithm}
\begin{defproof}
  Suppose that \( y \) is not zero. We will prove with induction on \( k < n \) that
  \begin{equation*}
    r_k = a x_k + b y_k.
  \end{equation*}

  For the base case \( k = 1 \), we have
  \begin{align*}
    r_{-1}    &= r_0 q_1 + r_1, \\
    x         &= y q_1 + r_1,   \\
    x - y q_1 &= r_1,
  \end{align*}
  hence \( r_1 = x + (-q_1) y = a_1 x + b_1 y \).

  For \( k > 1 \), we have
  \begin{align*}
    r_{k-2}                                               &= r_{k-1} q_k + r_k,                 \\
    x a_{k-2} + y b_{k-2}                                 &= (x a_{k-1} + y b_{k-1}) q_k + r_k, \\
    x (a_{k-2} - a_{k-1} q_k) + y (b_{k-2} - b_{k-1} q_k) &= r_k.
  \end{align*}

  This completes the induction.

  Finally, since \( r_n = \gcd(x, y) \), we conclude that
  \begin{equation*}
    \gcd(x, y) = a_n x + b_n y.
  \end{equation*}
\end{defproof}

\begin{proposition}\label{thm:polynomial_ring_over_gcd_domain}
  If the \hyperref[def:integral_domain]{integral domain} \( D \) is a \hyperref[def:greatest_factorization_domain]{greatest common divisor domain}, so is \( D[X] \).
\end{proposition}
\begin{proof}
  Let \( D \) be a GCD domain and let \( \BbbK \) be its \hyperref[thm:field_of_fractions]{field of fractions}.

  Let \( p(X) \) and \( q(X) \) be arbitrary polynomials in \( D[X] \). We will show that they have a greatest common divisor.

  By \fullref{thm:def:euclidean_domain/field_polynomials}, \( \BbbK[X] \) is an Euclidean domain, and thus \( p(X) \) and \( q(X) \) have a GCD in \( \BbbK[X] \), which is unique up to multiplication by a unit in \( \BbbK[X] \). Thus, taking an arbitrary GCD
  \begin{equation*}
    r(X) = \sum_{k=0}^n \frac {a_k} {b_k} X^k,
  \end{equation*}
  the polynomial \( b_0 \cdots b_n r(X) \) is also a GCD. Furthermore, the latter is actually a polynomial in \( D[X] \).

  Therefore, \( p(X) \) and \( q(X) \) have a GCD in \( D[X] \).
\end{proof}

\begin{definition}\label{def:polynomial_content}\mcite[def. V.4.6]{Aluffi2009}
  The \term{content} of a univariate polynomial over a \hyperref[def:gcd_domain]{GCD domain} is the \hyperref[def:gcd_and_lcm]{GCD} of its coefficients.

  Dividing \( p(X) \) by its content, we obtain another polynomial, which we call the \term{primitive part} of \( p(X) \). Polynomials whose content is a unit are called \term{primitive} in the context of \fullref{thm:gauss_lemma}, although this clashes with the unrelated concept of primitive polynomials in \hyperref[thm:finite_fields]{finite fields}.
\end{definition}

\begin{lemma}[Gauss' lemma]\label{thm:gauss_lemma}
  If \( p(X) \) and \( q(X) \) are \hyperref[def:polynomial_content]{primitive polynomials}, then \( p(X) q(X) \) is also primitive.
\end{lemma}
\begin{proof}
  Fix two primitive polynomials
  \begin{align*}
    p(X) = \sum_{k=0}^n a_k X^k,
    &&
    q(X) = \sum_{k=0}^m b_k X^k.
  \end{align*}

  Let \( d \) be the content of \( p(X) q(X) \). It divides every coefficient
  \begin{equation*}
    \sum_{i+j=k} a_i b_j
  \end{equation*}
  of \( p(X) q(X) \), and hence also \( a_i b_j \) for every particular pair of indices \( i < n \) and \( j < m \).

  For any fixed \( i < n \), \( d \) divides \( a_i b_j \) for every \( j < m \). Since \( q(X) \) is primitive, \( d \) cannot divide \( b_j \) for every \( j < m \) unless \( d \) is a unit. Hence, \( d \) necessarily divides \( a_i \). Our choice of \( i \) was arbitrary, hence \( d \) divides \( a_i \) for every \( i < n \). But \( p(X) \) is also primitive. Therefore, \( d \) can only be a unit.
\end{proof}

\begin{lemma}\label{thm:irreducible_primitive_polynomial_in_field_of_fractions}
  Let \( D \) be a \hyperref[def:gcd_domain]{GCD domain} and let \( \BbbK \) be its \hyperref[thm:field_of_fractions]{field of fractions}. If a \hyperref[def:polynomial_content]{primitive polynomial} \( p(X) \) is \hyperref[def:domain_divisibility/irreducible]{irreducible} in the polynomial ring \( \BbbK[X] \), then it is irreducible in \( D[X] \).
\end{lemma}
\begin{proof}
  Suppose that \( p(X) \) is primitive and irreducible in \( \BbbK[X] \). Let
  \begin{equation*}
    p(X) = q(X) r(X),
  \end{equation*}
  where \( q(X) \) and \( r(X) \) are polynomials from \( D[X] \). Then \( q(X) \) or \( r(X) \) is a unit in \( \BbbK[X] \), hence it is a nonzero constant polynomial. Suppose that \( q(X) = q_0 \).

  Then \( q_0 \) is an element of \( D \) that divides all coefficients of \( p(X) \). By assumption, the coefficients of \( p(X) \) are coprime. It follows that \( q_0 \) is a unit in \( D \).
\end{proof}

\begin{proposition}\label{thm:polynomial_ring_over_ufd}
  If the \hyperref[def:integral_domain]{integral domain} \( D \) is a \hyperref[def:unique_factorization_domain]{unique factorization domain}, so is \( D[X] \).
\end{proposition}
\begin{proof}
  Let \( D \) be a unique factorization domain and let \( \BbbK \) be its \hyperref[thm:field_of_fractions]{field of fractions}.

  By \fullref{thm:polynomial_ring_over_gcd_domain}, \( D[X] \) is a GCD domain. Then \fullref{thm:def:gcd_domain/irreducible_is_prime} is satisfied, with by \fullref{thm:def:irreducible_factorization/uniqueness} implies that if an element has at least one \hyperref[def:irreducible_factorizaion]{irreducible factorization}, all others are equivalent to it.

  We will now show existence of irreducible factorizations.

  Let \( p(X) \) be a polynomial in \( D[X] \). By \fullref{thm:def:euclidean_domain/field_polynomials}, \( p(X) \) has an irreducible factorization
  \begin{equation*}
    p(X) = u q_1(X) \cdots q_n(X),
  \end{equation*}
  where \( q_1(X), \ldots, q_n(X) \) belong to \( \BbbK[X] \).

  For a fixed index \( i = 1, \ldots, n \), if the polynomial \( q_i(X) \) has the form
  \begin{equation*}
    q_i(X) = \sum_{k=0}^n \frac {a_k} {b_k} X^k,
  \end{equation*}
  then \( b_1 \cdots b_n q_i(X) \) is a polynomial in \( D[X] \). Denote by \( r_i(X) \) the \hyperref[def:polynomial_content]{primitive part} this polynomial in \( D[X] \). Since every scalar from \( D \) is simply a unit in \( \BbbK[X] \), it follows that \( r_i(X) \) is also irreducible in \( \BbbK[X] \).

  Hence, for the appropriate scalar \( v \in \BbbK \),
  \begin{equation*}
    p(X) = v r_1(X) \cdots r_n(X)
  \end{equation*}
  is an irreducible factorization in \( \BbbK[X] \).

  By \fullref{thm:irreducible_primitive_polynomial_in_field_of_fractions}, \( r_1(X), \ldots, r_n(X) \) are irreducible elements of \( D[X] \). Thus, if \( v = \tfrac c d \) with \( c \) and \( d \) coprime, then
  \begin{equation*}
    d p(X) = c r_1(X) \cdots r_n(X)
  \end{equation*}
  is an irreducible factorization in \( D[X] \).

  By \fullref{thm:gauss_lemma}, the product \( r_1(X) \cdots r_n(X) \) is a primitive polynomial in \( D[X] \). Hence, \( d \) cannot divide neither \( c \) nor \( r_1(X) \cdots r_n(X) \), implying that \( d \) is a unit. Therefore,
  \begin{equation*}
    p(X) = c r_1(X) \cdots r_n(X)
  \end{equation*}
  is an irreducible factorization of \( p(X) \) in \( D[X] \).
\end{proof}


\section{Dynamic Programming}

The term Dynamic Programming (DP) refers to a collection of algorithms that can be used to compute optimal policies given perfect model of the environment as a Markov Decision Process (MDP). DP methods tend to be computationally expensive and we often don't have a perfect model of the environment, so they aren't used in practice. However, they provide useful theoretical basis for the rest of reinforcement learning. \\

Unless stated otherwise, will assume that the environment is a finite MDP. If the state or action space is continuous, then we will generally discretise it and apply finite MDP methods to the approximated problem.\\

The key idea of DP, and of reinforcement learning generally, is the use of value functions to organize and structure the search for good policies. We use DP and the Bellman equations to find optimal value functions.

\subsection{Policy Evaluation (Prediction)}
We can use the Bellman equation for the state-value function $v_\pi$ to construct an iterative updating procedure.

\subsubsection*{Iterative Policy Evaluation}
Consider a sequence of approximate value functions $v_0, v_1, v_2, \dots$ each mapping $\mathcal{S}^{+}$ to $\mathbb{R}$. The initial approximation, $v_0$, is chosen arbitrarily (except that the terminal state, if any, must be given value $0$), and each successive approximation is obtained by using the Bellman equation for $v_\pi$ as an update rule:

\begin{align}
    v_{k+1} &\doteq \Epi [R_{t+1} + \gamma v_{k}(S_{t+1}) | S_t = s] \\
            &= \sum_a \pi(s|a) \sum_{s', r} p(s', r| s, a) \left[r + \gamma v_k(s')\right]
\end{align}

Clearly, $v_k = v_\pi$ is a fixed point. The sequence $\{v_k\}$ can be shown in general to converge to $v_\pi$ as $k \to \infty$ under the same conditions that guarantee the existence of $v_\pi$. This algorithm is called \emph{iterative policy evaluation}. This update rule is an instance of an \emph{expected update} because it performs the updates by taking an expectation over all possible next states rather than by taking a sample next state.\\

\subsection{Policy Improvement}
\subsubsection*{Policy Improvement Theorem}

Let $\pi$, $\pi'$ be any pair of deterministic policies, such that
\begin{equation}
    q_\pi(s, \pi'(s)) \geq v_\pi(s) \quad \forall s \in \mathcal{S}.
\end{equation}
That is, $\pi'$ is as least as good as $\pi$. Then we have (shown below)
\begin{equation}
    v_{\pi'}(s) \geq v_\pi(s) \quad \forall s \in \mathcal{S}
\end{equation}
so $\pi'$ gives at least as good (expected) return as $\pi$.\\

The argument below also shows that if $q_\pi(s, \pi'(s)) > v_\pi(s)$ at any $s$, then there is at least one $s$ for which $v_{\pi'}(s) > v_\pi(s)$.
\subsubsection*{proof:}
\begin{align*}
    v_\pi(s) & \leq q_\pi(s, \pi'(s)) \\
             & = \E{}[R_{t+1} + \gamma v_\pi(S_{t+1}) | S_t=s, A_t=\pi'(s)] \\
             & = \E{}_{\pi'} [R_{t+1} + \gamma v_\pi(S_{t+1}) | S_t=s] \\
             & \leq \E{}_{\pi'} [R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \dots| S_t=s] \\
             & = v_{\pi'}(s)
\end{align*}

\subsubsection*{Policy Improvement Algorithm}
Now consider a policy that is greedy with respect to $q_\pi(s, a)$. Define
\begin{align}
    \pi'(s) &= \argmax_a q_\pi(s, a) \\
            &= \argmax_a \E{} [R_{t+1} + \gamma v_\pi(S_{t+1}) | S_t=s, A_t=a] \\
            &= \argmax_a \sum_{s', r} p(s', r|s, a)[ r + \gamma v_\pi(s')].
\end{align}
Now we can use $v_\pi$ to get $\pi' \geq \pi$, then use $v_{\pi'}$ to get \emph{another} policy. (In the above, ties are broken arbitrarily when the policy is deterministic. If the policy is stochastic, we accept any policy that assigns zero probability to sub-optimal actions.)\\

Note that by construction
\[
    q_\pi(s, \pi'(s)) \geq v_\pi(s)
\]
therefore
\[
    v_{\pi'} \geq v_\pi
\]
so we get from this process a monotonically increasing sequence of policies.\\

Note also that if $\pi'$ is as good as $\pi$ then $v_{\pi'} = v_\pi$ and $\forall s \in \mathcal{S}$
\begin{align*}
    v_\pi &= \max_a \E{}[R_{t+1} + \gamma v_{\pi'(S_{t+1})}| S_t=s, A_t=a]\\
          &= \max_a \sum_{s', r} p(s', r|s, a)(r + \gamma v_{\pi'}(s'))
\end{align*}
which is the Bellman optimality condition for $v_*$, so both $\pi$ and $\pi'$ are optimal. This means that policy improvement gives a strictly better policy unless the policy is already optimal. \\

The policy improvement theorem holds for stochastic policies too, but we don't go into that here.

\subsection{Policy Iteration}
We can exploit policy improvement iteratively to get the policy iteration algorithm.

\includegraphics[width=\textwidth]{\ProjectDir/data/notes_images/policy_iteration_algorithm.png}
\mbox{}\\
A finite MDP has only a finite number of policies (as long as they are deterministic, of course) so this process is guaranteed to converge.

\subsection{Value Iteration}
Policy iteration can be slow because each iteration involves running the entire policy evaluation until convergence. \\

It turns out that one can truncate the policy evaluation step of policy iteration in many ways without losing convergence guarantees. One special case of this is \emph{value iteration}, where we truncate policy evaluation after only one update of each state. This algorithm converges to $v_*$ under the same conditions that guarantee the existence of $v_*$.

\includegraphics[width=\textwidth]{\ProjectDir/data/notes_images/value_iteration_algorithm.png}
\mbox{}\\

Note the $\max_a$ in the assignment of $V(s)$, since we only one sweep of the state space and then choose the greedy policy.\\

It may be more efficient to interpose multiple policy evaluation steps in between policy improvement iterations, all of these algorithms converge to an optimal policy for discounted finite MDPs.

\subsection{Asynchronous Dynamic Programming}
The DP methods that we have described so far all involve a full sweep of the state space on each iteration. This is potentially a very costly procedure. \\

\emph{Asynchronous} DP algorithms update the values in-place and cover states in any order whatsoever. The values of some states may be updated several times before the values of others are updated once. To converge correctly, however, an asynchronous algorithm must continue to update the values of all the states: it can’t ignore any state after some point in the computation.\\

Asynchronous DPs give a great increase in flexibility, meaning that we can choose the updates we want to make (even stochastically) based on the interaction of the agent with the environment. This procedure might not reduce computation time in total if the algorithm is run to convergence, but it could allow for a better rate of progress for the agent.

\subsection{Generalised Policy Iteration}
We use the term \emph{generalised policy iteration} (GPI) to refer to the general idea of letting policy evaluation and policy improvement processes interact, independent of the granularity and other details of the two processes. Almost all reinforcement learning methods are well described as GPI, including the policy iteration algorithms we have discussed in this section. GPI works via the competing but complementary nature of the two processes. In some cases it can be guaranteed to converge.

\subsection{Efficiency of Dynamic Programming}
If we ignore a few technical details, then the (worst case) time DP methods take to find an optimal policy is polynomial in the number of states and actions. Compare this to the searching the states directly, which is exponential.


\chapter{Random Variables}

\begin{ex}
  Note that
  \begin{align*}
    F(x^+)-F(x^-)
     & =F(x)-\lim_{\substack{y\to x    \\ y < x}}F(y) & & \text{(by Theorem 2.8)} \\
     & =\lim_{\substack{y\to x         \\ y < x}}F(x)-F(y) \\
     & =\lim_{\substack{y\to x         \\ y < x}}\P{X\leq x}-\P{X\leq y} \\
     & =\lim_{\substack{y\to x         \\ y < x}}\P{X\in (-\infty, x]}-\P{X\in (-\infty, y]} \\
     & =\lim_{\substack{y\to x         \\ y < x}}\P{X\in (-\infty, y]\cup(y, x]}-\P{X\in (-\infty, y]} \\
     & =\lim_{\substack{y\to x         \\ y < x}}\P{X\in (y, x]} \\
     & =\P{\bigcap_{i=1}^n (x-1/n, x]} \\
     & =\P{X=x}.
  \end{align*}
\end{ex}

\begin{ex}~
  \inputminted{python}{../code/02-02.py}

  \begin{figure}[H]
    \centering
    \includegraphics[scale=0.98]{../images/02-02}
    \caption{Plot of the CDF $F$.}
  \end{figure}

  Note that
  \[
    \P{2< X\leq 4.8}
    =\P{X\leq 4.8}-\P{X\leq 2}
    =\frac{2}{10}-\frac{1}{10}
    =\frac{1}{10},
  \]
  while
  \[
    \P{2\leq X\leq 4.8}=\P{X\leq 4.8}-\P{X< 2}=\frac{2}{10}-0=\frac{2}{10}.
  \]
\end{ex}

\begin{ex}
  \begin{enumerate}[1.]
    \item[]
    \item We already proved this claim as part of our solution to Exercise 1.
    \item Note that
          \begin{align*}
            F(y) - F(x)
             & = \P{X\in (-\infty, y]} - \P{X\in (-\infty, x]}           \\
             & = \P{X\in (-\infty, x]\cup(x, y]} - \P{X\in (-\infty, x]} \\
             & = \P{X\in (x, y]}                                         \\
             & = \P{x < X\leq y}.
          \end{align*}
    \item We have
          \begin{align*}
            \P{X>x}
             & = \P{X\in (x, \infty)}      \\
             & = \P{X\in (-\infty, x]^c}   \\
             & = 1 - \P{X\in (-\infty, x]} \\
             & = 1 - F(x).
          \end{align*}
    \item Note that by Part 2, $F(b)-F(a)=\P{a<X\leq b}$. Moreover, note that
          since $X$ is continuous, $\P{\{a\}}=\P{\{b\}}=0$, and hence we can
          freely add or remove the endpoints of the interval on the right-hand
          side without changing the value of the probability.
  \end{enumerate}
\end{ex}

\begin{ex}
  \begin{enumerate}[(a)]
    \item []
    \item We have
          \[
            F_X(x)=\begin{cases}
              0                                & x \leq 0,        \\
              \frac{1}{4}x                     & 0 < x < 1,       \\
              \frac{1}{4}                      & 1 \leq x \leq 3, \\
              \frac{1}{4} + \frac{3}{8}(x - 3) & 3 < x < 5,       \\
              1                                & x \geq 5.
            \end{cases}
          \]
    \item Let $Y=1/X$. Note that
          \[
            \P{Y\leq y}
            =\P{\frac{1}{X}\leq y}
            =\P{X\geq \frac{1}{y}}
            =1-F_X(y^{-1}),
          \]
          and that therefore
          \[
            F_Y(y)=\begin{cases}
              0                         & y\leq\frac{1}{5},          \\
              \frac{15}{8}-\frac{3}{8y} & \frac{1}{5}<y<\frac{1}{3}, \\
              \frac{3}{4}               & \frac{1}{3}\leq y\leq 1,   \\
              1 - \frac{1}{4y}          & y>1,
            \end{cases}
          \]
          and
          \[
            f_Y(y)=\begin{cases}
              0              & y\leq\frac{1}{5},          \\
              \frac{3}{8y^2} & \frac{1}{5}<y<\frac{1}{3}, \\
              0              & \frac{1}{3}\leq y\leq 1,   \\
              \frac{1}{4y^2} & y>1.
            \end{cases}
          \]
  \end{enumerate}
\end{ex}

\begin{ex}
  Let X and Y be independent discrete random variables. Then, for any
  $x,y\in\R$,
  \begin{align*}
    f_{X,Y}(x,y)
     & =\P{(X, Y)=(x, y)}            \\
     & =\P{(X, Y)\in \{(x, y)\}}     \\
     & =\P{X\in \{x\}, Y\in \{y\}}   \\
     & =\P{X\in \{x\}}\P{Y\in \{y\}} \\
     & =\P{X=x}\P{Y=y}               \\
     & =f_X(x)f_Y(y).
  \end{align*}

  Conversely, suppose that $f_{X,Y}(x,y)=f_X(x)f_Y(y)$ for all $x$ and $y$.
  Then for any $A$ and $B$,
  \begin{align*}
    \P{X\in A, Y\in B}
     & =\P{(X, Y)\in A\times B}                          \\
     & =\sum_{\substack{(a,b)\in A\times B}}f_{X,Y}(a,b) \\
     & =\sum_{a\in A}\sum_{b\in B}f_{X,Y}(a,b)           \\
     & =\sum_{a\in A}\sum_{b\in B}f_{X}(a)f_{Y}(b)       \\
     & =\sum_{a\in A}f_X(a)\sum_{b\in B}f_{Y}(b)         \\
     & =\P{X\in A}\P{Y\in B}.
  \end{align*}
\end{ex}

\begin{ex}
  Note that $Y$ is a discrete random variable with outcomes $0$ or $1$. In
  particular,
  \[
    \P{Y=1}=\E{I_A(X)}=\int_A\! f(x)\,\d{x},
  \]
  and therefore
  \[
    f_Y(x)=\begin{cases}
      1-\int_A\! f(t)\,\d{t} & x=0, \\
      \int_A\! f(t)\,\d{t}   & x=1.
    \end{cases}
  \]
  Hence,
  \[
    F_Y(x)=\begin{cases}
      0                      & x < 0,       \\
      1-\int_A\! f(t)\,\d{t} & 0\leq x < 1, \\
      1                      & x\geq 1.
    \end{cases}
  \]
\end{ex}

\begin{ex}
  Consider
  \begin{align*}
    \P{Z>z}
     & =\P{X>z}\P{Y>z}                                       \\
     & =(1-\P{X \leq z})(1-\P{Y \leq z})                     \\
     & =1-\P{X \leq z}-\P{Y \leq z}+\P{X \leq z}\P{Y \leq z} \\
     & =\begin{cases}
      1        & z< 0,       \\
      1-2z+z^2 & 0\leq z< 1, \\
      0        & z\geq 1,
    \end{cases}
  \end{align*}
  and hence
  \[
    f_Z(z)
    =\frac{\d}{\d{z}} F_Z(z)
    =\frac{\d}{\d{z}} (1 - \P{Z>z})
    =\begin{cases}
      2-2z & 0\leq z< 1,       \\
      0    & \text{otherwise}.
    \end{cases}
  \]
\end{ex}

\begin{ex}
  Note that $X^+$ is not supported on negative values and that therefore
  $\P{X^+\leq x}=0$ for $x<0$. Note that $X^+=0$ whenever $X\leq 0$ and
  therefore $\P{X^+=0}=\P{X<0}$. Finally, for $x>0$,
  \[
    \P{X^+\leq x}
    =\P{X^+<0}+\P{X^+=0}+\P{0<X\leq x}
    =F(0)+F(x)-F(0)=F(x).
  \]
  Hence,
  \[
    F_{X^+}(x)=\begin{cases}
      0    & x < 0,    \\
      F(x) & x \geq 0.
    \end{cases}
  \]
\end{ex}

\begin{ex}
  Suppose $X\sim \text{Exp}(\beta)$. Then
  \begin{align*}
    F(x)
     & =\int_{0}^x\!\beta e^{-\beta t}\,\d{t}                   \\
     & =-\int_{0}^{-\beta x}\!e^{u}\,\d{u}    &  & (u=-\beta t) \\
     & =1-e^{-\beta x}.
  \end{align*}
  Now, suppose that $q=1-e^{-\beta x}$. Then
  \[
    e^{-\beta x}= 1 - q,
  \]
  or,
  \[
    F^{-1}(q) = -\frac{\ln(1 - q)}{\beta}.
  \]
\end{ex}

% 10
\begin{ex}
  Let $A,B\subset \R$. Note that
  \begin{align*}
    \P{g(X)\in A, h(Y)\in B}
     & =\P{X\in g^{-1}(A), Y\in h^{-1}(B)}   \\
     & =\P{X\in g^{-1}(A)}\P{Y\in h^{-1}(B)} \\
     & =\P{g(X)\in A}\P{h(Y)\in B}.
  \end{align*}
  Since $A$ and $B$ were arbitrary, it follows that $g(X)$ and $h(Y)$ are
  independent.
\end{ex}

\begin{ex}
  \begin{enumerate}[(a)]
    \item[]
    \item Note that $\P{X=1}=\P{Y=1}=1/2$, but $\P{(X,Y)=(1,1)}=0$, instead of
          1/4 as we would have expected if $X$ and $Y$ were independent.
    \item We have
          \begin{align*}
            f_{N,X,Y}(n,x,y)
             & =f_{N}(n)f_{X|N}(x|n)f_{Y|X,N}(y|x,n)                                      \\
             & =\frac{\lambda^n e^{-\lambda}}{n!}\binom{n}{x}p^x(1-p)^{n-x}\delta(x+y-n),
          \end{align*}
          and therefore
          \begin{align*}
            f_{X,Y}(x,y)
             & =\sum_{n=0}^\infty\frac{\lambda^n e^{-\lambda}}{n!}\binom{n}{x}p^x(1-p)^{n-x}\delta(x+y-n) \\
             & =\frac{\lambda^{x+y} e^{-\lambda}}{(x+y)!}\binom{x+y}{x}p^x(1-p)^y                         \\
             & =e^{-\lambda}\frac{\lambda^x p^x}{x!} \frac{\lambda^y (1-p)^y}{y!}                         \\
             & =\frac{(\lambda p)^xe^{-\lambda p}}{x!} \frac{(\lambda(1-p))^ye^{-\lambda (1-p)}}{y!}      \\
             & = f_X(x)f_Y(y).
          \end{align*}
          Hence, by Problem 2.5, it follows that $X\amalg Y$.
  \end{enumerate}
\end{ex}

\begin{ex}
  Let $I_1\times I_2$ be the range of $X$ and $Y$ where $I_1$ and $I_2$ are
  (possibly unbounded) intervals. Then
  \begin{align*}
    f_X(x)
     & =\int_{I_2}\!f(x, y)\,\d{y}  \\
     & =\int_{I_2}\!g(x)h(y)\,\d{y} \\
     & =g(x)\int_{I_2}\!h(y)\,\d{y} \\
     & =h_0g(x),
  \end{align*}
  where the integral is known to be finite by Fubini's theorem since
  $\int_{I_1\times I_2}f(x,y)\,\d{(x,y)}=1$.

  Likewise,
  \[
    f_Y(y)
    =\int_{I_1}\!g(x)h(y)\,\d{y}
    =g_0h(y).
  \]

  Therefore,
  \[
    \int_{I_1}\!f_X(x)f_Y(y)\,\d{x}
    =h_0g_0\int_{I_1}\!g(x)h(y)\,\d{x}
    =h_0g_0\int_{I_1}\!f(x,y)\,\d{x},
  \]
  or, taking the leftmost and rightmost integrals,
  \[
    f_Y(y)=h_0g_0f_Y(y).
  \]
  Hence, $h_0g_0=1$, and so
  \[
    f_X(x)f_Y(y)=h_0g_0 g(x)h(y)=g(x)h(y)=f(x,y).
  \]
  Thus, $X$ and $Y$ are independent by Theorem 2.30.
\end{ex}

\begin{ex}
  \begin{itemize}[(a)]
    \item
          Note that
          \[
            F(y)
            =\P{Y\leq y}
            =\P{e^X \leq y}
            =\P{X \leq \ln {y}},
          \]
          and therefore
          \[
            f(y)
            =\frac{\d}{\d{y}}\Phi(\ln {y})
            =\frac{1}{y\sqrt{2\pi}}e^{-\frac{1}{2}(\ln{y})^2}.
          \]

          \inputminted{python}{../code/02-13a.py}
          \begin{figure}[H]
            \centering
            \includegraphics[scale=0.98]{../images/02-13a}
            \caption{Graph of PDF for $Y=e^X$.}
          \end{figure}

    \item[(b)]
          \inputminted{python}{../code/02-13b.py}

          \begin{figure}[H]
            \centering
            \includegraphics[scale=0.98]{../images/02-13b}
            \caption{Histogram of 10,000 samples from $Y=e^X$.}
          \end{figure}
  \end{itemize}
\end{ex}

\begin{ex}
  Note that since $(X,Y)$ is uniformly distributed, the probability that
  $\P{R\leq r}$ for $0\leq r<1$ is going to be proportional to the area of the
  disc of radius $r$. Therefore,
  \[
    F_R(r)=\begin{cases}
      0   & r < 0,          \\
      r^2 & 0\leq r \leq 1, \\
      1   & r > 1,
    \end{cases}
  \]
  and
  \[
    f_R(r)=\begin{cases}
      2r & 0\leq r \leq 1,   \\
      0  & \text{otherwise}.
    \end{cases}
  \]
\end{ex}

% 15
\begin{ex}
  We have
  \[
    F_X(x)
    =\P{X\leq x}         \\
    =\P{F^{-1}(U)\leq x} \\
    =\P{U\leq F(x)}
    =F(x),
  \]
  and therefore $X\sim F$.

  \inputminted{python}{../code/02-15.py}

  \begin{figure}[H]
    \centering
    \includegraphics[scale=1]{../images/02-15}
    \caption{A histogram of samples drawn from a uniform distribution and then
      transformed under the inverse of the CDF of an $\text{Exponential}(1)$
      distribution.}
  \end{figure}
\end{ex}

\begin{ex}
  We have
  \begin{align*}
    \cP{X=x}{X+Y=n}
     & =\frac{\P{X=x,X+Y=n}}{\P{X+Y=n}}                                                                               \\
     & =\frac{\P{X=x,Y=n-x}}{\P{X+Y=n}}                                                                               \\
     & =\frac{\P{X=x}\P{Y=n-x}}{\P{X+Y=n}}                                                                            \\
     & =\frac{\lambda^x e^{-\lambda}}{x!}\frac{\mu^{n-x} e^{-\mu}}{(n-x)!}\frac{n!}{(\lambda +\mu)^ne^{-\lambda-\mu}} \\
     & =\binom{n}{x}\left(\frac{\lambda}{\lambda+\mu}\right)^x\left(\frac{\mu}{\lambda+\mu} \right)^{n-x}             \\
     & =\binom{n}{x}\left(\frac{\lambda}{\lambda+\mu}\right)^x\left(1-\frac{\lambda}{\lambda+\mu} \right)^{n-x},
  \end{align*}
  but this is precisely the probability mass function of a
  $\text{Binomial}(n, \lambda/(\lambda+\mu))$.
\end{ex}

\begin{ex}
  We begin by obtaining the marginal distribution for $Y$:
  \[
    \int_0^1\! c(x+y^2)\,\d{x}
    =c\left[\frac{x^2}{2}+xy^2\right]_{x=0}^1
    =c\left(\frac{1}{2}+y^2\right),
  \]
  and therefore
  \[
    f_Y(y)=c\left(\frac{1}{2}+y^2\right)I_{[0,1]}(y).
  \]

  Using the definition of conditional probability, we get
  \[
    f_{X|Y}(x\,|\,y)
    =\frac{f_{X,Y}(x,y)}{f_Y(y)}
    =\frac{(x+y^2)}{\left(\frac{1}{2}+y^2\right)}I_{[0,1]^2}(x,y).
  \]
  Therefore,
  \[
    \cP{X<\frac{1}{2}}{Y=\frac{1}{2}}
    =\int_0^{1/2}\!\frac{x+\frac{1}{4}}{\frac{1}{2}+\frac{1}{4}}\,\d{x}
    =\frac{4}{3}\int_0^{1/2}\!\left(x+\frac{1}{4}\right)\,\d{x}
    =\frac{1}{3}.
  \]
\end{ex}

\begin{ex}
  \begin{enumerate}[(a)]
    \item[]
    \item
          \begin{align*}
            \P{X<7}
            =\P{Z<\frac{7 - 3}{4}}
            =\P{Z<1}
            \approx 0.8413.
          \end{align*}
    \item
          \begin{align*}
            \P{X>-2}
            =1-\P{Z<\frac{-2 - 3}{4}}
            =1 - \P{Z<-1.25}
            \approx 0.8944.
          \end{align*}
    \item
          \begin{align*}
            .05
            =\P{X>x}
            =1-\P{Z<\frac{x-3}{4}}
          \end{align*}
          Therefore
          \begin{align*}
            1.64485\approx \Phi^{-1}(0.95)=\frac{x-3}{4},
          \end{align*}
          and so
          \begin{align*}
            x\approx 9.5794.
          \end{align*}

    \item
          \begin{align*}
            \P{0\leq X < 4}
             & =\P{X<4}-\P{X < 0}          \\
             & =\P{Z < 0.25}-\P{Z < -0.75} \\
             & \approx 0.3702.
          \end{align*}
    \item
          \begin{align*}
            .05
             & =\P{|X|>|x|}                                 \\
             & =\P{X>x}+\P{X<-x}                            \\
             & =1-\P{X<x}+\P{X<-x}                          \\
             & =1-\P{Z<\frac{x-3}{4}}+\P{Z<\frac{-x-3}{4}}.
          \end{align*}

          We use the following Python code to learn that $x$ is
          approximately $9.61098387$.

          \inputminted{python}{../code/02-18.py}
  \end{enumerate}
\end{ex}

\begin{ex}
  Suppose that $r$ is strictly monotone increasing with an inverse $s=r^{-1}$.
  Let $X$ be a random variable and let $Y=r(X)$.
  Then, since the inverse of a strictly monotone increasing function is also
  strictly monotone increasing,
  \[
    A_y
    =\{x \mid r(x) \leq y \}
    =\{x \mid x \leq s(y) \},
  \]
  and hence
  \[
    F_Y(y) = F_X(s(y)).
  \]
  Thus, by the Chain Rule,
  \[
    f_Y(y)=\frac{\d}{\d{y}}F_X(s(y))=f_X(s(y))\frac{\d{s(y)}}{\d{y}}.
  \]

  Next, suppose that $r$ is strictly monotone decreasing. Then, since the
  inverse of a strictly monotone decreasing function is also
  strictly monotone decreasing,
  \[
    A_y
    =\{x \mid r(x) \leq y \}
    =\{x \mid x \geq s(y) \}
  \]
  and hence
  \[
    F_Y(y) = 1-F_X(s(y)),
  \]
  and
  \[
    f_Y(y)=\frac{\d}{\d{y}}\left(1-F_X(s(y))\right)=-f_X(s(y))\frac{\d{s(y)}}{\d{y}}.
  \]

  Finally, note that in the strictly monotone increasing case,
  $\frac{\d{s(y)}}{\d{y}}>0$, and therefore we can replace it with
  $\left|\frac{\d{s(y)}}{\d{y}}\right|$, while in the strictly monotone
  decreasing case $\frac{\d{s(y)}}{\d{y}}<0$, and therefore we can replace
  $-\frac{\d{s(y)}}{\d{y}}$ with $\left|\frac{\d{s(y)}}{\d{y}}\right|$.
  Hence, in both cases
  \[
    f_Y(y)=f_X(s(y))\left|\frac{\d{s(y)}}{\d{y}}\right|.
  \]
\end{ex}

\begin{ex}
  Let $Z=X-Y$ and note that if $0<z\leq 1$,
  \[
    A_z=\{(x,y)\mid x-y\leq z\}
  \]
  is the unit square minus the triangle with vertices $(z,0)$, $(1,1)$ and
  $(1,1-z)$, while if $-1<z\leq 0$, $A_z$ is the triangle with vertices
  $(0, -z)$, $(0, 1)$ and $(1+z,1)$. Hence,
  \[
    F_Z(z)=\begin{cases}
      0                   & z\leq -1,         \\
      \frac{(1+z)^2}{2}   & -1<z\leq 0,       \\
      1-\frac{(1-z)^2}{2} & 0<z\leq 1,        \\
      1                   & \text{otherwise},
    \end{cases}
  \]
  and
  \[
    f_Z(z)=\begin{cases}
      1+z & -1<z\leq 0,       \\
      1-z & 0<z\leq 1,        \\
      0   & \text{otherwise}.
    \end{cases}
  \]

  Next, let $U=X/Y$, and note that then
  \[
    A_u=\{(x,y)\mid x/y\leq u\}
  \]
  is the area in the unit square above the graph of the line $y=\frac{1}{u}x$.
  If $0<u\leq 1$, this is the triangle with vertices $(0,0)$, $(0, 1)$ and
  $(u, 1)$, while if $u>1$, this is the entire unit square except the triangle
  with vertices $(0, 0)$, $(1, 0)$ and $\left(1, \frac{1}{u}\right)$. Therefore,
  \[
    F_U(u)=\begin{cases}
      0              & u\leq 0,   \\
      \frac{u}{2}    & 0<u\leq 1, \\
      1-\frac{1}{2u} & u> 1,
    \end{cases}
  \]
  and
  \[
    f_U(u)=\begin{cases}
      \frac{1}{2}    & 0<u\leq 1,        \\
      \frac{1}{2u^2} & u>1,              \\
      0              & \text{otherwise}.
    \end{cases}
  \]
\end{ex}

\begin{ex}
  Note that
  \begin{align*}
    F_Y(y)
     & =\P{Y\leq y}                                                                                \\
     & =\P{X_1\leq y, X_2\leq y, \cdots, X_n\leq y}                                                   \\
     & =\P{X_1\leq y}\P{X_2\leq y}\cdots\P{X_n\leq y} &  & \text{(independence of $X_i$'s)}           \\
     & =\P{X_1\leq y}^n                               &  & \text{(identical distribution of $X_i$'s)} \\
     & =(1-e^{-\beta y})^n,
  \end{align*}
  and therefore,
  \[
    f_Y(y)
    =\frac{\d}{\d{y}} (1-e^{-\beta y})^n
    =n\beta e^{-\beta y}(1-e^{-\beta y})^{n-1}.
  \]
\end{ex}


\subsection{Anti-derivative}

Taking the derivative of a function provides another function. The anti-derivative of a function is a function which, when differentiated, provides the original function.

As this function can include any additive constant, there are an infinite number of anti-derivatives for any function.


\documentclass[11pt]{article}

\usepackage{latexsym}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{pseudocode}
\usepackage{url}
\usepackage[backref, colorlinks=true, citecolor=red, urlcolor=blue, pdfauthor={Jyh-Ming Lien}]{hyperref}

\newcommand{\handout}[5]{
  \noindent
  \begin{center}
  \framebox{
    \vbox{
      \hbox to 5.78in { {\bf CS 633 Computational Geometry} \hfill #2 }
      \vspace{4mm}
      \hbox to 5.78in { {\Large \hfill #5  \hfill} }
      \vspace{2mm}
      \hbox to 5.78in { {\em #3 \hfill #4} }
    }
  }
  \end{center}
  \vspace*{4mm}
}

\newcommand{\lecture}[4]{\handout{#1}{#2}{#3}{}{#1}}

\newtheorem{theorem}{Theorem}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{observation}[theorem]{Observation}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{claim}[theorem]{Claim}
\newtheorem{fact}[theorem]{Fact}
\newtheorem{assumption}[theorem]{Assumption}

% 1-inch margins, from fullpage.sty by H.Partl, Version 2, Dec. 15, 1988.
\topmargin 0pt
\advance \topmargin by -\headheight
\advance \topmargin by -\headsep
\textheight 8.9in
\oddsidemargin 0pt
\evensidemargin \oddsidemargin
\marginparwidth 0.5in
\textwidth 6.5in

\parindent 0in
\parskip 1.5ex
%\renewcommand{\baselinestretch}{1.25}

\begin{document}

\lecture{Voronoi Stippling}{Fall 2019}{Prof.\ Jyh-Ming Lien}{---}

The goal of this assignment is to deepen your understanding on various implementations of 2-d Voronoi Diagram.
You are given an implementation of 2-d Voronoi Diagram using well-known image-based wave propagation method. You task is to (1) understand the implementations and (2) improve the provided implementation.

\textbf{What to submit}: You  need to turn in a report in \LaTeX\ (see the template in report folder).  Your report should include two main sections: a summary of what the code does,
and your improvement. In the second section, you should include
all the example outputs (visual and/or statistical results). In the last section you should report known bugs, and known limitations.

\textbf{How to submit}:
Zip your entire folder including code and example input and output in a single file called ``your\_net\_id\_PA02.zip". For example, my GMU net id is jmlien,  so my submission will be jmlien\_PA02.zip.

\textbf{Due: Oct 30, 2019. At 11:59 pm.}

\section{Part 1: Understand the implementations (40 pts)}

\subsection{What should  you do?}

Your goal is to get a full grasp of what the code does.
Your summary should provide algorithms for computing Voronoi diagram, Centroidal Voronoi tessellation (CVT),
and stippling methods.
%Your summary should provide a discussion on what parameters are used to control their outputs.

{\sc Hint 1}: The implementation is based on the paper by Secord, Adrian. ``Weighted voronoi stippling." Proceedings of the 2nd international symposium on Non-photorealistic animation and rendering. ACM, 2002. It is highly recommend that you read the paper first.

{\sc Hint 2}: To compile hedcuter code, please use cmake or the solution file in folder {\em hedcuter/code/vc\_files}.
This code requires OpenCV 4.0. Installing OpenCV is straight forward on OSX and Linux.
By default, it requires 64 bits installation of OpenCV on Windows.
The solution file also uses two environment variables called ``OPENCV\_INCLUDEDIR'' and ``OPENCVX64\_LIBRARYDIR'' the point to
the include and library folders on the system. Therefore, make sure that you have those variable defined before you compile. You can  consult OpenCV documents
(\href{docs.opencv.org}{\it docs.opencv.org})  or (\href{https://www.learnopencv.com/install-opencv-4-on-windows/}{\it https://www.learnopencv.com/install-opencv-4-on-windows/}
if you encounter problems.

\section{Part 2: Improve  ``hedcuter'' code (60 pts)}

\subsection{What should  you do?}

Provide at least three improvements (each will worth 20 points) to the hedcuter code. Below are some possible improvements that
you can do. In your report, you should show the improvement either visually or/and statistically for timing/performance results
using images of your own, i.e. do not use the images in hedcuter/images.
Extra bonus of 20 points will be given for an additional improvement beyond the 3rd improvement.
\begin{enumerate}

\item Improve the distribution of the disks to avoid unnatural clustering of the disks. One idea is to use higher
image resolution (using subpixels) for computing the centroids of Voronoi cells.

\item Improve the computation efficiency. One way of doing this is via  the implementation of Fortune's algorithm. Another way is GPU. You can try the method by Hoff III, Kenneth E., et al. ``Fast computation of generalized Voronoi diagrams using graphics hardware." Proceedings of the 26th annual conference on Computer graphics and interactive techniques, 1999. The implementation should be pretty simple if you know OpenGL.

\item Add functionality to generate colorful disks. For example, you can implement functions that are not available in hedcuter code but provided in the voronoi code.

\item Using feature extraction, such as lines, to get better looking results.

\end{enumerate}

\bibliographystyle{plain}
\bibliography{shape-assignment}

\end{document}


%!TEX root=ClassNotes.tex

\section{Inequalities}
In this section we'll learn how to solve inequalities and in the process figure out how to write proofs of statements involving inequalities.

The biggest different between equalities and inequalities is that, unlike equalities, there are usually infinitely many (if any) solutions to inequalities. When we we're trying to solve an inequality we're not looking for the best solution, we're not even looking for a good solution, we're simply looking for {\bf one} solution. For example, both $x = 1.001$ and $x = 10^{100}$ are equally valid solutions to the inequality $x > 1$.

\begin{exercise}
	Find a positive real solution to each of the following inequalities.
	\begin{enumerate}
		\item $x ^ 3 + x < 1$
		\item $(1 + x)^2 - 1 < 1$
		\item $1 - (1 - x)^2 < 1$
		\item $x ^ 3 + 2x < 1 + x^2$
		\item $x^{10} + x > 10 $
		\item $x^3 - x > 1$
	\end{enumerate}
\end{exercise}

When we try to solve equalities we try to simply the equation until it becomes easy to solve. The same thing is true for inequalities, however, the ways to simplify an inequality are much more subtle.

\begin{example}
	If we're trying to solve $P(x) > \epsilon$, where $P$ is some complicated expression that cannot be simplified, then we try to find some simpler $Q(x)$, such that $P(x) > Q(x)$ and try to solve for $Q(x) > \epsilon$ instead. Similarly, if we're trying to solve $P(x) < \epsilon$, then we try to find some simpler $Q(x)$, such that $P(x) < Q(x)$ and try to solve for $Q(x) < \epsilon$ instead.

	 There is no {\it canonical way} in which an inequality can be solved. We somehow simplify the inequality and hope that we get a solution.
\end{example}

For solving complicated inequalities, we need a systematic approach towards estimating functions. There are different tricks and identities for estimating different kinds of functions. For now, we'll focus mainly on estimating polynomials.

\begin{exercise}$ $
	\label{q:inequality_1}
	Prove the following extremely useful set of inequalities
	\begin{center}
		\begin{tabular}{ll}
			if $1 \ge x > 0$ &then $ 1 \ge x \ge x^2 \ge x^3 \ge \dots$ \\
			if $1 \le x$ &then $1 \le x \le x^2 \le x^3 \le \dots$.
		\end{tabular}
	\end{center}
\end{exercise}

\noindent {\bf Word of Caution: } You should be very careful when negative numbers are involved. Multiplying an inequality by a negative number changes it's sign, for example, $2 < 3$ but $-2 > -3$. So,
\begin{center}
	\begin{tabular}{ll}
		if $1 \ge x > 0$ &then $ -1 \le -x \le -x^2 \le -x^3 \le \dots$ \\
		if $1 \le x$ &then $-1 \ge -x \ge -x^2 \ge -x^3 \ge \dots$.
	\end{tabular}
\end{center}

\subsection{Proofs involving Inequalities}
We'll encounter several statements in this class like: {\it For every $\epsilon> 0$ there exists a positive real number $x$ such that $x^3 + 3x < \epsilon$.} When we want to prove such a statement, we are essentially trying to solve $x^3 + 3x < \epsilon$. (Why?) For example,\\
\begin{q}
	\label{q:sample_problem_inequality_1}
	Prove that for every $\epsilon > 0$ there exists a positive real number $x$ such that $x^3 + 3x < \epsilon$.
\end{q}
\noindent We're asking for a solution to the equation $x^3 + 3x < \epsilon$ for an arbitrary positive real number $\epsilon$.
\begin{enumerate}
	\item The terms $x^3$ and $x$ have different degrees. We can try to use the inequalities in Exercise \ref{q:inequality_1} to simplify their sum, for this we need to know if $x \ge 1$ or $x \le 1$.
	\item We're probably looking for a small  $x$ so we could assume $x \le 1$. If this does not work we'll come back try something else. (Remember that we're trying to find {\bf one} solution.)
	\item As $x \le 1$, $x^3 \le x$ so that
		$x^3 + 3x \le x + 3x= 4x$.
	\item	As $x^3 + 3x \le 4x$ and we want to find a solution to $x^3 + 3x < \epsilon$, it suffices to solve $4x < \epsilon$. As $\epsilon$ is positive, \textbf{one} solution for this is $x = \epsilon / 5$.
	\item We still need to check if this solution actually works. We can do this by trying to write down a direct proof and making sure that all the implications are logically sound.
	\item We required the condition $x \le 1$ for this solution to work. To ensure this we'll set $x = \min \{ 1, {\epsilon}/{5}\}$.
\end{enumerate}

Now we know what the solution is, but we still need to write down a proof for the original statement. A proof should always start with some assumptions and logically derive the required conclusion. For us the assumption will be $\epsilon > 0$ and $x = \min \{ 1, {\epsilon}/{5}\}$ and the expected conclusion is $x^3 + 3x < \epsilon$.

\begin{proof}[Proof of Q. \ref{q:sample_problem_inequality_1}]
	Let $\epsilon > 0$ and let $x = \min \{ 1, {\epsilon}/{5}\}$. Note that $x \le 1$ and hence $x^3 \le x$. Then,
	\begin{align*}
		x^3 + 3x
		&\le x + 3x \\
		&= 4x \\
		&= 4 \min  \{ 1, {\epsilon}/{5}\} \\
		&\le 4 \epsilon /5 \\
		&< \epsilon & \mbox{ as $\epsilon$ is positive}
	\end{align*}
	Hence, $x= \min \{ 1, {\epsilon}/{5}\}$ is a solution of $x^3 + 3x < \epsilon$, which proves the proposition.
\end{proof}
First we had to solve the inequality then reverse the steps: start with the solution and write a {\it direct proof} for the proposition. This is how proofs are usually discovered. You make an ``educated guess'' and hope that it works. Sometimes it doesn't, so you go back to finding another ``educated guess''.

\begin{remark}[A note on quantifiers]
	 For proving the statement {\it``prove that for every $\epsilon > 0$ there exists a positive real number $x$ such that $x^3 + 3x < \epsilon$''} we had to start with the statement {\it ``let $\epsilon > 0$ and $x = \min \{ 1, {\epsilon}/{5}\}$''}.
	 The variable $x$ depends on the variable $\epsilon$.
	 This is fine because of the order of quantifiers: {\it``\dots for every $\epsilon > 0$ there exists a positive real number $x$ \dots''}; the quantifier for $\epsilon$ comes before the quantifier for $x$ and hence the variable $x$ can depend on the variable $\epsilon$.

	If instead, suppose we were trying to prove {\it ``there exists a positive real number $x$ such that for every $\epsilon > 0$, $x^3 + 3x < \epsilon$''}.
	We're still trying to solve the equation $x^3 + 3x < \epsilon$ but now the quantifier for $x$ comes before the quantifier for $\epsilon$ and hence $x$ cannot depend on the variable $\epsilon$ but instead should be a constant that universally solves the equation $x^3 + 3x < \epsilon$ for every $\epsilon > 0$.
	No such $x$ exists (why?) and hence the statement {\it ``there exists a positive real number $x$ such that for every $\epsilon > 0$, $x^3 + 3x < \epsilon$''} is false.
\end{remark}

\begin{exercise}
	Prove that for every $\epsilon > 0$ there exists a positive real number $x$ such that \dots
		\begin{enumerate}
			\item  $x ^ 3 + x < \epsilon$.
			\item  $(1 + x)^2 - 1 < \epsilon$.
			\item $1 - (1 - x)^2 < \epsilon$.
			\item  $x ^ 3 + 2x < \epsilon + x^2$.
			\item  $x^{10} + x > \epsilon $.
			\item  $x^3 - x > \epsilon $.
						\hint{Break $x^3$ as $\frac{x^3}{2} + \frac{x^3}{2}$ and find the conditions on $x$ for which $\left(\frac{x^3}{2} - x\right) > 0$.}\\
		\end{enumerate}
\end{exercise}

Finally, we are sometimes required to find not one solution but a range of solutions. We try to come up an ``educated guess'' by the same method, and many a times this gives us the required range for free with a few changes in the final proof.

\begin{q}
	\label{q:sample_problem_inequality_2}
	Prove that for every $\epsilon > 0$, there exists a positive real number $\delta$ such that, for all $x$, if $0 < x < \delta$ then $x^3 + 3x < \epsilon$.
\end{q}
\begin{proof}
	Let $\epsilon > 0$, let $\delta = \min \{ 1, \epsilon/5\}$ and let $ 0 < x < \delta$. Because $\delta \le 1$, we have $x < 1$ and hence $x^3 < x$. As before,
	\begin{align*}
		x^3 + 3x
		&\le x + 3x \\
		&= 4x \\
		&< 4 \delta \\
		&= 4 \min  \{ 1, {\epsilon}/{5}\} \\
		&\le 4 \epsilon /5 \\
		&< \epsilon& \mbox{ as $\epsilon$ is positive}
	\end{align*}
	Hence, every $0 < x <  \min \{ 1, \epsilon/5\}$ is a solution of $x^3 + 3x < \epsilon$, which proves the proposition.
\end{proof}

\begin{exercise}
	Prove that for every $\epsilon > 0$, there exists a positive real number $\delta$ such that, for all $x$, if $ 0 < x < \delta$ then \dots
		\begin{enumerate}
			\item  $x ^ 3 + x < \epsilon$.
			\item  $(1 + x)^2 - 1 < \epsilon$.
			\item $1 - (1 - x)^2 < \epsilon$.
			\item  $x ^ 3 + 2x < \epsilon + x^2$.
		\end{enumerate}
		\noindent Prove that for every $\epsilon > 0$, there exists a positive real number $\delta$ such that, for all $x$, if $ x > \delta$ then
		\begin{enumerate}
			\item $x^{10} + x > \epsilon $.
			\item $x^3 - x > \epsilon$.
		\end{enumerate}
\end{exercise}

\subsubsection*{Optional Problems}
\begin{exercise}
	\begin{enumerate}
		\item Prove that for every $\epsilon > 0$, for every real number $x > 0$, there exists a $\delta > 0$ such that, for all real numbers $y > 0$, if $ 0 < y - x < \delta$ then $y^2 - x^2 < \epsilon$.
		\item Prove that the following statement is false: For every $\epsilon > 0$, there exists a $\delta > 0$ such that, for every real number $x > 0$, for all real numbers $y > 0$, if $ 0 < y - x < \delta$ then $y^2 - x^2 < \epsilon$.
	\end{enumerate}
\end{exercise}


In this lab you will practice using the command window to carry out basic calculations, learn how to use the help function, MATLAB documentation, and record your input and output using the \verb`diary` function. First, follow the Windows Intructions to create the working directory.

%---------------------------------------------
\section{Arithmetics in MATLAB}
%---------------------------------------------
%---------------------------------------------
\begin{enumerate}[(1)]
    \item In the command window enter the command \verb`diary('lab_01_output.txt')` this will create a \verb`.txt` file. This will record all input and output in the command window until you type the command \verb`diary off`.
    \item Type the command \verb`beep off`. This will disable the sound that plays when there is an error in your code.
    \item Use the \verb`help functionName` to search for the relevant function in the problems below. Consider your `answer' to this question to be the output generated by the \verb`help` command.
    \begin{enumerate}
         \item What type of logarithm does the \verb`log(x)` function calculate?
         \item Does the MATLAB default setting calculate trigonometric functions in radians or degrees?
     \end{enumerate}
     \item \label{enum:4}Carry out the following calculations using normal math operators:
        \begin{enumerate}
          \item \label{enum:a} $2+5$
          \item \label{enum:b} $4^5$
          \item \label{enum:c} $7\cdot 6$
          \item \label{enum:d} $3/8$
          \item \label{enum:e} $54460-2342$
          \item \label{enum:f} $\cos(50^{\circ})$
          % built in function commands
          \item \label{enum:g} $\sqrt{4}$
          \item \label{enum:h} $\ln(3)$
          \item \label{enum:i} $\sin\left(\frac{\pi}{2}\right)$
          \item \label{enum:j} $e^{34}$
        \end{enumerate}
      \item For questions \ref{enum:a} -- \ref{enum:e}, you must also carry out each calculation using functional notation for each operation. Use the \verb`help` command and/or search the MATLAB documentation to find what the functional notation is for each operation.
    \item When you complete the above tasks enter the command \verb`diary off`. This will stop recording the input and output in the command window.
\end{enumerate}
Follow the Overleaf Instructions to set up an account, make a copy of the template, then upload \verb`lab_01_output.txt` to the folder \verb`src` on Overleaf. Next open \verb|body.tex| under the folder \verb|LaTeX|. In the last section of the report, you will reproduce the following using \LaTeX{}.
Once you finish, recompile, and submit the generated \verb|.pdf| file to WyoCourses.

%---------------------------------------------
\section{Basics of \LaTeX{}}
%---------------------------------------------
\subsection{Simplifying Fractions}
Consider the function
$$
f(x) = \frac{x^2 - 1}{x + 1}.
$$
To simplify this funciton we can factor the numerator and cancel like terms
\begin{align*}
    f(x)
       & = \frac{x^2 - 1}{x + 1}
    \\ & = \frac{(x - 1) (x + 1)}{x + 1}
    \\ & = x - 1.
\end{align*}

\subsection{Matrix}
A general $3 \times 3$ matrix $A$ has the form
$$
A =
\begin{bmatrix}
    1 & 2 & 3 \\
    4 & 5 & 6 \\
    7 & 8 & 9 \\
\end{bmatrix}.
$$

\subsection{The Millennium Prize Problems}
\emph{The Millennium Prize Problems} are seven problems in mathematics that were stated by the \textbf{Clay Mathematics Institute} on May 24, 2000. The problems are
\begin{enumerate}[(1)]
  \item Birch and Swinnerton-Dyer conjecture,
  \item Hodge conjecture,
  \item Navier–Stokes existence and smoothness,
  \item P versus NP problem,
  \item Poincaré conjecture,
  \item Riemann hypothesis,
  \item Yang–Mills existence and mass gap.
\end{enumerate}
\textsc{The Riemann zeta function} is defined for complex $s$ with real part greater than $1$ by the absolutely convergent infinite series
$$
\zeta(s) = \sum_{n=1}^{\infty} \frac{1}{n^s} = \frac{1}{1^s} + \frac{1}{2^s} + \frac{1}{3^s} + \cdots
$$
The practical uses of the Riemann hypothesis include many propositions known true under the Riemann hypothesis, and some that can be shown to be equivalent to the Riemann hypothesis:
\begin{itemize}
  \item Distribution of prime numbers,
  \item Growth of arithemtic functions,
  \item Large prime gap conjecture,
  \item Criteria equivalent to the Riemann hypothesis.
\end{itemize}