\chapter{Gretl and Julia}
\label{chap:gretlJulia}

\section{Introduction}
\label{Julia-intro}

According to \url{julialang.org}, \app{Julia} is ``a high-level,
high-performance dynamic programming language for technical computing,
with syntax that is familiar to users of other technical computing
environments. It provides a sophisticated compiler, distributed
parallel execution, numerical accuracy, and an extensive mathematical
function library.'' \app{Julia} is well known for being very fast;
however, you should be aware that by default starting \app{Julia}
takes some time due to Just-in-Time compilation of the input. This
fixed cost is well worth bearing if you are asking \app{Julia} to
perform a big computation, but small jobs are likely to run faster if
you use the (\app{Julia}-specific) \option{no-compile} option with the
\texttt{foreign} command.\footnote{Caveat: it seems that this option
  is not supported by all builds of \app{Julia}.}

\section{Julia support in gretl}
\label{sec:Julia-support}

The support offered for \app{Julia} in gretl is similar to that
offered for \app{Octave} (chapter~\ref{chap:gretlOctave}). You can
open and edit \app{Julia} scripts in the gretl GUI.  Clicking
the ``execute'' icon in the editor window will send your code to
\app{Julia} for execution. In addition you can embed \app{Julia}
code within a gretl script using a \texttt{foreign} block, as
described in connection with \app{R}.

When you launch \app{Julia} from within gretl one variable and
two convenience functions are pre-defined, as follows.
\begin{code}
gretl_dotdir
gretl_loadmat(filename, autodot=true)
gretl_export(M, filename, autodot=true)
\end{code}
The variable \verb|gretl_dotdir| holds the path to the user's ``dot
directory.''  The first function loads a matrix of the given
\texttt{filename} as written by gretl's \texttt{mwrite}
function, and the second writes matrix \texttt{M}, under the given
\texttt{filename}, in the format wanted by gretl.

By default the traffic in matrices goes via the dot directory on the
\app{Julia} side; that is, the name of this directory is prepended to
\texttt{filename} for both reading and writing. (This is complementary
to use of the \textsl{export} and \textsl{import} parameters with
gretl's \texttt{mwrite} and \texttt{mread} functions, respectively.)
However, if you wish to take control over the reading and writing
locations you can supply a zero value for \texttt{autodot} (or give an
absolute path) when calling \verb|gretl_loadmat| and
\verb|gretl_export|: in that case the \texttt{filename} argument is
used as is.

\section{Illustration}

Listing~\ref{Julia-example} shows a minimal example of how to interact
with Julia from a gretl script.

\begin{script}[htbp]
  \scriptcaption{Simple \app{Julia} I/O example}
  \label{Julia-example}
\begin{scode}
set verbose off

matrix A = mnormal(4,4)                # generate a random matrix
mwrite(A, "A", 1)                      # and save it to a file

foreign language=julia                 # call Julia
    print("Hi from Julia!\n");         # output a string
    A = gretl_loadmat("A");            # grab the matrix from gretl
    gretl_export(inv(A), "iA.mat");    # and save its inverse
end foreign                            # go back to gretl

matrix iA = mread("iA.mat", 1)         # read the inverse from Julia
matrix check = A * iA                  # compute the product
print check                            # print out the check (should be I)
\end{scode}
Output (good approximation to identity matrix):
\begin{code}
Hi from Julia!

check (4 x 4)

      1.0000   6.9389e-18   1.6653e-16   1.6653e-16 
      0.0000       1.0000       0.0000       0.0000 
 -4.4409e-16  -8.3267e-17       1.0000  -6.6613e-16 
 -4.4409e-16  -1.3878e-17  -1.1102e-16       1.0000 
\end{code}
\end{script}

Since this is a very small job JIT compilation is not worthwhile; in
our testing the script runs almost 4 times faster if the \app{Julia}
block is opened with
\begin{code}
foreign language=julia --no-compile
\end{code}
This has the effect of passing the option \option{compile=no} to the
\app{Julia} executable.

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: "gretl-guide"
%%% End: 

