matlab interface.

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1887 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
vsc 2007-05-24 15:11:46 +00:00
parent 5affad32b6
commit 244d4128cf
9 changed files with 7275 additions and 6216 deletions

View File

@ -2,24 +2,9 @@
:- module(bnt, [dump_as_bnt/2,
check_if_bnt_done/1]).
:- use_module(library(terms), [term_variables/2
]).
:- use_module(library(ordsets), [ord_subtract/3,
ord_add_element/3
]).
:- use_module(library(lists), [reverse/2
]).
:- use_module(library(atts)).
:- use_module(library(heaps), [empty_heap/1,
add_to_heap/4,
heap_to_list/2
]).
:- use_module(library(system), [exec/3
:- use_module(library(matlab), [start_matlab/2,
close_matlab/1,
eval_string/2
]).
:- attribute topord/1, map/1.
@ -28,10 +13,20 @@ check_if_bnt_done(Var) :-
get_atts(Var, [map(_)]).
dump_as_bnt(GVars, [_|_]) :-
exec('matlab -nojvm -nosplash',[pipe(CommandStream),pipe(Answer),pipe(Answer)],_),
wait_for_matlab_prompt(Answer),
start_matlab(´-nojvm -nosplash',MatEnv),
get_value(clpbn_key, Key),
send_command(CommandStream, Answer, 'cd /u/vitor/sw/BNT;~n', []),
eval_string(MatEnv, 'cd /u/vitor/sw/BNT'),
eval_string(MatEnv, 'add_BNT_to_path'),
extract_graph(GVars, Graph),
dgraph_top_sort(Graph, SortedGraph),
number_graph(SortedGraph),
zeros(MatEnv, GLength, GLength, dag),
build_dag(SortedGraph),
dump_cpts(SortedGraph).
% send_command(CommandStream, Answer, 'cd /home/vitor/Yap/CLPBN/BNT;~n', []),
send_command(CommandStream, Answer, 'add_BNT_to_path;~n', []),
send_command(CommandStream, Answer, '~w = ~w;~n', ['$VAR'(Key), Key]),

View File

@ -556,6 +556,7 @@ all: startup
@INSTALL_DLLS@ (cd library/tries; make)
@INSTALL_DLLS@ (cd library/lammpi; make)
@INSTALL_DLLS@ (cd library/matrix; make)
@INSTALL_MATLAB@ (cd library/matlab; make)
@ENABLE_JPL@ @INSTALL_DLLS@ (cd LGPL/JPL/src; make)
startup: yap@EXEC_SUFFIX@ $(PL_SOURCES)
@ -593,6 +594,7 @@ install_unix: startup libYap.a
@INSTALL_DLLS@ (cd library/tries; make install)
@INSTALL_DLLS@ (cd library/lammpi; make install)
@INSTALL_DLLS@ (cd library/matrix; make install)
@INSTALL_MATLAB@ (cd library/matlab; make install)
@ENABLE_JPL@ @INSTALL_DLLS@ (cd LGPL/JPL/src; make install)
mkdir -p $(DESTDIR)$(INCLUDEDIR)
for h in $(INTERFACE_HEADERS); do $(INSTALL) $$h $(DESTDIR)$(INCLUDEDIR); done
@ -617,7 +619,8 @@ install_win32: startup
(cd library/regex; make install)
(cd library/system; make install)
(cd library/yap2swi; make install)
@# (cd library/tries; make install)
@INSTALL_MATLAB@ (cd library/matlab; make install)
(cd library/tries; make install)
install_library: @YAPLIB@
mkdir -p $(DESTDIR)$(INCLUDEDIR)
@ -658,6 +661,7 @@ clean: clean_docs
@INSTALL_DLLS@ (cd library/yap2swi; make clean)
@INSTALL_DLLS@ (cd library/tries; make clean)
@INSTALL_DLLS@ (cd library/lammpi; make clean)
@INSTALL_MATLAB@ (cd library/matlab; make clean)
@ENABLE_JPL@ @INSTALL_DLLS@ (cd LGPL/JPL/src; make clean)

11860
configure vendored

File diff suppressed because it is too large Load Diff

View File

@ -150,6 +150,18 @@ AC_ARG_WITH(readline,
fi,
[yap_cv_readline=yes])
AC_ARG_WITH(matlab,
[ --with-matlab[=DIR] use MATLAB package in DIR],
if test "$withval" = yes; then
yap_cv_matlab=yes
elif test "$withval" = no; then
yap_cv_matlab=no
else
yap_cv_matlab=$with_matlab
MATLAB_INCLUDE="-I${yap_cv_matlab}/extern/include"
fi,
[yap_cv_matlab=no])
AC_ARG_WITH(mpi,
[ --with-mpi[=DIR] use MPI library in DIR],
if test "$withval" = yes; then
@ -262,6 +274,28 @@ eval "AC_DEFINE(MAX_WORKERS,$yap_cv_max_workers)"
fi
dnl condor does not like dynamic linking on Linux, DEC, and HP-UX platforms.
if test "$yap_cv_matlab" = no
then
INSTALL_MATLAB="# "
elif test "$yap_cv_matlab" = yes
then
INSTALL_MATLAB=""
else
case "$target_cpu" in
i*86*)
LDFLAGS="$LDFLAGS -L${yap_cv_matlab}/bin/glnx86"
;;
x86*)
LDFLAGS="$LDFLAGS -L${yap_cv_matlab}/bin/glnxa64"
;;
sparc*)
LDFLAGS="$LDFLAGS -L${yap_cv_matlab}/bin/sol2"
;;
esac
INSTALL_MATLAB=""
fi
dnl condor does not like dynamic linking on Linux, DEC, and HP-UX platforms.
if test "$use_condor" = yes
then
@ -1039,6 +1073,8 @@ AC_SUBST(LAMOBJS)
AC_SUBST(MAX_WORKERS)
AC_SUBST(STATIC_MODE)
AC_SUBST(INSTALL_MATLAB)
AC_SUBST(MATLAB_INCLUDE)
dnl check for threaded code
AC_MSG_CHECKING(for gcc threaded code)
@ -1309,6 +1345,7 @@ AC_DEFINE(USE_DL_MALLOC,1)
fi
mkdir -p library/matrix
mkdir -p library/matlab
mkdir -p library/mpi
mkdir -p library/random
mkdir -p library/regex
@ -1329,7 +1366,7 @@ mkdir -p LGPL/clp
mkdir -p LGPL/clpr
mkdir -p LGPL/chr
AC_OUTPUT(Makefile library/matrix/Makefile library/regex/Makefile library/system/Makefile library/random/Makefile library/yap2swi/Makefile library/mpi/Makefile .depend library/Makefile LGPL/chr/Makefile LGPL/chr/chr_swi_bootstrap.yap CLPBN/Makefile LGPL/clp/Makefile LGPL/clpr/Makefile library/lammpi/Makefile library/tries/Makefile LGPL/JPL/Makefile LGPL/JPL/src/Makefile LGPL/JPL/java/Makefile LGPL/JPL/jpl_paths.yap)
AC_OUTPUT(Makefile library/matrix/Makefile library/matlab/Makefile library/regex/Makefile library/system/Makefile library/random/Makefile library/yap2swi/Makefile library/mpi/Makefile .depend library/Makefile LGPL/chr/Makefile LGPL/chr/chr_swi_bootstrap.yap CLPBN/Makefile LGPL/clp/Makefile LGPL/clpr/Makefile library/lammpi/Makefile library/tries/Makefile LGPL/JPL/Makefile LGPL/JPL/src/Makefile LGPL/JPL/java/Makefile LGPL/JPL/jpl_paths.yap)
make depend

View File

@ -40,6 +40,7 @@ PROGRAMS= $(srcdir)/apply_macros.yap \
$(srcdir)/logtalk.yap \
$(srcdir)/nb.yap \
$(srcdir)/ordsets.yap \
$(srcdir)/matlab.yap \
$(srcdir)/matrix.yap \
$(srcdir)/prandom.yap \
$(srcdir)/queues.yap \

119
library/matlab.yap Normal file
View File

@ -0,0 +1,119 @@
:- module(matlab,
[start_matlab/1,
close_matlab/0,
matlab_on/0,
matlab_eval_string/1,
matlab_eval_string/2,
matlab_eval_sequence/1,
matlab_eval_sequence/2,
matlab_call/2,
matlab_call/3,
matlab_cells/2,
matlab_cells/3,
matlab_initialized_cells/4,
matlab_zeros/2,
matlab_zeros/3,
matlab_zeros/4,
matlab_matrix/4,
matlab_vector/3,
matlab_vector/3,
matlab_set/4,
matlab_get_variable/2,
matlab_item/3,
matlab_item/4,
matlab_item1/3,
matlab_item1/4,
matlab_sequence/3]).
:- ensure_loaded(library(lists)).
:- load_foreign_files([matlab], ['/s/pkg/matlab/7r14sp3/bin/glnx86/libeng.so','/s/pkg/matlab/7r14sp3/bin/glnx86/libmx.so','/s/pkg/matlab/7r14sp3/bin/glnx86/libut.so'], init_matlab).
matlab_eval_sequence(S) :-
atomic_concat(S,S1),
matlab_eval_string(S1).
matlab_eval_sequence(S,O) :-
atomic_concat(S,S1),
matlab_eval_string(S1,O).
matlab_vector( Vec, L) :-
length(Vec, LV),
matlab_vector(LV, Vec, L).
matlab_sequence(Min,Max,L) :-
mksequence(Min,Max,Vector),
Dim is (Max-Min)+1,
matlab_matrix(1,Dim,Vector,L).
mksequence(Min,Min,[Min]) :- !.
mksequence(Min,Max,[Min|Vector]) :-
Min1 is Min+1,
mksequence(Min1,Max,Vector).
matlab_call(S,Out) :-
S=..[Func|Args],
build_args(Args,L0,[]),
process_arg_entry(L0,L),
build_output(Out,Lf,['= ',Func|L]),
atomic_concat(Lf,Command),
matlab_eval_string(Command).
matlab_call(S,Out,Result) :-
S=..[Func|Args],
build_args(Args,L0,[]),
process_arg_entry(L0,L),
build_output(Out,Lf,[' = ',Func|L]),
atomic_concat(Lf,Command),
matlab_eval_string(Command,Result).
build_output(Out,['[ '|L],L0) :-
is_list(Out), !,
build_outputs(Out,L,[']'|L0]).
build_output(Out,Lf,L0) :-
build_arg(Out,Lf,L0).
build_outputs([],L,L).
build_outputs([Out|Outs],[Out,' '|L],L0) :-
build_outputs(Outs,L,L0).
build_args([],L,L).
build_args([Arg],Lf,L0) :-
build_arg(Arg,Lf,[')'|L0]).
build_args([Arg|Args],L,L0) :-
build_arg(Arg,L,[', '|L1]),
build_args(Args,L1,L0).
build_arg(Arg,[Arg|L],L) :- atomic(Arg), !.
build_arg(\S0,['\'',S0,'\''|L],L) :-
atom(S0), !.
build_arg([S1|S2],['['|L],L0) :-
is_list(S2), !,
build_arglist([S1|S2],L,L0).
build_arg([S1|S2],L,L0) :- !,
build_arg(S1,L,['.'|L1]),
build_arg(S2,L1,L0).
build_arg(S1:S2,L,L0) :- !,
build_arg(S1,L,[':'|L1]),
build_arg(S2,L1,L0).
build_arg(F,[N,'{'|L],L0) :- %N({A}) = N{A}
F=..[N,{A}], !,
build_arg(A,L,['}'|L0]).
build_arg(F,[N,'('|L],L0) :-
build_args(A,L,L0).
build_arglist([A],L,L0) :- !,
build_arg(A,L,[' ]'|L0]).
build_arglist([A|As],L,L0) :-
build_arg(A,L,[' ,'|L1]),
build_arglist(As,L1,L0).
build_string([],['\''|L],L).
build_string([S0|S],[C|Lf],L0) :-
char_code(C,S0),
build_string(S,Lf,L0).
process_arg_entry([],[]) :- !.
process_arg_entry(L,['('|L]).

View File

@ -0,0 +1,58 @@
#
# default base directory for YAP installation
# (EROOT for architecture-dependent files)
#
prefix = @prefix@
ROOTDIR = $(prefix)
EROOTDIR = @exec_prefix@
#
# where the binary should be
#
BINDIR = $(EROOTDIR)/bin
#
# where YAP should look for libraries
#
LIBDIR=$(EROOTDIR)/lib/Yap
#
#
CC=@CC@
CFLAGS= @CFLAGS@ $(YAP_EXTRAS) $(DEFS) -I$(srcdir) -I../.. -I$(srcdir)/../../include @MATLAB_INCLUDE@
#
#
# You shouldn't need to change what follows.
#
INSTALL=@INSTALL@
INSTALL_DATA=@INSTALL_DATA@
INSTALL_PROGRAM=@INSTALL_PROGRAM@
SHELL=/bin/sh
RANLIB=@RANLIB@
srcdir=@srcdir@
SHLIB_CFLAGS=@SHLIB_CFLAGS@
SHLIB_SUFFIX=@SHLIB_SUFFIX@
#4.1VPATH=@srcdir@:@srcdir@/OPTYap
CWD=$(PWD)
#
OBJS=matlab.o
SOBJS=matlab@SHLIB_SUFFIX@
#in some systems we just create a single object, in others we need to
# create a libray
all: $(SOBJS)
matlab.o: $(srcdir)/matlab.c
$(CC) -c $(CFLAGS) $(SHLIB_CFLAGS) $(srcdir)/matlab.c -o matlab.o
@DO_SECOND_LD@%@SHLIB_SUFFIX@: %.o
@DO_SECOND_LD@ @SHLIB_LD@ -o $@ $<
@DO_SECOND_LD@matlab@SHLIB_SUFFIX@: matlab.o
@DO_SECOND_LD@ @SHLIB_LD@ -o matlab@SHLIB_SUFFIX@ matlab.o
install: all
$(INSTALL_PROGRAM) $(SOBJS) $(DESTDIR)$(LIBDIR)
clean:
rm -f *.o *~ $(OBJS) $(SOBJS) *.BAK

View File

@ -0,0 +1,109 @@
/*
add_BNT_to_path
N = 4;
dag = zeros(N,N);
C = 1; S = 2; R = 3; W = 4;
dag(C,[R S]) = 1;
dag(R,W) = 1;
dag(S,W)=1;
discrete_nodes = 1:N;
node_sizes = 2*ones(1,N);
bnet = mk_bnet(dag, node_sizes, 'discrete', discrete_nodes);
bnet.CPD{W} = tabular_CPD(bnet, W, 'CPT', [1 0.1 0.1 0.01 0 0.9 0.9 0.99]);
bnet.CPD{C} = tabular_CPD(bnet, C, [0.5 0.5]);
bnet.CPD{R} = tabular_CPD(bnet, R, [0.8 0.2 0.2 0.8]);
bnet.CPD{S} = tabular_CPD(bnet, S, [0.5 0.9 0.5 0.1]);
engine = jtree_inf_engine(bnet);
evidence = cell(1,N);
evidence{W} = 2;
[engine, loglik] = enter_evidence(engine, evidence);
marg = marginal_nodes(engine, S);
marg.T
*/
:- ensure_loaded(library(matlab)).
:- yap_flag(write_strings, on).
% syntactic sugar for matlab_call.
:- op(800,yfx,<--).
G <-- Y :-
matlab_call(Y,G).
do(Out,Out2) :-
init_bnt,
N = 4,
C = 1, S = 2, R = 3, W = 4,
mkdag(N,[C-R,C-S,R-W,S-W]),
matlab_sequence(1,N,discrete_nodes),
mk2s(N,L), % domain has size 2
matlab_matrix(1,N,L,node_sizes),
bnet <-- mk_bnet(dag, node_sizes, \discrete, discrete_nodes),
mkcpt(bnet,W,[1, 0.1, 0.1, 0.01, 0, 0.9, 0.9, 0.99]),
mkcpt(bnet,C,[0.5, 0.5]),
mkcpt(bnet,R,[0.8, 0.2, 0.2, 0.8]),
mkcpt(bnet,S,[0.5, 0.9, 0.5, 0.1]),
engine <-- jtree_inf_engine(bnet),
mkevidence(N,[W-2]),
marg <-- marginal_nodes(engine, S),
matlab_get_variable( marg.'T', Out),
add_evidence([R-2]),
marg <-- marginal_nodes(engine, S),
matlab_get_variable( marg.'T', Out2).
init_bnt :-
matlab_on, !.
init_bnt :-
getcwd(D),
cd('~/Yap/CLPBN/FullBNT/BNT'),
start_matlab('matlab -nojvm -nosplash'),
matlab_eval_string("add_BNT_to_path",_),
cd(D).
mk2s(0, []) :- !.
mk2s(I, [2|L]) :-
I0 is I-1,
mk2s(I0, L).
mkdag(N,Els) :-
Tot is N*N,
functor(Dag,dag,Tot),
add_els(Els,N,Dag),
Dag=..[_|L],
addzeros(L),
matlab_matrix(N,N,L,dag).
add_els([],_,_).
add_els([X-Y|Els],N,Dag) :-
Pos is (X-1)*N+Y,
arg(Pos,Dag,1),
add_els(Els,N,Dag).
addzeros([]).
addzeros([0|L]) :- !,
addzeros(L).
addzeros([1|L]) :-
addzeros(L).
mkcpt(BayesNet, V, Tab) :-
(BayesNet.'CPD'({V})) <-- tabular_CPD(BayesNet,V,Tab).
mkevidence(N,L) :-
mkeventries(L,LN),
matlab_initialized_cells( 1, N, LN, evidence),
[engine, loglik] <-- enter_evidence(engine, evidence).
mkeventries([],[]).
mkeventries([A-V|L],[ev(1,A,V)|LN]) :-
mkeventries(L,LN).
add_evidence(L) :-
add_to_evidence(L),
[engine, loglik] <-- enter_evidence(engine, evidence).
add_to_evidence([]).
add_to_evidence([Pos-Val|L]) :-
matlab_item1(evidence,Pos,Val),
add_to_evidence(L).

780
library/matlab/matlab.c Normal file
View File

@ -0,0 +1,780 @@
/*************************************************************************
* *
* YAP Prolog *
* *
* Yap Prolog was developed at NCCUP - Universidade do Porto *
* *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* *
**************************************************************************
* *
* File: random.c *
* Last rev: *
* mods: *
* comments: regular expression interpreter *
* *
*************************************************************************/
#include "config.h"
#include "YapInterface.h"
#include <math.h>
#include <string.h>
#if defined(__MINGW32__) || _MSC_VER
#include <windows.h>
#endif
#include <mex.h>
#include <engine.h>
#include <matrix.h>
#define MAT_ACCESS(I,J,ROWS,COLS) ((I)+((J)*(ROWS)))
#define BUFSIZE 512
#define OBUFSIZE 2048
void PROTO(init_matlab, (void));
static YAP_Functor MatlabAddress;
static Engine *Meng = NULL;
static mxArray *
matlab_getvar(YAP_Term t)
{
return engGetVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(t)));
}
static YAP_Term
address2term(mxArray *mat)
{
YAP_Term t[1];
t[0] = YAP_MkIntTerm((YAP_Int)mat);
return YAP_MkApplTerm(MatlabAddress,1,t);
}
static int
cp_back(YAP_Term vart, mxArray *mat)
{
if (!YAP_IsAtomTerm(vart)) {
return TRUE;
}
/* save back to matlab */
return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(vart)), mat);
}
static int
p_startmatlab(void)
{
char opts[BUFSIZE];
const char *ptr;
YAP_Term topts = YAP_ARG1;
if (Meng)
return TRUE;
if (YAP_IsAtomTerm(topts))
ptr = YAP_AtomName(YAP_AtomOfTerm(topts));
else
{
if (!YAP_StringToBuffer(topts,opts, BUFSIZE))
return YAP_Unify(YAP_ARG2, YAP_MkIntTerm(-1));
ptr = opts;
}
if (!strlen(ptr)) {
if (!(Meng = engOpen("\0"))) {
return YAP_Unify(YAP_ARG2, YAP_MkIntTerm(-2));
}
} else {
if (!(Meng = engOpen(ptr))) {
return YAP_Unify(YAP_ARG2, YAP_MkIntTerm(-2));
}
}
engOutputBuffer(Meng, NULL, 0);
return TRUE;
}
static int
p_matlabon(void)
{
return Meng != NULL;
}
static int
p_closematlab(void)
{
Engine *eng = Meng;
Meng = NULL;
if (Meng)
return engClose(eng);
else
return FALSE;
}
static int
p_evalstring2(void)
{
char com[BUFSIZE];
YAP_Term tcom = YAP_ARG1;
const char *comd;
if (YAP_IsAtomTerm(tcom))
comd = YAP_AtomName(YAP_AtomOfTerm(tcom));
else {
if (!YAP_StringToBuffer(tcom, com, BUFSIZE))
return YAP_Unify(YAP_ARG2, YAP_MkIntTerm(-1));
comd = com;
}
return !engEvalString(Meng, comd);
}
static int
p_evalstring3(void)
{
int out;
YAP_Term tcom = YAP_ARG1;
const char *comd;
char com[BUFSIZE];
char buf[OBUFSIZE];
buf[0] = '\0';
if (YAP_IsAtomTerm(tcom))
comd = YAP_AtomName(YAP_AtomOfTerm(tcom));
else {
if (!YAP_StringToBuffer(tcom, com, BUFSIZE))
return YAP_Unify(YAP_ARG2, YAP_MkIntTerm(-1));
comd = com;
}
engOutputBuffer(Meng, buf, OBUFSIZE);
out = !engEvalString(Meng, comd);
engOutputBuffer(Meng, NULL, 0);
return YAP_Unify(YAP_ARG2, YAP_BufferToString(buf));
}
static int
p_create_cell_vector(void)
{
int dims[1];
mxArray *mat;
dims[0] = YAP_IntOfTerm(YAP_ARG1);
if (!(mat = mxCreateCellArray(1, dims)))
return FALSE;
if (YAP_IsAtomTerm(YAP_ARG2)) {
return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG2)), mat);
}
return YAP_Unify(YAP_ARG2,address2term(mat));
}
static int
p_create_cell_array(void)
{
int rows, cols;
mxArray *mat;
rows = YAP_IntOfTerm(YAP_ARG1);
cols = YAP_IntOfTerm(YAP_ARG2);
if (!(mat = mxCreateCellMatrix(rows, cols)))
return FALSE;
if (YAP_IsAtomTerm(YAP_ARG3)) {
return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3)), mat);
}
return YAP_Unify(YAP_ARG3,address2term(mat));
}
static int
p_create_double_vector(void)
{
int dims[1];
mxArray *mat;
dims[0] = YAP_IntOfTerm(YAP_ARG1);
if (!(mat = mxCreateNumericArray(1, dims, mxDOUBLE_CLASS, mxREAL)))
return FALSE;
if (YAP_IsAtomTerm(YAP_ARG2)) {
return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG2)), mat);
}
return YAP_Unify(YAP_ARG2,address2term(mat));
}
static int
p_create_double_array(void)
{
int rows, cols;
mxArray *mat;
rows = YAP_IntOfTerm(YAP_ARG1);
cols = YAP_IntOfTerm(YAP_ARG2);
if (!(mat = mxCreateDoubleMatrix(rows, cols, mxREAL)))
return FALSE;
if (YAP_IsAtomTerm(YAP_ARG3)) {
return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3)), mat);
}
return YAP_Unify(YAP_ARG3,address2term(mat));
}
static int
p_create_double_array3(void)
{
int dims[3];
mxArray *mat;
dims[0] = YAP_IntOfTerm(YAP_ARG1);
dims[1] = YAP_IntOfTerm(YAP_ARG2);
dims[2] = YAP_IntOfTerm(YAP_ARG3);
if (!(mat = mxCreateNumericArray(3, dims, mxDOUBLE_CLASS, mxREAL)))
return FALSE;
if (YAP_IsAtomTerm(YAP_ARG3)) {
return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG4)), mat);
}
return YAP_Unify(YAP_ARG3,address2term(mat));
}
static int
p_set_int_array(void)
{
int rows, cols, i = 0, j = 0;
YAP_Int *input;
mxArray *mat;
YAP_Term tl = YAP_ARG4;
mat = matlab_getvar(YAP_ARG1);
rows = YAP_IntOfTerm(YAP_ARG2);
cols = YAP_IntOfTerm(YAP_ARG3);
input = (YAP_Int *)mxGetPr(mat);
/* copy ints to matrix. */
for (i = 0; i < rows*cols; i++) {
YAP_Term th;
if (!YAP_IsPairTerm(tl)) {
return FALSE;
}
th = YAP_HeadOfTerm(tl);
if (!YAP_IsIntTerm(th)) {
/* ERROR */
return FALSE;
}
input[MAT_ACCESS(i++,j,rows,cols)] = YAP_IntOfTerm(th);
if (i == rows) {
i = 0;
j++;
}
tl = YAP_TailOfTerm(tl);
}
if (YAP_IsAtomTerm(YAP_ARG4)) {
return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG4)), mat);
}
return YAP_Unify(YAP_ARG4,address2term(mat));
}
static int
p_set_float_array(void)
{
int rows, cols, i = 0, j = 0;
double *input;
mxArray *mat;
YAP_Term tl = YAP_ARG3;
rows = YAP_IntOfTerm(YAP_ARG1);
cols = YAP_IntOfTerm(YAP_ARG2);
if (!(mat = mxCreateDoubleMatrix(rows, cols, mxREAL)))
return FALSE;
input = mxGetPr(mat);
/* copy ints to matrix. */
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
YAP_Term th;
if (!YAP_IsPairTerm(tl)) {
return FALSE;
}
th = YAP_HeadOfTerm(tl);
if (YAP_IsIntTerm(th)) {
input[MAT_ACCESS(i,j,rows,cols)] = YAP_IntOfTerm(th);
} else if (YAP_IsFloatTerm(th)) {
input[MAT_ACCESS(i,j,rows,cols)] = YAP_FloatOfTerm(th);
} else {
/* ERROR */
return FALSE;
}
tl = YAP_TailOfTerm(tl);
}
}
if (YAP_IsAtomTerm(YAP_ARG4)) {
return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG4)), mat);
}
return YAP_Unify(YAP_ARG4,address2term(mat));
}
static int
p_set_float_vector(void)
{
int len, i = 0;
double *input;
mxArray *mat;
YAP_Term tl = YAP_ARG2;
len = YAP_IntOfTerm(YAP_ARG1);
if (!(mat = mxCreateNumericArray(1,&len, mxDOUBLE_CLASS, mxREAL)))
return FALSE;
input = mxGetPr(mat);
/* copy ints to matrix. */
for (i = 0; i < len; i++) {
YAP_Term th;
if (!YAP_IsPairTerm(tl)) {
return FALSE;
}
th = YAP_HeadOfTerm(tl);
if (YAP_IsIntTerm(th)) {
input[i] = YAP_IntOfTerm(th);
} else if (YAP_IsFloatTerm(th)) {
input[i] = YAP_FloatOfTerm(th);
} else {
/* ERROR */
return FALSE;
}
tl = YAP_TailOfTerm(tl);
}
if (YAP_IsAtomTerm(YAP_ARG3)) {
return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3)), mat);
}
return YAP_Unify(YAP_ARG3,address2term(mat));
}
static int
p_set_int(void)
{
int rows, cols, i, j;
YAP_Int *input;
mxArray *mat;
mat = matlab_getvar(YAP_ARG1);
i = YAP_IntOfTerm(YAP_ARG2);
j = YAP_IntOfTerm(YAP_ARG3);
input = (YAP_Int *)mxGetPr(mat);
rows = mxGetM(mat);
cols = mxGetN(mat);
input[MAT_ACCESS(i-1,j-1,rows,cols)] = YAP_IntOfTerm(YAP_ARG4);
return TRUE;
}
static int
p_set_float(void)
{
int rows, cols, i, j;
double *input;
mxArray *mat;
YAP_Term t = YAP_ARG4;
mat = matlab_getvar(YAP_ARG1);
i = YAP_IntOfTerm(YAP_ARG2);
j = YAP_IntOfTerm(YAP_ARG3);
input = mxGetPr(mat);
rows = mxGetM(mat);
cols = mxGetN(mat);
if (YAP_IsIntTerm(t))
input[MAT_ACCESS(i-1,j-1,rows,cols)] = YAP_IntOfTerm(t);
else
input[MAT_ACCESS(i-1,j-1,rows,cols)] = YAP_FloatOfTerm(t);
return TRUE;
}
/* this has to be done carefully because we all need to transpose the matrix */
static YAP_Term
cp_ints32(int ndims, int *dims, INT32_T *input, int factor, int base, YAP_Term t)
{
int i;
if (ndims == 1)
for (i=dims[0]; i>0; i--) {
t = YAP_MkPairTerm(YAP_MkIntTerm(input[base+factor*(i-1)]),t);
}
else
for (i=dims[0]; i>0; i--) {
t = cp_ints32(ndims-1, dims+1, input, factor*dims[0], base+factor*(i-1),t);
}
return t;
}
static YAP_Term
cp_ints64(int ndims, int *dims, INT64_T *input, int factor, int base, YAP_Term t)
{
int i;
if (ndims == 1)
for (i=dims[0]; i>0; i--) {
t = YAP_MkPairTerm(YAP_MkIntTerm(input[base+factor*(i-1)]),t);
}
else
for (i=dims[0]; i>0; i--) {
t = cp_ints64(ndims-1, dims+1, input, factor*dims[0], base+factor*(i-1),t);
}
return t;
}
static YAP_Term
cp_cells(int ndims, int *dims, mxArray *mat, int factor, int base, YAP_Term t)
{
int i;
if (ndims == 1)
for (i=dims[0]; i>0; i--) {
t = YAP_MkPairTerm(YAP_MkIntTerm((YAP_Int)mxGetCell(mat,base+factor*(i-1))),t);
}
else
for (i=dims[0]; i>0; i--) {
t = cp_cells(ndims-1, dims+1, mat, factor*dims[0], base+factor*(i-1),t);
}
return t;
}
/* this has to be done carefully because we all need to transpose the matrix */
static YAP_Term
cp_floats(int ndims, int *dims, double *input, int factor, int base, YAP_Term t)
{
int i;
if (ndims == 1)
for (i=dims[0]; i>0; i--) {
t = YAP_MkPairTerm(YAP_MkFloatTerm(input[base+factor*(i-1)]),t);
}
else
for (i=dims[0]; i>0; i--) {
t = cp_floats(ndims-1, dims+1, input, factor*dims[0], base+factor*(i-1),t);
}
return t;
}
static mxArray*
get_array(YAP_Term ti)
{
if (YAP_IsIntTerm(ti)) {
return mxCreateDoubleScalar(YAP_IntOfTerm(ti));
} else if (YAP_IsFloatTerm(ti)) {
return mxCreateDoubleScalar(YAP_FloatOfTerm(ti));
} else if (YAP_IsAtomTerm(ti)) {
return matlab_getvar(ti);
} else if (YAP_IsPairTerm(ti)) {
YAP_Term tv = YAP_HeadOfTerm(ti);
YAP_Term tf = YAP_TailOfTerm(ti);
const mxArray *mout;
if (!YAP_IsAtomTerm(tv)) {
char s[BUFSIZE];
if (!YAP_StringToBuffer(ti, s, BUFSIZE))
return FALSE;
return mxCreateString(s);
}
mout = matlab_getvar(tv);
if (!mout)
return FALSE;
if (YAP_IsIntTerm(tf)) {
return mxGetFieldByNumber(mout, 0, YAP_IntOfTerm(tf));
} else if (YAP_IsAtomTerm(tf)) {
const char *s=YAP_AtomName(YAP_AtomOfTerm(tf));
return mxGetField(mout, 0, s);
} else {
return NULL;
}
} else {
return (mxArray *)YAP_IntOfTerm(YAP_ArgOfTerm(1,ti));
}
}
static int
p_get_variable(void)
{
YAP_Term t;
mxArray *mat;
const int *dims;
int ndims;
mat = get_array(YAP_ARG1);
if (!mat)
return FALSE;
dims = mxGetDimensions(mat);
ndims = mxGetNumberOfDimensions(mat);
if (mxIsInt32(mat)) {
INT32_T *input = (INT32_T *)mxGetPr(mat);
t = cp_ints32(ndims, (int *)dims, input, 1, 0, YAP_TermNil());
} else if (mxIsInt64(mat)) {
INT64_T *input = (INT64_T *)mxGetPr(mat);
t = cp_ints64(ndims, (int *)dims, input, 1, 0, YAP_TermNil());
} else if (mxIsInt32(mat) || mxIsInt64(mat) || mxIsCell(mat)) {
t = cp_cells(ndims, (int *)dims, mat, 1, 0, YAP_TermNil());
} else if (mxIsDouble(mat)) {
double *input = mxGetPr(mat);
t = cp_floats(ndims, (int *)dims, input, 1, 0, YAP_TermNil());
} else {
return FALSE;
}
return YAP_Unify(YAP_ARG2, t);
}
static int
item1(YAP_Term tvar, YAP_Term titem, int off)
{
mxArray *mat;
mat = get_array(tvar);
if (!mat)
return FALSE;
if (mxIsInt32(mat)) {
INT32_T *input = (INT32_T *)mxGetPr(mat);
if (YAP_IsIntTerm(titem)) {
input[off] = YAP_IntOfTerm(titem);
} else if (YAP_IsFloatTerm(titem)) {
input[off] = YAP_FloatOfTerm(titem);
} else if (YAP_IsVarTerm(titem)) {
return YAP_Unify(titem, YAP_MkIntTerm(input[off]));
} else
return FALSE;
} else if (mxIsInt64(mat)) {
INT64_T *input = (INT64_T *)mxGetPr(mat);
if (YAP_IsIntTerm(titem)) {
input[off] = YAP_IntOfTerm(titem);
} else if (YAP_IsFloatTerm(titem)) {
input[off] = YAP_FloatOfTerm(titem);
} else if (YAP_IsVarTerm(titem)) {
return YAP_Unify(titem, YAP_MkIntTerm(input[off]));
} else
return FALSE;
} else if (mxIsCell(mat)) {
if (YAP_IsVarTerm(titem)) {
return YAP_Unify(titem, YAP_MkIntTerm((YAP_Int)mxGetCell(mat,off)));
} else {
mxArray *mat2 = get_array(titem);
mxSetCell(mat,off, mat2);
}
} else if (mxIsDouble(mat)) {
double *input = mxGetPr(mat);
if (YAP_IsFloatTerm(titem)) {
input[off] = YAP_FloatOfTerm(titem);
} else if (YAP_IsIntTerm(titem)) {
input[off] = YAP_IntOfTerm(titem);
} else {
return YAP_Unify(titem, YAP_MkFloatTerm(input[off]));
}
} else
return FALSE;
return cp_back(tvar, mat);
}
static int
p_item(void)
{
YAP_Term titem;
int off = YAP_IntOfTerm(YAP_ARG2);
titem = YAP_ARG3;
return item1(YAP_ARG1,titem,off);
}
static int
p_item_1(void)
{
YAP_Term titem;
int off = YAP_IntOfTerm(YAP_ARG2)-1;
titem = YAP_ARG3;
return item1(YAP_ARG1,titem,off);
}
static int
item2(YAP_Term tvar, YAP_Term titem, int offx, int offy)
{
mxArray *mat;
int rows;
int cols;
int off;
mat = get_array(tvar);
rows = mxGetM(mat);
cols = mxGetN(mat);
off = MAT_ACCESS(offx,offy,rows,cols);
if (!mat)
return FALSE;
if (mxIsInt32(mat)) {
INT32_T *input = (INT32_T *)mxGetPr(mat);
if (YAP_IsIntTerm(titem)) {
input[off] = YAP_IntOfTerm(titem);
} else if (YAP_IsFloatTerm(titem)) {
input[off] = YAP_FloatOfTerm(titem);
} else if (YAP_IsVarTerm(titem)) {
return YAP_Unify(titem, YAP_MkIntTerm(input[off]));
} else
return FALSE;
} else if (mxIsInt64(mat)) {
INT64_T *input = (INT64_T *)mxGetPr(mat);
if (YAP_IsIntTerm(titem)) {
input[off] = YAP_IntOfTerm(titem);
} else if (YAP_IsFloatTerm(titem)) {
input[off] = YAP_FloatOfTerm(titem);
} else if (YAP_IsVarTerm(titem)) {
return YAP_Unify(titem, YAP_MkIntTerm(input[off]));
} else
return FALSE;
} else if (mxIsCell(mat)) {
if (YAP_IsVarTerm(titem)) {
return YAP_Unify(titem, YAP_MkIntTerm((YAP_Int)mxGetCell(mat,off)));
} else {
mxArray *mat2 = get_array(titem);
mxSetCell(mat,off, mat2);
}
} else if (mxIsDouble(mat)) {
double *input = mxGetPr(mat);
if (YAP_IsFloatTerm(titem)) {
input[off] = YAP_FloatOfTerm(titem);
} else if (YAP_IsIntTerm(titem)) {
input[off] = YAP_IntOfTerm(titem);
} else {
return YAP_Unify(titem, YAP_MkFloatTerm(input[off]));
}
} else
return FALSE;
return cp_back(tvar, mat);
}
static int
p_item2(void)
{
YAP_Term titem;
int x = YAP_IntOfTerm(YAP_ARG2);
int y = YAP_IntOfTerm(YAP_ARG3);
titem = YAP_ARG4;
return item2(YAP_ARG1,titem,x,y);
}
static int
p_item2_1(void)
{
YAP_Term titem;
int offx = YAP_IntOfTerm(YAP_ARG2)-1;
int offy = YAP_IntOfTerm(YAP_ARG3)-1;
titem = YAP_ARG4;
return item2(YAP_ARG1,titem,offx,offy);
}
static int
p_call_matlab(void)
{
YAP_Term tlength = YAP_ARG2,
tl = YAP_ARG3,
tname = YAP_ARG1,
tolength = YAP_ARG4,
tout = YAP_ARG5;
int i = 0;
mxArray *inps[50], *outs[50];
const char *name;
int olength = YAP_IntOfTerm(tolength);
if (!YAP_IsAtomTerm(tname))
return FALSE;
name = YAP_AtomName(YAP_AtomOfTerm(tname));
if (!YAP_IsIntTerm(tlength))
return FALSE;
while (YAP_IsPairTerm(tl)) {
inps[i] = get_array(YAP_HeadOfTerm(tl));
i++;
tl = YAP_TailOfTerm(tl);
}
if (mexCallMATLAB(olength, outs, i, inps, name))
return FALSE;
/* output arguments */
if (YAP_IsPairTerm(tout)) {
for (i=0; i<olength; i++) {
YAP_Term ti = YAP_HeadOfTerm(tout);
if (YAP_IsAtomTerm(ti)) {
return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(ti)), outs[i]);
} else {
return YAP_Unify(ti,address2term(outs[i]));
}
}
} else {
YAP_Term to = YAP_MkAtomTerm(YAP_LookupAtom("[]"));
for (i=olength; i>0; i--) {
to = YAP_MkPairTerm(address2term(outs[i-1]),to);
}
}
return TRUE;
}
static int
p_create_cell_matrix_and_copy1(void)
{
int rows, cols;
mxArray *mat;
YAP_Term tl = YAP_ARG3;
rows = YAP_IntOfTerm(YAP_ARG1);
cols = YAP_IntOfTerm(YAP_ARG2);
if (!(mat = mxCreateCellMatrix(rows, cols)))
return FALSE;
while (YAP_IsPairTerm(tl)) {
YAP_Term th = YAP_HeadOfTerm(tl);
int off = MAT_ACCESS(YAP_IntOfTerm(YAP_ArgOfTerm(1,th))-1,
YAP_IntOfTerm(YAP_ArgOfTerm(2,th))-1,
rows,cols);
mxArray *mat2 = get_array(YAP_ArgOfTerm(3,th));
mxSetCell(mat,off, mat2);
tl = YAP_TailOfTerm(tl);
}
if (YAP_IsAtomTerm(YAP_ARG4)) {
return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG4)), mat);
}
return YAP_Unify(YAP_ARG4,address2term(mat));
}
void
init_matlab(void)
{
MatlabAddress = YAP_MkFunctor(YAP_LookupAtom("MATLAB"),1);
YAP_UserCPredicate("start_matlab", p_startmatlab, 1);
YAP_UserCPredicate("close_matlab", p_closematlab, 0);
YAP_UserCPredicate("matlab_on", p_matlabon, 0);
YAP_UserCPredicate("matlab_eval_string", p_evalstring2, 1);
YAP_UserCPredicate("matlab_eval_string", p_evalstring3, 2);
YAP_UserCPredicate("matlab_cells", p_create_cell_vector, 2);
YAP_UserCPredicate("matlab_cells", p_create_cell_array, 3);
YAP_UserCPredicate("matlab_initialized_cells", p_create_cell_matrix_and_copy1, 4);
YAP_UserCPredicate("matlab_zeros", p_create_double_vector, 2);
YAP_UserCPredicate("matlab_zeros", p_create_double_array, 3);
YAP_UserCPredicate("matlab_zeros", p_create_double_array3, 4);
YAP_UserCPredicate("matlab_int_array", p_set_int_array, 4);
YAP_UserCPredicate("matlab_vector", p_set_float_vector, 3);
YAP_UserCPredicate("matlab_matrix", p_set_float_array, 4);
YAP_UserCPredicate("matlab_set_int", p_set_int, 4);
YAP_UserCPredicate("matlab_set", p_set_float, 4);
YAP_UserCPredicate("matlab_get_variable", p_get_variable, 2);
YAP_UserCPredicate("matlab_item", p_item, 3);
YAP_UserCPredicate("matlab_item", p_item2, 4);
YAP_UserCPredicate("matlab_item1", p_item_1, 3);
YAP_UserCPredicate("matlab_item1", p_item2_1, 4);
YAP_UserCPredicate("matlab_call_matlab", p_call_matlab, 5);
}
#ifdef _WIN32
int WINAPI PROTO(win_matlab, (HANDLE, DWORD, LPVOID));
int WINAPI win_matlab(HANDLE hinst, DWORD reason, LPVOID reserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return 1;
}
#endif