\chapter{Arithmetic}
\label{numberchapter}
\index{number}

This chapter describes Scheme's libraries for more specialized
numerical operations: fixnum and flonum arithmetic, as well as bitwise
operations on exact integer objects.

\section{Bitwise operations}

A number of procedures operate on the binary two's-complement
representations of exact integer objects: Bit positions within an
exact integer object are counted from the right, i.e.\ bit 0 is the
least significant bit.  Some procedures allow extracting \defining{bit
  fields}, i.e., number objects representing subsequences of the
binary representation of an exact integer object.  Bit fields are
always positive, and always defined using a finite number of bits.

\section{Fixnums}
\label{fixnumssection}

Every implementation must define its fixnum range as a closed
interval
%
\begin{displaymath}
[-2^{w-1}, 2^{w-1} - 1]
\end{displaymath}
%
such that $w$ is a (mathematical) integer $w \geq 24$.  Every
mathematical integer within an implementation's fixnum range must
correspond to an exact integer object that is representable within the
implementation.
A fixnum is an exact integer object whose value lies within this
fixnum range.

This section describes the \defrsixlibrary{arithmetic fixnums} library,
which defines various operations on fixnums.
Fixnum operations perform integer arithmetic on their fixnum
arguments, but raise an exception with condition type
{\cf\&implementation-restriction} if the result is not a fixnum.

This section uses \var{fx}, \vari{fx}, \varii{fx}, etc., as parameter
names for arguments that must be fixnums.

\begin{entry}{%
\rproto{fixnum?}{ obj}{procedure}}

Returns \schtrue{} if \var{obj} is an exact
integer object within the fixnum range, \schfalse{} otherwise.
\end{entry}

\begin{entry}{%
\rproto{fixnum-width}{}{procedure}
\rproto{least-fixnum}{}{procedure}
\rproto{greatest-fixnum}{}{procedure}}

These procedures return $w$,
$-2^{w-1}$ and $2^{w-1} - 1$: the
width, minimum and the maximum value of the fixnum range, respectively.
\end{entry}

\begin{entry}{%
\proto{fx=?}{ \vari{fx} \varii{fx} \variii{fx} \dotsfoo}{procedure}
\proto{fx>?}{ \vari{fx} \varii{fx} \variii{fx} \dotsfoo}{procedure}
\proto{fx<?}{ \vari{fx} \varii{fx} \variii{fx} \dotsfoo}{procedure}
\proto{fx>=?}{ \vari{fx} \varii{fx} \variii{fx} \dotsfoo}{procedure}
\proto{fx<=?}{ \vari{fx} \varii{fx} \variii{fx} \dotsfoo}{procedure}}

These procedures return \schtrue{} if their arguments are (respectively):
equal, monotonically increasing, monotonically decreasing,
monotonically nondecreasing, or monotonically nonincreasing,
\schfalse{} otherwise.
\end{entry}

\begin{entry}{%
\proto{fxzero?}{ fx}{procedure}
\proto{fxpositive?}{ fx}{procedure}
\proto{fxnegative?}{ fx}{procedure}
\proto{fxodd?}{ fx}{procedure}
\proto{fxeven?}{ fx}{procedure}}

These numerical predicates test a fixnum for a particular property,
returning \schtrue{} or \schfalse{}.  The five properties tested by
these procedures are: whether the number object is zero, greater than zero,
less than zero, odd, or even.
\end{entry}

\begin{entry}{%
\proto{fxmax}{ \vari{fx} \varii{fx} \dotsfoo}{procedure}
\proto{fxmin}{ \vari{fx} \varii{fx} \dotsfoo}{procedure}}

These procedures return the maximum or minimum of their arguments.
\end{entry}

\begin{entry}{%
\proto{fx+}{ \vari{fx} \varii{fx}}{procedure}
\proto{fx*}{ \vari{fx} \varii{fx}}{procedure}}

These procedures return the sum or product of their arguments,
provided that sum or product is a fixnum.  An exception with condition
type {\cf\&implementation-restriction} is raised if
that sum or product is not a fixnum.
\end{entry}

\begin{entry}{%
\proto{fx-}{ \vari{fx} \varii{fx}}{procedure}
\rproto{fx-}{ fx}{procedure}}

With two arguments, this procedure returns the difference
$\vari{fx}-\varii{fx}$, provided that difference is a fixnum.

With one argument, this procedure returns the additive
inverse of its argument, provided that integer object is a
fixnum.

An exception with condition type {\cf\&implementation-restriction} is raised if the
mathematically correct result of this procedure is not a fixnum.

\begin{scheme}
(fx- (least-fixnum))  \lev  \exception{\&implementation-restriction}%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{fxdiv-and-mod}{ \vari{fx} \varii{fx}}{procedure}
\proto{fxdiv}{ \vari{fx} \varii{fx}}{procedure}
\proto{fxmod}{ \vari{fx} \varii{fx}}{procedure}
\proto{fxdiv0-and-mod0}{ \vari{fx} \varii{fx}}{procedure}
\proto{fxdiv0}{ \vari{fx} \varii{fx}}{procedure}
\proto{fxmod0}{ \vari{fx} \varii{fx}}{procedure}}

\domain{\varii{Fx} must be nonzero.}
These procedures implement number-theoretic integer division and
return the results of the corresponding mathematical operations
specified in report section~\extref{report:integerdivision}{Integer division}.

\begin{scheme}
(fxdiv \vari{fx} \varii{fx})         \ev \(\vari{fx}~\mathrm{div}~\varii{fx}\)
(fxmod \vari{fx} \varii{fx})         \ev \(\vari{fx}~\mathrm{mod}~\varii{fx}\)
(fxdiv-and-mod \vari{fx} \varii{fx})     \lev \(\vari{fx}~\mathrm{div}~\varii{fx}, \vari{fx}~\mathrm{mod}~\varii{fx}\)\\\>\>; \textrm{two return values}
(fxdiv0 \vari{fx} \varii{fx})        \ev \(\vari{fx}~\mathrm{div}\sb{0}~\varii{fx}\)
(fxmod0 \vari{fx} \varii{fx})        \ev \(\vari{fx}~\mathrm{mod}\sb{0}~\varii{fx}\)
(fxdiv0-and-mod0 \vari{fx} \varii{fx})   \lev \(\vari{fx} \vari{fx}~\mathrm{div}\sb{0}~\varii{fx}, \vari{fx}~\mathrm{mod}\sb{0}~\varii{fx}\)\\\>\>; \textrm{two return values}%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{fx+/carry}{ \vari{fx} \varii{fx} \variii{fx}}{procedure}}

Returns the two fixnum results of the following computation:
%
\begin{scheme}
(let* ((s (+ \vari{fx} \varii{fx} \variii{fx}))
       (s0 (mod0 s (expt 2 (fixnum-width))))
       (s1 (div0 s (expt 2 (fixnum-width)))))
  (values s0 s1))%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{fx-/carry}{ \vari{fx} \varii{fx} \variii{fx}}{procedure}}

Returns the two fixnum results of the following computation:
%
\begin{scheme}
(let* ((d (- \vari{fx} \varii{fx} \variii{fx}))
       (d0 (mod0 d (expt 2 (fixnum-width))))
       (d1 (div0 d (expt 2 (fixnum-width)))))
  (values d0 d1))%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{fx*/carry}{ \vari{fx} \varii{fx} \variii{fx}}{procedure}}

Returns the two fixnum results of the following computation:
\begin{scheme}
(let* ((s (+ (* \vari{fx} \varii{fx}) \variii{fx}))
       (s0 (mod0 s (expt 2 (fixnum-width))))
       (s1 (div0 s (expt 2 (fixnum-width)))))
  (values s0 s1))%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{fxnot}{ \var{fx}}{procedure}}

Returns the unique fixnum that is congruent
mod $2^w$ to the one's-complement of \var{fx}.
\end{entry}

\begin{entry}{%
\proto{fxand}{ \vari{fx} \dotsfoo}{procedure}
\proto{fxior}{ \vari{fx} \dotsfoo}{procedure}
\proto{fxxor}{ \vari{fx} \dotsfoo}{procedure}}

These procedures return the fixnum that is the bit-wise ``and'',
``inclusive or'', or ``exclusive or'' of the two's complement
representations of their arguments.  If they are passed only one
argument, they return that argument.  If they are passed no arguments,
they return the fixnum (either $-1$ or $0$) that acts as identity for the
operation.
\end{entry}

\begin{entry}{%
\proto{fxif}{ \vari{fx} \varii{fx} \variii{fx}}{procedure}}

Returns the fixnum that is the bit-wise ``if'' of the two's complement
representations of its arguments, i.e.\ for each bit, if it is 1 in
\vari{fx}, the corresponding bit in \varii{fx} becomes the value of
the corresponding bit in the result, and if it is 0, the corresponding
bit in \variii{fx} becomes the corresponding bit in the value of the
result.  This is the fixnum result of the following computation:
\begin{scheme}
(fxior (fxand \vari{fx} \varii{fx})
       (fxand (fxnot \vari{fx}) \variii{fx}))%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{fxbit-count}{ \var{fx}}{procedure}}

If \var{fx} is non-negative, this procedure returns the
number of 1 bits in the two's complement representation of \var{fx}.
Otherwise it returns the result of the following computation:
%
\begin{scheme}
(fxnot (fxbit-count (fxnot \var{fx})))%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{fxlength}{ \var{fx}}{procedure}}

Returns the number of bits needed to represent \var{fx} if it is
positive, and the number of bits needed to represent {\cf (fxnot
  \var{fx})} if it is negative, which is the fixnum result of the
following computation:
\begin{scheme}
(do ((result 0 (+ result 1))
     (bits (if (fxnegative? \var{fx})
               (fxnot \var{fx})
               \var{fx})
           (fxarithmetic-shift-right bits 1)))
    ((fxzero? bits)
     result))%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{fxfirst-bit-set}{ \var{fx}}{procedure}}

Returns the index of the least significant $1$ bit in
the two's complement representation of \var{fx}.  If
\var{fx} is $0$, then $-1$ is returned.
%
\begin{scheme}
(fxfirst-bit-set 0)        \ev  -1
(fxfirst-bit-set 1)        \ev  0
(fxfirst-bit-set -4)       \ev  2%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{fxbit-set?}{ \vari{fx} \varii{fx}}{procedure}}

\domain{\varii{Fx} must be non-negative.}  The {\cf fxbit-set?} procedure returns
\schtrue{} if the \varii{fx}th bit is 1 in the two's complement
representation of \vari{fx}, and \schfalse{} otherwise.  This is the
result of the following computation:
%
\begin{scheme}
(if (fx>=? \varii{fx} (fx- (fixnum-width) 1))
    (fxnegative? \vari{fx})
    (not
      (fxzero?
         (fxand \vari{fx}
                (fxarithmetic-shift-left 1 \varii{fx})))))%
\end{scheme}
%
\end{entry}

\begin{entry}{%
\proto{fxcopy-bit}{ \vari{fx} \varii{fx} \variii{fx}}{procedure}}

\domain{\varii{Fx} must be non-negative and less than {\cf
  $w-1$}. \variii{Fx} must be 0 or
1.}  The {\cf fxcopy-bit} procedure returns the result of replacing
the \varii{fx}th bit of \vari{fx} by \variii{fx}, which is
the result of the following computation:
\begin{scheme}
(let* ((mask (fxarithmetic-shift-left 1 \varii{fx})))
  (fxif mask
        (fxarithmetic-shift-left \variii{fx} \varii{fx})
        \vari{fx}))%
\end{scheme}
%
\end{entry}

\begin{entry}{%
\proto{fxbit-field}{ \vari{fx} \varii{fx} \variii{fx}}{procedure}}

\domain{\varii{Fx} and \variii{fx} must be non-negative and less than
  $w$.  Moreover, \varii{fx} must be less than or
  equal to \variii{fx}.}  The {\cf fxbit-field} procedure returns the
number represented by the bits at the positions from \varii{fx} (inclusive) to
$\variii{fx}$ (exclusive), which is
the fixnum result of the following computation:
%
\begin{scheme}
(let* ((mask (fxnot
              (fxarithmetic-shift-left -1 \variii{fx}))))
  (fxarithmetic-shift-right (fxand \vari{fx} mask)
                            \varii{fx}))%
\end{scheme}
%
\end{entry}

\begin{entry}{%
\proto{fxcopy-bit-field}{ \vari{fx} \varii{fx} \variii{fx} \variv{fx}}{procedure}}

\domain{\varii{Fx} and \variii{fx} must be non-negative and less than
  $w$.  Moreover, \varii{fx} must be less than or
  equal to \variii{fx}.}  The {\cf fxcopy-bit-field} procedure returns
the result of replacing in \vari{fx} the bits at positions from
\varii{fx} (inclusive) to $\variii{fx}$ (exclusive) by the bits in
\variv{fx} from position 0 (inclusive) to position
$\variii{fx}-\varii{fx}$ (exclusive), which
is the fixnum result of the following computation:
\begin{scheme}
(let* ((to    \vari{fx})
       (start \varii{fx})
       (end   \variii{fx})
       (from  \variv{fx})
       (mask1 (fxarithmetic-shift-left -1 start))
       (mask2 (fxnot
               (fxarithmetic-shift-left -1 end)))
       (mask (fxand mask1 mask2))
       (mask3 (fxnot (fxarithmetic-shift-left
                       -1 (- end start)))))
  (fxif mask
        (fxarithmetic-shift-left (fxand from mask3)
                                 start)
        to))%
\end{scheme}

\begin{scheme}
(fxcopy-bit-field \sharpsign{}b0000001 2 5 \sharpsign{}b1111000) \lev 1
(fxcopy-bit-field \sharpsign{}b0000001 2 5 \sharpsign{}b0001111) \lev 29
(fxcopy-bit-field \sharpsign{}b0001111 2 5 \sharpsign{}b0001111) \lev 31%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{fxarithmetic-shift}{ \vari{fx} \varii{fx}}{procedure}}

\domain{The absolute value of \varii{fx} must be less than
$w$.}  If
%
\begin{scheme}
(floor (* \vari{fx} (expt 2 \varii{fx})))%
\end{scheme}
%
is a fixnum, then that fixnum is returned.  Otherwise an exception
with condition type {\cf\&implementation-\hp{}restriction} is
raised.
\end{entry}

\begin{entry}{%
\proto{fxarithmetic-shift-left}{ \vari{fx} \varii{fx}}{procedure}
\proto{fxarithmetic-shift-right}{ \vari{fx} \varii{fx}}{procedure}}

\domain{\varii{Fx} must be non-negative, and less than $w$.}
  The {\cf fxarithmetic-shift-left} procedure behaves the same as {\cf
  fxarithmetic-shift}, and {\cf (fxarithmetic-shift-right \vari{fx}
  \varii{fx})} behaves the same as {\cf (fxarithmetic-shift \vari{fx}
  (fx- \varii{fx}))}.
\end{entry}

\begin{entry}{%
\proto{fxrotate-bit-field}{ \vari{fx} \varii{fx} \variii{fx} \variv{fx}}{procedure}}

\domain{\varii{Fx}, \variii{fx}, and \variv{fx} must be non-negative
  and less than $w$.  \varii{Fx} must be less than or
  equal to \variii{fx}. \variv{Fx} must be less than or equal to the difference
between \variii{fx} and \varii{fx}.}  The {\cf fxrotate-bit-field}
procedure returns the result of cyclically permuting in \vari{fx} the
bits at positions from \varii{fx} (inclusive) to \variii{fx}
(exclusive) by \variv{fx} bits
towards the more significant bits, which is the result of the
following computation:
\begin{scheme}
(let* ((n     \vari{fx})
       (start \varii{fx})
       (end   \variii{fx})
       (count \variv{fx})
       (width (fx- end start)))
  (fxcopy-bit-field n start end
    (fxior
      (fxarithmetic-shift-left
        (fxbit-field n start (fx- end count))
        count)
      (fxarithmetic-shift-right
        (fxbit-field n start end)
        (fx- width count)))))%
\end{scheme}

\end{entry}

\begin{entry}{%
\proto{fxreverse-bit-field}{ \vari{fx} \varii{fx} \variii{fx}}{procedure}}

\domain{\varii{Fx} and \variii{fx} must be non-negative and less than
  $w$.  Moreover, \varii{fx} must be less than or
  equal to \variii{fx}.}  The {\cf fxreverse-bit-field} procedure
returns
the fixnum obtained from \vari{fx} by reversing the
order of the bits at positions from \varii{fx} (inclusive) to
\variii{fx} (exclusive).
\begin{scheme}
(fxreverse-bit-field \sharpsign{}b1010010 1 4)    \lev  88 ; \sharpsign{}b1011000%
\end{scheme}

\end{entry}

\section{Flonums}
\label{flonumssection}

This section describes the \defrsixlibrary{arithmetic flonums} library.

This section uses \var{fl}, \vari{fl}, \varii{fl}, etc., as
parameter names for arguments that must be flonums, and \var{ifl}
as a name for arguments that
must be integer-valued flonums, i.e., flonums for which the
{\cf integer-valued?} predicate returns true.

\begin{entry}{%
\proto{flonum?}{ obj}{procedure}}

Returns \schtrue{} if \var{obj} is a flonum, \schfalse{} otherwise.
\end{entry}

\begin{entry}{%
\proto{real->flonum}{ x}{procedure}}

Returns the best flonum representation of
\var{x}.

The value returned is a flonum that is numerically closest to the
argument.

\begin{note}
  If flonums are represented in binary floating point, then
  implementations should break ties by preferring
  the floating-point representation whose least significant bit is
  zero.
\end{note}
\end{entry}

\begin{entry}{%
\proto{fl=?}{ \vari{fl} \varii{fl} \variii{fl} \dotsfoo}{procedure}
\proto{fl<?}{ \vari{fl} \varii{fl} \variii{fl} \dotsfoo}{procedure}
\proto{fl<=?}{ \vari{fl} \varii{fl} \variii{fl} \dotsfoo}{procedure}
\proto{fl>?}{ \vari{fl} \varii{fl} \variii{fl} \dotsfoo}{procedure}
\proto{fl>=?}{ \vari{fl} \varii{fl} \variii{fl} \dotsfoo}{procedure}}

These procedures return \schtrue{} if their arguments are (respectively):
equal, monotonically increasing, monotonically nondecreasing,
monotonically decreasing, or monotonically nonincreasing,
\schfalse{} otherwise.  These
predicates must be transitive.

\begin{scheme}
(fl=? +inf.0 +inf.0)           \ev  \schtrue{}
(fl=? -inf.0 +inf.0)           \ev  \schfalse{}
(fl=? -inf.0 -inf.0)           \ev  \schtrue{}
(fl=? 0.0 -0.0)                \ev  \schtrue{}
(fl<? 0.0 -0.0)                \ev  \schfalse{}
(fl=? +nan.0 \var{fl})               \ev  \schfalse{}
(fl<? +nan.0 \var{fl})               \ev  \schfalse{}%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{flinteger?}{ fl}{procedure}
\proto{flzero?}{ fl}{procedure}
\proto{flpositive?}{ fl}{procedure}
\proto{flnegative?}{ fl}{procedure}
\proto{flodd?}{ ifl}{procedure}
\proto{fleven?}{ ifl}{procedure}
\proto{flfinite?}{ fl}{procedure}
\proto{flinfinite?}{ fl}{procedure}
\proto{flnan?}{ fl}{procedure}}

These numerical predicates test a flonum for a particular property,
returning \schtrue{} or \schfalse{}.
The {\cf flinteger?} procedure tests whether the number object is an integer,
{\cf flzero?} tests whether
it is {\cf fl=?} to zero, {\cf flpositive?} tests whether it is greater
than zero, {\cf flnegative?} tests whether it is less
than zero, {\cf flodd?} tests whether it is odd,
{\cf fleven?} tests whether it is even,
{\cf flfinite?} tests whether it is not an infinity and not a NaN,
{\cf flinfinite?} tests whether it is an infinity, and
{\cf flnan?} tests whether it is a NaN.

\begin{scheme}
(flnegative? -0.0)   \ev \schfalse{}
(flfinite? +inf.0)   \ev \schfalse{}
(flfinite? 5.0)      \ev \schtrue{}
(flinfinite? 5.0)    \ev \schfalse{}
(flinfinite? +inf.0) \ev \schtrue{}%
\end{scheme}

\begin{note}
{\cf (flnegative? -0.0)} must return \schfalse{},
else it would lose the correspondence with
{\cf (fl<? -0.0 0.0)}, which is \schfalse{}
according to IEEE 754~\cite{IEEE}.
\end{note}
\end{entry}

\begin{entry}{%
\proto{flmax}{ \vari{fl} \varii{fl} \dotsfoo}{procedure}
\proto{flmin}{ \vari{fl} \varii{fl} \dotsfoo}{procedure}}

These procedures return the maximum or minimum of their arguments.
They always return a NaN when one or more of the arguments is a NaN.
\end{entry}

\begin{entry}{%
\proto{fl+}{ \vari{fl} \dotsfoo}{procedure}
\proto{fl*}{ \vari{fl} \dotsfoo}{procedure}}

These procedures return the flonum sum or product of their flonum
arguments.  In general, they should return the flonum that best
approximates the mathematical sum or product.  (For implementations
that represent flonums using IEEE binary floating point, the
meaning of ``best'' is defined by the IEEE standards.)

\begin{scheme}
(fl+ +inf.0 -inf.0)      \ev  +nan.0
(fl+ +nan.0 \var{fl})          \ev  +nan.0
(fl* +nan.0 \var{fl})          \ev  +nan.0%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{fl-}{ \vari{fl} \varii{fl} \dotsfoo}{procedure}
\rproto{fl-}{ fl}{procedure}
\proto{fl/}{ \vari{fl} \varii{fl} \dotsfoo}{procedure}
\rproto{fl/}{ fl}{procedure}}

With two or more arguments, these procedures return the flonum
difference or quotient of their flonum arguments, associating to the
left.  With one argument, however, they return the additive or
multiplicative flonum inverse of their argument.  In general, they
should return the flonum that best approximates the mathematical
difference or quotient.  (For implementations that represent flonums
using IEEE binary floating point, the meaning of ``best'' is
reasonably well-defined by the IEEE standards.)

\begin{scheme}
(fl- +inf.0 +inf.0)      \ev  +nan.0%
\end{scheme}

For undefined quotients, {\cf fl/} behaves as specified by the
IEEE standards:

\begin{scheme}
(fl/ 1.0 0.0)  \ev +inf.0
(fl/ -1.0 0.0) \ev -inf.0
(fl/ 0.0 0.0)  \ev +nan.0%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{flabs}{ fl}{procedure}}

Returns the absolute value of \var{fl}.
\end{entry}

\begin{entry}{%
\proto{fldiv-and-mod}{ \vari{fl} \varii{fl}}{procedure}
\proto{fldiv}{ \vari{fl} \varii{fl}}{procedure}
\proto{flmod}{ \vari{fl} \varii{fl}}{procedure}
\proto{fldiv0-and-mod0}{ \vari{fl} \varii{fl}}{procedure}
\proto{fldiv0}{ \vari{fl} \varii{fl}}{procedure}
\proto{flmod0}{ \vari{fl} \varii{fl}}{procedure}}

These procedures implement number-theoretic integer division and
return the results of the corresponding mathematical operations
specified in report section~\extref{report:integerdivision}{Integer division}.
In the cases where the
mathematical requirements in section~\extref{report:integerdivision} cannot be
satisfied by any number object, either an exception is raised with
condition type {\cf\&implementation-restriction}, or unspecified
flonums (one for {\cf fldiv} {\cf flmod}, {\cf fldiv0} and {\cf
  flmod0}, two for {\cf fldiv-and-mod} and {\cf fldiv0-and-mod0}) are
returned.

\begin{scheme}
(fldiv \vari{fl} \varii{fl})         \ev \(\vari{fl}~\mathrm{div}~\varii{fl}\)
(flmod \vari{fl} \varii{fl})         \ev \(\vari{fl}~\mathrm{mod}~\varii{fl}\)
(fldiv-and-mod \vari{fl} \varii{fl})     \lev \(\vari{fl}~\mathrm{div}~\varii{fl}, \vari{fl}~\mathrm{mod}~\varii{fl}\)\\\>\>; \textrm{two return values}
(fldiv0 \vari{fl} \varii{fl})        \ev \(\vari{fl}~\mathrm{div}_0~\varii{fl}\)
(flmod0 \vari{fl} \varii{fl})        \ev \(\vari{fl}~\mathrm{mod}_0~\varii{fl}\)
(fldiv0-and-mod0 \vari{fl} \varii{fl})   \lev \(\vari{fl}~\mathrm{div}_0~\varii{fl}, \vari{fl}~\mathrm{mod}_0~\varii{fl}\)\\\>\>; \textrm{two return values}%
\end{scheme}

\end{entry}

\begin{entry}{%
\proto{flnumerator}{ fl}{procedure}
\proto{fldenominator}{ fl}{procedure}}

These procedures return the numerator or denominator of \var{fl}
as a flonum; the result is computed as if \var{fl} was represented as
a fraction in lowest terms.  The denominator is always positive.  The
denominator of 0.0 is defined to be 1.0.
%
\begin{scheme}
(flnumerator +inf.0)           \ev  +inf.0
(flnumerator -inf.0)           \ev  -inf.0
(fldenominator +inf.0)         \ev  1.0
(fldenominator -inf.0)         \ev  1.0
(flnumerator 0.75)             \ev  3.0 ; \textrm{probably}
(fldenominator 0.75)           \ev  4.0 ; \textrm{probably}%
\end{scheme}

Implementations should implement following behavior:

\begin{scheme}
(flnumerator -0.0)             \ev -0.0%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{flfloor}{ fl}{procedure}
\proto{flceiling}{ fl}{procedure}
\proto{fltruncate}{ fl}{procedure}
\proto{flround}{ fl}{procedure}}

These procedures return integral flonums for flonum arguments that are
not infinities or NaNs.  For such arguments, {\cf flfloor} returns the
largest integral flonum not larger than \var{fl}.  The {\cf flceiling}
procedure
returns the smallest integral flonum not smaller than \var{fl}.
The {\cf fltruncate} procedure returns the integral flonum closest to \var{fl} whose
absolute value is not larger than the absolute value of \var{fl}.
The {\cf flround} procedure returns the closest integral flonum to \var{fl},
rounding to even when \var{fl} represents a number halfway between two integers.

Although infinities and NaNs are not integer objects, these procedures return
an infinity when given an infinity as an argument, and a NaN when
given a NaN:

\begin{scheme}
(flfloor +inf.0)                       \ev  +inf.0
(flceiling -inf.0)                     \ev  -inf.0
(fltruncate +nan.0)                    \ev  +nan.0%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{flexp}{ fl}{procedure}
\proto{fllog}{ fl}{procedure}
\rproto{fllog}{ \vari{fl} \varii{fl}}{procedure}
\proto{flsin}{ fl}{procedure}
\proto{flcos}{ fl}{procedure}
\proto{fltan}{ fl}{procedure}
\proto{flasin}{ fl}{procedure}
\proto{flacos}{ fl}{procedure}
\proto{flatan}{ fl}{procedure}
\rproto{flatan}{ \vari{fl} \varii{fl}}{procedure}}

These procedures compute the usual transcendental functions.
The {\cf flexp} procedure computes the base-$e$ exponential of \var{fl}.
The {\cf fllog} procedure with a single argument computes the natural logarithm of
\var{fl} (not the base ten logarithm); {\cf (fllog \vari{fl}
  \varii{fl})} computes the base-\varii{fl} logarithm of \vari{fl}.
The {\cf flasin}, {\cf flacos}, and {\cf flatan} procedures compute arcsine,
arccosine, and arctangent, respectively.  {\cf (flatan \vari{fl}
  \varii{fl})} computes the arc tangent of \vari{fl}/\varii{fl}.

See report
section~\extref{report:transcendentalfunctions}{Transcendental functions} for the underlying
mathematical operations.  In the event that these operations do not
yield a real result for the given arguments, the result may be a NaN,
or may be some unspecified flonum.

Implementations that use IEEE binary floating-point arithmetic
should follow the relevant standards for these procedures.

\begin{scheme}
(flexp +inf.0)                \ev +inf.0
(flexp -inf.0)                \ev 0.0
(fllog +inf.0)                \ev +inf.0
(fllog 0.0)                   \ev -inf.0
(fllog -0.0)                  \ev \unspecified\\\>; \textrm{if -0.0 is distinguished}
(fllog -inf.0)                \ev +nan.0
(flatan -inf.0)               \lev -1.5707963267948965\\\>; \textrm{approximately}
(flatan +inf.0)               \lev 1.5707963267948965\\\>; \textrm{approximately}%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{flsqrt}{ fl}{procedure}}

Returns the principal square root of \var{fl}. For $-0.0$,
{\cf flsqrt} should return $-0.0$; for other negative arguments,
the result may be a NaN or some unspecified flonum.

\begin{scheme}
(flsqrt +inf.0)               \ev  +inf.0
(flsqrt -0.0)                 \ev  -0.0%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{flexpt}{ \vari{fl} \varii{fl}}{procedure}}

\domain{Either \vari{fl} should be non-negative, or, if \vari{fl} is
  negative, \varii{fl} should be an integer object.}
The {\cf flexpt} procedure returns \vari{fl} raised to the power \varii{fl}.  If \vari{fl} is
negative and \varii{fl} is not an integer object, the result may be a
NaN, or may be some unspecified flonum.
If \vari{fl} and \varii{fl} are both zero, the result is
1.0.  If \vari{fl} is zero and \varii{fl} is positive, the result is zero.
If \vari{fl} is zero and \varii{fl} is negative, the result may be a NaN, or may be
some unspecified flonum.
\end{entry}

\begin{entry}{%
\ctproto{no-infinities}
\proto{make-no-infinities-violation}{}{procedure}
\proto{no-infinities-violation?}{ obj}{procedure}
\ctproto{no-nans}
\proto{make-no-nans-violation}{}{procedure}
\proto{no-nans-violation?}{ obj}{procedure}}

These condition types could be defined by the following code:

\begin{scheme}
(define-condition-type \&no-infinities
    \&implementation-restriction
  make-no-infinities-violation
  no-infinities-violation?)

(define-condition-type \&no-nans
    \&implementation-restriction
  make-no-nans-violation no-nans-violation?)%
\end{scheme}

These types describe that a program has executed an arithmetic
operations that is specified to return an infinity or a NaN,
respectively, on a Scheme implementation that is not able to represent
the infinity or NaN.  (See report section~\extref{report:infinitiesnanssection}{Representability of infinities and NaNs}.)
\end{entry}

\begin{entry}{%
\proto{fixnum->flonum}{ fx}{procedure}}

Returns a flonum that is numerically closest to \var{fx}.

\begin{note}
The result of this procedure may not be
numerically equal to \var{fx}, because the fixnum precision
may be greater than the flonum precision.
\end{note}
\end{entry}

\section{Exact bitwise arithmetic}
\label{exactsection}

This section describes the \defrsixlibrary{arithmetic bitwise}
library.  The exact bitwise arithmetic provides generic operations on
exact integer objects.  This section uses \var{ei}, \vari{ei}, \varii{ei}, etc.,
as parameter names that must be exact integer objects.

\begin{entry}{%
\proto{bitwise-not}{ ei}{procedure}}

Returns the exact integer object whose two's complement representation is the
one's complement of the two's complement representation of \var{ei}.
\end{entry}

\begin{entry}{%
\proto{bitwise-and}{ \vari{ei} \dotsfoo}{procedure}
\proto{bitwise-ior}{ \vari{ei} \dotsfoo}{procedure}
\proto{bitwise-xor}{ \vari{ei} \dotsfoo}{procedure}}

These procedures return the exact integer object that is the bit-wise
``and'', ``inclusive or'', or ``exclusive or'' of the two's complement
representations of their arguments.  If they are passed only one
argument, they return that argument.  If they are passed no arguments,
they return the integer object (either $-1$ or $0$) that acts as identity for
the operation.
\end{entry}

\begin{entry}{%
\proto{bitwise-if}{ \vari{ei} \varii{ei} \variii{ei}}{procedure}}

Returns the exact integer object that is the bit-wise ``if'' of the two's complement
representations of its arguments, i.e.\ for each bit, if it is 1 in
\vari{ei}, the corresponding bit in \varii{ei} becomes the value of
the corresponding bit in the result, and if it is 0, the corresponding
bit in \variii{ei} becomes the corresponding bit in the value of the
result.
This is the result of the following computation:
\begin{scheme}
(bitwise-ior (bitwise-and \vari{ei} \varii{ei})
             (bitwise-and (bitwise-not \vari{ei}) \variii{ei}))%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{bitwise-bit-count}{ ei}{procedure}}

If \var{ei} is non-negative, this procedure returns the number of
1 bits in the two's complement representation of \var{ei}.
Otherwise it returns the result of the following computation:
%
\begin{scheme}
(bitwise-not (bitwise-bit-count (bitwise-not \var{ei})))%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{bitwise-length}{ ei}{procedure}}

Returns the number of bits needed to represent \var{ei} if it is
positive, and the number of bits needed to represent {\cf (bitwise-not
  \var{ei})} if it is negative, which is the exact integer object that
is the result of the following computation:
\begin{scheme}
(do ((result 0 (+ result 1))
     (bits (if (negative? \var{ei})
               (bitwise-not \var{ei})
               \var{ei})
           (bitwise-arithmetic-shift bits -1)))
    ((zero? bits)
     result))%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{bitwise-first-bit-set}{ ei}{procedure}}

Returns the index of the least significant $1$
bit in the two's complement representation of \var{ei}.
If \var{ei} is $0$, then $-1$ is returned.
\begin{scheme}
(bitwise-first-bit-set 0)        \ev  -1
(bitwise-first-bit-set 1)        \ev  0
(bitwise-first-bit-set -4)       \ev  2%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{bitwise-bit-set?}{ \vari{ei} \varii{ei}}{procedure}}

\domain{\varii{Ei} must be non-negative.}
The {\cf bitwise-bit-set?} procedure returns
\schtrue{} if the \varii{ei}th bit is 1 in the two's complement
representation of \vari{ei}, and \schfalse{}
otherwise.  This is the result of the following computation:
\begin{scheme}
(not (zero?
       (bitwise-and
         (bitwise-arithmetic-shift-left 1 \varii{ei})
         \vari{ei})))%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{bitwise-copy-bit}{ \vari{ei} \varii{ei} \variii{ei}}{procedure}}

\domain{\varii{Ei} must be non-negative, and \variii{ei}
must be either $0$ or $1$.}
The {\cf bitwise-copy-bit} procedure returns the result of replacing
the \varii{ei}th bit of \vari{ei} by \variii{ei}, which is
the result of the following computation:
\begin{scheme}
(let* ((mask (bitwise-arithmetic-shift-left 1 \varii{ei})))
  (bitwise-if mask
            (bitwise-arithmetic-shift-left \variii{ei} \varii{ei})
            \vari{ei}))%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{bitwise-bit-field}{ \vari{ei} \varii{ei} \variii{ei}}{procedure}}

\domain{\varii{Ei} and \variii{ei} must be non-negative, and
  \varii{ei} must be less than or equal to \variii{ei}.}
The {\cf bitwise-bit-field} procedure returns the
number represented by the bits at the positions from \varii{ei}
(inclusive) to $\variii{ei}$ (exclusive), which is
the result of the following computation:
%
\begin{scheme}
(let ((mask
       (bitwise-not
        (bitwise-arithmetic-shift-left -1 \variii{ei}))))
  (bitwise-arithmetic-shift-right
    (bitwise-and \vari{ei} mask)
    \varii{ei}))%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{bitwise-copy-bit-field}{ \vari{ei} \varii{ei} \variii{ei} \variv{ei}}{procedure}}

\domain{\varii{Ei} and \variii{ei} must be non-negative,
and \varii{ei} must be less than or equal to \variii{ei}.}
The {\cf bitwise-copy-bit-field} procedure returns
the result of replacing in \vari{ei} the bits at positions from
\varii{ei} (inclusive) to $\variii{ei}$ (exclusive) by the bits in
\variv{ei} from position 0 (inclusive) to position
$\variii{ei}-\varii{ei}$ (exclusive), which
is the result of the following computation:
%
\begin{scheme}
(let* ((to    \vari{ei})
       (start \varii{ei})
       (end   \variii{ei})
       (from  \variv{ei})
       (mask1
         (bitwise-arithmetic-shift-left -1 start))
       (mask2
         (bitwise-not
           (bitwise-arithmetic-shift-left -1 end)))
       (mask (bitwise-and mask1 mask2)))
  (bitwise-if mask
              (bitwise-arithmetic-shift-left from
                                             start)
              to))%
\end{scheme}
\end{entry}

\begin{entry} {%
\proto{bitwise-arithmetic-shift}{ \vari{ei} \varii{ei}}{procedure}}

Returns the result of the following computation:
%
\begin{scheme}
(floor (* \vari{ei} (expt 2 \varii{ei})))%
\end{scheme}

Examples:
%
\begin{scheme}
(bitwise-arithmetic-shift -6 -1) \lev -3
(bitwise-arithmetic-shift -5 -1) \lev -3
(bitwise-arithmetic-shift -4 -1) \lev -2
(bitwise-arithmetic-shift -3 -1) \lev -2
(bitwise-arithmetic-shift -2 -1) \lev -1
(bitwise-arithmetic-shift -1 -1) \lev -1%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{bitwise-arithmetic-shift-left}{ \vari{ei} \varii{ei}}{procedure}
\proto{bitwise-arithmetic-shift-right}{ \vari{ei} \varii{ei}}{procedure}}

\domain{\varii{Ei} must be non-negative.}  The {\cf
  bitwise-\hp{}arithmetic-\hp{}shift-\hp{}left} procedure returns the same result as {\cf
  bitwise-arithmetic-shift}, and
\begin{scheme}
(bitwise-arithmetic-shift-right \vari{ei} \varii{ei})%
\end{scheme}
returns the same result as
\begin{scheme}
(bitwise-arithmetic-shift \vari{ei} (- \varii{ei}))\textrm{.}%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{bitwise-rotate-bit-field}{ \vari{ei} \varii{ei} \variii{ei} \variv{ei}}{procedure}}

\domain{\varii{Ei}, \variii{ei}, \variv{ei} must be non-negative,
\varii{ei} must be less than or equal to \variii{ei}.}
The {\cf bitwise-rotate-bit-field} procedure returns the result of cyclically permuting in \vari{ei} the
bits at positions from \varii{ei} (inclusive) to \variii{ei} (exclusive) by \variv{ei} bits
towards the more significant bits, which is the result of the
following computation:
%
\begin{scheme}
(let* ((n     \vari{ei})
       (start \varii{ei})
       (end   \variii{ei})
       (count \variv{ei})
       (width (- end start)))
  (if (positive? width)
      (let* ((count (mod count width))
             (field0
               (bitwise-bit-field n start end))
             (field1 (bitwise-arithmetic-shift-left
                       field0 count))
             (field2 (bitwise-arithmetic-shift-right
                       field0
                       (- width count)))
             (field (bitwise-ior field1 field2)))
        (bitwise-copy-bit-field n start end field))
      n))%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{bitwise-reverse-bit-field}{ \vari{ei} \varii{ei} \variii{ei}}{procedure}}

\domain{\varii{Ei} and \variii{ei} must be non-negative, and
  \varii{ei} must be less than or equal to \variii{ei}.}  The {\cf bitwise-reverse-bit-field} procedure returns
the result obtained from \vari{ei} by reversing the
order of the bits at positions from \varii{ei} (inclusive) to
\variii{ei} (exclusive).
\begin{scheme}
(bitwise-reverse-bit-field \sharpsign{}b1010010 1 4)   \lev  88 ; \sharpsign{}b1011000%
\end{scheme}
\end{entry}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "r6rs-lib"
%%% End:


%\section{nomenclature}

E $\equiv$ electric field in the random sample

$ {\cal E}  \equiv$ energy

$ \xi \equiv$ localization length

x/L $\equiv$ normalized system length

$x_o$/L $\equiv$ normalized defect position

L  $\equiv$ system length

$\omega \equiv$ frequency

$\omega_o \equiv$ resonant frequency

T  $\equiv$ transmission

$J_+ \equiv$ transmission flux

R  $\equiv$ reflection

$J_- \equiv$ reflection flux

A $\equiv$ forward propogating electric field coefficient

B $\equiv$ backward propogating electric field coefficient

n $\equiv$ refractive index for random-width layers

$\epsilon \equiv$ dielectric constant for random-width layers

$x_o \equiv$ defect position

$x_1 \equiv$ turning point position

$\lambda \equiv$ wavelength

$\psi _1 = \psi _{LR} \equiv$ wave in passive material, left-to-right orientation

$\psi _2 = \psi _{RL} \equiv$ wave in passive material, right-to-left orientation

$\tilde{ \psi} _1 = \tilde{\psi} _{LR} \equiv$ wave in active material, left-to-right orientation

$\tilde{ \psi} _2 = \tilde{\psi} _{RL} \equiv$ wave in active material, right-to-left orientation

$\alpha \equiv$ coefficent of $\psi _1 $ with respect to building $\tilde{\psi}$

$\beta \equiv$ coefficent of $\psi _2 $ with respect to building $\tilde{\psi}$

%%%% %%%%%%

W $\equiv$ width

L $\equiv$ length

E $\equiv$ electric field

c $\equiv$ speed of light

%%%% from quasi1d %%%%%%

N $\equiv$ number of open channels

$N_c \equiv$ number of closed channels

$\alpha \equiv$ scattering strength

$l_s \equiv$ scattering length

$\rho \equiv$ density

g $\equiv$ conductivity

$num_{scat} \equiv$ number of scatterers

n $\equiv$ mode number

c $\equiv$ speed of light in a vacuum

W $\equiv$ system width

L $\equiv$ system length

k $\equiv$ wave number

chunk size = self embedding length = number of scatters in a chunk

$\lambda \equiv$ wavelength

$\Gamma \equiv$ scatterer matrix

$\tilde{ \Gamma} _1 \equiv$ reduced scatterer matrix for the first scatterer, of size NxN

$\tilde{ \Gamma} ^{combined} \equiv$ reduced scatterer matrix after being combined

$\tilde{ \Gamma} _{NNC} ^{combined} \equiv$ reduced scatterer matrix with reduced factors being combined

$\hat{G} \equiv$ a matrix of size $2*(N+N_c)$x$2*(N+N_c)$ describing

$\hat{H} \equiv$ a matrix of size $2*(N+N_c)$x$2*(N+N_c)$ describing

$\hat{S} \equiv$ a matrix of size $2*(N+N_c)$x$2*(N+N_c)$ describing


\subsection{Sequence Characterization}

\frame{
  \frametitle{Sequence Characterization}

  \begin{multline*}
    \begin{array}{cccccccccc}
      \underbrace{aaa} & b & \underbrace{aa} & b & \underbrace{aa} & b & \underbrace{aaa} & b & \underbrace{aa} & b \\
      3 && 2 && 2 && 3 && 2 &
    \end{array}\\
    \begin{array}{cccccccc}
      \underbrace{aa} & b & \underbrace{aaa} & b & \underbrace{aa} & b & \underbrace{aaa} & \dots \\
      2 && 3 && 2 && 3 & \dots
    \end{array}
  \end{multline*}
}

\frame{
  \frametitle{Sequence Characterization}

  \begin{gather*}
    \begin{array}{cccccccc}
      3 & \underbrace{22} & 3 & \underbrace{22} & 3 & \underbrace{2} & 3 & \dots\\
      & 2 && 2 && 1 & \dots
    \end{array}
  \end{gather*}
}

\subsection{Algorithm}

\frame{
  \frametitle{Algorithm}

  \begin{align*}
    dx_n& = \bigcap_{i=0}^n \left( \frac{i}{1 + \sum_{j=0}^i n_j}, \frac{1}{-1 + \sum_{j=0}^i n_j} \right)\\
    \delta_n& = \bigcap_{i=0}^n \left( i-dx_{n, max} (1 + \sum_{j=0}^i n_j), i-dx_{n, min} (1 + \sum_{j=0}^i n_j) \right)
  \end{align*}
}


\chapter{Simplification strategies for coupled fundamental commutators}\label{app:jscheme_commutator_tricks}

For the coupled fundamental commutators,
the key to arriving at the simplest expressions
is to avoid unnecessary recoupling.
Recoupling,
where bra or ket couplings have to be broken and recreated
to relate matrix elements on both sides of the expression,
is unnecessary when symmetries of the matrix elements
or of the expression can be exploited
to bring the uncoupled expression into a form
where its coupled analog no longer requires recoupling.
Recoupling comes at the cost of an additional sum over an angular momentum
and the inclusion of a Wigner $6j$ or $9j$ symbol.
Thus avoiding unnecessary recoupling is essential for achieving better performance
for implementations of the coupled expressions.
In the following,
we discuss two strategies for avoiding recoupling
and apply them each to one example.

\section{Using antisymmetry of input operator matrix elements}

The uncoupled matrix elements are antisymmetric under exchange
of any two bra or ket indices.
This symmetry can easily be exploited to permute indices into a form
that minimizes recoupling.

Consider the $[\onebodyop{A}, \threebodyop{B}] \rightarrow \twobodyop{C}$ fundamental commutator.
The uncoupled expression for the matrix elements of $C$ is
\begin{equation}
    C_{ijkl} = \sum_{ab} (n_a - n_b) A_{ab} B_{bijakl}\,,
\end{equation}
and the coupled expression is
\begin{equation}
    \begin{split}
        C_{ijkl}^{J_C} &= (-1)^{j_i + j_j + j_k + j_l}
        \sum_{j_a} \sum_{(J_{B,3}, J_{B,pq}, J_{B,st})} \hat{J}_{B,pq}\hat{J}_{B,st}\hat{J}_{B,3}^2 \\
        &\quad\sum_{ab} (n_a \bar{n}_b - \bar{n}_a n_b)
        \sixj{j_j}{j_i}{J_C}{j_a}{J_{B,3}}{J_{B,pq}}
        \sixj{j_l}{j_k}{J_C}{j_a}{J_{B,3}}{J_{B,st}} \\
        &\quad A_{ab}^{j_a} B_{bijakl}^{(J_{B,3}, J_{B,pq}, J_{B,st})}\,.
    \end{split}
\end{equation}

The appearance of 6$j$ symbols is a result of
breaking the $bi$ coupling on the right to form the $ij$ coupling on the left
and breaking the $ak$ coupling on the right to form the $kl$ coupling on the left.
By exploiting antisymmetry in the uncoupled matrix elements of $B$,
we can arrive at the uncoupled expression
\begin{equation}
    C_{ijkl} = \sum_{ab} (n_a \bar{n}_b - \bar{n}_a n_b) A_{ab} B_{ijbkla}\,,
\end{equation}
which yields the coupled expression
\begin{equation}
    C_{ijkl}^{J_C} = \frac{1}{\hat{J}_{C}^2}
    \sum_{j_a}
    \sum_{J_{B,3}} \hat{J}_{B,3}^2
    \sum_{ab} (n_a \bar{n}_b - \bar{n}_a n_b)
    A_{ab}^{j_a} B_{ijbkla}^{(J_{B,3}, J_{C}, J_{C})}\,.
\end{equation}

\section{Using antisymmetry of output operator matrix elements}

As a reminder, in this work we typically work with
non-antisymmetrized expressions for the fundamental commutators,
so one can not simply permute the indices on the output matrix elements.
However, one can exploit that the output matrix elements
will at some point be antisymmetrized
and choose the term that contributes to the antisymmetrized result
with the minimal recoupling.

To make this clear, consider the following example:
Suppose the three-body uncoupled matrix elements $O_{pqrstu}$
are already antisymmetric in $stu$, but not in $pqr$.
Thus to get the final antisymmetrized matrix elements $O^{\text{as}}_{pqrstu}$,
one needs to evaluate
\begin{equation}\label{eq:antisymmetrize_short}
    O^{\text{as}}_{pqrstu} = \mathcal{A}_{pqr} O_{pqrstu}\,,
\end{equation}
with
\begin{equation}
    \mathcal{A}_{pqr} = \frac{1}{6} (1 + P_{prq} + P_{prq}^2) (1 - P_{pq})\,,
\end{equation}
where $P_{prq}$ cyclically permutes the indices $p$, $q$, and $r$
and $P_{pq}$ exchanges the indices $p$ and $q$
as discussed in Section~\ref{sec:jscheme_many_body_expressions}.

Writing Eq.~\eqref{eq:antisymmetrize_short} out, one gets
\begin{equation}\label{eq:antisymmetrize_long_normal}
    O^{\text{as}}_{pqrstu} = \frac{1}{6} (O_{pqrstu} + O_{qrpstu} + O_{rpqstu} - O_{qprstu} - O_{rqpstu} - O_{prqstu})\,.
\end{equation}
Now consider
\begin{equation}
    \overline{O}_{pqrstu} \equiv O_{qrpstu}\,.
\end{equation}
Antisymmetrizing $\overline{O}_{pqrstu}$ gives
\begin{equation}
    \overline{O}^{\text{as}}_{pqrstu} = \frac{1}{6} (\overline{O}_{pqrstu} + \overline{O}_{qrpstu} + \overline{O}_{rpqstu}
    - \overline{O}_{qprstu} - \overline{O}_{rqpstu} - \overline{O}_{prqstu})\,,
\end{equation}
which one can rewrite in terms of the original matrix elements $O_{pqrstu}$:
\begin{equation}\label{eq:antisymmetrize_long_alternate}
    \overline{O}^{\text{as}}_{pqrstu} = \frac{1}{6} (O_{qrpstu} + O_{rpqstu} + O_{pqrstu}
    - O_{rqpstu} - O_{prqstu} - O_{qprstu})\,.
\end{equation}
One can quickly see that Eqs.~\eqref{eq:antisymmetrize_long_normal} and~\eqref{eq:antisymmetrize_long_alternate}
contain the exact same terms, making them equal.
Thus if $O_{pqrstu}$ is given by some many-body expression,
one can permute the indices $p$, $q$, and $r$ on the right-hand (or left-hand) side of the expression,
including an overall minus sign if the permutation is odd,
and know that it will give the same result after antisymmetrization.

Again, it is useful to consider a concrete application to illustrate how this strategy can be applied.
Consider the $[\twobodyop{A}, \twobodyop{B}] \rightarrow \threebodyop{C}$ fundamental commutator.
The uncoupled non-antisymmetrized expression is
\begin{equation}
    C_{ijklmn} = 9 \sum_{a} (A_{ijla} B_{akmn} - B_{ijla} A_{akmn})\,.
\end{equation}
which gives the coupled expression
\begin{equation}\label{eq:comm_223_bad}
    \begin{split}
        C_{ijklmn}^{(J_{C,3}, J_{C,ij}, J_{C,lm})} & =
        9 \hat{J}_{C,ij} \hat{J}_{C,lm}
        (-1)^{j_m + j_n}
        \sum_{J_{mn}}
        \hat{J}_{mn}^2
        \sum_{a} (-1)^{j_a + j_k} \\
        & \quad \quad \times
        \sixj{j_k}{j_a}{J_{mn}}{j_{l}}{J_{C,3}}{J_{C,ij}}
        \sixj{j_l}{j_m}{J_{C,lm}}{j_{n}}{J_{C,3}}{J_{mn}} \\
        & \quad \quad \times \left(
        A_{ijla}^{J_{C,ij}} B_{akmn}^{J_{mn}}
        - B_{ijla}^{J_{C,ij}} A_{akmn}^{J_{mn}}
        \right).
    \end{split}
\end{equation}
One can immediately see that
the $mn$ coupling on the right in $B$ and $A$ needs to be broken up
to form the $lm$ coupling on the left,
leading to some of the recoupling.

However, the uncoupled expression
\begin{equation}
    D_{ijklmn} \equiv 9 \sum_{a} (A_{ijna} B_{aklm} - B_{ijna} A_{aklm})
\end{equation}
has the indices $l$, $m$, and $n$ on the right-hand side permuted cyclically,
and it will no longer have this recoupling problem when the expression is coupled.
The antisymmetrized matrix elements of $C$ and $D$ are equal,
so one might as well simply calculate $D$ instead of $C$ and antisymmetrize that.
The coupled expression for $D$ is
\begin{equation}\label{eq:comm_223_good}
    \begin{split}
        D_{ijklmn}^{(J_{C,3}, J_{C,ij}, J_{C,lm})} & =
        -9 (-1)^{J_{C,lm}} \hat{J}_{C,ij} \hat{J}_{C,lm}
        \sum_{a} (-1)^{j_a + j_k}
        \sixj{j_k}{j_a}{J_{C,lm}}{j_{n}}{J_{C,3}}{J_{C,ij}} \\
        & \quad \quad \times \left(
        A_{ijna}^{J_{C,ij}} B_{aklm}^{J_{C,lm}}
        - B_{ijna}^{J_{C,ij}} A_{aklm}^{J_{C,lm}}
        \right).
    \end{split}
\end{equation}
The final $6j$ symbol cannot be avoided
as it forms the three-body angular-momentum coupling
that is not present on the right-hand side.
Comparing Eq.~\eqref{eq:comm_223_bad} and Eq.~\eqref{eq:comm_223_good},
one can see the simplification afforded by carefully considering
which quantity one actually wants to calculate.


\documentclass[a4paper]{scrartcl}

% Text
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}

% Math
\usepackage{amsmath, amssymb}
\usepackage{mathtools}
\usepackage{bm}
	\newcommand{\vv}[1]{\ensuremath{\bm{#1}}}
	\newcommand\lphi{\ensuremath{\phi^{\text{left}}}}
	\newcommand\lH{\ensuremath{H^{\text{left}}}}
	\newcommand\lh{\ensuremath{h^{\text{left}}}}

	\newcommand\rphi{\ensuremath{\phi^{\text{right}}}}
	\newcommand\rH{\ensuremath{H^{\text{right}}}}
	\newcommand\rh{\ensuremath{h^{\text{right}}}}

	\newcommand\R{\ensuremath{\mathbb{R}}}
	\newcommand\Z{\ensuremath{\mathbb{Z}}}
	\DeclareMathOperator\supp{supp}
	\DeclareMathOperator\spn{span}

% Set
% \given is part of the syntax, make sure it exists
% \providecommand\given{}
\newcommand\given{\:\vert\:}
\newcommand\SetSymbol[1][]{
	\nonscript\: #1 \vert \nonscript\:
	\mathopen{} % make - etc behave as sign
	\allowbreak % in text, allow line break after the \given
}
\DeclarePairedDelimiterX\Set[1]{\{}{\}}{
	% Change is local to \Set
	\renewcommand\given{\SetSymbol[\delimsize]}
	#1
}

\usepackage{graphicx}
\usepackage{asymptote}

\usepackage[colorlinks=true]{hyperref}

\usepackage{cleveref}

\usepackage{biblatex}
\bibliography{literature.bib}

\title{Daubechies wavelets}
\author{Robert Dahl Jacobsen}

\begin{document}

\maketitle

The purpose of the Julia package \href{https://github.com/robertdj/IntervalWavelets.jl}{IntervalWavelets.jl} is to compute ordinary Daubechies scaling functions (see e.g.\ \cite{Mallat:2009}) and the moment-preserving boundary scaling functions from \cite{Cohen:Daubechies:Vial:1993}.
We rely on the recursive approach of \cite{Strang:1989}.
In this note I summarize these methods with all the details used in the implementation.

\section{Interior scaling functions}
\label{sec:internal}

A Daubechies scaling function $\phi$ and associated wavelet $\psi$ with $p$ vanishing moments are defined by a filter $\{h_k\}_{k\in\Z}$.
The filter, scaling function and wavelet have supports of the same lengths and we know from \cite[Theorem 7.5]{Mallat:2009} that if $\supp \{h_k\}_k = [N_1, N_2]$, then
\begin{equation*}
	\supp\phi = [N_1, N_2],
	\quad
	\supp\psi = \Bigl[\frac{N_1 - N_2 + 1}2, \frac{N_2 - N_1 + 1}2\Bigr].
\end{equation*}
It is customary to let $N_1 = 0$ and $N_2 = 2p - 1$.
However, when constructing the boundary scaling functions we have $N_1 = -p+1$ and $N_2 = p$.

The scaling function satisfies the dilation equation
\begin{equation}
	\label{eq:internal_scaling_function_definition}
	\phi(x)
	= \sqrt2 \sum_{k=0}^{2p-1} h_k \phi_k(2x)
	= \sqrt2 \sum_{k=0}^{2p-1} h_k \phi(2x - k).
\end{equation}
For $p\geq2$, $\phi$ is continuous and hence zero at the endpoints of the support.
These properties allow us to compute $\phi$ at the integer values (in the support).
As an example, for $p = 3$:
\begin{align*}
	\phi(1)
    & = \sqrt2 \bigl(h_1\phi(1) + h_0\phi(2)\bigr),
	\\
	\phi(2)
    & = \sqrt2 \bigl(h_4\phi(1) + h_3\phi(2) + h_2\phi(3) + h_1\phi(4)\bigr),
	\\
	\phi(3)
    & = \sqrt2 \bigl(h_5\phi(1) + h_4\phi(2) + h_3\phi(3) + h_2\phi(4)\bigr),
	\\
	\phi(4)
    & = \sqrt2 \bigl(h_5\phi(3) + h_4\phi(4)\bigr).
\end{align*}
In matrix form, we have an eigenvalue problem:
\begin{equation*}
	\begin{bmatrix}
		\phi(1) \\ \phi(2) \\ \phi(3) \\ \phi(4)
	\end{bmatrix}
	=
	\sqrt2
	\begin{bmatrix}
		h_1 & h_0 & 0 & 0
		\\
		h_3 & h_2 & h_1 & h_0
		\\
		h_5 & h_4 & h_3 & h_2
		\\
		0 & 0 & h_5 & h_4
	\end{bmatrix}
	\begin{bmatrix}
		\phi(1) \\ \phi(2) \\ \phi(3) \\ \phi(4)
	\end{bmatrix}.
\end{equation*}
For a general support $[N_1, N_2]$ the $(i,j)$'th entry is $\sqrt2 h_{2i-j+N_1}$ and the vector $\vv\phi = [\phi(n)]_{n=1}^4$ is an eigenvector of the eigenvalue 1.
This eigenspace is one-dimensional, so the only question is how to scale $\vv\phi$.
From e.g.\ \cite[page 69]{Cohen:Daubechies:Vial:1993} we know that
\begin{equation*}
	\sum_{k\in\mathbb{Z}} \phi(k)
	= \sum_{k=0}^{2p-1} \phi(k)
	= 1.
\end{equation*}

From the function values at the integers we can compute the function values at the half-integers using \eqref{eq:internal_scaling_function_definition}.
As an example,
\begin{equation*}
	\phi\Bigl(\frac32\Bigr)
    = \sqrt2 \bigl(h_0 \phi(3) + h_1 \phi(2) + h_2 \phi(1)\bigr).
\end{equation*}
This process can be repeated to recursively yield $\phi(k/2^R)$, for all integers $k$ and positive integers $R$.

Note that the filter $\{h_k\}_k$ defining the scaling function is not unique.
In \cref{fig:Daubechies4} is shown the usual, minimum-phase Daubechies 4 scaling function along with Daubechies 'symmlet'/linear phase scaling function used in \cref{sec:boundary_Daubechies} and \cite{Cohen:Daubechies:Vial:1993} -- see e.g.\ \cite[Section 7.2.3]{Mallat:2009}.

\begin{figure}
	\centering
	\includegraphics[scale=0.5]{interior}
	\caption{The usual minimum-phase Daubechies 4 scaling function (black) and the symmlet version (red).}
	\label{fig:Daubechies4}
\end{figure}

\section{Boundary scaling functions}
\label{sec:boundary_Daubechies}

The moment preserving Daubechies boundary scaling functions were introduced in \cite{Cohen:Daubechies:Vial:1993} and are also described in \cite{Mallat:2009} (albeit with some indexing errors).

An important difference between the internal and boundary scaling functions is that the left (right) boundary scaling functions are \emph{not} continuous at the left (right) endpoint of their support.

As in \cref{sec:internal}, the dilation equations defining the boundary scaling functions can yield function values at all dyadic rationals once we have the function values at the integers (in the support).
In the subsequent sections the focus is therefore on how to compute these functions at the integers.

The filters used for the boundary scaling functions are available at \url{https://services.math.duke.edu/~ingrid/publications/54.txt} and \url{http://numerical.recipes/contrib}.

\subsection{Left boundary scaling functions}

Let $p$ denote the number of vanishing moments and $\phi$ be the interior symmlet Daubechies scaling function associated with the wavelet with $p$ vanishing moments translated such that $\supp\phi = [-p+1, p]$.

We want a family of functions satisfying a multiresolution analysis on $L^2([0,\infty))$ or, equivalently, a dilation equation like \cref{eq:internal_scaling_function_definition}.
The starting point is $\{\phi_k\}_{k\geq 0}$.
The functions $\phi_k$ with $\supp\phi_k \subseteq [0,\infty)$ do not need any alteration.
But the $\phi_k$ with $\supp\phi_k \cap (-\infty,0) \neq \emptyset$ (i.e., with $0\leq k < p-1$) must be replaced with a corresponding $\lphi_k$.
It turns out that we should also replace $\phi_{p-1}$ with $\lphi_{p-1}$ in order to keep the number of vanishing moments even though $\supp\phi_{p-1} = [0,2p-1]$.
The boundary scaling functions are constructed such that $\supp\bigl(\lphi_k\bigr) = [0,p+k]$.

The relevant counterpart to the dilation equation \cref{eq:internal_scaling_function_definition} for interior scaling functions is
\begin{align}
	\lphi_k(x)
	& = \sqrt2 \sum_{l=0}^{p-1} \lH_{k,l} \lphi_l(2x) + \sqrt2 \sum_{m=p}^{p+2k} \lh_{k,m} \phi_m(2x)
	\nonumber
	\\
	& = \sqrt2 \sum_{l=0}^{p-1} \lH_{k,l} \lphi_l(2x) + \sqrt2 \sum_{m=p}^{p+2k} \lh_{k,m} \phi(2x-m),
	\label{eq:left_scaling_function_definition}
\end{align}
for $0\leq k\leq p-1$, where $(\lH_{k,l})$ and $(\lh_{k,m})$ are filter coefficients.

For $x \neq 0$ we make use of the compact support.
Consider e.g.\ the case $p=2$ (where is $\phi$ is supported on $[-1,2]$, $\lphi_0$ is supported on $[0,2]$ and $\lphi_1$ is supported on $[0,3]$):
\begin{align*}
	\lphi_0(1)
    & = \sqrt2 \bigl( \lH_{0,1} \lphi_1(2) + \lh_{0,2} \phi(0) \bigr),
	\\
	\lphi_1(1)
    & = \sqrt2 \bigl( \lH_{1,1} \lphi_1(2) + \lh_{1,2} \phi(0) \bigr),
	\\
	\lphi_1(2)
    & = \sqrt2 \bigl( \lh_{1,3} \phi(1) + \lh_{1,4} \phi(0) \bigr).
\end{align*}
From Section \ref{sec:internal} we know how to calculate the internal scaling function and by starting with the largest values we can also compute the boundary scaling function.

The function value $\lphi_k(0)$ require special treatment.
For $x = 0$ \eqref{eq:left_scaling_function_definition} becomes
\begin{equation*}
	\lphi_k(0)
    = \sqrt2 \sum_{l=0}^{p-1} \lH_{k,l} \lphi_l(0).
\end{equation*}
These function values are therefore an eigenvector of the matrix with $(i,j)$'th entry $[\sqrt2 \lH_{i,j}]$.
To find the proper normalization of this eigenvector we can do as follows:
Let $\lphi_\ell(0) = y_\ell$ for $0\leq \ell < p$.
Computing an eigenvector as described we have $\lphi_\ell(0) = z_\ell = c y_\ell$ for some $c \in \R$.
We know that the left boundary scaling functions are a basis capable of reconstructing polynomials.
In particular, there exists $a_0, \ldots, a_{p-1}$ such that for all $x \in [0, 1]$ we have
\begin{equation*}
    \sum_{\ell=0}^{p-1} a_\ell \lphi_\ell(x) = 1.
\end{equation*}
On this interval all the interior scaling functions are zero and we only need the left boundary scaling functions.
The coefficients can be determined by choosing $p$ dyadic rationals $x_0, \ldots, x_{p-1}$ with $0 < x_k \leq 1$ and solving the linear equations
\begin{equation*}
    \sum_{\ell=0}^{p-1} a_\ell \lphi_\ell(x_k) = 1, \quad 0\leq k< p.
\end{equation*}
In order to have have the $p$ function values $\lphi_\ell(x_k)$ the iterative refinement must proceed until the resolution of the dyadic rationals is at least $R = \lceil \log_2 p \rceil$.
This gives us the constant $c$:
\begin{equation*}
    1 = \sum_{\ell=0}^{p-1} a_\ell \lphi_\ell(0)
    \quad\Rightarrow\quad
    c = \sum_{\ell=0}^{p-1} a_\ell z_\ell.
\end{equation*}

The four boundary scaling functions related to four vanishing moments are seen in \cref{fig:left_Daubechies4}.
There is a large resemblance between $\lphi_3$ and the symmlet scaling function in \cref{fig:Daubechies4} (here denoted $\phi_4$).

\begin{figure}
	\centering
	\includegraphics[scale=0.5]{left}
	\caption{The left boundary scaling function with 4 vanishing moments.}
	\label{fig:left_Daubechies4}
\end{figure}

\subsection{Right boundary scaling functions}

Let again $\phi$ denote the interior symmlet Daubechies scaling function and $p$ denote the number of vanishing moments of the associated wavelet.
The idea for the right boundary scaling functions is the same as for the the left:
We want a multiresolution analysis on $L^2((-\infty,0])$ by modifiying the interior scaling functions.
The $\phi_k$ with $\supp\phi_k \subset (-\infty,0)$ are unaltered, but those with $\supp\phi_k \cap [0, \infty) \neq \emptyset$ are replaced by a corresponding $\rphi_k$.
In conclusion, for $k=0,\ldots,p-1$, the right boundary scaling functions satisfies the dilation equations
\begin{equation}
	\label{eq:right_scaling_function_definition}
	\rphi_k(x)
	= \sqrt2 \sum_{l=0}^{p-1} \rH_{k,l} \rphi_l(2x) + \sqrt2 \sum_{m=p}^{p+2k} \rh_{k,m} \phi(2x+m+1),
\end{equation}
where $(\rH_{k,l})$ and $(\rh_{k,m})$ are filter coefficients.
The support of $\rphi_k$ is $[-p-k,0]$.

The four right boundary scaling functions related to four vanishing moments are seen in \cref{fig:right_Daubechies4}.

\begin{figure}
	\centering
	\includegraphics[scale=0.5]{right}
	\caption{The right boundary scaling function with 4 vanishing moments.}
	\label{fig:right_Daubechies4}
\end{figure}

\section{Scaling functions on an interval}

The left and right scaling functions are multiresolutions for the postive and negative halflines, respectively.
We can combine the three kind of scaling functions to obtain a multiresoltion for an interval.
Let us first recall a few properties of a multiresoltion analysis for $L^2(\R)$.
We use the notation
\begin{equation*}
    \phi_{J,k}(x) = 2^{J/2} \phi(2^J x - k)
\end{equation*}
and
\begin{equation*}
    V_J = \spn\Set{\phi_{J,k} \given k \in \Z}
\end{equation*}
Then
\begin{equation*}
    L^2(\R) = \overline{\bigcup_{J\in\Z} V_J}.
\end{equation*}

Initially we want a similar multiresoltion for the interval $[0, 1]$, that is, a relation of the form
\begin{equation*}
    L^2([0, 1]) = \overline{\bigcup_{J\in\Z} \widetilde V_J}
\end{equation*}
for suitably defined $\widetilde V_J$.
We index the basis functions in $\widetilde V_J$ as $\widetilde \phi_1, \ldots, \widetilde \phi_N$.
The first thing to notice is that all three kinds of scaling functions must be dilated such that their support is at most of length 1.
Just as in the classic multiresoltion we dilate with numbers of the form $2^J$.
Let $\tau_h$ deonte a translation operator, meaning that $(\tau_h f)(x) = f(x - h)$.
Define
\begin{equation*}
    \widetilde \phi_k(x) =
    \begin{dcases}
        \lphi_k(x), & 1\leq k\leq p,
        \\
        \tau_k \phi(x) & p < k \leq N - p,
        \\
        \tau_1 \rphi_k(x) & N - p < k \leq N.
    \end{dcases}
\end{equation*}
Then
\begin{equation*}
    \widetilde V_J = \spn\Set[\big]{\widetilde \phi_{J,k} \given 0\leq k\leq N},
    \quad
    \widetilde \phi_{J,k}(x) = 2^{J/2} \widetilde \phi_k(2^J x).
\end{equation*}
As an example of the support of the interval basis functions consider \cref{fig:support}.

\begin{figure}
    \centering
    \asyinclude{support.asy}
    \caption{The support of the interval scaling functions with $p = 2$ vanishing moments at scale $J = 4$ computed at dyadic rationals of resolution $R = 4$.}
    \label{fig:support}
\end{figure}

In \emph{IntervalWavelets} we want to evalute the interval scaling functions at the dyadic rationals of a predefined resolution $R$.
Note that we have an interaction between the scale and the resolution:
\begin{equation*}
    \widetilde\phi_{J,k}\biggl(\frac n{2^R}\biggr)
    = 2^{J/2} \widetilde \phi_k\biggl(2^J \frac n{2^R}\biggr)
    = 2^{J/2} \widetilde \phi_k\biggl(\frac n{2^{R - J}}\biggr).
\end{equation*}
This means that in order to evaluate $\widetilde\phi_{J,k}$ in dyadic rationals of resolution $R$ we only need to compute $\widetilde\phi$ in dyadic rationals of resolution $R - J$.

\printbibliography

\end{document}


\section{Graph Model of Set Theory}

\begin{itemize}

	\item Directed graphs: $ G = \pair{V}{A} $

%	\begin{itemize}
%
%		\item $ V $ is the set of vertices
%
%		\item $ A \subseteq V \times V $ is the set of ordered pairs of vertices representing arrows
%
%	\end{itemize}

	\item A graph is \textbf{well-founded} if it has no looping paths and no infinite descending paths

	\item A graph is \textbf{extensional} if for any $ v_0, v_1 $ such that $ v_0 $ has the same incoming arrows as $ v_1 $, $ v_0 = v_1 $

	\item Two graphs are isomorphic if there is a function (\textit{isomorphism}) $ \sigma $ between them such that:

	\begin{itemize}

		\item $ \sigma $ is a bijection (surjection + injection)

		\item $ vA_0 u \leftrightarrow \sigma(v) A_1 \sigma(u) $

	\end{itemize}

	\item An automorphism is an isomorphism between some graph and itself (the identity is a trivial one)

	\item $ G $ is a subgraph of $ G' $ if $ V \subseteq V' $ and $ v_0 A v_1 \leftrightarrow v_0 A' v_1 $ for all $ v_0, v_1 \in V $

	\item $ G $ is \textit{maximal} in some property $ \Phi $ if $ G $ possesses $ \Phi $ and there exists no graph $ G' $ such that:

	\begin{itemize}

		\item $ G' $ possesses $ \Phi $; and
		\item $ G $ is a proper subgraph of $ G' $

	\end{itemize}

	\item Let $ G $ be a \textit{maximal} well-founded graph with no non-trivial automorphisms

	\item Equivalently, $ G $ is a maximal well-founded graph which is extensional

	\item $ G $ is then an \textit{intended model} of Set Theory

\end{itemize}

\section{First Order Logic}

\begin{itemize}

	\item Logical symbols: $ \lnot $, $ \land $, $ \lor $, $ \implies $, $ \iff $, $ \exists $, $ \forall $, $ ( $, $ ) $

	\item Non-logical symbols: constant symbols ($ a, b, c $), relation symbols ($ P, Q, R $), function symbols ($ f, g, h $)

	\item Language: $ \Lang = \set{a, b, c, \dots, P, Q, R, \dots, f, g, h, \dots}$

	\item Individual variables will be denoted $ v_1, v_2, \dots $

	\item Metavariables / arbitrary variables will be denoted $ x, y, z, \dots $

	\item The set of terms, $ Term $ is defined recursively:

	\begin{itemize}

		\item If $ t $ is a constant symbol or individual variable, $ t $ is a term

		\item If $ t_1, \dots, t_n $ are terms and $ f $ is a function symbol with arity $ n $, $ f(t_1, \dots, t_n) $ is a term

		\item Nothing else is a term

	\end{itemize}

	\item A string $ \varphi $ is an \textit{atom} if $ \varphi = Rt_1, \dots, t_n $ where $ t_1, \dots, t_n $ are terms and $ R $ is a relation with arity $ n $

	\item If $ t $ is a term with no variables occurring in it, $ t $ is a \textit{closed term}

	\item The set of Well Formed Formulae $ WFF $ is defined recursively, with atoms as the base case

	\begin{itemize}
		\item If $ \varphi $ consists of a combination of logical operators over well formed formulae, $ \varphi \in WFF $
	\end{itemize}

%	\item The set of Well Formed Formulae, $ WFF $ is defined recursively:
%
%	\begin{itemize}
%
%		\item If $ \varphi $ is an atom, $ \varphi \in WFF $
%
%		\item If $ \varphi \in \set{\lnot \psi, \forall x \psi, \exists x \psi} $ where $ \psi \in WFF $ and $ x $ is a variable, $ \varphi \in WFF $
%
%		\item If $ \varphi \in \set{(\psi \land \chi), (\psi \lor \chi), (\psi \implies \chi)} $ where $ \psi, \chi \in WFF $, $ \varphi \in WFF $
%
%		\item Nothing else is in $ WFF $
%
%	\end{itemize}

	\item $ x $ is \textit{free} in $ \varphi \in WFF $ if:

	\begin{itemize}

		\item If $ \varphi = R t_1, \dots, t_n $ and $ x = t_i $ for some $ i $

		\item If $ \varphi $ consists of logical operators over well formed formula, where $ x $ is free in at least one

		\item If $ \varphi $ is a quantification over a formula where $ x $ is free, and $ x $ is not the bound variable

	\end{itemize}

%	\item The variable $ x $ `being free' in $ \varphi \in WFF $ is defined recursively:
%
%	\begin{itemize}
%
%		\item If $ \varphi = R t_1, \dots, t_n $ and $ x = t_i $ for some $ i $, then $ x $ is free in $ \varphi $
%
%		\item If $ \varphi \in \set{\forall y \psi, \exists y \psi} $ and $ x $ is free in $ \psi $ and $ x \ne y $, $ x $ is free in $ \varphi $
%
%		\item If $ \varphi \in \set{\lnot \psi, (\psi \land \chi), (\psi \lor \chi), (\psi \implies \chi)} $ and $ x $ is free in $ \psi $ or $ \chi $, then $ x $ is free in $ \varphi $
%
%		\item Otherwise $ x $ is not free in $ \varphi $
%
%	\end{itemize}

	\item If $ \varphi $ has no free variables, $ \varphi $ is a sentence, $ \varphi \in Sent $

\end{itemize}

\newpage

\section{Model Theory}

\begin{itemize}

	\item A model $ \Model $ requires:

	\begin{itemize}

		\item A language $ \Lang $

		\item A domain $ M $ of objects

		\item An interpretation:

		\begin{itemize}

			\item Constant symbols $ c $ are interpreted by some object from the domain: $ c^\Model \in M $

			\item Relation symbols $ R $ (with arity $ n $) are interpreted by a set of tuples of objects within the domain; so $ R^\Model \subseteq \setcomp{\tuple{m_1, \dots, m_n}}{m_1, \dots, m_n \in M} $

			\item Function symbols $ f $ with arity $ n $ are interpreted by functions taking some $ m_1, \dots, m_n \in M$, and returning some $ m \in M $

		\end{itemize}

	\end{itemize}

	\item A model of $ \Lang = \set{a, b, c, \dots, P, Q, R, \dots, f, g, h, \dots} $ would then be:
	\begin{equation*}
	\Model = \tuple{M, a^\Model, b^\Model, c^\Model, \dots, P^\Model, Q^\Model, R^\Model, \dots, f^\Model, g^\Model, h^\Model, \dots}
	\end{equation*}

	\item If $ t $ is a closed term of $ \Lang $ and $ \Model $ is a model of $ \Lang $, then the \textit{denotation} $ t^\Model $ is:

	\begin{itemize}
		\item If $ t $ is a constant symbol $ c $, $ t^\Model = c^\Model $

		\item If $ t = f(t_1, \dots, t_n) $, $ t^\Model = f^\Model (t_1^\Model, \dots, t_n^\Model) $
	\end{itemize}

	\item A sentence $ \varphi $ in some language $ \Lang $ may then be \textit{true} in $ \Model $ ($ \Model \models \varphi $)

	\item $ \varphi $ is \textit{satisfiable} if there exists some model $ \Model $ in the language $ \Lang $ of $ \varphi $ such that $ \Model \models \varphi $

	\item $ \varphi $ is \textit{valid} ($ \models \varphi$) if for every model $ \Model $ in the language $ \Lang $ of $ \varphi $, $ \Model \models \varphi $

	\item Given $ \Gamma \subseteq Sent_\Lang $ and $ \varphi \in Sent_\Lang $, we say $ \varphi $ \textit{is a consequence of} $ \Gamma $ ($ \Gamma \models \varphi $) if for every model $ \Model $ of $ \Lang $, if $ \Model \models \gamma $ for all $ \gamma \in \Gamma $, then $ \Model \models \varphi $

	\item $ \varphi $ is \textit{derivable} from $ \Gamma $ ($ \Gamma \vdash \varphi $) if:

	\begin{itemize}

		\item (Ax) $ \varphi $ is an axiom of first order logic

		\item (Ass) $ \varphi \in \Gamma $

		\item (MP) $ \Gamma \vdash \psi \implies \varphi $ and $ \Gamma \vdash \psi $

		\item (UG) If $ \Gamma \vdash \varphi $, then $ \Gamma \vdash \forall y \varphi(y \mapsto x) $ when:

		\begin{itemize}
			\item $ x $ is not free in any formula in $ \Gamma $;
			\item $ y = x $; or
			\item $ y $ is not free in $ \varphi $
		\end{itemize}

	\end{itemize}

	\item A model can describe a structure \textit{externally} (as in the graph model), or \textit{internally} using sentences true in the intended model

	\item A \textit{theory} $ \Gamma $ is a set of sentences closed under consequence (i.e. $ \Gamma \vdash \varphi \implies \varphi \in \Gamma $)

	\begin{itemize}

		\item It is \textit{consistent} if there is no sentence $ \varphi $ such that $ \Gamma \vdash \varphi \land \lnot \varphi $

		\item It is \textit{complete} if for all $ \varphi $ we have either $ \Gamma \vdash \varphi $ or $ \Gamma \vdash \lnot \varphi $

		\item It is \textit{categorical} if there is exactly one model $ \Model $ such that $ \Model \models \Gamma $

	\end{itemize}

	\item A theory is \textbf{algebraic} if it has multiple intended models, and \textbf{non-algebraic} if it has one unique intended model

\end{itemize}

\newpage


\section{Unsupervised text classification}
\label{uns-class}

\paragraph{Text classification}
The problem of associating texts, so documents, to classes, represented by labels.
Classes may be a partition of the document space, or they can overlap.
The approaches to solve the problem are basically two, pre-trained models,
so supervised, or based only on documents, so unsupervised.

Depending on the number of classes and how the cover the document space, we define
four tasks
\begin{center}
    \begin{tabular}{c | c |c}
            & 2 classes & Many classes\\
            \hline
            Disjoint classes & Binary classification & Multi-class classification\\
            \hline
            Overlapping classes & Soft binary classification & Multi-label classification\\
    \end{tabular}
\end{center}

Note that ranking can be seen as soft binary classification.

\subsection{Clustering}

\paragraph{Goal}
Clustering is the problem of grouping objects in clusters, in particular
given a distance metric,
we want to minimize the distance within a cluster and maximize the one
between different clusters.

\paragraph{Parameters}
Choosing the right number of clusters is pretty difficult, a high number
leads to consistency inside clusters, but poor aggregation, while a low number
increases aggregation but reduce quality of the single clusters.

\paragraph{Types of clustering}
There are many approaches to clustering, one could for instance
consider flat or hierarchical clustering, in the first one there is no structure, where
in the second clusters might contain other clusters.
There is also a difference in cluster assignment, one could assign in an hard or soft way.

\subsubsection{Support to document retrieval}

\paragraph{Terminological analysis}
After computing a clustering of documents we could analyze terminology
of documents of the same cluster to extract relevant terms, this
information can then be used for query expansion.

\paragraph{Cluster pruning}
By selecting representative documents for clusters, we can compare a query
only to those documents. After finding a match all the documents in the cluster
are returned.

\subsubsection{Evaluation}
Given $\Omega = \{ \omega_1, \dots, \omega_n\}$ the set of clusters
and $\mathbb{C} = \{ c_1, \dots, c_n\}$ the set of classes, we can evaluate
the clustering by focusing on pairs of documents.

Let's consider the pair $(x_i, x_j)$, we ask ourselves if
$$\exists \omega_k : (x_i, x_j) \in \omega_k\;\;\exists c_z : (x_i, x_j) \in c_z$$
They represent the clustering and the real world scenario.
If the two are true, we find a true positive, if they are both false a true negative.
In the case the first one is true and the second is not, we have a false positive,
a false negative otherwise.

We can observe that choosing a small number of clusters leads to a large
quantity of false positives, while a fine grain partition, i.e. a high number
of clusters, produces a high number og false negatives.

\paragraph{Measures}
We can use some measures of quality in clustering,
for instance
$$\mathit{Rand} = \frac{\mathit{TP} + \mathit{TN}}{\mathit{TP} + \mathit{TN} + \mathit{FP} + \mathit{FN}}$$
but also \emph{Purity} and \emph{Normalized Mutual Information}.

\subsection{K-means}
This approach starts with $k$ random centroids and assign each data point
to the closets one. We then recompute the centroids with the current clusters
and repeat the first point until termination.

The termination criterion could be a fixed number of iterations, no change
in documents assignments or \emph{RSS} measure below a threshold:
$$\mathit{RSS} = \sum_{k=1}^K\sum_{\vec{x} \in \omega_k}|\vec{x} - \vec{\mu}(\omega_k)|^2$$
where the measure is the sum of the distances between a point and it's centroid squared.
Note that at each iteration the \emph{RSS} decrease.

Two issues of this approach is that the initial position of clusters
changes the results a lot, and also, the $k$ parameter is crucial to the
algorithm.
A good value for $k$ is $K = \min_K(\min(\mathit{RSS}_K) + \lambda K )$

\subsection{Model-based clustering}
A k-means generalization is obtained by interpreting the centroids as a model
that generates data. A centroid with some added noise can generate a document.

\paragraph{Idea}
Instead of generating classes, we start from the points,
we assume that they were generated with a generative model
and we estimate the latent model parameters.

\paragraph{Latent parameters}
$\Theta = \{ \vec{\mu}_1, \dots, \vec{\mu}_n\}$ are the centroids
to be found by k-means.

$L(D | \Theta)$ is the log-likelihood that the data $D$ wa generated by $\Theta$,
this is the objective function, so $\Theta$ becomes:
$$\Theta = \max_{\Theta} L(D|\Theta) = \max_\Theta\sum_{n=1}^N \log P(d_n | \Theta)$$

That means maximizing the sum of probabilities that documents are generated by
the model. We are in a soft clustering scenario, as we are
basically dealing with probabilities
as cluster assignments.

\subsubsection{Expectation Maximization algorithm}
This is an iterative algorithm that maximizes $L(D|\Theta)$, but can also be
used to find latent models in a variety of applications.

Let's consider a multivariate Bernoulli distribution for data,
so all documents are binary vectors,
we want to estimate the probability that a given document is assigned to a
specific cluster given the models parameters.
$$P(d | w_k; \Theta) = \prod_{t_m \in d}q_{mk}
\cdot \prod_{t_m \notin d}(1-q_{mk})\;\;\;\Theta_k = (\alpha_k, q_{1k}, \dots, q_{Mk})$$
where $q_{mk} = P(U_m =1 | \omega _k)$ is the probability that a document from the cluster
$\omega_k$ contains the term $t_m$.

$\alpha_k$ si the prior of the cluster $k$, so the probability that of a document
to be in that cluster, not having any information about it.

\paragraph{Process}
We start by generating a document by picking a cluster $w_k$ with probability
$\alpha_k$, generating terms with probability $q_{mk}$.

We need to estimate the two parameters, and we do that iteratively similarly to k-means.
The iteration is composed by two steps, \emph{Maximization} and \emph{Expectation}.

\paragraph{Maximization step}
The goal here is to estimate the parameters of the model, so the prior and the
terms probability given the cluster:
$$q_{mk} = \frac{\sum_{n=1}^N r_{nk}I(t_m \in d_m)}{\sum_{n=1}^N r_{nk}}
\;\;\; \alpha_k = \frac{\sum_{n=1}^N r_{nk}}{N}$$
where $I$ can be one or zero wether the term appears or not in the
document, and $r_{nk}$ is the soft assignment of the document $n$ to the
cluster $k$.

\paragraph{Expectation step}
We now estimate the probability of each term to be assigned to a cluster.
$$r_{nk} = \frac{\alpha_k(\prod_{t_m \in d_n}q_{mk})(\prod_{t_m \notin d_n}(1-q_{mk}))}
{\sum_{k=1}^K\alpha_k(\prod_{t_m \in d_n}q_{mk})(\prod_{t_m \notin d_n}(1-q_{mk}))}$$
this is basically, for each cluster, the prior, multiplied by the product of $q_{mk}$ or $1- q_{mk}$ for each
term in the document
computed before,
normalized by the same quantity over all the clusters.

\paragraph{Why do EM works?}
The main idea of the algorithm is to push documents that contains the same
words in the same cluster. Basically if two words appears in a document
they should have a high probability of being assigned to the same cluster,
so they have the same $q_{mk}$. This leads to assigning documents with the
same words in the same clusters.

\subsection{Affinity propagation clustering}

\paragraph{Input}
The input is a similarly matrix, for instance $s(i,j) = - ||\vec{i} - \vec{j}||^2$.
There are also some special values for the diagonal of the matrix,
where larger values are more likely to be chosen as exemplars for
clusters.

\paragraph{Idea}
Documents exchange messages to decide exemplars and clusters assignments.
This approach is similar to \emph{page rank with authorities} \footnote{\url{https://nlp.stanford.edu/IR-book/html/htmledition/hubs-and-authorities-1.html}}.

\paragraph{Responsibility messages}
A message $r(i,k)$ that denotes how well $k$ is an exemplar for $i$.
An example can be
$$r(i, k) = s(i, k) - \max_{k^\prime s.t.k^\prime \neq k} \{ a(i, k^\prime) + s(i, k^\prime)\}$$
basically we take the similarity of a pair of nodes and subtract the maximum
similarity for any other document. This value will be zero for the most similar
document, and negative for all other, as the quantity $a(i, k^\prime)$ is initially zero.

\paragraph{Availability}
This message $a(i, k)$ represent how appropriate is for $i$ to choose
$k$ as exemplar.
$$a(i, k) = \min \bigg\{ 0, r(k, k) + \sum_{i^\prime s.t.i^\prime \notin \{i, k\}} \max\{0, r(i^\prime, k)\}\bigg\}$$

\paragraph{Self availability}
The availability measure given the same document as argument, is
$$a(k, k) = \sum_{i^\prime s.t.i^\prime \neq k} \max\{0, r(i^\prime, k)\}$$

\paragraph{Process}
At first, documents are connected by similarity, then we recompute the availability
with respect to the responsibility and vice versa. This leads to making documents
connections stronger and stronger and to the election of an exemplar for clusters.
After some iterations convergence is reached.

\subsection{Hierarchical clustering}
This approach produces a hierarchy of clusters. It does not need the number of clusters
but a criterion of optimal cluster selection. Most of this algorithms are deterministic.

\paragraph{Process}
Staring from a similarity matrix for the documents, we start taking the pair
of documents with maximum similarity and group them in the same cluster.
An idea is to have documents as nodes, and to add a cluster node connected
to the two most similar documents.

The next step is to delete the rows and columns of the two selected documents,
as they are already assigned, but, we add a new row and column
for the cluster node.
The similarity value between a document and a cluster is the minimum similarity
in that cluster for the considered document, but many strategies can be applied.

After rebuilding the table, we continue with the initial step,
note that if a cluster and document are selected as most similar items,
we are adding a cluster connected to them, ergo we are considering the cluster
obtained in the first step, as a sub-cluster for the last one.

The last step is to select the number of clusters.
The previous logic created tree, as we grouped documents and clusters by adding
nodes connected to them.
We can choose a threshold for the intra-cluster similarity, or select a certain
number of clusters, or considering the maximum jump in similarity in the obtained cluster
tree and cut off the tree there. By cutting off the tree, we take the lower part, so
the one with the leaves.

\begin{figure}[h]
    \centering
    \includegraphics[width=0.8\textwidth]{images/hierarchical-clustering.png}
    \caption{Hierarchical clustering example with 4 clusters}
\end{figure}

\paragraph{Top-down clustering}
This approach starts from a cluster containing all the documents, and
recursively splits it keeping track of the parent of each document.
The process stops when a threshold of intra-cluster similarity is obtained
or when there's a cluster for each document.
This approach is the opposite of the bottom up tree creation.


\chapter{More Examples}
\label{chap:more-examples}

In addition to the examples already covered in this text, the \holn{}
distribution comes with a variety of instructive examples in the
\verb|examples| directory.  There the following examples (among
others) are to be found:

\begin{description}

\item [\tt autopilot.sml]

  This example is a \holn{} rendition (by Mark Staples) of a PVS
  example due to Ricky Butler of NASA. The example shows the use of
  the record-definition package, as well as illustrating some aspects
  of the automation available in \holn{}.

\item [\tt bmark]

  In this directory, there is a standard HOL benchmark: the proof of
  correctness of a multiplier circuit, due to Mike Gordon.

\item [\tt euclid.sml]

  This example is the same as that covered in
  Chapter~\ref{chap:euclid}: a proof of Euclid's theorem on the
  infinitude of the prime numbers, extracted and modified from a much
  larger development due to John Harrison. It illustrates the
  automation of \HOL{} on a classic proof.

\item[\tt ind\_def]

This directory contains some examples of an inductive definition package
in action. Featured are an operational semantics for a small imperative
programming language, a small process algebra, and combinatory logic
with its type system. The files were originally developed by Tom Melham
and Juanito Camilleri and are extensively commented.  The last is the
basis for Chapter~\ref{chap:combin}.

Most of the proofs in these theories can now be done much more easily by
using some of the recently developed proof tools, namely the simplifier
and the first order prover.

\item [\tt fol.sml]

  This file illustrates John Harrison's implementation of a
  model-elimination style first order prover.

\item[\tt lambda]

This directory develops theories of a ``de Bruijn'' style lambda calculus,
and also a name-carrying version. (Both are untyped.) The development
is a revision of the proofs underlying the paper
{\it ``5 Axioms of Alpha Conversion'',
            Andy Gordon and Tom Melham,
            Proceedings of TPHOLs'96, Springer LNCS 1125}.

\item[\tt parity]

  This sub-directory contains the files used in the parity example of
  Chapter~\ref{parity}.

\item [\tt MLsyntax]

  This sub-directory contains an extended example of a facility for
  defining mutually recursive types, due to Elsa Gunter of Bell Labs.
  In the example, the type of abstract syntax for a small but not
  totally unrealistic subset of ML is defined, along with a simple
  mutually recursive function over the syntax.

\item[\tt Thery.sml]

  A very short example due to Laurent Th\'ery, demonstrating a cute
  inductive proof.

\item[\tt RSA]

       This directory develops some of the mathematics underlying
       the RSA cryptography scheme. The theories have been
       produced by Laurent Th\'ery of INRIA Sophia-Antipolis.

\end{description}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "tutorial"
%%% End:


\section{Probabilistic Particle Tracing Model}

In order to analyze the uncertain flow field by particle tracing, in this section we introduce our probability-based particle tracing model. First, we describe the global modeling of streamlines and how to estimate the trace distributions for a given uncertain data. Then local distributions used by the global model are detailed.

\subsection{Global Modeling}

A single streamline $L$ originating from position ${x_0}$ can be modeled as $L = \{ {x_0},{x_1},...,{x_n}\} = {x_{0:n}}$, where $x_t$ refers to a position in $\mathrm{R}^d$. As mentioned by Otto et al. in~\cite{Otto10a, Otto11a}, conventional streamline integration methods such as RK4 are not well defined for uncertain vector fields, since there is no unique vector direction at a location ${x_t}$. Therefore, as with most previous methods~\cite{Otto10a, Otto11a}, we make use of the Euler integration model in this paper:
\begin{equation}
  {x_{t + 1}} = {x_t} + {v_t}\Delta t
\end{equation}
where ${v_t}$ and $\Delta t$ refer to the vector direction and the step size at step $t$. Since we focus on generating streamlines from steady vector fields in this work, it is safe to ignore the magnitude of the vectors. And if we set the step size $\Delta t$ as a constant, we can represent the streamline by a sequence of vector directions ${L = v_{0:n}}$, since the streamline trajectory only depends on the propagation directions $v_{0:n}$.

For the uncertain vector fields, there is no unique streamline $v_{0:n}$ for a given starting point $x_0$. For all possible streamlines which originate from $x_0$ given the uncertain data $\mathcal{H}$, we can define a probability density function (pdf) over the path space, which is:
\begin{equation}
  p(v_{0:n}|\mathcal{H})
\end{equation}
where $\mathcal{H}$ is a set of observations in distributions from the uncertain data along the streamline trajectory. Here, we denote the distribution obtained at the starting point $x_t$ of a vector $v_t$ as $\lambda_t=\mathcal{H}(v_t)$. By applying the Bayes theorem, the target distribution $p({v_{0:n}}|{\lambda_{0:n}})$ can be represented by the prior density $p({v_{0:n}})$ and the conditional observation density $p({\lambda_{0:n}}|{v_{0:n}})$, as:
\begin{equation}
  p({v_{0:n}}|{\lambda_{0:n}}) = \frac{{p({v_{0:n}})p({\lambda_{0:n}}|{v_{0:n}})}}{{p({\lambda_{0:n}})}}
\end{equation}
where ${p({\lambda_{0:n}})}$ is a normalizing constant for a fixed data realization, which equals to $\int {p({v_{0:n}},{\lambda_{0:n}})} d{v_{0:n}}$.

Scientific simulations commonly represent physical phenomena as continuous functions. Thus, the sequence of vector directions $v_{0:n}$ along the particle trace should be correlated. This constraint can be modeled as a conditional prior density $p({v_t}|{v_{0:t - 1}})$. In this paper, we assume the sequence $v_{0:n}$ forms a Markov chain, which means the vector direction $v_t$ only depends on the previous direction $v_{t-1}$, but not on $v_{t-2},...,v_0$; so:
\begin{equation}
  p({v_t}|{v_{0:t - 1}}) = p({v_t}|{v_{t - 1}})
\end{equation}
where $p({v_t}|{v_{t - 1}})$ denotes the probability density associated with the transition from $v_{t - 1}$ to $v_t$. Hence, the probability density for a given streamline can be formulated as:
\begin{equation}
  p({v_{0:n}}) = p({v_0})\prod\limits_{t = 1}^n {p({v_t}|{v_{t - 1}})}
\end{equation}
where $p(v_0)$ can be defined by a uniform distribution, since no prior knowledge is applied.

By measuring the observations $\lambda_{0:n}$ along a given streamline $v_{0:n}$, we can get the conditional observation density $p({\lambda_{0:n}}|v_{0:n})$, which defines a measure of how the observations match the given path. In the other word, the observation density gives how likely the distributions $\lambda_{0:n}$ will be observed if the given streamline $v_{0:n}$ actually exists in the flow field. Likewise, we assume that the observation measured at a point does not depend on any previous points in the trace, i.e.:
\begin{equation}
  p(\lambda_t|v_{0:t}) = p({\lambda_t}|{v_t})
\end{equation}
which defines the likelihood density:
\begin{equation}
  p({\lambda_{0:n}}|{v_{0:n}}) = \prod\limits_{t = 0}^n {p({\lambda_t}|{v_t})}
\end{equation}

By substituting (5) and (7) into (3), the posterior density $p({v_{0:n}}|{\lambda_{0:n}})$ can be expanded as:
\begin{equation}
  p({v_{0:n}}|{\lambda_{0:n}}) = \frac{{p({v_0})\prod\limits_{t = 1}^n {p({v_t}|{v_{t - 1}})} \prod\limits_{t = 0}^n {p({\lambda_t}|{v_t})} }}{{p({\lambda_{0:n}})}}
\end{equation}

Since $p({v_{0:n}}|{\lambda_{0:n}})$ is high-dimensional, non-standard, and only known up to a proportionality constant, which makes it infeasible to be evaluated in closed-form. Therefore, a Monte Carlo based method needs to be used to approximate the target distribution. In this case, particle filtering~\cite{doucet2001sequential} as one of Sequential Monte Carlo methods is suitable to approximate the target distribution in iterations. We first put a number of weighted particles at the seed position and update them iteratively. At each iteration, the vector directions which the particles will propagate along with are sampled from an importance function. Then the weight of each particle can be updated based on the importance sampling. Furthermore, the particles can be resampled based on their weights. In the end, a set of weighted particle tracing are obtained to represent the target posterior distribution, from which the most likely trace can be chosen by maximum a posteriori (MAP) estimation.

\subsection{Local Modeling}

Based on the particle filtering algorithm, the prior density $p({v_t}|{v_{t - 1}})$, the observation density $p({\lambda_t}|{v_t})$, and an importance function need to be modeled and estimated. In this section, we will elaborate how to model and estimate these local densities in detail.

\noindent\textbf{Prior Density.} As mentioned before, prior density is used to characterize the correlation between two adjacent vector directions. In this work, a prior density which prefers to continue in the previous direction and gives decreasing probability for sharper turns is used. As presented by Zhang et al. in~\cite{Zhang20095}, the von Mises-Fisher distribution~\cite{fisher} has been selected as the prior density due to its mathematical simplicity and tractability.

For a random $d$-dimensional unit vector $v$ on the $(d-1)$-dimensional sphere, with respect to the mean direction $\mu$ and the concentration parameter $\kappa$, the probability density function of the von Mises-Fisher distribution is given by
\begin{equation}
  f_{d}(v| \mu, \kappa)=C_{d}(\kappa)\exp \left( {\kappa \mu^T v } \right)
\end{equation}
where the normalization constant $C_{d}(\kappa)\,$ is
\begin{equation}
  C_{d}(\kappa)=\frac {\kappa^{d/2-1}} {(2\pi)^{d/2}I_{d/2-1}(\kappa)} \,
\end{equation}
in which $I_{d/2-1}$ denotes the modified Bessel function of the first kind and order $d/2-1$.

The parameter $\kappa$ is used to control the concentration of the distribution around the mean direction $\mu$. Figure~\ref{fisher} gives examples of points sampled from 2-dimensional von Mises-Fisher distributions with different $\kappa$.

\begin{figure}[htb]
  \centering
  \begin{subfigure}[b]{0.16\textwidth}
    \centering
    \includegraphics[width=0.8in]{../figures/vf_100.eps}
    \caption{$\kappa=100$}
  \end{subfigure}~
  \begin{subfigure}[b]{0.16\textwidth}
    \centering
    \includegraphics[width=0.8in]{../figures/vf_10.eps}
    \caption{$\kappa=10$}
  \end{subfigure}~
  \begin{subfigure}[b]{0.16\textwidth}
    \centering
    \includegraphics[width=0.8in]{../figures/vf_1.eps}
    \caption{$\kappa=1$}
  \end{subfigure}
  \caption{Points sampled from three von Mises-Fisher distributions on $2$-dimensional spheres with different value of $\kappa$. The mean directions are shown as arrows.}
  \label{fisher}
\end{figure}

In this work, the mean direction $\mu$ of the prior density is given by the previous vector direction ${v_{t - 1}}$. The concentration parameter $\kappa$ is given manually as a constant value, in this work a $\kappa = 60$ is used. For more turbulent flow fields, a larger $\kappa$ can be used.

\noindent\textbf{Observation Density.} We make use of the observation density to characterize local uncertainties, which defines a likelihood function that measures how observations match the current prior model. At step $t$, for a given vector direction $v_t$, the observation density $p({\lambda_t}|{v_t})$ is a conditional density representing how likely the observation $\lambda_t$ measured at position $x_t$ will happen based on the given vector $v_t$. According to the observation $\lambda_t$, the observation density $p({\lambda_t}|{v_t})$ is estimated based on the method presented by Friman et al. in~\cite{frimanTMI06}. We first model the observation matching perfectly to the current state $v_t$ as a distribution $\mu_t$ where the probability of $v_t$ equals to $1$. Then treat $\lambda_t$ as a histogram and let the probability $\lambda_t(i)$ of bin $i$ be an uncertain observation of $\mu_t(i)$, i.e. $\log{\mu_t(i)}=\log{\lambda_t(i)}+\epsilon$. The noise $\epsilon$ can be modeled as additive Gaussian distribution~\cite{Basser1994,Salvador05}, such as $\epsilon \sim N(0,{\sigma ^2}/{\mu _t}{(i)^2})$. Then the observation density, or the likelihood, can be written as
\begin{equation}
  p({\lambda_t}|{v_t}) = \prod\limits_{i = 0}^N {\frac{{{\mu _t}(i)}}{{\sqrt {2\pi {\sigma ^2}} }}} {e^{ - \frac{{{\mu _t}{{(i)}^2}}}{{{2\sigma ^2}}}{{(\log{{\lambda _t}(i)} - \log{{\mu _t}(i)})}^2}}}
\end{equation}

\noindent\textbf{Importance Density.} A usual approach is to use the same distribution as the prior density as the importance function, which is called bootstrap filter or condensation algorithm. However, such importance function may not be always effective, since no observation information is used. As a result, the resulting particles are often outliers of the posterior distribution. Therefore, the observation density is used to determine the $v_t$. Since it is expensive to evaluate the observation density function and there is no need to use the actual observation density as the importance density for the particle filtering method, we choose the observed distribution $\lambda_t$ as the importance density function.


\documentclass{article}
%\usepackage{mathrsfs}
\usepackage{amssymb}
\usepackage{amsmath}

% table padding
\renewcommand{\arraystretch}{1.5}
\begin{document}

\title{Set theory}

\section{Concepts}

\begin{itemize}
  \item Set
  \item Element
  \item Subset
  \item Superset
\end{itemize}

\section{Special sets}

\begin{itemize}
  \item Universal set: U
  \item Empty set: $\emptyset$
  \item Natural numbers: $\mathbb{N}$
  \item Integer: $\mathbb{Z}$
  \item Rational numbers: $\mathbb{Q}$
  \item Real numbers: $\mathbb{R}$
  \item Complex numbers: $\mathbb{C}$
\end{itemize}

\section{Set operations}

\subsection{Union and interception}

\subsubsection{Union}
$$
A \cup B = \{ x \mid x \in A \lor x \in B \}
$$

\subsubsection{Interception}
$$
A \cap B = \{ x \mid x \in A \land x \in B \}
$$

Properties:
\begin{itemize}
  \item $A \cap B \subseteq A, A \cap B \subseteq B$
  \item $A \subseteq A \cup B, B \subseteq A \cup B$
\end{itemize}

Theorem 1.3: For any sets A and B, we have:
\begin{gather*}
A \cap B \subseteq A \subseteq A \cup B \\
A \cap B \subseteq B \subseteq A \cup B
\end{gather*}

Theorem 1.4: The followings are equivalent:
$$
A \subseteq B, A \cap B = A, A \cup B = B
$$

\subsection{Complement}

\subsubsection{Absolute complement}

$$
A^c = \{ x \mid x \in \text{U}, x \not\in A \}
$$

\subsubsection{Relative Complement}

The relative complement of A and B, donoted by $A\setminus B$, is:
$$
A\setminus B = \{ x \mid x \in A, x \not\in B \}
$$

\subsubsection{Symmetric difference}

Symmetric difference of sets A and B, denoted by $A \oplus B$, is:
$$
A \oplus B = \{ x \mid (x \in A \land x \not\in B) \lor (x \in B \land x \not\in A) \}
$$

That is:
\begin{gather*}
A \oplus B = (A \cup B)\setminus(A \cap B) \\
A \oplus B = (A \setminus B) \cup (B \setminus A)
\end{gather*}

\subsection{Fundamental products}

Consider $n$ distinct sets $A_1, A_2, \cdots, A_n$,
a fundanmental product is a set of form

$$
A_1^* \cap A_2^* \cap \cdots \cap A_n^* (A_i^* = A_i \lor A_i^* = A_i^c)
$$

\section{Algebra of sets, duality}

Theorem 1.5: Sets satisfy the laws in table:
\begin{table}
\caption{Laws of the algebra of sets}
\label{Table: 1-1}
\begin{tabular}{l | l | l}
  \hline
  Idempotent laws  & $A \cup A = A$ & $A \cap A = A$ \\
  \hline
  Associative laws & $(A \cup B) \cup C = A \cup (B \cup C)$ & $(A \cap B) \cap C = A \cap (B \cap C)$ \\
  \hline
  Commutative laws & $A \cup B = B \cup A$ & $A \cap B = B \cap A$ \\
  \hline
  Distributive laws
  & $A \cup (B \cap C) = (A \cup B) \cap (A \cup C)$
  & $A \cap (B \cup C) = (A \cap B) \cup (A \cap C)$ \\
  \hline
  Identity laws & $A \cup \emptyset = A$      & $A \cap \text{U} = A$ \\
                & $A \cup \text{U} = \text{U}$ & $A \cap \emptyset = \emptyset$ \\
  \hline
  Involution laws & $(A^c)^c = A$ & \\

  \hline
  Complement laws & $A \cup A^c = \text{U}$ & $A \cap A^c = \emptyset$ \\
                  & $\text{U}^c = \emptyset$ & $\emptyset^c = \text{U}$ \\
  \hline
  DeMorgan's laws & $(A\cup B)^c = A^c \cap B^c$ & $(A\cap B)^c = A^c \cap B^c$\\
  \hline
\end{tabular}
\end{table}

\section{Venn diagrams}

\end{document}


    \filetitle{uniform}{Create function proportional to log of uniform distribution}{logdist/uniform}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
F = logdist.uniform(Lo,Hi)
\end{verbatim}

\paragraph{Input arguments}\label{input-arguments}

\begin{itemize}
\item
  \texttt{Lo} {[} numeric {]} - Lower bound of the uniform distribution.
\item
  \texttt{Hi} {[} numeric {]} - Upper bound of the uniform distribution.
\end{itemize}

\paragraph{Output arguments}\label{output-arguments}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \texttt{F} {[} function\_handle {]} - Handle to a function returning a
  value that is proportional to the log of the uniform density.
\end{itemize}

\paragraph{Description}\label{description}

See \href{logdist/Contents}{help on the logdisk package} for details on
using the function handle \texttt{F}.

\paragraph{Example}\label{example}