--- Math-Matlab-Engine-0.02/Engine.xs 2003-11-26 22:16:08.000000000 +0100
+++ Math-Matlab-Engine/Engine.xs 2006-11-15 19:21:49.000000000 +0100
@@ -3,7 +3,7 @@
#include "XSUB.h"
#include "engine.h"
-#define BUFSIZE 256
+#define BUFSIZE 1024*256
static int
not_here(char *s)
@@ -19,8 +19,13 @@
return 0;
}
-MODULE = Math::Matlab::Engine PACKAGE = Math::Matlab::Engine PREFIX = eng
+typedef struct {
+ Engine * ep;
+ char * EvalBuf;
+} EngineIface;
+MODULE = Math::Matlab::Engine PACKAGE = Math::Matlab::Engine PREFIX = eng
+PROTOTYPES: ENABLE
double
constant(sv,arg)
@@ -37,81 +42,112 @@
-Engine *
+EngineIface *
new(CLASS)
char * CLASS
CODE:
{
- Engine *ep;
+ EngineIface *eng;
- if (!(ep = engOpen("\0"))) {
- croak ("Can't start MATLAB engine");
- }
+ if (NULL == (eng = malloc(sizeof(EngineIface))))
+ croak ("Cannot allocate engine object");
+
+ if (!(eng->ep = engOpen("") ))
+ croak ("Failed to start MATLAB engine");
+
+ // Alloc the buffer for the output of evals
+ if (NULL == (eng->EvalBuf = malloc(BUFSIZE)))
+ croak ("Failed to allocate eval output buffer");
+
+ engOutputBuffer(eng->ep, eng->EvalBuf, BUFSIZE);
+
- RETVAL = ep;
+ RETVAL = eng;
}
OUTPUT:
RETVAL
+
+
int
-engClose(ep)
- Engine * ep
+engClose(eng)
+ EngineIface * eng
CODE:
{
- RETVAL = 1 - engClose(ep);
+ RETVAL = 1 - engClose(eng->ep);
}
OUTPUT:
RETVAL
-int
-engEvalString(ep, string)
- Engine * ep
+void
+engDESTROY(eng)
+ EngineIface * eng
+ CODE:
+ {
+ engClose(eng->ep);
+ free(eng->EvalBuf);
+ free(eng);
+ }
+
+SV*
+engEvalString(eng, string)
+ EngineIface * eng
const char * string
CODE:
{
- RETVAL = (engEvalString(ep, string) == 0);
+ if (engEvalString(eng->ep, string) == 0) {
+# fprintf(stderr, "DEBUG: String frmo eval=%s$\n",eng->EvalBuf);
+# fprintf(stderr, "DEBUG: EvalString on MATLAB engine %x\n",eng->ep);
+ RETVAL = newSVpv(eng->EvalBuf,0);
+ } else {
+ // We couldnt talk to the matlab engine.
+ // return undef
+# fprintf(stderr, "ERROR: Could not talk to MATLAB engine: %x\n",eng->ep);
+ XSRETURN_UNDEF;
+ }
}
OUTPUT:
RETVAL
AV *
-engGetArrayList(ep, name)
- Engine * ep
+engGetArrayList(eng, name)
+ EngineIface * eng
const char * name
PPCODE:
{
mxArray *matrix;
- int nrdim, *dims, i, nelem;
+ int *dims, i, nelem;
+ int nrdim;
double *vals;
- matrix = engGetArray(ep, name);
+ matrix = engGetVariable(eng->ep, name);
if (matrix == NULL)
XSRETURN_UNDEF;
- nrdim = mxGetNumberOfDimensions(matrix);
- printf("%d dimensions\n",nrdim);
+ nrdim = (int) mxGetNumberOfDimensions(matrix);
+// printf("%d dimensions\n",nrdim);
- dims = mxGetDimensions(matrix);
+ dims = (int*) mxGetDimensions(matrix);
nelem = 1;
for(i=0;i<nrdim;i++) {
- printf("\t%d\n",*(dims+i));
+// printf("\t%d\n",*(dims+i));
nelem *= *(dims+i);
}
vals = mxGetPr(matrix);
for(i=0;i<nelem;i++) {
XPUSHs(sv_2mortal(newSVnv(*(vals+i))));
- printf("%6.4f,",*(vals+i));
+// printf("%6.4f,",*(vals+i));
}
- printf("\n");
+// printf("\n");
}
SV *
-engGetArrayListRef(ep, name)
- Engine * ep
+engGetArrayListRef(eng, name)
+ EngineIface * eng
const char * name
CODE:
@@ -121,26 +157,26 @@
double *vals;
AV *arr;
- matrix = engGetArray(ep, name);
+ matrix = engGetVariable(eng->ep, name);
if (matrix == NULL)
XSRETURN_UNDEF;
nrdim = mxGetNumberOfDimensions(matrix);
- printf("%d dimensions\n",nrdim);
+# printf("%d dimensions\n",nrdim);
- dims = mxGetDimensions(matrix);
+ dims = (int*) mxGetDimensions(matrix);
nelem = 1;
for(i=0;i<nrdim;i++) {
- printf("\t%d\n",*(dims+i));
+# printf("\t%d\n",*(dims+i));
nelem *= *(dims+i);
}
vals = mxGetPr(matrix);
arr = newAV();
for(i=0;i<nelem;i++) {
av_push(arr, newSVnv(*(vals+i)));
- printf("%6.4f,",*(vals+i));
+# printf("%6.4f,",*(vals+i));
}
- printf("\n");
+# printf("\n");
RETVAL = newRV_inc((SV *) arr);
}
@@ -148,8 +184,8 @@
RETVAL
SV *
-engGetArray2dim(ep, name)
- Engine * ep
+engGetArray2dim(eng, name)
+ EngineIface * eng
const char * name
CODE:
@@ -159,20 +195,20 @@
double *vals;
AV *arr, *mat;
- matrix = engGetArray(ep, name);
+ matrix = engGetVariable(eng->ep, name);
if (matrix == NULL)
XSRETURN_UNDEF;
nrdim = mxGetNumberOfDimensions(matrix);
- printf("%d dimensions\n",nrdim);
+# printf("%d dimensions\n",nrdim);
if (nrdim != 2)
XSRETURN_UNDEF;
- dims = mxGetDimensions(matrix);
+ dims = (int*) mxGetDimensions(matrix);
nelem = 1;
for(i=0;i<nrdim;i++) {
- printf("\t%d\n",*(dims+i));
+# printf("\t%d\n",*(dims+i));
nelem *= *(dims+i);
}
vals = mxGetPr(matrix);
@@ -180,12 +216,12 @@
for(i=0;i<*(dims+1);i++) {
arr = newAV();
for(j=0;j<*dims;j++) {
- printf("%6.4f,",*vals);
+# printf("%6.4f,",*vals);
av_push(arr, newSVnv(*(vals++)));
}
av_push(mat, newRV_inc((SV *) arr));
}
- printf("\n");
+# printf("\n");
RETVAL = newRV_inc((SV *) mat);
}
@@ -193,8 +229,8 @@
RETVAL
SV *
-engGetArray(ep, name)
- Engine * ep
+engGetArray(eng, name)
+ EngineIface * eng
const char * name
CODE:
@@ -204,14 +240,14 @@
double *data;
AV **arrays;
- matrix = engGetArray(ep, name);
+ matrix = engGetVariable(eng->ep, name);
if (matrix == NULL)
XSRETURN_UNDEF;
dim = mxGetNumberOfDimensions(matrix);
# 30.10.02: reverse order of d
- dtmp = mxGetDimensions(matrix);
+ dtmp = (int*) mxGetDimensions(matrix);
d = (int *) calloc(dim + 1, sizeof(int));
for(i=0;i<dim;i++)
*(d+dim-1-i) = *(dtmp+i);
@@ -256,8 +292,8 @@
RETVAL
int
-engPutArray(ep, name, dims, data)
- Engine * ep
+engPutArray(eng, name, dims, data)
+ EngineIface * eng
const char * name
SV * dims
SV * data
@@ -277,8 +313,9 @@
# printf("found %d dimensions\n", ndim + 1);
d = (int *) calloc(ndim + 1, sizeof(int));
# 30.10.02: reverse order of creation of d
-# for(i=0;i<=ndim;i++) {
- for(i=ndim;i>=0;i--) {
+# 08.11.06: Reset this back to original order. Now is consistent wtih PutMatrix
+ for(i=0;i<=ndim;i++) {
+# for(i=ndim;i>=0;i--) {
sv = av_shift(dimarr);
if (SvIOK(sv)) {
# printf("Integer value: %d\n", SvIV(sv));
@@ -294,7 +331,7 @@
# printf("Don't know what it is!\n");
*(d+i) = 1;
}
-# d[i] = (int) SvIV(sv);
+ d[i] = (int) SvIV(sv);
# printf("dimension %d: %d\n", i, d[i]);
}
@@ -303,7 +340,8 @@
# printf("Array has %d elements\n", nelem);
- mat = mxCreateDoubleMatrix(1, nelem + 1, mxREAL);
+ mat = mxCreateNumericArray( ndim+1, (size_t*) d, mxDOUBLE_CLASS, mxREAL);
+ #mat = mxCreateDoubleMatrix(1, nelem + 1, mxREAL);
values = mxGetPr(mat);
# printf("array has %d elements\n", nelem + 1);
@@ -329,17 +367,22 @@
# printf("value nr. %d is %f\n", i + 1, *(values+i));
}
- mxSetName(mat, name);
- mxSetDimensions(mat, d, ndim + 1);
+ // This is now obsolete. Not needed.
+ //mxSetName(mat, name);
- RETVAL = 1 - engPutArray(ep, mat);
+ // We now set the dims when we create the array
+ //mxSetDimensions(mat, (mwSize*) d, ndim + 1);
+
+
+ RETVAL = 1 - engPutVariable(eng->ep, name, mat);
+# RETVAL = 1;
}
OUTPUT:
RETVAL
int
-engPutMatrix(ep, name, rows, cols, data)
- Engine * ep
+engPutMatrix(eng, name, rows, cols, data)
+ EngineIface * eng
const char * name
SV * rows
SV * cols
@@ -396,17 +439,17 @@
if (j > nelem) j-=nelem;
}
+ // Obsolete. Now not needed
+ // mxSetName(mat, name);
- mxSetName(mat, name);
-
- RETVAL = 1 - engPutArray(ep, mat);
+ RETVAL = 1 - engPutVariable(eng->ep, name, mat);
}
OUTPUT:
RETVAL
SV *
-engGetMatrix(ep, name)
- Engine * ep
+engGetMatrix(eng, name)
+ EngineIface * eng
const char * name
CODE:
@@ -416,7 +459,7 @@
double *vals;
AV *arr, *mat;
- matrix = engGetArray(ep, name);
+ matrix = engGetVariable(eng->ep, name);
if (matrix == NULL)
XSRETURN_UNDEF;
@@ -426,7 +469,7 @@
if (nrdim != 2)
XSRETURN_UNDEF;
- dims = mxGetDimensions(matrix);
+ dims = (int*) mxGetDimensions(matrix);
nelem = 1;
for(i=0;i<nrdim;i++) {
# printf("\t%d\n",*(dims+i));
--- Math-Matlab-Engine-0.02/typemap 2003-11-26 22:16:08.000000000 +0100
+++ Math-Matlab-Engine/typemap 2006-11-15 18:31:15.000000000 +0100
@@ -1,13 +1,13 @@
TYPEMAP
-Engine * ENG_OBJ
+EngineIface * ENG_OBJ
const char * T_PV
double * T_PV
INPUT
ENG_OBJ
- {
$var = ($type) SvIV((SV*)SvRV($arg));
- }
+ENG_REF
+ $var = ($type) SvIV((SV*)SvRV($arg));
OUTPUT
ENG_OBJ
--- Math-Matlab-Engine-0.02/t/matrix.t 2003-11-26 22:16:08.000000000 +0100
+++ Math-Matlab-Engine/t/matrix.t 2006-11-15 18:36:49.000000000 +0100
@@ -6,11 +6,15 @@
ok(1);
$ep = Math::Matlab::Engine->new;
-ok($ep->PutArray('N',[3,2],[1,2,3,4,5,6]));
+ok($ep->PutArray('N',[2,3],[1,4,2,5,3,6]));
-ok($ep->PutArray('E1',[1,3],[1,0,0]));
-ok($ep->PutArray('E2',[1,3],[0,1,0]));
-ok($ep->PutArray('E3',[1,3],[0,0,1]));
+ok($ep->PutArray('E1',[3,1],[1,0,0]));
+ok($ep->PutArray('E2',[3,1],[0,1,0]));
+ok($ep->PutArray('E3',[3,1],[0,0,1]));
+
+#$ep->PutMatrix('Ntest',2,3,[1,2,3,4,5,6]);
+#$ep->EvalString("desktop");
+#sleep 30;
ok($ep->EvalString("N1=N*E1"));
ok($ep->EvalString("N2=N*E2"));
@@ -20,3 +24,5 @@
ok($n1->[0]->[0],1);
ok(!defined($n1->[0]->[1]));
ok($n1->[1]->[0],4);
+
+$ep->Close;