11 #ifndef EASYLINK_ARRAY_H 12 #define EASYLINK_ARRAY_H 34 Array(
string name=
"untitled array")
44 printf(
"easyLink test message: constructing empty array called \"%s\".\n",getFullName().c_str());
52 Array(
int nrows,
int ncols=1,
string name=
"untitled array",
bool moveable=
false)
57 this->data=
new double[nrows*ncols];
59 this->moveable=moveable;
60 this->releaseData=
true;
61 memset((
void*)data,0,nrows*ncols*
sizeof(
double));
64 printf(
"easyLink test message: constructing array called \"%s\".\n",getFullName().c_str());
73 Array(
const mxArray *mxarray,
string name=
"untitled mxArray",
bool moveable=
false,
bool dataCopy=
false)
75 if (mxIsSparse(mxarray)||!mxIsDouble(mxarray))
76 throw runtime_error(
"easyLink error: the array must be a dense array of real values.");
78 this->nrows=(int)mxGetM(mxarray);
79 this->ncols=(int)mxGetN(mxarray);
83 this->data=
new double[nrows*ncols];
84 memcpy((
void*)data,(
void*)mxGetPr(mxarray), nrows*ncols*
sizeof(
double) );
85 this->releaseData=
true;
92 this->mxarray=(mxArray*)mxarray;
93 this->data=mxGetPr(mxarray);
94 this->releaseData=
false;
97 this->moveable=moveable;
99 printf(
"easyLink test message: constructing array (using allocated mxarray) called \"%s\".\n",getFullName().c_str());
110 Array(
const double *data,
int nrows,
int ncols=1,
string name=
"untitled data",
bool moveable=
false,
bool releaseData=
false)
115 this->data=(
double*)data;
117 this->moveable=moveable;
118 this->releaseData=releaseData;
120 printf(
"easyLink test message: constructing array (using allocated double array) \"%s\".\n",getFullName().c_str());
134 printf(
"easyLink test message: hard copy of array \"%s\" in copy constructor.\n",array.
name.c_str());
152 printf(
"easyLink test message: destroying \"%s\".\n",getFullName().c_str());
182 if (nrows==0 && ncols==0)
189 printf(
"easyLink test message: hard copy in assignment \"%s=%s\".\n",name.c_str(),array.
name.c_str());
194 else if (releaseData)
201 else if (nrows==array.
nrows && ncols==array.
ncols)
204 printf(
"easyLink test message: hard copy in assignment \"%s=%s\".\n",name.c_str(),array.
name.c_str());
206 memcpy((
void*)data,(
void*)array.
data, nrows*ncols*
sizeof(
double) );
211 printf(
"easyLink test message: hard copy in assignment \"%s=%s\".\n",name.c_str(),array.
name.c_str());
220 throw runtime_error(
"easyLink error: Unable to assign "+array.
name+
" to shared array "+name+
". Array dimensions must agree.");
223 printf(
"easyLink test message: hard copy in assignment \"%s=%s\".\n",name.c_str(),array.
name.c_str());
225 memcpy((
void*)data,(
void*)array.
data, nrows*ncols*
sizeof(
double) );
231 void operator=(
const Array &&array)
233 if (nrows==0 && ncols==0)
237 else if (releaseData)
244 if (nrows!=array.nrows || ncols!=array.ncols)
245 throw runtime_error(
"easyLink error: Unable to move assign "+array.name+
" to shared array "+name+
". Array dimensions must agree.");
248 printf(
"easyLink test message: hard copy in assignment \"%s=%s\".\n",name.c_str(),array.name.c_str());
250 memcpy((
void*)data,(
void*)array.data, nrows*ncols*
sizeof(
double) );
267 copy=mxCreateDoubleMatrix(nrows,ncols,mxREAL);
268 memcpy((
void*)mxGetPr(copy),(
void*)data, nrows*ncols*
sizeof(
double) );
309 if ( (i<0) || (i>=nrows*ncols) )
310 throw range_error(
"easyLink error: index exceeds array dimensions when accessing to "+name+
"["+
toString(i)+
"].");
318 if ( (row<0) || (row>=nrows) || (col<0) || (col>=ncols) )
319 throw range_error(
"easyLink error: index exceeds array dimensions when accessing to "+name+
"("+
toString(row)+
","+
toString(col)+
").");
320 return *(data+row+nrows*col);
336 if (this->nrows*this->ncols!=nrows*ncols)
337 throw runtime_error(
"easyLink error: unable to reshape "+name+
".");
347 printf(getFullName().c_str());
349 printf(name.c_str());
353 for (
int row=0; row<nrows; row++)
355 for (
int col=0; col<ncols; col++)
356 printf(
" %7g",data[row+nrows*col]);
369 mxArray *input,*output;
372 input=getmxArrayCopy();
376 mexCallMATLAB(1,&output,1,&input,cmd.c_str());
378 Array result(output,cmd+
"("+name+
")",
true,
true);
379 mxDestroyArray(output);
382 mxDestroyArray(input);
392 mxArray *input[2],*output;
395 input[0]=getmxArrayCopy();
404 mexCallMATLAB(1,&output,2,input,cmd.c_str());
406 Array result(output,cmd+
"("+name+
","+operand2.
name+
")",
true,
true);
407 mxDestroyArray(output);
410 mxDestroyArray(input[0]);
413 mxDestroyArray(input[1]);
423 mxArray *input[2],*output;
426 input[0]=getmxArrayCopy();
430 input[1] = mxCreateDoubleMatrix(1,1,mxREAL);
431 *mxGetPr(mxarray)=operand2;
432 mexCallMATLAB(1,&output,2,input,cmd.c_str());
434 Array result(output,cmd+
"("+name+
","+
toString(operand2)+
")",
true,
true);
435 mxDestroyArray(output);
438 mxDestroyArray(input[0]);
439 mxDestroyArray(input[1]);
524 if (nrows!=operand.
nrows || ncols!=operand.
ncols)
525 throw runtime_error(
"easyLink error: Unable to add "+name+
" and "+operand.
name+
". Array dimensions must agree.");
529 double* s=operand.
data;
538 if (nrows!=operand.
nrows || ncols!=operand.
ncols)
539 throw runtime_error(
"easyLink error: Unable to substract "+name+
" and "+operand.
name+
". Array dimensions must agree.");
543 double* s=operand.
data;
552 if (nrows!=operand.
nrows || ncols!=operand.
ncols)
553 throw runtime_error(
"easyLink error: Unable to multiply "+name+
" and "+operand.
name+
". Array dimensions must agree.");
557 double* s=operand.
data;
566 if (nrows!=operand.
nrows || ncols!=operand.
ncols)
567 throw runtime_error(
"easyLink error: Unable to divide "+name+
" and "+operand.
name+
". Array dimensions must agree.");
571 double* s=operand.
data;
579 return callMatlab(
"plus",operand);
585 return callMatlab(
"minus",operand);
591 return callMatlab(
"mtimes",operand);
606 double *q=operand.
data;
607 for (;i-- &&
areEqual(*p,*q);p++,q++)
647 double* p=result.
data;
651 result.
name=
"-"+name;
658 return callMatlab(
"inv");
662 static Array eye(
int nrows,
int ncols,
string name=
"eye array")
664 Array result(nrows,ncols,name,
true);
666 for(
int i=0; i<min(nrows,ncols); i++)
698 data=
new double[nrows*ncols];
699 memcpy((
void*)data,(
void*)array.
data, nrows*ncols*
sizeof(
double) );
700 name=string(
"copy of ")+array.
name;
715 result+=
"releaseData ";
717 result+=
"mex array)";
719 result+=
"double array)";
726 static int allocationNumber;
731 int Array::allocationNumber = 0;
737 return operand.operator+(x);
743 return operand.
opposite().operator+(x);
749 return operand.operator*(x);
bool areEqual(double x, double y, double tolerance=EQUALITY_TOLERANCE)
void operator+=(const Array &operand)
void operator-=(double x)
bool operator==(const Array &operand)
Array operator-(const Array &operand)
Array operator+(double x)
Array operator-(double x, Array &operand)
void operator/=(const Array &operand)
void stealData(const Array &array)
static Array eye(int nrows, int ncols, string name="eye array")
Array callMatlab(string cmd, const Array &operand2)
Array operator*(const Array &operand)
void operator-=(const Array &operand)
void operator*=(double x)
void copyOf(const Array &array)
void operator/=(double x)
void operator*=(const Array &operand)
mxArray * getmxArrayCopy() const
Array(int nrows, int ncols=1, string name="untitled array", bool moveable=false)
Array callMatlab(string cmd)
void reshape(int nrows, int ncols)
Array operator+(const Array &operand)
double & operator()(int row, int col)
Array(const double *data, int nrows, int ncols=1, string name="untitled data", bool moveable=false, bool releaseData=false)
Array(const mxArray *mxarray, string name="untitled mxArray", bool moveable=false, bool dataCopy=false)
Array callMatlab(string cmd, double operand2)
void operator=(const Array &array)
Array(const Array &array)
Array operator*(double x, Array &operand)
Array operator+(double x, Array &operand)
Array operator/(double x)
Array operator*(double x)
double & operator[](int i)
Array operator-(double x)
Array(string name="untitled array")
void operator+=(double x)