diff --git a/blog-langref.tex b/blog-langref.tex index c12bee6..de5c789 100644 --- a/blog-langref.tex +++ b/blog-langref.tex @@ -47,7 +47,7 @@ \renewcommand{\optional}[1]{} %uncomment for published version \mkclean -\newcommand{\blogversion}{0.9\xspace} +\newcommand{\blogversion}{0.10\xspace} \title{The \bl Language Reference \\ {\large (\bl version \blogversion)} @@ -957,6 +957,20 @@ \section{Issuing queries}\label{query-section} \end{blogcode} +\section{Input/Output} +BLOG provides an utility function to load data from a file. +\blog|loadRealMatrix(filename)| +\blog|loadRealMatrix(filename, row-num)| + +The data must contain same number of columns in each row, and separated by commas or spaces. + +For example, to load feature matrix from the file "feature.txt". +\begin{blogcode} +fixed RealMatrix feature = loadRealMatrix("feature.txt"); +\end{blogcode} + +To output the results to a file, one can issue the command with the option \verb|-o [filename]| and \verb|--writer [writer-class-name]|. The latter is used to specify the output format. Valid options are \verb|blog.io.TableWriter| (default), \verb|blog.io.JsonWriter|, and \verb|blog.io.DummyWriter| (for omitting output). + \section{Extending \bl}\label{extending-section} \input{extending.tex} @@ -1109,11 +1123,6 @@ \section{Built-in distributions}\label{builtin-distribution-appendix} %% import urn-ball; %% \end{blogcode} %% -%% \subsection{Referring to external library}\label{-section} -%% \begin{blogcode} -%% extern blog.distribution.*; -%% \end{blogcode} -%% It will make all implementation of Distribution visible to blog inference engine. %% %% %% \section{Variable size array}\label{-section} diff --git a/built-in-funcs.tex b/built-in-funcs.tex index 7239dba..a864e9a 100644 --- a/built-in-funcs.tex +++ b/built-in-funcs.tex @@ -14,7 +14,7 @@ \subsection{Unary operators} \midrule \verb|-| & Integer & Integer & minus \\ \verb|-| & Real & Real & minus \\ -\verb|-| & RealMatrix & RealMatrix & minus \\ +%\verb|-| & RealMatrix & RealMatrix & minus \\ % not supported yet \verb|!| & Boolean & Boolean & negation \\ \bottomrule \end{tabular} @@ -106,38 +106,72 @@ \subsection{Matrix operations} that the function \verb|eye| takes a single \verb|Integer| and produces a \verb|RealMatrix|. -\blog|RealMatrix inv(RealMatrix x)| -\myindent Inverse of the matrix \verb|x|. +\blog|RealMatrix abs(RealMatrix m)| +\myindent Element-wise absolute value. -\blog|RealMatrix transpose(RealMatrix x)| -\myindent Transpose of the matrix \verb|x|. +\blog|RealMatrix colsum(RealMatrix m)| +\myindent Column-wise sum of a matrix. +For example, \verb|colsum([1, 2; 3, 4])| returns \verb|[4, 6]|. \blog|Real det(RealMatrix x)| \myindent Determinant of the matrix \verb|x|. -\blog|Real trace(RealMatrix x)| -\myindent Trace of the matrix \verb|x|, i.e. the sum of the elements on the diagonal. - \blog|RealMatrix diag(RealMatrix vals)| \myindent Diagonal matrix with the given values on the diagonal. \verb|vals| must be a column vector. -\blog|RealMatrix repmat(RealMatrix m, Integer rows, Integer cols)| -\myindent Return the matrix \verb|m| tiled \verb|rows| times vertically and -\verb|cols| -times horizontally. +\blog|RealMatrix exp(RealMatrix m)| +\myindent Element-wise exponential of a matrix. -\blog|RealMatrix sum(RealMatrix m)| -\myindent Column-wise sum of a matrix. -For example, \verb|sum([1, 2; 3, 4])| returns \verb|[4, 6]|. +\blog|RealMatrix getcol(RealMatrix m, Integer i)| +\myindent get i-th column of a matrix. + +\blog|RealMatrix getcols(RealMatrix m, Integer i, Integer j)| +\myindent return a sub-matrix from i-th (inclusive) to j-th (inclusive) column of a matrix. + +\blog|RealMatrix getrow(RealMatrix m, Integer i)| +\myindent get i-th row of a matrix. + +\blog|RealMatrix getrows(RealMatrix m, Integer i, Integer j)| +\myindent return a sub-matrix from i-th (inclusive) to j-th (inclusive) row of a matrix. \blog|RealMatrix hstack(RealMatrix arg1, ...)| \blog|RealMatrix hstack(Real arg1, ...)| \myindent Stack scalars or matrices horizontally. Accepts an arbitrary number -of -arguments. +of arguments. For example, \verb|hstack(1, 2, 3)| returns the row vector {\tt [1, 2, 3]}. +\blog|RealMatrix inv(RealMatrix x)| +\myindent Inverse of the matrix \verb|x|. + +\blog|RealMatrix log(RealMatrix m)| +\myindent Element-wise logarithm of a matrix. + +\blog|Real matsum(RealMatrix m)| +\myindent Total sum of a matrix. +For example, \verb|matsum([1, 2; 3, 4])| returns \verb|10|. + +\blog|Integer numrows(RealMatrix m)| +\myindent Number of rows of a matrix. + +\blog|Integer numcols(RealMatrix m)| +\myindent Number of columns of a matrix. + +\blog|RealMatrix rowsum(RealMatrix m)| +\myindent Row-wise sum of a matrix. +For example, \verb|rowsum([1, 2; 3, 4])| returns \verb|[[3, 7]]|. + +\blog|RealMatrix transpose(RealMatrix x)| +\myindent Transpose of the matrix \verb|x|. + +\blog|Real trace(RealMatrix x)| +\myindent Trace of the matrix \verb|x|, i.e. the sum of the elements on the diagonal. + +\blog|RealMatrix repmat(RealMatrix m, Integer rows, Integer cols)| +\myindent Return the matrix \verb|m| tiled \verb|rows| times vertically and +\verb|cols| +times horizontally. + \blog|RealMatrix vstack(RealMatrix arg1, ...)| \blog|RealMatrix vstack(Real arg1, ...)| \myindent Stack scalars or matrices vertically. Accepts an arbitrary number of @@ -156,11 +190,7 @@ \subsection{Matrix operations} \myindent Matrix of the given size, filled with ones. For example, \verb|ones(3, 4)| returns a 3x4 matrix of ones. -\blog|RealMatrix abs(RealMatrix m)| -\myindent Element-wise absolute value. -\blog|RealMatrix exp(RealMatrix m)| -\myindent Element-wise exponential of a matrix. Use square brackets to index into a matrix. If \verb|m| is a two-dimensional matrix, \verb|m[i]| returns the \verb|i|-th row of \verb|m|. If \verb|m| is a @@ -268,3 +298,7 @@ \subsection{Miscellaneous functions} \blog|RealMatrix loadRealMatrix(String s)| \myindent Load a \verb|RealMatrix| from a text file. The space-separated formats produced by numpy and Matlab are supported. + +\blog|RealMatrix loadRealMatrix(String s, Integer i)| +\myindent Load i-th line as a \verb|RealMatrix| from a text file. +The space-separated formats produced by numpy and Matlab are supported. \ No newline at end of file diff --git a/extending.tex b/extending.tex index 3d17faf..b875493 100644 --- a/extending.tex +++ b/extending.tex @@ -15,8 +15,8 @@ \subsection{User-defined functions}\label{user-defined-function-section} import blog.model.AbstractFunctionInterp; -public class Log10Interp extends AbstractFunctionInterp { - public Log10Interp(List args) { +public class Log10 extends AbstractFunctionInterp { + public Log10(List args) { } public Object getValue(List args) { @@ -26,17 +26,18 @@ \subsection{User-defined functions}\label{user-defined-function-section} } \end{minted} -To use this function from a model, declare it like this: +To use an externally defined function, a proper declaration should be added in the blog file using \verb|extern| keyword. +For example, to declare an externally defined 10-based logarithm. \begin{blogcode} -fixed Real log10(Real val) = my_package.Log10Interp(); +extern Real Log10(Real x); \end{blogcode} -Then \verb|log10| can be used just like any other fixed function, e.g. -\verb|query log10(100.0)|. +Then \verb|Log10| can be used just like any other fixed function, e.g. +\verb|query Log10(100.0)|. -Make sure \verb|my_package| is on the class path. If you get an error like "No -definition found for non-random function log10", you most likely have a +Make sure \verb|Log10| the same as the defined Class name in Java.If you get an error like "No +definition found for non-random function Log10", you most likely have a class-path problem. If \verb|my_package| is in the current working directory, you need not do anything. Otherwise, if \verb|my_package| lives at \verb|/some/absolute/path/my_package|, launch \bl with an environment variable: @@ -50,6 +51,13 @@ \subsection{User-defined functions}\label{user-defined-function-section} \subsection{User-defined distributions}\label{user-defined-distribution-section} +For externally defined distribution, we need to add declaration of its signature in blog using \verb|distribution|. + +For example, to declare a \verb|UniformReal| distribution. +\begin{blogcode} +distribution Real UniformReal(Real a, Real b); +\end{blogcode} + Probability distributions are implemented in Java. Distribution classes should implement the interface \verb|blog.distrib.CondProbDistrib|. By default, the \bl engine will look up distribution classes in the package \verb|blog.distrib|. In addition, it will look up distribution classes under the default empty package. diff --git a/readme.md b/readme.md index 00e08e9..87d9180 100644 --- a/readme.md +++ b/readme.md @@ -7,7 +7,7 @@ 2. install Pygments This package requires pygments package. You may install by - easy_install Pygments + sudo easy_install Pygments 3. install blog_py_lexer Latex highlighting needs the blog_py_lexer.