\section{LOGICALOPS Logical Array Operators}

\subsection{Usage}

There are three Boolean operators available in FreeMat.  The syntax for their use is:
\begin{verbatim}
  y = ~x
  y = a & b
  y = a | b
\end{verbatim}
where \verb|x|, \verb|a| and \verb|b| are \verb|logical| arrays.  The operators are
\begin{itemize}
\item  NOT (\verb|~|) - output \verb|y| is true if the corresponding element of \verb|x| is false, and ouput \verb|y| is false if the corresponding element of \verb|x| is true.

\item  OR (\verb|||) - output \verb|y| is true if corresponding element of \verb|a| is true or if corresponding element of \verb|b| is true (or if both are true).

\item  AND (\verb|\&|) - output \verb|y| is true only if both the corresponding elements of \verb|a| and \verb|b| are both true.

\end{itemize}
The binary operators AND and OR can take scalar arguments as well as vector arguments, in which case, the scalar is operated on with each element of the vector.
As of version 1.10, FreeMat supports \verb|shortcut| evaluation.  This means that
if we have two expressions
\begin{verbatim}
  if (expr1 & expr2)
\end{verbatim}
then if \verb|expr1| evaluates to \verb|false|, then \verb|expr2| is not evaluated at all.
Similarly, for the expression
\begin{verbatim}
  if (expr1 | expr2)
\end{verbatim}
then if \verb|expr1| evaluates to \verb|true|, then \verb|expr2| is not evaluated at all.
Shortcut evaluation is useful for doing a sequence of tests, each of which is
not valid unless the prior test is successful.  For example,
\begin{verbatim}
  if isa(p,'string') & strcmp(p,'fro')
\end{verbatim}
is not valid without shortcut evaluation (if \verb|p| is an integer, for example,
the first test returns false, and an attempt to evaluate the second expression
would lead to an error).  Note that shortcut evaluation only works with scalar
expressions.
\subsection{Examples}

Some simple examples of logical operators.  Suppose we want to calculate the exclusive-or (XOR) of two vectors of logical variables.  First, we create a pair of vectors to perform the XOR operation on:
\begin{verbatim}
--> a = (randn(1,6)>0)

a = 

 1 0 0 1 0 1 

--> b = (randn(1,6)>0)

b = 

 0 1 0 0 0 1 


\end{verbatim}
Next, we can compute the OR of \verb|a| and \verb|b|:
\begin{verbatim}
--> c = a | b

c = 

 1 1 0 1 0 1 


\end{verbatim}
However, the XOR and OR operations differ on the fifth entry - the XOR would be false, since it is true if and only if exactly one of the two inputs is true.  To isolate this case, we can AND the two vectors, to find exactly those entries that appear as true in both \verb|a| and \verb|b|:
\begin{verbatim}
--> d = a & b

d = 

 0 0 0 0 0 1 


\end{verbatim}
At this point, we can modify the contents of \verb|c| in two ways -- the Boolean way is to AND \verb|\sim d| with \verb|c|, like so
\begin{verbatim}
--> xor = c & (~d)

xor = 

 1 1 0 1 0 0 


\end{verbatim}
The other way to do this is simply force \verb|c(d) = 0|, which uses the logical indexing mode of FreeMat (see the chapter on indexing for more details).  This, however, will cause \verb|c| to become an \verb|int32| type, as opposed to a logical type.
\begin{verbatim}
--> c(d) = 0

c = 

 1 1 0 1 0 0 


\end{verbatim}
