\documentclass{article}
\usepackage{marginnote}

\begin{document}

\title{DMRG++ Feature Proposal:\\
Operators Expressions}
\author{various}
\maketitle

\marginnote{Description of this Feature} 
This feature proposes to allow for \emph{operator expressions} 
(\texttt{opExpr} for short) where an \texttt{opSpec} was expected before.
For the definition of \texttt{opSpec} see the manual.
In this proposal, we shall first define \texttt{opExpr}, then explain
where it can be used, and then explain the implementation.

\marginnote{Definitions}{\textsc An operator expression} is an algebraic expression involving
floating point numbers and operator specifications or \texttt{opSpec}s.
For example, 
\begin{verbatim}
3*nup + ndown*(1.5*:sz.txt - 3.5*(c?1'*c?1 + I)).
\end{verbatim}
A \emph{canonical operator expression} is an operator expression composed of one or
more canonical operator terms separated by the plus $+$ sign.
A \emph{canonical operator term} is a string that starts with an 
optional canonical scalar,
followed by a star $*$, followed by one or more \texttt{opSpec}s concatenated
by starts $*$.

A canonical scalar is of one the following 
forms.
\begin{enumerate}
\item One or more digits, followed by an optional dot $.$, followed by zero or
more digits. For example, $3.5$, $3.$, $3$.
\item A dot $.$ followed by one or more digits. For example, $.5$.
\item A parenthesis followed by an optional minus sign $-$, 
followed by a scalar prefix of form (1) or (2) above, and ending in a
parenthesis.
For example, $(-3.5)$ $(3)$ $(.5)$ $-(.5)$ $(3.)$ $(-3.)$.
\end{enumerate}
Note that these scalars are \emph{not} canonical: $+3.5$, $(+3.5)$, $-3.5$.
The sample expression above in canonical form is
\begin{verbatim}
3*nup + 1.5*ndown*:sz.txt + (-3.5)*ndown*c?1'*c?1 + 3.5*ndown*I.
\end{verbatim}

\marginnote{Where expressions (canonical or general) can be used} 
An \texttt{opExpr} can be used in the following cases.

(1) In a bare braket spec:
 \texttt{braketSpec = opExpr;opExpr;...}
(2) In a dressed braket spec:
 \texttt{braketSpec =} \texttt{<bra|opExpr;opExpr;...|ket>}.
(3) After the input label \texttt{OperatorExpression=} when
 \texttt{TSPOperator=expression} is specified.
And (4) after the option \verb!-e! in the operator driver.

We shall first implement canonical operator expressions, and then general expressions,
as we now describe.
To compute a canonical expression we split on $+$, and obtain one or more terms.
For each term we split on $*$. We check if the first factor is a 
canonical scalar and take note. All remaining factors are \texttt{opSpec}s.
We loop over each factor, multiplying them as we go, and
we finally multiply by the optional scalar. This term is then added to an
accumulation variable that yields the final result at the end of the process.

\marginnote{Implementation and Coder Perspective}
For a general operator expression, we first assign a depth to each
opening parenthesis.\footnote{We assign depth to opening parentheses as follows. 
We set counter=0. Each
time we find an opening parenthesis we assign it depth=counter, and increase counter.
Each time we find a closing parenthesis we decrease counter.}
We then find the deepest opening parenthesis and its closing parenthesis;
we take the contents, which shall contain no parentheses, and is thus
almost canonical. We canonicalize this expression by removing all leading $+$ signs;
by converting $-opsec...$ into $+(-1)*opsec...$; and 
by converting $-scalar$ into $+(-scalar)$. We evaluate the resulting
canonical expressions, label it uniquely, and replace it in
the full original operator expression. We proceed recursively until
there are no more parentheses left.
Syntax in errors in expression shall be found while parsing it in the way
just described.



\end{document}
