Scientific calculation and visualization (MATLAB CC ++ function library)

zhaozj2021-02-16  46

1 Introduction

In 1984, the US MathWorks launched Matlab, so far, it has developed into one of the best technology applications in the international technology. Its powerful scientific computing and visualization function, simple and easy-to-use open scale-extended environments, as well as more than 30 toolbox supported to different fields, making MATLABs into computer-aided design and analysis in many disciplines , Algorithm research and application development basic tools and preferred platforms. However, due to its compiler with pseudo-compilation, the program written in Matlab cannot be run independently of its working environment. In response to this problem, MathWorks provides MATLAB, allows MATLAB and other applications to exchange data exchange, and provides a C / C Mathematical and Graphic Function, providing a possible possible to other programming language calls MATLAB efficient algorithms.

The C language is a new generation of object-oriented concept-based advanced programming languages. Its object-oriented concept is more compliant with programmer development software thinking habits, class encapsulation and modular construction. And maintenance, use C development helps to improve the quality of software engineering. VC is a new generation of compilers developed by Microsoft's underlying mechanism of C , which is the same as other C compilers, compiled in C languages. VC is a leader of similar products in compilation speed and code optimization, and most software development kits provide an interface to it, which is the main reason for programmers recognized by programmers. For engineering technicians and some engineers and technicians that are compensated in numerical calculations, in order to complete a program that combines a friendly interface and stabilization computing kernel within the short term, the software interface technology can be used to use the third party's function. Library.

2 MATLAB application interface

The application interface provided by MATLAB can be divided into the following: 1. Use the advantages of other applications, such as the interface to which the speed is fast and the existing algorithm is used; 2. Establish MATLAB and other applications Data exchange; 3. Development of MATLAB applications and applications. The development of the corresponding application is the MEX file, the MAT file, and the MATLAB engine application. As a dynamic link library file, the MEX file must be operated by calling in MATLAB's working environment; MAT files are used for data exchange, and cannot utilize the functions provided by MATLAB to complete the computing task; the MATLAB engine application is an independent The application is executed, but when the application is executed, a MATLAB process will be launched in the background, which is used to receive instructions sent from the application and execute, and then return to the calculation result. In summary, Applications developed based on MATLAB application interfaces are not perfect. This application cannot be separated from the MATLAB environment.

3 VC call matlab mathematics and graphics library

6.1 Version of Mathematical and graphics libraries are available in the MATLAB package, and the M files written in matlab can be converted into files in C / C code through their compiler support, and the M file can generate a DLL library. Even we can directly call the library function, generate and publish executables that do not have to rely on MATLAB. Generate C / C code via the MCC compiler, which can be generated in VC or other compiler to generate independently executable applications, and the specific flow is shown below:

figure 1

The following specifically discusses how to generate a C / C code from the M file and embed user code, generate an application that does not require MATLAB support when running. Set the matsum.m file, the content is as follows: function sum = matbs (a, b)

% SUM

SUM = a b

Convert to C / C code with the MCC command, the command line parameters are as follows

Generate a C file

MCC -T -L C Matsum.m

Generate C files

MCC -T -L CPP Matsum.m

Generate dynamic link library functions

MCC -T -W LIB: SUM -T LINK: LIB MATSUM.M

Note 1LIB: After the generation of the library file name cannot be the same as the original M file name (here SUM), 2 can only generate a dynamic link library of C language mode here. The compile command has a total of 9 files, where Sum.Lib and Sum.dll are dynamic link library files that can be called in the C / C application.

Analyze the generated C files, it is not difficult to find the compiler according to the function in the m file, generate a corresponding C function: Static Mwarray mmatsum (int Nargout_, Mwarray A, Mwarray B); and interface according to C and MEX files, respectively Mode generation interface functions: extern mwarray matsum (mwarray a = mwarray :: din, mwarray b = mwarray :: din); and void mlxmatsum (int NLHS, MxArray * PLHS [], int NRHS, MXAARRAY * PRHS [ ]); There is also a partial function table declaration 1.

Below we are programming in the VC IDE environment, in order to facilitate the first generation of C simple console applications, and then use the MATSum.cpp and matsum.hpp generated by the MCC command in the project (Project) (PROJECT) In this item, if the item contains the pre-translated header file stdafx.h, then in matsum.cpp includes the first to include #include "stdafx.h". Then open the Project / Setting ... menu, select PreProcessor in C / C Category, add MSVC, MSWIND, __STDC__ in the predessor definitions column, where __stdc__ is optional, can save in the C project; choose Code Generation, Select Multithreaded DLL in the User Run-Time Library bar. Add library file libmatlb.lib, libmx.lib, libmatpm.lib, libmx.lib, libmatpm.lib, which must be used for programs that use graphics functions.

Modify the option Reference Table 1, generally select Multithreaded DLL in the Code Generation.

Use Static or DLL project

Do you use multi-threaded libraries?

Whether to use a C debug library

C link inventory

Static No Libc.lib Static No is libcd.lib static whether libcmt.lib static is a libcmtd.lib DLL no msvcrt.lib (msvcrt.dll) DLL No is MSVCRTD.LIB (MSVCRTD.DLL) DLL Whether MSVCRT.LIB (mvcrt.dll) DLL is MSVCRTD.LIB (MSVCRTD.DLL) table one

The routines are as follows:

#include "stdafx.h"

// #include "matlab.hpp" // C way

#include "matsum.hpp"

#pragma comment (Lib, "Libmatlb.lib")

#pragma comment (Lib, "libmx.lib")

#pragma comment (lib, "libmatpm.lib") // c Maths Library

Int main (int Argc, char * argv [])

{

Mwarray A (100), B (250);

Mwarray C;

C = matsum (a, b);

Return 0;

}

We can call functions in the m file in a dynamic link library, and create 9 files, copying libs, and dll files, including Sum.lib and Sum.dll, from the previously seen, copies the LIB and DLL files to the project. Call in the following format: SUMINIALIZE (); // Initialize mlfmatsum (); // corresponds to matsum ... Sumterminate (); // Release Note: The parameters of the function in Sum.h and the return value are mostly matrix Mxarry form. Because it is a C language, it is not detailed, the routines are as follows:

{

MxArray * AX, * bx, * cx;

Suminitialize ();

// ax = mxcreatedoublemtrix (1, 1, mxreal);

// bx = mxcreatedoublemtrix (1, 1, mxreal);

MLFASSIGN (& AX, MLFSCALAR (200)); // C language matrix structure assignment

MLFASSIGN (& BX, MLFSCALARARAR (522));

CX = mlfmatsum (ax, bx);

// mlfprintmatrix (cx);

Sumterminate ();

}

4 VC MATLAB development in the environment

From the above conversion, it can be seen that the C / C file generated by the MATLAB compiler command is used, and the input output parameter of its function is Mwarray object. Then you can consider using the MATLAB library function to use Matlab library functions directly in the VC or other C compiler.

The most basic data type in the MATLAB C / C Mathematical Library is a matrix object, that is, the Mwarray class object, almost all library functions take them as the object of calculation and processing, master the operation of the Mwarray object, for learning MATLAB C / C mathematics The use of the library is crucial.

MATLAB C / C Mathematics library provides support for numeric matrices, sparse matrices, character matrices, unit matrices, and structural matrices by class Mwarray, and the creation of matrix objects as follows:

Mwarray a; // Construct an empty matrix object

Mwarray A (Double / Int); / / Moisture matrix object

Mwarray A (int Row, int COL, Double / INT / Unsigned Short * real = NULL,

Double / int / unsigned short * image = null) MWARRAY A (const mwarray &) // copy constructor matrix object

C / C calls the MATLAB function to use the index operation of the matrix object, complete the C / C data to the assignment of the MWARRAY object and the retrieval of the Mwarray object to C / C data. The MATLAB C / C function library provides both functions of Extractscalar and ExtractData to complete indexes. In addition, using getDate () and mxgetpr, the MxGetPI combination can get a pointer to the data of the MWARRAY object.

Call the MATLAB math library in the VC environment, but also pay attention to the abnormal processing. MATLAB defines a group of unusual classes based on MWEXCEPTION. The abnormality is treated as follows:

Try {

......

}

Catch (mwexception) {

MWDisPlayException (ex);

......

}

5 提 ¾ ¾ 简 例

In the computer graphics, the curve is interposed or fitted to the curve, and the solution linear equation group will be used. If the B-spline is interpolation problem, you must first perform the reverse control vertex, then in accordance with the B-spline base, we use the VC to complete the interface, and use the math function provided by the IMSL to complete the calculation.

Example: known value point sequence PI, I = 0, 1, 2 ..., 7:

P [0] p [1] p [2] p [3] p [4] p [5] p [6] p [7] Abscissae x 8.125 8.400 9.000 9.485 9.600 9.959 10.166 10.099 0.28 0.6 0.708 1.20 1.80 2.177

Table II

Seeking control vertex sequences.

The mathematical problems of the crowd of B Yang strip curve control are:

D represents the DE BOOR control vertex, P known value point column and boundary condition vector.

The MATLAB C / C function library is called in the VC, and the left elimination of the matrix can quickly reflect the coordinate value of the DEBOOR to control the vertex, the function is as follows:

Void ResdeBoorpt (int N, double * ppxs, point * pt, point * deboor)

{

Assert (Pt! = Deboor && Pt! = 0 && Deboor! = 0);

// Assign Point Data

Double * PPT = New Double [N * 2];

For (int i = 0; i

{

* (PPT I) = Pt [i] .x;

* (ppt i n) = pt [i] .y;

}

Mwarray xzpt (n, 2, ppt);

// Assign Coefficient Matrix

Mwarray XSJZ (n, n, ppxs);

Mwarray Kzdjz;

Try

{

// Compute Deboor Point Matrix

Kzdjz = MLDIVIDE (XSJZ, 6 * xzpt);

}

Catch (MWEXCEPTION EX)

{

MWDisPlayException (ex);

}

delete [] PPA; Delete [] PPT;

For (i = 0; i

{

Deboor [i] .x = kzdjz.extractscalar (i 1);

Deboor [i] .y = kzdjz.extractscalar (i n 1);

}

}

Calculation results di, i = 0, 1, ..., 9

D [0] = D [1] d [2] d [3] d [4] d [5] d [6] p [7] d [8] = d [9] abscissae x 8.08523 8.32384 9.01941 9.59854 9.49644 10.0157 10.1948 10.201 Ordinates Y 0.07852 0.07183 0.22819 0.69544 0.59008 1.19226 1.84088 2.24422 Table 3

The declaration mode in the MATLAB C / C library is extern, and there is no need to declare when the external reference library function. Since the MATLAB C / C library function uses a dynamic link library, it must include the corresponding application extension file when publishing its own application. The specific method is as follows: Run% matLab% /extern/lib/win32/mglinstaller.exe, generated * .dll includes Bin and Toolbox packaging. Note Set the path on the runner.

6 small knot

This article

Matlab

Application interface, detailed discussion in visual programming environment, use

VC

transfer

Matlab

Mathematical Graphics Library, prepare applications that can run independently. After practical prove, the software development efficiency can be greatly improved, and the level of stability of the algorithm can be increased. There are currently some software systems that use hybrid programming development: such as radar characteristics analysis, real-time prediction software, etc., has been applied in practice.

1 Reference 3.

转载请注明原文地址:https://www.9cbs.com/read-27271.html

New Post(0)