Programming and Graphics

(Kiana) #1

308 Introduction to C++ Programming and Graphics


//--- Evaluate the square:

engEvalString(oliver, "mat2 = mat^2");
cout << BuFFer;

//--- Retrieve the square:

mxArray * x2 = engGetVariable(oliver, "mat2");
double * square = mxGetPr(x2);
cout << "C++ domain:" << endl;
cout << square[0] <<""<<square[2] << endl;
cout << square[1] <<""<<square[3] << endl;

//--- End the session:

engClose(oliver);

return 0;
}

The session produces the following output on the screen:


>>
determinant =
-5
>>
mat2 =
916
817
C++ domain:
916
817

The indented output originates from theMatlabdomain, and the non-indented
output originates from the C++ domain. It is important to observe that the
Matlabmatrixx2is retrieved as the C++ vectorsquarein acolumn-wise
fashion.


Eigenvalues and eigenvectors


A complex number is composed of a real and an imaginary part. The real
and imaginary parts of a scalar, vector, or matrix produced byMatlabare
placed in consecutive memory blocks.


The following code contained in the filemuse2.ccdefines a matrix in the
C++ domain, callsMatlabto compute the eigenvalues and eigenvectors, and
transfers the results back to the C++ domain:

Free download pdf