![]() |
Languages »
C / C++ Language »
General
Intermediate
Solving Engineering Problems Using MATLAB C++ Math LibraryBy A. RiaziUsing MATLAB C++ Math Library to solve engineering problems |
VC6Win2K, WinXP, MFC, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||

In the previous article, we studied how can use MATLAB C API to solve engineering problems. In this article I will show you how can use MATLAB C++ math library. The MATLAB� C++ Math Library serves two separate constituencies: MATLAB programmers seeking more speed or complete independence from interpreted MATLAB, and C++ programmers who need a fast, easy-to-use matrix math library. To each, it offers distinct advantages.
MATLAB is abbreviation of Matrix Laboratory. This means every computation was performed in matrix form. In other hand every data type wrapped in matrix form and every function take these matrix as an input argument.
For example you want to multiply to polynomial as follow:
A = (3x2 + 5x + 7) (4x5 + 3x3 - x2 + 1)
You can use two matrices for coefficients of any polynomials:
[3 5 7] for (3x2 + 5x + 7) and [4 0 3 -1 0 1] for (4x5 + 3x3 - x2 + 1), using conv function, we can obtain coefficients of result:
conv([3 5 7], [4 0 3 -1 0 1]): A = [12 20 37 12 16 -4 5 7]
means:
A = 12x7 + 20x6 + 37x5 + 12x4 + 16x3 - 4x2 + 5x + 7
The MATLAB C++ Math Library consists of approximately 400 MATLAB math functions. It includes the built-in MATLAB math functions and many of the math functions that are implemented as MATLAB M-files. The MATLAB C++ Math Library is layered on top of the MATLAB C Math Library. The major value added by this C++ layer is ease of use.
The MATLAB C++ Math Library is firmly rooted in the traditions of the MATLAB runtime environment. Programming with the MATLAB C++ Math Library is very much like writing M-files in MATLAB. While the C++ language imposes several differences, the syntax used by the MATLAB C++ Math Library is very similar to the syntax of the MATLAB language. Like MATLAB, the MATLAB C++ Math Library provides automatic memory management, which protects the programmer from memory leaks.
Every matrices represented by mwArray class, a data type introduced by MATLAB for constructing a matrix. As I said before, every data must be wrapped in a matrix form in other hand: mwArray.
One C++ prototype supports all the possible ways to call a particular MATLAB C++ Math Library function. You can reconstruct the C++ prototype by examining the MATLAB syntax for a function. In the following procedure, the MATLAB function svd() is used to illustrate the process.
s = svd (X)
[U, S, V] = svd (X)
[U, S, V] = svd (X, 0)
In this example, the prototype that corresponds to [U, S, V] = svd (X, 0) is constructed step-by-step. Until the last step, the prototype is incomplete.
mwArray.
mwArray svd(
mwArray*.
mwArray svd (mwArray* S, mwArray* V,
mwArray.
mwArray svd (mwArray* S, mwArray* V, const mwArray& X,
const mwArray& Zero);
The prototype is complete.
This procedure translates the MATLAB call [U, S, V] = svd (X, 0) into a C++ call. The procedure applies to library functions in general. Note that within a call to a MATLAB C++ Math Library function, an output argument is preceded by &; an input argument is not.
mwArray variables, and assign values to the input variables.U = svd (
U = svd (&S, &V,
U = svd (&S, &V, X, 0);The translation is complete.
Note that if you see [] as a MATLAB input argument, you should pass mwArray() as the C++ argument. For example,
B = cplxpair (A, [], dim)
becomes
B = cplxpair (A, mwArray(), dim);
The mwArray class public interface is relatively small, consisting of constructors and destructor, overloaded new and delete operators, one user-defined conversion, four indexing operators, the assignment operator, input and output operators, and array size query routines. The mwArray�s public interface does not contain any mathematical operators or functions.
The mwArray interface provides many useful constructors. You can construct a mwArray object from the following types of data: a numerical scalar, an array of scalars, a string, an mxArray*, or another mwArray object.
mwArray()
Create an uninitialized array. An uninitialized array produces warnings when passed to MATLAB C++ Math Library functions. If an array is created using this default constructor, a value must be assigned to it before passing it to a MATLAB C++ Math Library function.
To create an empty double matrix that corresponds to [] in MATLAB, use the
function empty().
mwArray (const char *str)
Create an array from a string. The constructor copies the string.
mwArray(int32 rows, int32 cols, double *real, double *imag = NULL)
Create a mwArray from either one or two arrays of double-precision floating-point numbers. If two arrays are specified, the constructor creates a complex array; both input arrays must be the same size. The data in the input arrays must be in column-major order, the reverse of C++�s usual row-major order. This constructor copies the input arrays.
Note that the last argument, imag, is assigned a value of NULL in the constructor. imag is an optional argument. When you call this constructor, you do not need to specify the optional argument.
mwArray (const mwArray& mtrx)
Copy a mwArray. This constructor is the familiar C++ copy constructor, which copies the input array. For efficiency, this routine does not actually copy the data until the data is modified. The data is referenced through a pointer until a modification occurs.
mwArray (const mxArray* mtrx)
Make a mwArray from an mxArray*, such as might be returned by any of the routines in the MATLAB C Math Library or the Application Program Interface Library. This routine does not copy its input array, yet the destructor frees it; therefore the input array must be allocated on the heap. In most cases, for example, with matrices returned from the Application Program Interface Library, this is the desired behavior.
mwArray (double start, double step, double stop)
Create a ramp. This constructor operates just like the MATLAB colon (:) operator. For example, the call mwArray(1, 0.5, 3) creates the vector [ 1, 1.5, 2, 2.5, 3 ].
mwArray (int32 start, int32 step, int32 stop)
Create an integer ramp.
mwArray (const mwSubArray & a)
Create an mwArray from an mwSubArray. When an indexing operation is applied to an array, the result is not another array, but an mwSubArray object. An mwSubArray object remembers the indexing operation. Evaluation of the operation is deferred until the result is assigned or used in another expression. This constructor evaluates the indexing operation encoded by the mwSubArray object and creates the appropriate array.
mwArray (double)
Create a 1-by-1 mwArray from a double-precision floating-point number.
mwArray (int)
Create an mwArray from an integer.
Table 1 shows mwArray constructors in brief.
| Constructor | Creates | Example |
|---|---|---|
|
|
Uninitialized array |
|
|
|
String array |
|
|
|
Complex array |
|
|
|
Copy of input array |
|
|
|
Copy of mxArray* |
|
|
|
Ramp |
|
|
|
Integer ramp |
|
|
|
Array from subarray (used in indexing) |
|
|
|
Scalar double array |
|
|
|
Scalar integer array |
|
Below is a list of useful mathematical functions of MATLAB C++ math library:
| plus, minus | mtimes, mpower | acos, asin | conv |
| conj | dec2bin, dec2hex | disp | fft, fft2 |
| linspace | max, min | roots | rot90 |
To add support of MATLAB C++ Math Library follow these instructions:\
#include <matlab.hpp>
matlab.hpp is interface of MATLAB C++ math library. Add directory of MATLAB interface files (*.hpp) to Visual Studio (Tools -> Options -> Directories). For example: x:\matlab\extern\include\cpp, where x is drive letter of matlab path.
#include "stdafx.h" #include "matlab.hpp" //Interface of MATLAB CPP Math Library //Add C++ Math Library to project #pragma comment(lib, "libmatpm.lib") #pragma comment(lib, "libmx.lib") #pragma comment(lib, "libmatlb.lib") #pragma comment(lib, "libmat.lib") #pragma comment(lib, "libmmfile.lib") #pragma comment(lib, "libmatpm.lib") int main(int argc, char* argv[]) { mwArray A, B, C; A = magic(mwArray(5)); B = transpose(A); //B=A' C = plus(A,B); //C = A + B; C = minus(A,B); //C = A - B; C = mtimes(A,B); //C = A * B; double arr1[]={3.0, 2.0, 5.0, -1.0}; double arr2[]={8.0, 1.0, 3.0, -2.0}; mwArray D(1, 4, arr1); mwArray E(4, 1, arr2); C = D * E; //4*4 matrix mwArray F(0.0, 0.1, 5.0); //create double ramp F = F * transpose(F); A.Print("A"); //Magic Matrix, Order=5 C.Print("C"); D.Print("D"); return 0; }
c:\matlab\extern\include\cpp\matmtxif.h (16):
fatal error C1083: Cannot open include file strstream.h.
No such file or directory.
This error is due to missing preprocessor definitions in the MSVC environments. In order to alleviate this problem, the following definitions must be added to the project file for the application:
Add these preprocessors to your project: Project->Settings->C/C++->Preprocessor definitions.
LINK : warning LNK4098: defaultlib "MSVCRT" conflicts with use of other libs;
use /NODEFUALTLIB:library
.\ex1.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe
To resolve this problem change project settings to build a Multithread Dll in the Runtime Library. Do this by following these instructions:
Enjoy!
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 21 Jul 2003 Editor: Nishant Sivakumar |
Copyright 2003 by A. Riazi Everything else Copyright © CodeProject, 1999-2009 Web15 | Advertise on the Code Project |