\documentclass[a4paper]{article}

%% Language and font encodings
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}

%% Sets page size and margins
\usepackage[a4paper,top=3cm,bottom=2cm,left=1.75cm,right=2cm,marginparwidth=1.75cm]{geometry}

%% Useful packages
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage[colorlinks=true, allcolors=blue]{hyperref}
\usepackage{braket}
\usepackage{scalerel}

\title{\vspace{-2.0cm}Notes about VSVB energy}
\author{\vspace{-2.0cm}Colleen Bertoni, Graham Fletcher}

\begin{document}
\maketitle

\section{Implemented energy calculation}

Without giving too much detail, for a VSVB wave function with $N$ electrons,  $N_p$ spin coupled pairs, and $N_{sc}$ spin couplings, the VSVB energy can be written:
\begin{equation}
 \begin{aligned}
 E &= \\
 =& \frac{
  \begin{aligned} &\sum_{ij}^{N} (\sum_q^{M_{sc}} d^1_{q,ij} w_{q,ij} ) h_{s,ij}
    + \sum_i^{N}\sum_{j<i}\sum_k^{N}\sum_{l<k}
         \left(\sum_q^{M_{sc}} d^2_{q,ikjl}
          (  w_{q,ikjl} \left< \phi_i(1) \phi_j(2) | \phi_k(1) \phi_l(2) \right>_s
           - w_{q,iljk} \left< \phi_i(1) \phi_j(2) | \phi_l(1) \phi_k(2) \right>_s \right)
            \end{aligned}
     }
     { \displaystyle \sum_{ij}^{N} (\sum_q^{M_{sc}} d^1_{q,ij} w_{q,ij} ) \left< \phi_i(1) | \phi_j(1) \right>_s / N}
 \end{aligned}
 \end{equation}

where
\begin{itemize}
\item $M_{sc} = N_{sc}^2 2^{2N_p}$ if there are spin couplings, $M_{sc} = 1$ if there are no spin couplings
\item $\phi$ is a one-electron spin orbital
\item $h_{s,ij} = \left< \phi_i(1) | h_s | \phi_j(1) \right>  $ where h is the standard one-electron integral. The s subscript is meant to denote that this integral does not include spin. ($w_{q,ij}$ contains the corresponding spin function integration)
\item $d^1_{q,ij} $ is the first-order cofactor of the matrix of overlap integrals between spin orbitals. That is, it is the determinant of the overlap matrix with row i and column j removed, multiplied by $(-1)^{i+j}$ . $q$ denotes the term in the spin coupling/pairs expansion
\item $w_{q,ij}$ is the spin function integration, where the spin functions are $\alpha$ or $\beta$, whichever are associated with spin orbital $\phi_i(1)$ and $\phi_j(1)$. $q$ denotes the term in the spin coupling/pairs expansion
\item $ \left< \phi_i(1) \phi_j(2) | \phi_k(1) \phi_l(2) \right>_s$ is the electron-electron repulsion integral. the s subscript is meant to denote that this integral does not include spin integration (this is in the "w" variable)
\item $d^2_{q,ikjl} $ is the second-order cofactor of the matrix of overlap integrals between the spin orbitals. That is, it is the determinant of the overlap matrix with row i, column k, row j, and column l removed multiplied by $(-1)^{i+j+k+l}$. $q$ denotes the term in the spin coupling/pairs expansion
\item $ w_{q,ijkl} $ is the spin function integration, where the spin functions are alpha or beta, whichever are associated with spin orbitals $\phi_i(1),\phi_k(1),\phi_j(2),\phi_l(2)$. $q$ denotes the term in the spin coupling/pairs expansion
\end{itemize}

The rest of the notes show where this expression comes from.

\section{General energy expression}

The VSVB wave function with one spin coupling is defined as an antisymmetrized product of orbitals, where some are double occupied, some contain spin coupled electron pairs, and some contain unpaired electrons:

\begin{equation}
 \begin{aligned}
 \Psi_{VSVB} = &\hat{A} \{ \Xi_{(docc+unpaired)}\Phi_{sc} \}
 \label{init_wave}
 \end{aligned}
 \end{equation}

     \begin{itemize}
     \item $\Xi_{(docc+unpaired)} $ is a product of double occupied orbitals and singly occupied orbitals with unpaired electrons.

     Expanding, $\Xi_{(docc+unpaired)} = \phi_1(1)\alpha(1)\phi_1(2) \beta(2)\phi_2(3)\alpha(3)\phi_2(2) \beta(4)...$.

     where $\phi$ is a spatial orbital with no spin coordinates

     \item $\Phi_{sc}$ is the weighted sum of products of spin-coupled pairs of spin orbitals.
     That is, for $N_{sc}$ spin couplings and $N_p$ spin coupled pairs (so $2*N_p$ total orbitals) ,

     $\displaystyle \Phi_{sc} = \phi_i(i) \phi_j(j) \phi_k(k) \phi_l(l)...\sum_m^{N_{sc}} C_m \Theta_m(i,j,k,l,...)  $,
     where $C_m$ is an expansion coefficient, $\phi_i(i) \phi_j(j) \phi_k(k) \phi_l(l)...$ is the product of $2*N_p$ spatial orbitals, and each $\Theta_m$ is a spin eigenfunction which couples the $N_p$ electron pairs.
     For example,

     $\Theta_1(i,j,k,l...) = \big[\alpha(i)\beta(j)-\alpha(j)\beta(i)\big] \big[\alpha(k)\beta(l)-\alpha(l)\beta(k)\big]... $ ( $i$ is coupled to $j$ and $k$ is coupled to $l$)

     $\Theta_2(i,j,k,l...) = \big[\alpha(i)\beta(k)-\alpha(k)\beta(i)\big] \big[\alpha(j)\beta(l)-\alpha(l)\beta(j)\big]... $ ( $i$ is coupled to $k$ and $i$ is coupled to $l$)

     ...

     See other resources, such as "A Chemist's Guide to Valence Bond Theory" for more information about spin eigenfunctions. Each $\Theta_m$ will have $2^{N_p}$ terms if it is expanded out.

      Expanding and collecting related terms,  $\displaystyle \Phi_{sc} = C_1 \phi_i(i) \phi_j(j) \phi_k(k) \phi_l(l)...  \big[\alpha(i)\beta(j)-\alpha(j)\beta(i)\big] \big[\alpha(k)\beta(l)-\alpha(l)\beta(k)\big]... + C_2 \phi_i(i) \phi_j(j) \phi_k(k) \phi_l(l) \big[\alpha(i)\beta(k)-\alpha(k)\beta(i)\big] \big[\alpha(j)\beta(l)-\alpha(l)\beta(j)\big]... + C_3... $
     \end{itemize}

Expanding out Eq. \ref{init_wave} :

 \begin{equation}
 \begin{aligned}
 \Psi_{VSVB} = &\hat{A} \{ \Xi_{(docc+unpaired)}\Phi_{sc} \} \\
 = &\hat{A} \{\phi_1(1)\alpha(1)\phi_1(2) \beta(2)... \phi_i(i) \phi_j(j) \phi_k(k) \phi_l(l)...\sum_m^{N_{sc}} C_m \Theta_m(i,j,k,l,...)  \} \\
 =& \hat{A} \{ \phi_1(1)\alpha(1)\phi_1(2) \beta(2)... C_1 \phi_i(i) \phi_j(j) \phi_k(k) \phi_l(l)...  \big[\alpha(i)\beta(j)-\alpha(j)\beta(i)\big]\big[\alpha(k)\beta(l)-\alpha(l)\beta(k)\big]... \\ &
 + \phi_1(1)\alpha(1)\phi_1(2) \beta(2)... C_2\phi_i(i) \phi_j(j) \phi_k(k) \phi_l(l)...  \big[\alpha(i)\beta(k)-\alpha(k)\beta(i)\big] \big[\alpha(j)\beta(l)-\alpha(l)\beta(j)\big]... \\ &+  ... \}
 \label{exp_init_wave}
 \end{aligned}
 \end{equation}

 If Eq. \ref{exp_init_wave} is expanded out, there will be $N_{sc}2^{N_p}$ total terms in the wavefunction if there are spin couplings. If there are no spin couplings, there is only 1 term (the docc+unpaired term).

The general energy expression is \[E = \frac{\Braket{\Psi|\hat{H}|\Psi}}{\Braket{\Psi|\Psi}} \]

Subbing in the VSVB wave function:

 \begin{equation}
 \begin{aligned}
     E &= \frac{    \Braket{\Psi_{VSVB}|H|\Psi_{VSVB}} }{\Braket{\Psi_{VSVB}|\Psi_{VSVB}}} \\
     &= \frac{   \Braket{   \begin{aligned}
     \hat{A}& \{ \phi_1(1)\alpha(1)... C_1 \phi_i(i) \phi_j(j)... \big[\alpha(i)\beta(j)-\alpha(j)\beta(i)\big]... \\ &
 + \phi_1(1)\alpha(1)... C_2\phi_i(i) \phi_j(j) ...  \big[\alpha(i)\beta(k)-\alpha(k)\beta(i)\big] ... \\ &+ ...\}
       \end{aligned}
       |H|
  \begin{aligned}
     \hat{A}& \{ \phi_1(1)\alpha(1)... C_1 \phi_i(i) \phi_j(j) ... \big[\alpha(i)\beta(j)-\alpha(j)\beta(i)\big]... \\ &
 + \phi_1(1)\alpha(1)... C_2\phi_i(i) \phi_j(j) ...  \big[\alpha(i)\beta(k)-\alpha(k)\beta(i)\big] ... \\ &+ ...\}
       \end{aligned}
         }}
       {\Braket{  \begin{aligned}
     \hat{A}& \{ \phi_1(1)\alpha(1)... C_1 \phi_i(i) \phi_j(j)... \big[\alpha(i)\beta(j)-\alpha(j)\beta(i)\big]... \\ &
 + \phi_1(1)\alpha(1)... C_2\phi_i(i) \phi_j(j) ...  \big[\alpha(i)\beta(k)-\alpha(k)\beta(i)\big] ... \\ &+ ...\}
       \end{aligned}
              |
               \begin{aligned}
     \hat{A}& \{ \phi_1(1)\alpha(1)... C_1 \phi_i(i) \phi_j(j)... \big[\alpha(i)\beta(j)-\alpha(j)\beta(i)\big]... \\ &
 + \phi_1(1)\alpha(1)... C_2\phi_i(i) \phi_j(j) ...  \big[\alpha(i)\beta(k)-\alpha(k)\beta(i)\big] ... \\ &+ ...\}
       \end{aligned}
       }}
     \end{aligned}
     \end{equation}

Since $\hat{A}$ is Hermitian, commutes with the Hamiltonian, and $\hat{A}\hat{A}\phi = \sqrt{N!}\hat{A}\phi$, this can be written as

 \begin{equation}
 \begin{aligned}
     &= \frac{  \Braket{   \begin{aligned}
     \hat{A}& \{ \phi_1(1)\alpha(1)... C_1 \phi_i(i) \phi_j(j)... \big[\alpha(i)\beta(j)-\alpha(j)\beta(i)\big]... \\ &
 + \phi_1(1)\alpha(1)... C_2\phi_i(i) \phi_j(j) ...  \big[\alpha(i)\beta(k)-\alpha(k)\beta(i)\big] ... \\ &+ ...\}
       \end{aligned}
       |H|
  \begin{aligned}
     &  \phi_1(1)\alpha(1)... C_1 \phi_i(i) \phi_j(j) ... \big[\alpha(i)\beta(j)-\alpha(j)\beta(i)\big]... \\ &
 + \phi_1(1)\alpha(1)... C_2\phi_i(i) \phi_j(j) ...  \big[\alpha(i)\beta(k)-\alpha(k)\beta(i)\big] ... \\ &+ ...
       \end{aligned}
         }}
       {\Braket{  \begin{aligned}
     \hat{A}& \{ \phi_1(1)\alpha(1)... C_1 \phi_i(i) \phi_j(j)... \big[\alpha(i)\beta(j)-\alpha(j)\beta(i)\big]... \\ &
 + \phi_1(1)\alpha(1)... C_2\phi_i(i) \phi_j(j) ...  \big[\alpha(i)\beta(k)-\alpha(k)\beta(i)\big] ... \\ &+ ...\}
       \end{aligned}
              |
               \begin{aligned}
     & \phi_1(1)\alpha(1)... C_1 \phi_i(i) \phi_j(j)... \big[\alpha(i)\beta(j)-\alpha(j)\beta(i)\big]... \\ &
 + \phi_1(1)\alpha(1)... C_2\phi_i(i) \phi_j(j) ...  \big[\alpha(i)\beta(k)-\alpha(k)\beta(i)\big] ... \\ &+ ...
       \end{aligned}
       }}
     \end{aligned}
     \end{equation}

To shed light on the expression, we can expand the spin coupled pairs in each spin coupling term. This results in a sum of $2^{N_p}$ terms
(where each spatial function is associated with one spin function) for each $N_{sc}$ spin coupling (so $N_{sc}2^{N_{p}}$ total terms if there are spin couplings. If there are no spin couplings, there is only 1 term) .

For the first spin coupling (associated with $C_1$) this is:

\begin{equation}
 \begin{aligned}
     =& \hat{A} \{ \phi_1(1)\alpha(1)... C_1\phi_i(i) \phi_j(j) \phi_k(k) \phi_l(l)... \big[\alpha(i)\beta(j)-\alpha(j)\beta(i) \big] \big[\alpha(k)\beta(l)-\alpha(l)\beta(k) \big]... \} \\
     =& \hat{A} \{ \phi_1(1)\alpha(1)... C_1\phi_i(i) \phi_j(j) \phi_k(k) \phi_l(l)... \alpha(i)\beta(j) \alpha(k)\beta(l)... \\
     &- \phi_1(1)\alpha(1)... C_1\phi_i(i) \phi_j(j) \phi_k(k) \phi_l(l)...\alpha(j)\beta(i) \alpha(k)\beta(l)... \\
     &+...
     \}
     \end{aligned}
     \end{equation}

Since $\hat{A}$ is a linear operator, each term in the sum can be written separately with $\hat{A}$ operating on it:

\begin{equation}
 \begin{aligned}
     =& \hat{A} \{ \phi_1(1)\alpha(1)... C_1\phi_i(i) \phi_j(j) \phi_k(k) \phi_l(l)... \alpha(i)\beta(j) \alpha(k)\beta(l)... \} \\
     &- \hat{A} \{ \phi_1(1)\alpha(1)... C_1\phi_i(i) \phi_j(j) \phi_k(k) \phi_l(l)...\alpha(j)\beta(i) \alpha(k)\beta(l)... \} \\
     &+...
     \end{aligned}
     \end{equation}

So the wave function is a sum of antisymmetrized products of spin orbitals.

We can plug this back into the energy expression to get:

 \begin{equation}
 \begin{aligned}
  E &= \\
     &= \frac{    \Braket{
     \begin{aligned}
     &\hat{A} \{ \phi_1(1)\alpha(1)... C_1 \phi_i(i) \phi_j(j) ... \alpha(i)\beta(j)\alpha(k)\beta(l)... \}   \\
 - &\hat{A} \{ \phi_1(1)\alpha(1)... C_1\phi_i(i) \phi_j(j) ...  \alpha(j)\beta(i)\alpha(k)\beta(l) ... \}\\
  +    &\hat{A} \{ \phi_1(1)\alpha(1)... C_2 \phi_i(i) \phi_j(j)... \alpha(i)\beta(k)\alpha(j)\beta(l)... \} \\
 - &\hat{A} \{ \phi_1(1)\alpha(1)... C_2\phi_i(i) \phi_j(j) ...  \alpha(k)\beta(i)\alpha(j)\beta(l)... \}\\
 +& ...
       \end{aligned}
       |H|
   \begin{aligned}
     & \phi_1(1)\alpha(1)... C_1 \phi_i(i) \phi_j(j)... \alpha(i)\beta(j)\alpha(k)\beta(l)...    \\
 - & \phi_1(1)\alpha(1)... C_1\phi_i(i) \phi_j(j) ...  \alpha(j)\beta(i)\alpha(k)\beta(l) ... \\
  +    & \phi_1(1)\alpha(1)... C_2 \phi_i(i) \phi_j(j)... \alpha(i)\beta(k)\alpha(j)\beta(l)...  \\
 - &\phi_1(1)\alpha(1)... C_2\phi_i(i) \phi_j(j) ...  \alpha(k)\beta(i)\alpha(j)\beta(l)... \\
 +& ...
       \end{aligned}
         }   }
       {\Braket{   \begin{aligned}
&\hat{A} \{ \phi_1(1)\alpha(1)... C_1 \phi_i(i) \phi_j(j) ... \alpha(i)\beta(j)\alpha(k)\beta(l)... \}   \\
 - &\hat{A} \{ \phi_1(1)\alpha(1)... C_1\phi_i(i) \phi_j(j) ...  \alpha(j)\beta(i)\alpha(k)\beta(l) ... \}\\
  +    &\hat{A} \{ \phi_1(1)\alpha(1)... C_2 \phi_i(i) \phi_j(j)... \alpha(i)\beta(k)\alpha(j)\beta(l)... \} \\
 - &\hat{A} \{ \phi_1(1)\alpha(1)... C_2\phi_i(i) \phi_j(j) ...  \alpha(k)\beta(i)\alpha(j)\beta(l)... \}\\
 +& ...
       \end{aligned}
              |
   \begin{aligned}
      & \phi_1(1)\alpha(1)... C_1 \phi_i(i) \phi_j(j)... \alpha(i)\beta(j)\alpha(k)\beta(l)...    \\
 - & \phi_1(1)\alpha(1)... C_1\phi_i(i) \phi_j(j) ...  \alpha(j)\beta(i)\alpha(k)\beta(l) ... \\
  +    & \phi_1(1)\alpha(1)... C_2 \phi_i(i) \phi_j(j)... \alpha(i)\beta(k)\alpha(j)\beta(l)...  \\
 - &\phi_1(1)\alpha(1)... C_2\phi_i(i) \phi_j(j) ...  \alpha(k)\beta(i)\alpha(j)\beta(l)... \\
 +& ...
       \end{aligned}
       }}
     \label{gen_energy}
     \end{aligned}
     \end{equation}

where the kets have been expanded as well.

Since $\hat{H}$ is linear, we can look at one term at a time. If there are spin couplings, there will be $N_{sc}^22^{2N_{p}}$ total terms, since the wavefunction in the bra contains $N_{sc}2^{N_{p}}$ terms, and the ket also has  $N_{sc}2^{N_{p}}$ terms. If there are no spin couplings, there is only 1 term in the bra and ket. We will come back to the full expression later.

\section{Single term in the energy expansion}

\begin{equation}
 \begin{aligned}
 T_1 = \frac{\Braket{\hat{A} \{ \phi_1(1)\alpha(1)... \phi_i(i) \phi_j(j) \alpha(i)\beta(j)... \}
     |H|  ( \phi_1(1)\alpha(1)... \phi_i(i) \phi_j(j) \alpha(i)\beta(j)...)
     }}{\Braket{\hat{A} \{ \phi_1(1)\alpha(1)... \phi_i(i) \phi_j(j) \alpha(i)\beta(j)... \}
     |  ( \phi_1(1)\alpha(1)... \phi_i(i) \phi_j(j) \alpha(i)\beta(j)...)
    }}
    \label{t1}
     \end{aligned}
     \end{equation}

Let $T_1$ arbitrarily be one of the terms in the expansion in \ref{gen_energy}. (Leaving the $C_m$ out for simplicity.) First, let's combine the spatial and spin parts associated with each electron, and replace them with spin orbitals, $\psi$.
Also, note that an antisymmetrized product of spin orbitals can be written as a Slater determinant.

\begin{equation}
 \begin{aligned}
  &=\frac{\Braket{\hat{A} \{ \psi_1(1)\psi_2(2)...\psi_N(N) \}
     |H|  (\psi_1(1)\psi_2(2)...\psi_N(N))
     }}{\Braket{\hat{A} \{ \psi_1(1)\psi_2(2)...\psi_N(N) \}
     |  ( \psi_1(1)\psi_2(2)...\phi_N(N))
    }} \\
    &=\frac{\Braket{
    \begin{vmatrix} \psi_1(1) & \psi_1(2) & ... \\
    \psi_2(1) & \psi_2(2) & ... \\
    ... & ... & ...\\
    \psi_N(1) & ... & \psi_N(N)\\
    \end{vmatrix}
     |H|  (\psi_1(1)\psi_2(2)...\psi_N(N))
     }}{\Braket{ \begin{vmatrix} \psi_1(1) & \psi_1(2) & ... \\
    \psi_2(1) & \psi_2(2) & ... \\
    ... & ... & ...\\
    \psi_N(1) & ... & \psi_N(N)\\
    \end{vmatrix}
     |  ( \psi_1(1)\psi_2(2)...\psi_N(N))
    }}
     \end{aligned}
     \end{equation}

where there are $N$ electrons.

This can be written as the equation below, using derivations carried out in "Handbook of Computational Quantum Chemistry" by Cook, "Method of Molecular Quantum Mechanics" by McWeeney, or in the section "Calculating matrix elements" below. The section below also discusses how the cofactors are calculated in the code. (Note that this is different than Hartree-Fock, since we do not assume that orbitals are orthogonal.)

\begin{equation}
 \begin{aligned}
  &=\frac{\displaystyle \sum_{ij}^{N} d^1_{ij} h_{ij}
     + \sum_i^{N}\sum_{j<i}\sum_k^{N}\sum_{l<k}
          d^2_{ikjl} \big(\Braket{  \psi_i(1) \psi_j(2) | \psi_k(1) \psi_l(2) }-\Braket{  \psi_i(1) \psi_j(2) | \psi_l(1) \psi_k(2) } \big) }{\displaystyle \sum_{j}^{N}  d^1_{1j} \Braket{\psi_1(1) | \psi_j(1)}  }
          \label{spin_orb_form}
     \end{aligned}
     \end{equation}

Note that the nuclear repulsion term is being neglected for clarity.

\subsection{Terms in \ref{spin_orb_form} }
\subsubsection{One electron term}
    The first term in the numerator is the one electron term.

    $h_{ij} = \Braket{ \psi_i(1) | \hat{h} | \psi_j(1) }$ where $\hat{h}$ is the standard one-electron kinetic and nuclei-electron attraction operator.

    $d^1_{ij}$ is the first-order cofactor of the matrix of overlap integrals
    between the spin orbitals. That is, it is the determinant of the
    matrix of overlap integrals between spin functions with row i and column j removed, multiplied by $(-1)^{i+j}$.

To be explicit, the matrix of overlap integrals between spin functions for $N$ electrons is a $N \times N$ matrix shown in Eq. \ref{overlap}:

\begin{equation}
 \begin{aligned}
    \begin{vmatrix}
    \Braket{ \psi_1(1)| \psi_1(1) }  &  \Braket{ \psi_1(2) | \psi_2(2) } & \Braket{ \psi_1(3) | \psi_3(3) } & ... &  \Braket{ \psi_1(N) | \psi_N(N) } \\
    \Braket{ \psi_2(1) |\psi_1(1) }  &  \Braket{ \psi_2(2) | \psi_2(2) } & \Braket{ \psi_2(3) | \psi_3(3) } & ... &  \Braket{ \psi_2(N) | \psi_N(N) } \\
    \Braket{ \psi_3(1)|  \psi_1(1) }  &  \Braket{ \psi_3(2) | \psi_2(2) } & \Braket{ \psi_3(3) | \psi_3(3) } &... &  \Braket{ \psi_3(N) | \psi_N(N) } \\
    ... & ... & ... & ...& ...\\
     \Braket{ \psi_N(1)|  \psi_1(1) } &  \Braket{ \psi_N(2)|  \psi_2(2) }& \Braket{ \psi_N(3) | \psi_3(3) } & ...&  \Braket{ \psi_N(N) | \psi_N(N) }\\
    \end{vmatrix}
    \label{overlap}
         \end{aligned}
     \end{equation}

Then $d^1_{21}$ is a $(N-1) \times (N-1)$ matrix below:

\begin{equation}
 \begin{aligned}
   (-1)^{2+1} \begin{vmatrix}
    \Braket{ \psi_1(2) | \psi_2(2) } & \Braket{ \psi_1(3) | \psi_3(3) } & ... &  \Braket{ \psi_1(N) | \psi_N(N) } \\
      \Braket{ \psi_3(2) | \psi_2(2) } & \Braket{ \psi_3(3) | \psi_3(3) } &... &  \Braket{ \psi_3(N) | \psi_N(N) } \\
     ... & ... & ...& ...\\
      \Braket{ \psi_N(2)|  \psi_2(2) }& \Braket{ \psi_N(3) | \psi_3(3) } & ...&  \Braket{ \psi_N(N) | \psi_N(N) }\\
    \end{vmatrix}
    \label{cof}
         \end{aligned}
     \end{equation}

Note that by the definition of determinants, Eq. \ref{cof} is equivalent to computing:

\begin{equation}
 \begin{aligned}
    \begin{vmatrix}
   0  &  \Braket{ \psi_1(2) | \psi_2(2) } & \Braket{ \psi_1(3) | \psi_3(3) } & ... &  \Braket{ \psi_1(N) | \psi_N(N) } \\
   1 & 0& 0 & ... & 0 \\
   0  &  \Braket{ \psi_3(2) | \psi_2(2) } & \Braket{ \psi_3(3) | \psi_3(3) } &... &  \Braket{ \psi_3(N) | \psi_N(N) } \\
    ... & ... & ... & ...& ...\\
    0&  \Braket{ \psi_N(2)|  \psi_2(2) }& \Braket{ \psi_N(3) | \psi_3(3) } & ...&  \Braket{ \psi_N(N) | \psi_N(N) }\\
    \end{vmatrix}
         \end{aligned}
     \end{equation}

\subsubsection{Normalization integral}

    The denominator is the normalization integral. It is the determinant of the matrix of overlap integrals between spin orbitals, and can be expressed as in \ref{spin_orb_form}.

\subsubsection{Two electron term}
    The second term in the numerator is the two electron term.

    $\Braket{ i(1) j(2) | k(1) l(2) }$ is the electron-electron repulsion integral.

    $d^2_{ikjl}$ is the second-order cofactor of the matrix of overlap integrals
    between the spin orbitals. That is, it is the determinant of the overlap
    matrix with row i, column k, row j, and column l removed,
    multipled by $(-1)^{i+j+k+l}$.

\subsection{ Switching to spatial orbitals }
Then we pull the spin functions out of the spin orbitals, since spin integration should help remove a lot of terms.

\subsubsection{One electron term}

First we expand the spin orbitals into spatial and spin parts:

\begin{equation}
 \begin{aligned}
  & \displaystyle \sum_{ij}^{N} d^1_{ij,T_1} h_{ij,T_1}                \\
 &=  \displaystyle \sum_{ij}^{N} d^1_{ij,T_1} \Braket{ \psi_{i}(1)  | \hat{h}| \psi_{j}(1)  }      \\
 &=  \displaystyle \sum_{ij}^{N} d^1_{ij,T_1}  w_{T_1}( i(1) j(1) ) \Braket{ \phi_{i}(1)  | \hat{h}| \phi_{j}(1)  }
 \label{onee}
     \end{aligned}
     \end{equation}

Where $w_{T_1}( i(1) j(1) )$ is a function containing the integrated spins of spin orbitals $i,j$. As used above, $\phi_i$, is the spatial portion of the spin orbital $\psi_i$. The $T_1$ subscript is to be clear which term in \ref{gen_energy} this refers to. Note that each each will only differ in spin, so will only affect $d^1_{ij, T_1}$ and  $w_{T_1}( i(1) j(1) )$, not the integration over spatial orbitals.

Letting
\begin{equation}
 \begin{aligned}
 	\text{D}_{T_1, ij}^1 = d^1_{ij,T_1}  w_{T_1}( i(1) j(1) )
     \end{aligned}
     \end{equation}

     Eq. \ref{onee} can be written as

 \begin{equation}
 \begin{aligned}
 &=  \displaystyle \sum_{ij}^{N}\text{D}_{T_1, ij}^1 \Braket{ \phi_{i}(1)  | \hat{h}| \phi_{j}(1)  }
 \label{final_onee}
     \end{aligned}
     \end{equation}

This form, looping over spin orbitals, is used in the code. Note that the sum is over spin orbitals. This means that if there are doubly occupied orbitals, this form could compute the spatial orbital integral twice--once when both spins are alpha and once when both are beta. However, one-electron integrals are not too expensive, so this is not important.

\subsubsection{Normalization integral}

Similar to the one-electron term, we split the spin orbitals to the spin and spatial forms:

\begin{equation}
 \begin{aligned}
  & \displaystyle \sum_{j}^{N}  d^1_{1j,T_1} \Braket{\psi_1(1) | \psi_j(1)}_{T_1} \\
  &= \displaystyle \sum_{j}^{N}  d^1_{1j} w_{T_1}( i(1) j(1) ) \Braket{\phi_1(1) | \phi_j(1)}\\
  &= \displaystyle \sum_{j}^{N}  \text{D}_{T_1, 1j}^1 \Braket{\phi_1(1) | \phi_j(1)}
 \label{norm}
     \end{aligned}
     \end{equation}

 using the same density as in the one-electron term.

 Note that the normalization integral is the determinant of the spin orbital overlap matrix (Eq. \ref{overlap} ). The expression in Eq. \ref{norm} is an expansion in terms of cofactors along the first row. Calculating the determinant by expanding along any over row is equivalent (the cofactor provides the appropriate sign change). That is,  $\displaystyle \sum_{j}^{N}  \text{D}_{T_1, 1j}^1 \Braket{\phi_1(1) | \phi_j(1)}$ = $ \displaystyle \sum_{j}^{N}  \text{D}_{T_1, 2j}^1 \Braket{\phi_2(1) | \phi_j(1)}$ = $ \displaystyle\sum_{j}^{N}  \text{D}_{T_1, 3j}^1 \Braket{\phi_3(1) | \phi_j(1)}$ and so on.

Thus, we can write \ref{norm} as

\begin{equation}
 \begin{aligned}
  &=  \frac{\displaystyle \sum_{ij}^{N}  \text{D}_{T_1, ij}^1 \Braket{\phi_i(1) | \phi_j(1)}}{N}
    \label{final_norm}
     \end{aligned}
     \end{equation}

This form is used in the code, in the same place as the one-electron term.

\subsubsection{Two electron term}

Since two electron integrals are expensive, we don't want to recompute the integral between spatial orbitals if we already computed them.
So this time the loops are over spatial orbitals only, not spin orbitals.

\begin{equation}
 \begin{aligned}
  &  \displaystyle \sum_i^{N}  \sum_{j<i}\sum_k^{N}\sum_{l<k}
          d^2_{ikjl, T_1} \big(\Braket{  \psi_i(1) \psi_j(2) | \psi_k(1) \psi_l(2) }_{T_1}-\Braket{  \psi_i(1) \psi_j(2) | \psi_l(1) \psi_k(2) }_{T_1} \big) \\
          =& \sum_{io}^{orbitals}\sum_{jo\leq io}\sum_{ko}^{orbitals}\sum_{lo\leq ko}
          \sum_l^{\substack{spins \\ \in lo<ko}}
 \sum_i^{\substack{spins \\ \in io}}\sum_j^{\substack{spins \\ \in jo<io}}\sum_k^{\substack{spins \\ \in ko}}
  d^2_{io+i,ko+k,jo+j,lo+l,T_1}      \\
    & \Big(  w_{T_1}( i(1) j(2), k(1) l(2) ) \Braket{ \phi_{io}(1) \phi_{jo}(2) | \phi_{ko}(1) \phi_{lo}(2) }
    - w_{T_1}( i(1) j(2), l(1) k(2) ) \Braket{ \phi_{io}(1) \phi_{jo}(2) | \phi_{lo}(1) \phi_{ko}(2) } \Big)
    \label{spat}
     \end{aligned}
     \end{equation}

where $io,jo,ko,lo$ are indexes which run over spatial orbitals, not spin functions.

($io,jo,ko,lo$ are what they are called in the code, so I'm trying to match notation.)

$w_{T_1}( i(1) j(2), l(1) k(2) )$ is a function containing the integrated spins of spin orbitals $i,j,l,k$. The possible spins depend on the associated spatial orbital, since in VSVB some are doubly occupied, and can be $\alpha$ or $\beta$, and some can be unpaired or spin coupled. For the spin coupled orbitals, the spin depends on which term in the sum in Eq.\ref{gen_energy} we're evaluating. Since \ref{t1} is called $T_1$, there is a $T_1$ subscript.

Eq. \ref{spat} can be rearranged:

\begin{equation}
 \begin{aligned}
          =& \sum_{io}^{orbitals}\sum_{jo\leq io}\sum_{ko}^{orbitals}\sum_{lo\leq ko}
   \sum_i^{\substack{spins \\ \in io}}\sum_j^{\substack{spins \\ \in jo<io}}\sum_k^{\substack{spins \\ \in ko}}
   \sum_l^{\substack{spins \\ \in lo<ko}}      \\
    & \Big( \Braket{ \phi_{io}(1) \phi_{jo}(2) | \phi_{ko}(1) \phi_{lo}(2) }d^2_{io+i,ko+k,jo+j,lo+l,T_1} w_{T_1}( i(1) j(2), k(1) l(2) )        \\
    &-  \Braket{ \phi_{io}(1) \phi_{jo}(2) | \phi_{lo}(1) \phi_{ko}(2) } d^2_{io+i,ko+k,jo+j,lo+l,T_1} w_{T_1}( i(1) j(2), l(1) k(2) )  \Big)
\label{rearr_single}
    \end{aligned}
     \end{equation}

Letting
\begin{equation}
 \begin{aligned}
 	\text{D}_{T_1, io,ko,jo,lo}^2 =
       \sum_i^{\substack{spins \\ \in io}}\sum_j^{\substack{spins \\ \in jo<io}}\sum_k^{\substack{spins \\ \in ko}}
   \sum_l^{\substack{spins \\ \in lo<ko}} d^2_{io+i,ko+k,jo+j,lo+l,T_1} w_{T_1}(i(1) j(2) k(1) l(2))
     \end{aligned}
     \end{equation}

     and

\begin{equation}
 \begin{aligned}
 	\text{D}_{T_1,exch, io,ko,jo,lo}^2 =
       \sum_i^{\substack{spins \\ \in io}}\sum_j^{\substack{spins \\ \in jo<io}}\sum_k^{\substack{spins \\ \in ko}}
   \sum_l^{\substack{spins \\ \in lo<ko}}  d^2_{io+i,ko+k,jo+j,lo+l,T_1} w_{T_1}(i(1) j(2) l(1) k(2))
     \end{aligned}
     \end{equation}

The two electron part of Eq.\ref{rearr_single}  can be written as

\begin{equation}
 \begin{aligned}
          & \sum_{io}^{orbitals}\sum_{jo\leq io}\sum_{ko}^{orbitals}\sum_{lo\leq ko}
    \Big( \text{D}_{T_1, io,ko,jo,lo}^2 \Braket{ \phi_{io}(1) \phi_{jo}(2) | \phi_{ko}(1) \phi_{lo}(2) }   -  \text{D}_{T_1,exch, io,ko,jo,lo}^2\Braket{ \phi_{io}(1) \phi_{jo}(2) | \phi_{lo}(1) \phi_{ko}(2) } \Big)
    \label{final_twoe}
    \end{aligned}
     \end{equation}

     Note that the only differences between $\text{D}_{T_1, io,ko,jo,lo}^2$ and $\text{D}_{T_1,exch, io,ko,jo,lo}^2)$ are in the spin integration ($w_{T}$).  The cofactor ($d^2_{io+i,ko+k,jo+j,lo+l,T_1}$) is the same for both, so it only needs to be calculated once.

\section{Final expressions for the three terms}

For a single determinant wave function (that is, one that has no spin couplings), Eq. \ref{final_onee}, \ref{final_norm}, and \ref{final_twoe} are those which are calculated in the code.

For a muti-determinant wave function with multiple spin coupling, there is the expansion of terms as in  Eq.\ref{gen_energy}. However, the only thing that will differ between the $M_{sc} = {N_{sc}^22^{2N_p}}$ terms in Eq.\ref{gen_energy} is the cofactors and the spin, not the spatial functions.
That is, if we let Eq.\ref{gen_energy} be $T_1 + T_2 + ...+T_{M_{sc}}$, then the following expressions can be written:

\subsection{One electron term}

 \begin{equation}
 \begin{aligned}
 &=  \displaystyle \sum_{ij}^{N}  \big( \text{D}_{T_1, ij}^1 + \text{D}_{T_1, ij}^1 + \text{D}_{T_1, ij}^1 +...\big) \Braket{ \phi_{i}(1)  | \hat{h}| \phi_{j}(1)  }   \\
 &=  \displaystyle \sum_{ij}^{N}  \text{D}_{ij}^1 \Braket{ \phi_{i}(1)  | \hat{h}| \phi_{j}(1)  }
 \label{total_onee}
     \end{aligned}
     \end{equation}

where we let $\text{D}_{ij}^1 = \text{D}_{T_1, ij}^1 + \text{D}_{T_1, ij}^1 + \text{D}_{T_1, ij}^1 +...+\text{D}_{T_{M_{sc}}, ij}^1$

\subsection{Normalization integral}

Similarly, the normalization integral can be written as:

 \begin{equation}
 \begin{aligned}
 &=  \frac{\displaystyle \sum_{ij}^{N}  \text{D}_{ij}^1 \Braket{ \phi_{i}(1)  |  \phi_{j}(1)  }}{N}
 \label{total_norm}
     \end{aligned}
     \end{equation}

\subsection{Two electron term}

The expression for the two electron integral is similar:

\begin{equation}
 \begin{aligned}
         =& \sum_{io}^{orbitals}\sum_{jo\leq io}\sum_{ko}^{orbitals}\sum_{lo\leq ko}
     \Big( \big(\text{D}_{T_1, io,ko,jo,lo}^2 + \text{D}_{T_2, io,ko,jo,lo}^2 +...\big) \Braket{ \phi_{io}(1) \phi_{jo}(2) | \phi_{ko}(1) \phi_{lo}(2) }        \\
    &-  \big(\text{D}_{T_1,exch, io,ko,jo,lo}^2+\text{D}_{T_2,exch, io,ko,jo,lo}^2+...\big)\Braket{ \phi_{io}(1) \phi_{jo}(2) | \phi_{lo}(1) \phi_{ko}(2) } \Big) \\
    =&  \sum_{io}^{orbitals}\sum_{jo\leq io}\sum_{ko}^{orbitals}\sum_{lo\leq ko}
    \Big(\text{D}_{io,ko,jo,lo}^2\Braket{ \phi_{io}(1) \phi_{jo}(2) | \phi_{ko}(1) \phi_{lo}(2) }    -  \text{D}_{exch,io,ko,jo,lo}^2\Braket{ \phi_{io}(1) \phi_{jo}(2) | \phi_{lo}(1) \phi_{ko}(2) } \Big)
    \label{total_twoe}
    \end{aligned}
     \end{equation}

where we let $\displaystyle \text{D}_{io,ko,jo,lo}^2 = \sum_q^{M_{sc}} \text{D}_{T_q, io,ko,jo,lo}^2$

\section{Calculating the matrix elements}

In general, $\hat{H}$ can be split into a one-electron operator, which sums over all electrons in the system ($\displaystyle \sum_i^N\hat{O}(i)$) and a two-electron operator, which sums over all pairs of electrons in the system
($ \displaystyle  \sum_i^N  \sum_{j<i} \hat{O}(i,j)$).

To give an idea of how the integrals are calculated, here we go over the evaluation of the normalization integral, a generic one-electron operator, and the electron repulsion operator. This is only shown for a single determinant wave function, but it could be made more general.

\subsection{Normalization integral}

 \begin{equation}
 \begin{aligned}
 &= \Braket{  \psi_1(1)\psi_2(2)...\psi_N(N)  |
 \begin{vmatrix} \psi_1(1) & \psi_2(1)& \psi_3(1) & ... & \psi_N(1) \\
    \psi_1(2) & \psi_2(2) & \psi_3(2) &...& \psi_N(2) \\
    \psi_1(3) & \psi_2(3) & \psi_3(3) &...& \psi_N(3) \\
    ... & ... & ... & ...& ... \\
    \psi_1(N) & \psi_2(N) & \psi_3(N) &...& \psi_N(N)\\
    \end{vmatrix} } \\
    &=
    \Braket{
     \begin{vmatrix} \psi_1(1) &0 & 0&... & 0 \\
    0 & \psi_2(2) & 0& ...& 0\\
    0 & 0 & \psi_3(3)& ...& 0\\
    ... & ... & ... & ... &...\\
    0 & 0 & 0&...& \psi_N(N)\\
    \end{vmatrix}
    |
   \begin{vmatrix} \psi_1(1) & \psi_2(1)& \psi_3(1) & ... & \psi_N(1) \\
    \psi_1(2) & \psi_2(2) & \psi_3(2) &...& \psi_N(2) \\
    \psi_1(3) & \psi_2(3) & \psi_3(3) &...& \psi_N(3) \\
    ... & ... & ... & ...& ... \\
    \psi_1(N) & \psi_2(N) & \psi_3(N) &...& \psi_N(N)\\
    \end{vmatrix} } \\
    &=
      \Braket{
    \begin{vmatrix}
    \psi_1(1) \psi_1(1) & \psi_1(1)\psi_2(1)& \psi_1(1)\psi_3(1) & ... & \psi_1(1) \psi_N(1) \\
    \psi_2(2) \psi_1(2) & \psi_2(2)\psi_2(2) & \psi_2(2) \psi_3(2) &...& \psi_2(2)\psi_N(2) \\
    \psi_3(3) \psi_1(3) & \psi_3(3)\psi_2(3) & \psi_3(3) \psi_3(3) &...& \psi_3(3)\psi_N(3) \\
    ... & ... & ... & ...& ... \\
    \psi_N(N) \psi_1(N) &\psi_N(N) \psi_2(N) & \psi_N(N) \psi_3(N) &...& \psi_N(N)\psi_N(N)\\
    \end{vmatrix} } \\
        &=
    \begin{vmatrix}
     \Braket{\psi_1(1) \psi_1(1) }&  \Braket{\psi_1(1)\psi_2(1)}& \Braket{\psi_1(1)\psi_3(1) }& ... &  \Braket{\psi_1(1) \psi_N(1) }\\
     \Braket{\psi_2(2) \psi_1(2) }&  \Braket{\psi_2(2)\psi_2(2)} &  \Braket{\psi_2(2) \psi_3(2) }&...& \Braket{ \psi_2(2)\psi_N(2)} \\
     \Braket{\psi_3(3) \psi_1(3)} &  \Braket{\psi_3(3)\psi_2(3) }&  \Braket{\psi_3(3) \psi_3(3)} &...& \Braket{ \psi_3(3)\psi_N(3) }\\
    ... & ... & ... & ...& ... \\
    \Braket{ \psi_N(N) \psi_1(N) }& \Braket{\psi_N(N) \psi_2(N) }&  \Braket{\psi_N(N) \psi_3(N)} &...&  \Braket{\psi_N(N)\psi_N(N)}\\
    \end{vmatrix}
    \label{deriv_overlap}
 \end{aligned}
 \end{equation}

 Where we used the fact that the determinant of a diagonal matrix is the product of the terms in the diagonal, that det(A)det(B)=det(AB), and since each row depends on a different electron, and the integration is over electrons, the integration can be pulled into the determinant.

Thus, the normalization integral is the determinant of the matrix of overlap integrals between spin orbitals.

\subsection{One electron integral}

The one electron term is $\displaystyle \sum_i^N\hat{h}(i)$. First we start with $\hat{h}(1)$ and generalize from there.

 \begin{equation}
 \begin{aligned}
 &= \Braket{  \psi_1(1)\psi_2(2)...\psi_N(N)  | \hat{h}(1) |
\begin{vmatrix} \psi_1(1) & \psi_2(1)& \psi_3(1) & ... & \psi_N(1) \\
    \psi_1(2) & \psi_2(2) & \psi_3(2) &...& \psi_N(2) \\
    \psi_1(3) & \psi_2(3) & \psi_3(3) &...& \psi_N(3) \\
    ... & ... & ... & ...& ... \\
    \psi_1(N) & \psi_2(N) & \psi_3(N) &...& \psi_N(N)\\
    \end{vmatrix} } \\
    &=
    \Braket{
     \begin{vmatrix} \psi_1(1) &0 & 0&... & 0 \\
    0 & \psi_2(2) & 0& ...& 0\\
    0 & 0 & \psi_3(3)& ...& 0\\
    ... & ... & ... & ... &...\\
    0 & 0 & 0&...& \psi_N(N)\\
    \end{vmatrix}
    |\hat{h}(1) |
    \begin{vmatrix} \psi_1(1) & \psi_2(1)& \psi_3(1) & ... & \psi_N(1) \\
    \psi_1(2) & \psi_2(2) & \psi_3(2) &...& \psi_N(2) \\
    \psi_1(3) & \psi_2(3) & \psi_3(3) &...& \psi_N(3) \\
    ... & ... & ... & ...& ... \\
    \psi_1(N) & \psi_2(N) & \psi_3(N) &...& \psi_N(N)\\
    \end{vmatrix} } \\
    &=
      \Braket{
     \begin{vmatrix}
    \psi_1(1)\hat{h}(1) \psi_1(1) & \psi_1(1)\hat{h}(1)\psi_2(1)& \psi_1(1)\hat{h}(1)\psi_3(1) & ... & \psi_1(1) \hat{h}(1)\psi_N(1) \\
    \psi_2(2) \psi_1(2) & \psi_2(2)\psi_2(2) & \psi_2(2) \psi_3(2) &...& \psi_2(2)\psi_N(2) \\
    \psi_3(3) \psi_1(3) & \psi_3(3)\psi_2(3) & \psi_3(3) \psi_3(3) &...& \psi_3(3)\psi_N(3) \\
    ... & ... & ... & ...& ... \\
    \psi_N(N) \psi_1(N) &\psi_N(N) \psi_2(N) & \psi_N(N) \psi_3(N) &...& \psi_N(N)\psi_N(N)\\
    \end{vmatrix} } \\
        &=
   \begin{vmatrix}
     \Braket{\psi_1(1) \hat{h}(1)\psi_1(1) }&  \Braket{\psi_1(1)\hat{h}(1)\psi_2(1)}& \Braket{\psi_1(1)\hat{h}(1)\psi_3(1) }& ... &  \Braket{\psi_1(1)\hat{h}(1) \psi_N(1) }\\
     \Braket{\psi_2(2) \psi_1(2) }&  \Braket{\psi_2(2)\psi_2(2)} &  \Braket{\psi_2(2) \psi_3(2) }&...& \Braket{ \psi_2(2)\psi_N(2)} \\
     \Braket{\psi_3(3) \psi_1(3)} &  \Braket{\psi_3(3)\psi_2(3) }&  \Braket{\psi_3(3) \psi_3(3)} &...& \Braket{ \psi_3(3)\psi_N(3) }\\
    ... & ... & ... & ...& ... \\
    \Braket{ \psi_N(N) \psi_1(N) }& \Braket{\psi_N(N) \psi_2(N) }&  \Braket{\psi_N(N) \psi_3(N)} &...&  \Braket{\psi_N(N)\psi_N(N)}\\
    \end{vmatrix}
     \\   &=
     \sum_j^N d^1_{1j}h_{1j}
    \label{deriv_onee}
 \end{aligned}
 \end{equation}

where $d^1_{ij}$ is a first-order cofactor of the matrix of overlap integrals between spin orbitals, and $h_{1j} = \Braket{ \psi_1(1) \hat{h}(1)\psi_j(1) }$.

(Again we used the fact that the determinant of a diagonal matrix is the product of the terms in the diagonal, that det(A)det(B)=det(AB), and since each column depends on a different electron, and the integration is over electrons, the integration can be pulled into the determinant. Also that multiplying a determinant is the same as multiplying down one row or column.)

The same thing can be done for all $\hat{h}(i)$ in the one-electron term, which leads to $\Braket{\Phi| \sum_i^N\hat{h}(i) |\Phi} =  \sum_{ij}^{N} d^1_{ij} h_{ij} $.

\subsection{Two electron integral}

The two electron term is $\displaystyle \sum_i^N\sum_{j<i} \hat{g}(i,j)$. First we start with $\hat{g}(1,2)$ and generalize from there.

 \begin{equation}
 \begin{aligned}
 &= \Braket{  \psi_1(1)\psi_2(2)...\psi_N(N)  | \hat{g}(1,2) |
\begin{vmatrix} \psi_1(1) & \psi_2(1)& \psi_3(1) & ... & \psi_N(1) \\
    \psi_1(2) & \psi_2(2) & \psi_3(2) &...& \psi_N(2) \\
    \psi_1(3) & \psi_2(3) & \psi_3(3) &...& \psi_N(3) \\
    ... & ... & ... & ...& ... \\
    \psi_1(N) & \psi_2(N) & \psi_3(N) &...& \psi_N(N)\\
    \end{vmatrix} } \\
    &=
    \Braket{
     \begin{vmatrix} \psi_1(1) &0 & 0&... & 0 \\
    0 & \psi_2(2) & 0& ...& 0\\
    0 & 0 & \psi_3(3)& ...& 0\\
    ... & ... & ... & ... &...\\
    0 & 0 & 0&...& \psi_N(N)\\
    \end{vmatrix}
    |\hat{g}(1,2) |
    \begin{vmatrix} \psi_1(1) & \psi_2(1)& \psi_3(1) & ... & \psi_N(1) \\
    \psi_1(2) & \psi_2(2) & \psi_3(2) &...& \psi_N(2) \\
    \psi_1(3) & \psi_2(3) & \psi_3(3) &...& \psi_N(3) \\
    ... & ... & ... & ...& ... \\
    \psi_1(N) & \psi_2(N) & \psi_3(N) &...& \psi_N(N)\\
    \end{vmatrix} } \\
    &=
      \Braket{
     \begin{vmatrix}
    \psi_1(1)\hat{g}(1,2) \psi_1(1) & \psi_1(1) \hat{g}(1,2) \psi_2(1)& \psi_1(1) \hat{g}(1,2) \psi_3(1) & ... & \psi_1(1)\hat{g}(1,2)\psi_N(1) \\
    \psi_2(2) \psi_1(2) & \psi_2(2)\psi_2(2) & \psi_2(2) \psi_3(2) &...& \psi_2(2)\psi_N(2) \\
    \psi_3(3) \psi_1(3) & \psi_3(3)\psi_2(3) & \psi_3(3) \psi_3(3) &...& \psi_3(3)\psi_N(3) \\
    ... & ... & ... & ...& ... \\
    \psi_N(N) \psi_1(N) &\psi_N(N) \psi_2(N) & \psi_N(N) \psi_3(N) &...& \psi_N(N)\psi_N(N)\\
    \end{vmatrix} } \\
        &=  \int  \int  d1d2
   \begin{vmatrix}
      \psi_1(1)\hat{g}(1,2) \psi_1(1) & \psi_1(1) \hat{g}(1,2) \psi_2(1)& \psi_1(1) \hat{g}(1,2) \psi_3(1) & ... & \psi_1(1)\hat{g}(1,2)\psi_N(1) \\
    \psi_2(2) \psi_1(2) & \psi_2(2)\psi_2(2) & \psi_2(2) \psi_3(2) &...& \psi_2(2)\psi_N(2) \\
     \Braket{\psi_3(3) \psi_1(3)} &  \Braket{\psi_3(3)\psi_2(3) }&  \Braket{\psi_3(3) \psi_3(3)} &...& \Braket{ \psi_3(3)\psi_N(3) }\\
    ... & ... & ... & ...& ... \\
    \Braket{ \psi_N(N) \psi_1(N) }& \Braket{\psi_N(N) \psi_2(N) }&  \Braket{\psi_N(N) \psi_3(N)} &...&  \Braket{\psi_N(N)\psi_N(N)}\\
    \end{vmatrix}
     \\   &=
     \sum_k^N\sum_{l \neq k}^N d^2_{12kl}m_{kl}g_{12kl}
    \label{deriv_twoe}
 \end{aligned}
 \end{equation}

where $d^2_{ijkl}$ is a second-order cofactor of the matrix of overlap integrals between spin orbitals. $m_{kl}$ gets the proper sign, which comes from evaluating the determinant.

We used the techniques from before, but can't pull the integration for electrons 1 and 2 inside the determinant because of the $\hat{g}(1,2)$ operator, which acts on electrons 1 and 2.
However, we can still calculate the determinant using the cofactor and sign fix.

The same thing can be done for all $\hat{g}(i,j)$ in the two-electron term, which leads to

$\displaystyle \Braket{\Phi| \sum_i^N\sum_{j<i} \hat{g}(i,j) |\Phi } =   \sum_i^N\sum_{j<i} \sum_k^N\sum_{l \neq k}^N d^2_{ijkl}m_{kl}g_{ijkl}  $.

We can use symmetry of the cofactor in $k$ and $l$ to get the form in \ref{spin_orb_form}.

\section{Code}

In \emph{VALENCE}, routine vsvb\_energy calculates the one electron term, two electron term, and normalization integral. The one electron term and normalization terms are done in a loop over electrons (spin orbitals), and the two electron term is done in a loop over spatial orbitals.

The one electron term, normalization integral, and two electron term are calculated as in Eq. \ref{total_onee}, \ref{total_norm}, and \ref{total_twoe}.

The density cofactors $\text{D}_{io,ko,jo,lo}^2$ and $\text{D}_{ij}^1$ are split into alpha and beta blocks, so two determinants of half the size are calculated for each cofactor in the equations above.

%\section{Notes}

% Need to add more about how the determinant is calculated--split into alpha and beta blocks, 0s and 1s the rows/columns.

\end{document}

\begin{equation}
 \begin{aligned}
   E &= \\
     =& \frac{\Braket{ \begin{aligned}
        &\hat{A} \{ ... \phi_i(i) \phi_j(j) \alpha(i)\beta(j)... \}
     \\ &-
     \hat{A} \{  ... \phi_i(i) \phi_j(j) \alpha(j)\beta(i)...\}
     +... \end{aligned} |
     H
     |  \begin{aligned} &( ... \phi_i(i) \phi_j(j) \alpha(i)\beta(j)...)
     \\ &-
     ( ... \phi_i(i) \phi_j(j) \alpha(j)\beta(i)...)
     +... \end{aligned}
     }}{\Braket{\hat{A} \{ ... \phi_i(i) \phi_j(j) \alpha(i)\beta(j)... \}
     -\hat{A} \{ ... \phi_i(i) \phi_j(j) \alpha(j)\beta(i)...\}
     +...|  ( ... \phi_i(i) \phi_j(j) \alpha(i)\beta(j)...)
     -(  ... \phi_i(i) \phi_j(j) \alpha(j)\beta(i)...)
     +...}}
     \label{gen_energy}
     \end{aligned}
     \end{equation}


\vspace{-0\baselineskip}
\section{Geometry estimation with edge-aware depth-normal consistency}
\label{sec:approach}
\vspace{-0\baselineskip}

In our scenario, given a target image $I$, we aim at learning to estimate both depths and normals simultaneously. Formally, let $N$ be the predicted normals from our model, we embed it into the training pipeline and make it a regularization for depths estimation $D$, which helps to train a more robust model.

\vspace{-0\baselineskip}
\subsection{Framework}
\label{sub:framework}
\vspace{-0\baselineskip}

\figref{fig:pipeline} illustrates an overview of our approach. For training, we apply supervision from view synthesis following \cite{zhou2017unsupervised}. Specifically, the depth network (middle) takes only the target view as input, and
outputs a per-pixel depth map $D_t$, based on which a normal map $N_t$ is generated by the depth-to-normal layer. Then, given the $D_t$ and $N_t$, a new depth map $D_t^n$ is estimated from the normal-to-depth layer using local orthogonal compatibility between depth and normals. Both of the layers takes in image gradient to avoid non-compatible pixels involving in depth and normal conversion (detailed in \secref{sub:depth_and_normal_orthogonality}).
Then, the new depth map $D_t^n$, combined with poses and mask predicted from the motion network (left), are then used to inversely warp the source views to reconstruct the target view, and errors are back propagated through both networks. Here the normal representation naturally serves as a regularization for depth estimation. Finally, for training loss, additional to the usually used photometric reconstruction loss, we also add in smoothness over normals, which induces higher order interaction between pixels (\secref{sub:training_losses})

With the trained model, given a new image,  we infer per-pixel depth value and then compute the normal value, yielding consistent results between the two predictions.

\vspace{-0\baselineskip}
\subsection{Depth and normal orthogonality.}
\label{sub:depth_and_normal_orthogonality}
\vspace{-0\baselineskip}

In reconstruction, depth and normal are two strongly correlated information, which follows locally linear orthogonality. Formally, for each pixel $x_i$, such a correlation can be written as a quadratic minimization for a set of linear equations,
\begin{align}
\label{eq:orthognal}
&\scr{L}_{x_i}(D, N) = ||[\cdots,\omega_{ji}(\phi(x_j) - \phi(x_i)), \cdots]^T  N(x_i)||^2, \nonumber \\
&~\text{where~} \phi(x) = D(x)\ve{K}^{-1}h(x), \text{~} \|N(x_i)\|_2 = 1, \nonumber\\
&~\text{~~~~~~~~} \omega_{ji} > 0 \text{~~if~~} x_j \in \hua{N}(x_i)
\end{align}
where $\scr{N}(x_i)$ is a set of predefined neighborhood pixels of $x_i$, and $N(x_i)$ is a $3 \times 1$ vector. $\phi(x)$ is the back projected 3D point from 2D coordinate $x$. $\phi(x_j) - \phi(x_i)$ is a difference vector in 3D, and $\omega_{ji}$ is used to weight the equation for pixel $x_j$ \wrt $x_i$ which we will elaborate later.

As discussed in Sec. \ref{sec:related}, most previous works try to predict the two information independently without considering such a correlation, while only SURGE~\cite{peng2016depth} proposes to apply the consistency by a post CRF processing only over large planar regions. In our case, we enforce the consistency over the full image, and directly apply it to regularize the network to help the model learning. Specifically, to model their consistency, we developed two layers by solving \equref{eq:orthognal}, \ie a depth-to-normal layer and a normal-to-depth layer.

\textbf{Infer normals from depths.}
\label{chap:d2n}
Given a depth map $D$, for each point $x_i$, in order to get $N(x_i)$. From \equref{eq:orthognal}, we need to firstly define neighbors $\hua{N}(x_i)$ and weights $\omega_{ji}$, and then solve the set of linear equations. To deal with the first issue, we choose to use the 8-neighbor convention to compute normal directions, which considerably more robust than the 4-neighbor convention.
However, it is not always good to equally weight all pixels due to depth discontinuity or dramatic normal changes may occur nearby. Thus, for computing $\omega_{ji}$, we weight more for neighboring pixels $x_j$ having similar color with $x_i$, while weight less otherwise. Formally, in our case, it is computed as $\omega_{ji} = \exp\{-\alpha|I(x_j) - I(x_i)|\}$ and $\alpha = 0.1$.

For minimizing \equref{eq:orthognal}, one may apply a standard singular value decomposition (SVD) to obtain the solution. However, in our case, we need to embed such an operation in the network for training, and back-propagate the gradient respect to input depths. SVD is computationally non-efficient for back-propagation. Thus, we choose to use mean cross-product to approximate the minimization~\cite{jia2006using}, which is simpler and more efficient.
Specifically, from the 8 neighbor pixels around $x_i = [m, n]$, we split them to 4 pairs, where each pair of pixels is perpendicular at 2D coordinate \wrt $x_i$, and in a counter clock-wise order, \ie $\hua{P}(x_i) = \{([m-1, n], [m, n+1]), \cdot, ([m+1, n-1], [m-1, n-1])\}$.
Then, for each pair, cross product of their difference vector \wrt $x_i$ is computed, and the mean direction of the computed vectors is set as the normal direction of $x_i$. Formally, the solver for normals is written as,
\begin{align}
\label{eq:cross}
&\ve{n} = \sum_{p\in\hua{P}}(\omega_{p_{0}, x_i}(\phi(p_{0}) - \phi(x_i)) \times \omega_{p_{1}, x_i}(\phi(p_{1}) - \phi(x_i))), \nonumber \\
&N(x_i) = \ve{n} / \|\ve{n}\|_2
\end{align}
The process of calculating the normal direction for $x_i$ using one pair of pixels is in Fig. \ref{fig:d2n}.

%for each $q\in\theta(p)$, $R_q$ satisfies $(q-p)\cdot(r-p) = 0, \quad r \in R_q$.
% The normal direction of each point is computed based on the neighboring points after projecting to 3D space. The process of calculating normal direction of point $p$ is shown in Figure \ref{fig:d2n}. $\theta(p)$ is a set of neighboring (8) points of $p$.
% Take point $q \in \theta(p)$ for example. $R_{q}$ is a set of points that satisfy the requirement: when projecting to 3D space, for $\hat{q} \in \hat{\theta}(p)$ and for $\hat{r} \in \hat{R}_{q}$, $(\hat(q)-\hat(p) \cdot (\hat{r} - \hat(p)) \neq 0)$. Symbols with hat represent corresponding points in 3D space. Theoretically, the cross-product of any two non-collinear (in 3D space) vectors connecting $\hat{p}$ and $\hat{\theta}(p)$ is the normal direction $N(p)$. To reduce the possiblity that the two vectors being collinear in 3D space, we require the vectors to be perpendicular when projected in 2D plane. The normal directions are averaged when iterating $q \in \theta(p)$, and then $l_2$ normalized to make it a unit vector. The normal direction is calculated as:

\begin{figure}
\centering
\includegraphics[width=0.5\textwidth]{figures/d2n.pdf}
\caption{Illustration of computing normal base on a pair of neighboring pixels. $x_i, x_{i1}, x_{i2}$ are 2D points, and
$\phi(x_i), \phi(x_{i1}), \phi(x_{i2})$ are corresponding points projected to 3D space.
The normal direction $N(x_i)$ is computed with cross product between $\phi(x_{i1}) - \phi(x_i)$ and $\phi(x_{i2}) - \phi(x_i)$.}
\vspace{-0.3\baselineskip}
\label{fig:d2n}
\end{figure}

\textbf{Compute depths from normals.}
Due to the fact that we do not have ground truth normals for supervision, it is necessary to recover depths from normals to receive the supervision from photometric error as discussed in Sec.~\ref{sec:preliminaries}.
To recover depths, given normal map $N$, we still need to solve \equref{eq:orthognal}. However, there is no unique solution. Thus, to make it solvable, we provide an initial depth map $D_o$ as input, which might lack normal smoothness, \eg depth map from network output. Then, given $D_o(x_i)$, the depth solution for each neighboring pixel of $x_i$ is unique and can be easily computed. Formally, let $D_e(x_j | x_i) = \psi(D_o(x_i), N(x_i))$ be the solved depth value calculated for a neighbor pixel $x_j$ \wrt $x_i$.
However, when computing over the full image, we still need to solve 8 equations jointly for each pixel of the 8 neighbors. Finally, by minimum square estimation (MSE), the solution for depth of $x_i$ is,
\begin{align}
\label{eq:depth}
D_n(x_j) = \sum_{i\in\hua{N}}\hat{\omega}_{ij}D_e(x_j | x_i), \text{~~}
\hat{\omega}_{ij} = \omega_{ij} / \sum_i{\omega_{ij}}
\end{align}

 %ormal2depth layer takes depth map and normal map as input and outputs a ``shifted" depth map. Take Figure \ref{fig:d2n} for example, the depth values of points $\theta(p)$ can be calcuated with the depth and normal direction of point $p$ known. From the calculation of normal direction, $(\hat{p} - \hat{q}) \cdot N(p) = 0$. When projecting points from 2D plane to 3D space, $\hat{p} = K^{-1}p$. $\hat{p} = (\hat{x}_p),\hat{y}_p,\hat{z}_p$, $p = (x_p, y_p, z_p)$ is a homogeneous 2D point and $z_p$ is the depth value of point $p$. $K^{-1}$ is the inverse of intrinsic matrix, which is determined by the camera. In linear the equation between depth and normal direction, $(K^{-1}(x_p, y_p, z_p) - K^{-1}(x_q, y_q, z_q))\cdot N(p) = 0, q\in\theta(p)$, the only unknown $z_q$, \ie  depth value of point $q$, has a unique solution.

%As there are multiple points in the set $\theta(p)$, multiple depth maps can be recovered corresponding to each point $q \in \theta(p)$. In our pratice, the $\theta(p)$ includes 8 nearest points around point $p$. The 8 depth maps are weighted averaged to produce a reasonable depth output. As depth and normal discontinuity often happens where image gradients are large, similar to using image gradient in smoothness loss term, the image gradients are also calculated as weights to determine how much of each depth map contribute to final output. The output depth map is calculated as:
% $$z_p = \sum_{\theta(p)}z_q\frac{e^{(-\alpha|\partial_{\overrightarrow{p-q}}I_p|)}}{\sum_{\theta(p)}e^{(-\alpha|\partial_{\overrightarrow{p-q}}I_p|)}}, q\in\theta(p)$$
% In which, $\partial_{|\overrightarrow{p-q}}I_p|$ is the image gradient value along the $\overrightarrow{p-q}$ direction.
\vspace{-0\baselineskip}
\subsection{Training losses}
\label{sub:training_losses}
\vspace{-0.\baselineskip}

Given the consistency, in this section, we describe our training strategy. In order to supervise both the depth and normal predictions, we can directly apply the loss in \equref{eqn:full} by replacing the output from depth network $D_o$ with the output after our normal-to-depth layer $D_n$ to train the model. We show in our experiments (\secref{sec:experiments}), by doing this, we already outperform the previous state-of-the-art by around 10$\%$ in depth estimation using the same network architecture.

Additionally, with normal representation, we apply smoothness over neighboring normal values, which provides higher order interactive between pixels. Formally, the smoothness for normal has the same form as $\scr{L}_{s}$ in \equref{eqn:regular} for depth, while the first order gradient is applied, \ie $~\scr{L}_{s}(N, 1)$.

Last but not the least, matching corresponding pixels between frames is another central factor to find correct geometry. Additional to the photometric error from matching pixel colors, matching image gradient is more robust to lighting variations, which was frequently applied in computing optical flow~\cite{li2017pyramidal}.
In our case, we compute a gradient map of the target image and synthesized target images, and include the gradient matching error to our loss function. Formally, the loss is represented as,
\begin{equation}
\label{equ:gradient}
\scr{L}_{g}(D_n, \hua{T}, \hua{M}) = \sum_{s=1}^{S}\sum_{x_t}\ve{M}_s(x_t)\|\nabla I_t(x_t) - \nabla \hat{I_s}(x_t)\|_1, \nonumber
\end{equation}
%In the future, we hope to investigate more in matching criteria such as with stronger descriptors like SIFT~\cite{liu2011sift} or higher level convolutional features.

In summary, our final learning objective for multi-scale learning is,
\begin{align}
\label{eq:full_loss}
&\scr{L}(\hua{D}, \hua{N}, \hua{T}, \hua{M}) = \scr{L}_{o}(\{D_{nl}\}, \hua{T}, \hua{M}) + \nonumber\\
&\text{~~~~~~~~~}\sum_l\{\lambda_g\scr{L}_{g}(D_{nl},\hua{T},\hua{M}) + \lambda_n\scr{L}_{s}(N_l, 1)\}
\end{align}
where $\hua{D} = \{D_{nl}\}$ and $\hua{N} = \{N_{l}\}$ are the set of depth maps and normal maps for the target view.

%Our intuition is to train a CNN that is capable of modeling the geometry consistency of a mostly rigid scene. To facilitate the learning of the network, we explicitly propose to model the constraint between depth and normal. The training samples of the framework consist of frame sequences captured by a monocular moving camera.normalsize

\textbf{Model training.} For network architecture, similar to \cite{zhou2017unsupervised} and \cite{godard2016unsupervised}, we adopt the DispNet \cite{mayer2016large} architecture with skip connections as in \cite{zhou2017unsupervised}. All \textit{conv} layers are followed by a ReLU activation except for the top prediction layer. We train the network from scratch; since too many losses at beginning could be hard to optimize, we choose a two stage training strategy by first train the network with $\scr{L}_{o}$ with 5 epochs and then fine-tune it with the full loss for 1 epoch. We provide ablation study of each term in our experiments.

% To model a reasonable geometrical consistency, we propose the overall objective function as in Equation \ref{equ:1}.
% \begin{equation}
% \label{equ:1}
% \begin{split}
% L(D, I, Rt, \lambda) = L_{warp}(D, I, Rt) + L_{smooth}(D, N, I) \\
%  +  L_{grad}(D, I, Rt) + \lambda(L_{dn}(D, N))
% \end{split}
% \end{equation}
% Where

% This objective function is a Lagrange fuction aiming to minimize the loss term $L_{warp}(D, I, Rt) + L_{smooth}(D, N, I) \\
%  +  L_{grad}(D, I, Rt)$ subject to the constraint of geometrical constraint between depth map and normal map $L_dn(D, N) = 0$. The loss term consists of three components: photometric warping loss $L_{warp}(D, I, Rt)$, smoothness loss $L_{smooth}(D, N, I)$, image gradient matching loss $L_{grad}(D, I, Rt)$.

% By understanding the depth and normal

% \subsection{Geometry consistency}

% As depth and surface normal are not independent under the same scene,
% thus we model the 3D geometry consistency by explicitly incorporating the relationship of depth and normal into the training procedure and use the relationship as a regularization in the objective function. The regularization term $L_{dn}(D,N) = 0$ is realized by two layers in our framework: depth2normal layer and normal2depth layer.

% \subsection{Implementation details}


	\documentclass[a4paper]{article}
\newcommand{\dd}[1]{\mathrm{d}#1}
%% Language and font encodings
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}

%% Sets page size and margins
\usepackage[a4paper,top=3cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}

%% Useful packages
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage[colorlinks=true, allcolors=blue]{hyperref}
\usepackage{braket}
\usepackage{amssymb}
\usepackage{subcaption}
\usepackage[section]{placeins}
\usepackage{float}
\usepackage{color}
\restylefloat{table}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\DeclareMathOperator*{\argmin}{argmin}

\title{Gaussian Mixture Models and K-means Clustering}
\author{Shreyas Bapat, Bhavya Bhatt, GaganDeep Tomar}

\begin{document}
\maketitle

\begin{abstract}
The following discussion revolves around the modelling of data classifier in accordance with the bayesian decision theory. Unlike previous assignment here we do not assume class conditional probablity to be normal but instead we assume that the distribution is in the form of function that can be approximated as the linear combination\footnote{refer to the appendix for more discussion on functional vector space} of many gaussians(precisly k-gaussians).
\end{abstract}

\section{Introduction}

The Bayesian Decision Theory is a probablistic theory for classifying the data points on the basics of pre-known prior and class conditional probabilities(which in real scenario is not known in any closed form expression). We give below the basics of gaussian mixture models which we would use to estimate our class conditional probability and thereby applying bayesian decision rule to estimate the class of the data points. We also introduce the K-means clustering method which we would use to get the initial parameters for the GMM model.
\subsection{Gaussian Mixture Model}
In previous assignment we assumed that the class conditional probability is of the gaussian form and we there after derived the decision boundary. But in real scenario our data statistically may not be coming from such a well behaved closed form probability distribution(which here we considered normal distribution). So to make our probability distribution more general we write our class conditional probablity density as linear combination of linearly independent function vectors in infinite dimensional functional space as follows\footnote{complete discussion is given in appendix}
\begin{equation}
p(\bar{x}) = \sum_{k=1}^{K}\Pi_{k}G_{k}
\end{equation}where $K$ is number of clusters which we are using to approximate our true probability distribution\footnote{In principle we should take infinite number of such gaussians to exactly extract the true probability distribution from which data points are coming}. Now here $G_{k}$ represents gaussian distribution function with parameters as $\left(\mu_{k}, \sigma_{k}\right)$ and $\Pi_{k}$ represents the coefficients of $G_{k}$. Now to fit the above probability distribution such that it optimize the cost function we estimate the parameter vector $\bar{\theta}=\left[\Pi_{k}, \mu_{k}, \sigma_{k}\right] \forall k$ which would do this job.
\subsection{Maximum Loglikelihood Estimate of GMM and Clustering}
We know that to estimate the parameters we optimize the total loglikelihood which is as follows
\begin{equation}
p(D|\bar{\theta})=\prod_{n=1}^{N}p(\bar{x_{n}}|\bar{\theta})p(\bar{\theta})
\end{equation}Now we optimize the log likelihood function $l(\bar{\theta})$ to estimate the unknown parameter vector $\bar{\theta}$
\[
\begin{split}
l(\bar{\theta})&=\sum_{n=1}^{N}\ln(p(\bar{x_{n}}|\bar{\theta})p(\bar{\theta})) \\
               &=\sum_{n=1}^{N}\ln(\sum_{k=1}^{K}\Pi_{k}G_{k}(\bar{x_{n}}))
\end{split}
\]But Now we cannot apply our conventional approach as it would be too complex to evaluate, the reason behind is that we don't know how different data points are distributed under different gaussians in the summation. If we would know that then we can separately apply optimization for each gaussian considering only those data points which are coming from that gaussain itself. This problem of dividing the data points based on some distance measure is what we call clustering of the data set into $K$ clusters. We mention below the most common algorithm used for clustering, K-means clustering.
\subsection{K-means Clustering}
The cost function considered here is measure of distortion which is defined as follows
\begin{equation}
J=\sum_{n=1}^{N}\sum_{k=1}^{K}z_{nk}\left|\bar{x_{n}}-\bar{\mu_{k}}\right|^2
\end{equation}where $z_{nk}$ is $kth$ component of one-hot encoded vector $\bar{z_{n}}=\left[0\dots1\dots0\right]$ where one is at the position equal to the number of cluster to which $\bar{x_{n}}$ data points belong to. Now during optimization we have now an additional latent information variable $\bar{z}$ which is also a parameter to be optimized. But how we define that a particular data point belong to some particular cluster? the following is the criteria to assign the cluster number and thus vector $\bar{z}$ to a data point
\[
cluster\ to\ which\ x_{n}\ belongs\ to = \argmin_{k}\left|\bar{x_{n}}-\mu_{k}\right|
\]Now this is somewhat similar to chicken-egg problem because of the interdependence of $\bar{z}$ and $\mu_{k}$. So to deal with this kind of optimization\footnote{this type of optimization problems are called ill-posed optimization problem} we employ EM method of optimization. The algorithm is given below
\begin{algorithm}
\caption{Euclid’s algorithm}\label{alg:euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\section{Coutour Curves and Covariance Matrix}\label{appendix}
In this section\footnote{for a complete discussion refer to the appendix} we discuss the relation between the shape of the cross section produced by slicing the bivariate gaussian distribution with a hyperplane parallel to the 2D-feature plane and covariance matrix. The bivariate gaussian distribution is a follows
\begin{equation}
P(\bar{x}|C_{i}) = \frac{1}{\sqrt{det(2\pi\mathbf{\Sigma_{i}})}}exp\{\frac{-1}{2}(\bar{x}-\bar{\mu_{i}})^{\intercal}\mathbf{\Sigma_{i}}^{-1}(\bar{x}-\bar{\mu_{i}})\}
\end{equation}with $mu_{i}$ be a $2\times1$ mean column vector and $\Sigma_{i}$ be $2\times2$ covariance matrix. So we assume the covariance matrix in its expanded form as
\[
\mathbf{\Sigma} = \left[ {\begin{array}{cc}
\Sigma_{11} & \Sigma_{12} \\
\Sigma_{12} & \Sigma_{22} \\
\end{array}} \right]
\]
where diagonal terms are variance of the features and off-diagonal terms are covariance bewteen feature-1 and feature-2. The above matrix is symmetric precisly due to the fact that $cov(x_{i}, x_{j})=cov(x_{j}, x_{i})$. Now we set the above distribution function to some constant $k$ and find the resultant curve projected on the feature space which is called constant contour curve.
\[
\frac{1}{\sqrt{det(2\pi\mathbf{\Sigma_{i}})}}exp\{\frac{-1}{2}(\bar{x}-\bar{\mu_{i}})^{\intercal}\mathbf{\Sigma_{i}}^{-1}(\bar{x}-\bar{\mu_{i}})\} = k
\]after some manupilation and taking log both sides we get
\[
(\bar{x}-\bar{\mu})^{\intercal}\mathbf{\Sigma}^{-1}(\bar{x}-\bar{\mu})=-2\ln(\sqrt{2\pi\left|\mathbf{\Sigma}\right|}k)
\]where we have dropped the index $i$ for simplicity and the whole analysis can be done without the loss of generality. Now writing the matrix in full and evaluating the required operation on the column vector we get finally
\begin{equation}\label{cov}
\Sigma_{22}X_{1}^{2} + \Sigma_{11}X_{2}^{2} - 2\Sigma_{12}X_{1}X_{2} + 2\ln(\sqrt{2\pi\left|\mathbf{\Sigma}\right|}k)=0
\end{equation}where $\bar{x}=\left[x_{1}x_{2}\right]^{\intercal}$, $\bar{\mu}=\left[\mu_{1} \mu_{2}\right]^{\intercal}$, $X_{1}=x_{1}-\mu_{1}$ and $X_{2}=x_{2}-\mu_{2}$. This equation is in the form of general equation for conic section
\begin{equation}
ax^{2}+by^{2}+cxy+d=0
\end{equation}Now in our case the coefficients $a$ and $b$ are $\Sigma_{22}$ and $\Sigma_{11}$ respectively. The above equation thus represents an ellipse in our case as variances are always positive values. Now from the elementary analysis of conics we know that the coefficient of $xy$ represent the extend to which the ellipse is titled w.r.t to the axis. Also the coefficients of $x^{2}$ and $y^{2}$ represents the length of major and minor axis respectively. Now we consider $\Sigma_{12}=0$ (covariance matrix is diagonal) then the we recover the familiar equation of ellipse with major and minor axis parallel to the x-y axis. The equation is
\begin{equation}
\frac{X_{1}^{2}}{\left(\frac{-2\ln(\sqrt{2\pi\left|\mathbf{\Sigma}\right|}k)}{\Sigma_{22}}\right)}+\frac{X_{2}^{2}}{\left(\frac{-2\ln(\sqrt{2\pi\left|\mathbf{\Sigma}\right|}k)}{\Sigma_{11}}\right)}=1
\end{equation}which is of the form
\[
\frac{x^{2}}{A^{2}}+\frac{y^{2}}{B^{2}}=1
\]the above is the equation of countour curve projected in the feature space. Now we consider following three cases
\bibliographystyle{alpha}
\bibliography{sample}
\end{document}


\section{Fourier}%
\label{fourier.detailed}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Fourier folder}%
\label{fourier.folder}

\begin{figure}[h]
$$\image{0cm;0cm}{FFolder.eps}$$
\caption{The ``Fourier'' folder}
\end{figure}

This folder shows all the necessary information that has to do with
Fourier transformation.

The basic idea behind this folder is that different Fourier calculations
can be kept in memory simultaneously and that the setting for each of these
{\it Fourier sets} can be restored with a click of the mouse.

Because of this possibility there is a need for a {\bf Title} for
identification by the user.
Then there are {\bf From} and {\bf To}, which define the frequency range,
for which the Fourier spectrum should be calculated.
As with the \helpref{fit module}{period.folder} there is the possibility
to use weighted data for the Fourier calculations.

 In the next line the Nyquist frequency
is displayed. This value should be a good
estimate for the upper limiting frequency due to the sampling pattern in
the currently selected time string.
This frequency value given does {\it not} depend only on the time base
and the number of points in the currently selected time string,
but uses a more sophisticated algorithm, estimating the average
time gap between neighboring points, ignoring large gaps.

With the choice item {\bf Step rate} it is possible to change the accuracy
of the Fourier calculation. This means, that the step at which the frequency
range is sampled can be changed. {\bf High, medium and low} are good
estimates (High uses most time to calculate).
With {\bf Custom} a user defined value can be chosen
in the text item next to it.
For the other 3 possibilities the text item can not be edited and
will contain the value that would be used in this case.

Next the {\bf highest peak} line gives the highest peak that was
found during the calculation with the coresponding frequency and amplitude.

With {\bf Calculations based on} it is possible to select different data types
for the calculation. These correspond to the values that can be displayed in
the \helpref{Time string graph}{timestring.graph} and
\helpref{Time string table}{timestring.table}%
. {\bf Spectral window }allows the calculation of the spectral window centered
at zero frequency for the currently selected time string.

The {\bf Compact} option allows to limit the - often very massive - output
of Fourier, because with {\bf Peaks only} only the local minima and maxima
are kept.

The \button{Calculate} takes all the settings above and
calculates a new Fourier spectrum with this. (See
\helpref{Fourier calculate}{fourier.calculate}
for more details.

In the list box below the \button{Calculate}, all the previous
Fourier calculations are listed with their title. Selecting one of these will
update all the values above to the values used for that specific
calculation.

With the \button{Delete} the currently selected Fourier calculation
can be removed from memory - only the log file entries will remain.
The \button{Rename} allows to change the title for the currently
selected Fourier calculation. To do so, the
\helpfigref{rename title dialog}{fourier.rename.dialog} is opened.
\begin{figure}[h]
$$\image{0cm;0cm}{FRename.eps}$$%
\caption{The ``rename title'' dialog}%
\label{fourier.rename.dialog}
\end{figure}

The \button{Display data} opens up the
\helpref{Fourier data window}{fourier.table}
and allows examination of the output of the calculation.

Next the \button{Export} allows the output to be written to a file.
(See \helpref{Fourier export}{fourier.export} for details.)

Finally the \button{Display graph} opens up the
\helpref{Fourier graph window}{fourier.graph}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Fourier calculate}%
\label{fourier.calculate}
When starting a Fourier calculation with the \button{Calculate} in
the \helpref{Fourier folder}{fourier.folder}%
, the main window will change
to represent the
\helpfigref{current status of calculation}{fourier.calculate.dialog}%
. In this case it will give the percentage of the calculations completed.
Again the \button{Cancel} can stop the calculations.
\begin{figure}[h]
$$\image{0cm;0cm}{FCalculate.eps}$$%
\caption{The ``Fourier calculate'' window}%
\label{fourier.calculate.dialog}
\end{figure}

In the case of {\bf observed} or {\bf adjusted} values selected
for calculations, the user will first be
asked if the average zero point should be subtracted with the
\helpref{zero point dialog}{fourier.zeropoint}%
.

After the calculation has finished, the
\helpfigref{include dialog}{fourier.include.dialog}
will show up and ask, if the currently found highest peak
should be included in the list of frequencies of the
\helpref{Fit module}{period.folder}%
.

\begin{figure}[h]
$$\image{0cm;0cm}{FInclude.eps}$$%
\caption{The ``include'' dialog}%
\label{fourier.include.dialog}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Zero point}%
\label{fourier.zeropoint}
The Fourier transform has the property, that if the mean amplitude
of the currently selected time string for calculation is {\it not Zero}
additional features in the low frequency range will show up.
This is due to this Zero point shift and the features showing up are
comparable to a scaled spectral window centered at frequency 0.
This feature may even dominate the whole spectrum.

To overcome this problem \period will ask with the
\helpfigref{zero point dialog}{fourier.zeropoint.dialog}, if
it should subtract a (calculated) zero point, when the calculations
are based on {\bf Original} or {\bf Adjusted} data.
\begin{figure}[h]
$$\image{0cm;0cm}{FZeropoint.eps}$$%
\caption{The ``zero point'' dialog}%
\label{fourier.zeropoint.dialog}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Fourier export}%
\label{fourier.export}

\subsection{Fourier table}%
\label{fourier.table}
This \helpfigref{window}{fourier.table.window},
which shows up, when the \button{Display data} in the
\helpref{Fourier folder}{fourier.folder} has been pressed,
shows a table of {\bf frequencies, Fourier amplitudes and Fourier power},
of the currently active Fourier calculation.
\begin{figure}[h]
$$\image{0cm;0cm}{FTable.eps}$$%
\caption{The ``Fourier table'' window}%
\label{fourier.table.window}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Fourier graph}%
\label{fourier.graph}
The \helpfigref{Fourier graph window}{fourier.graph.window} will show up,
when the \button{Display Graph} in the
\helpref{Fourier folder}{fourier.folder} has been pressed.
It will show the graph of the currently active Fourier calculation.
\begin{figure}[h]
$$\image{0cm;0cm}{FGraph.eps}$$%
\caption{The ``Fourier graph'' window}%
\label{fourier.graph.window}
\end{figure}

Actually many of the menus are the same as for the
\helpref{time string graph}{timestring.graph}.

The only new feature is the \menu{display}, in which it is
possible to change the graph to display {\it power} instead of {\it amplitude}
with the \menuentry{Use power}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Noise}%
\label{fourier.noise}
Sadly observations are not perfect and usually contaminated with noise.
This noise may come from many sources: Observations, instrumentation,
the object itself, modes (frequencies) not yet found in the spectra, etc\ldots

As this noise is thus only pseudo-random there is no way to eliminate
it during data reduction.
Thus all the noise produces some unpredictable pattern in the Fourier spectrum.
So it is possible, that some peaks in the Fourier spectrum are found that
are not present in the object observed.

But how can noise be separated from a real signal?
There is never a guarantee for a sure identification,
but observational
(\urlref{Breger et al., 1993}{A\&A 271,482})
and numerical simulations
(\urlref{Kuschnig et al.,1997}{A\&A 328,544})
have shown, that the {\it ratio} between {\it signal} and {\it noise} in
amplitude should at least be 4.0 to give good confidence.

So {\bf signal} is defined as either
the amplitude in the {\bf Fourier spectrum}
or the amplitude of the {\bf least square fit solution}
for a certain peak, and {\bf noise} is defined as the average amplitude
in a close frequency range to the peak unde consideration
after subtracting the frequency with the
\helpref{fit module}{period.folder}
and using the resulting residuals for the calculation of noise.
The \helpref{Fourier module}{fourier.folder} offers two options
to calculate noise.
These are:
\helpref{Noise at frequency}{fourier.noisefrequency}
and
\helpref{Noise spectrum}{fourier.noisespectrum},
which both can be reached from the \menu{Fourier}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{Noise at frequency}%
\label{fourier.noisefrequency}
When the \menuentry{Noise at frequency} in the \menu{Fourier}
has been selected, the
\helpfigref{noise at frequency dialog}{fourier.noisefrequency.dialog}
will show up.
\begin{figure}[h]%
\label{fourier.noisefrequency.dialog}
$$\image{0cm;0cm}{FNoise.eps}$$%
\caption{The ``noise at frequency'' dialog}%
\end{figure}

Most of the contents of this dialog are similar to that of the
\helpref{Fourier folder}{fourier.folder}.

The only difference is that the frequency at which the noise
calculations should be centered can be entered as well as
the extent of the range.

After the \button{OK} has been pressed and the calculation has finished
a window will pop up and show the result.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{Noise spectrum}%
\label{fourier.noisespectrum}
The noiselevel is not necessarily constant in the whole frequency spectrum.
The {\bf Fourier module} also supports the possibility to calculate a
{\bf noise spectrum}.

This is basically the same procedure as for
\helpref{noise at frequency}{fourier.noisefrequency}, but the calculation
is repeated at different parts of the frequency range.

When the \menuentry{Noise spectrum} in the \menu{Fourier}
has been selected, the
\helpfigref{noise spectrum dialog}{fourier.noisespectrum.dialog}
will show up.
\begin{figure}[h]
$$\image{0cm;0cm}{FNoiseSpectrum.eps}$$%
\caption{The ``noise spectrum'' dialog}%
\label{fourier.noisespectrum.dialog}
\end{figure}

The dialog is very similar to that of
\helpref{noise at frequency}{fourier.noisefrequency}.
The main difference is that the frequency range {\bf from} and {\bf to}
has to be entered as well as the spacing between consecutive
noise values centered at frequencies of interest.
After the \button{OK} has been pressed and the calculation has finished,
a window will pop up and show the results.

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