Introduction to BLITZ ++ and MTL two numerical calculation library (C ++)

xiaoxiao2021-03-06  58

Excerpt from: http://blog.9cbs.net/newsuppy/archive/2004/08/29/88092.aspx

BLITZ and MTL are based on C Template efficient numerical calculation libraries, but they focus on different directions.

BLITZ provides an N-dimensional (1-10) Array class, this Array class implements the Reference Counting technology, supports any storage (Row-Major's C-style array, Column-Major's fortran-style array), array Slick, sub-array extraction, flexible Array related expression processing. It also provides a class that can produce different distributions (F, Beta, Chi-Square, normal, uniform distribution, etc.).

MTL focuses on linear algebraic computing tasks, such as the generation of various form matrices (diagonal, conjugated, sparse, symmetry, etc.), related calculations, transformation, and calculations with one-dimensional vector.

Two libraries have good support for importing data from Matlab import.

This article mainly shows how to use these two libraries under the Visual C 7.1 compiler.

The previous VC6 compiler is not enough support for ISO C 98 standards, especially in Template, so that this library constructed using Template technology is difficult to compile. BLITZ is completely not supported by VC6.

To VC7.1, due to the support of ISO standards reached 98%, we can easily compile these two libraries.

However, the document of these two libraries is not so friendly, especially MTL, only providing a document similar to Reference, and is not introduced for specific usage. BLITZ is relatively good, and it also provides a introductory entry document. So using these two libraries to read their source code is often necessary. Of course, the two libraries are Template code, and the source code must be fully open.

Let me introduce it first.

1, BLITZ , the current highest version is 0.7, Blitz has become a sourceforge project, so you can download it in SourceForge.net. After downloading, you will see / BLITZ -0.7/BLITZ and /BLITZ -0.7/random two folders, which is the source code of Blitz. /BLITZ -0.7/Manual is the folder where the document is located. /BLITZ -0.7/benchmarks ,/blitz -0.7/EXAMPLES and /BLITZ -0.7/testsuite have a lot of good usage examples available.

Now set the Ide of the VC IDE to /Blitz -0.7, because there is a form of #include in the Blitz source code.

, # include

. Or simply put the two source folders to get the COPY to the include folder. Then change the config.h under the Blitz folder to other names, and change the name of config-vs.net2003.h to config.h. OK, now you can compile all TestSuite and Benchmarks.

1, MTL configuration is more troublesome, now http://www.osl.iu.edu/research/mtl/ here downloads a VC 7, but you can't use it immediately. Since VC 7.1 is more close to standard support, it is more stringent for some syntax details (mainly for TypenAme and Template Partial Specialization), we have to make some small modifications to the code, especially MTL /mtl_config.h this file. There are some places to add Typename. In addition, there are two templates that are offset by two templates, plus template <>. Here http://newsuppy.go.nease.net/mtl.zip I have a version of the completed version, but I don't guarantee my new BUGS that may be introduced, so please use it with caution. The internal use of the MTL uses a certain number of STL components and algorithms. The MTL source code is in the MTL folder, and since the include "..." is in the form of #include "..." in the form of the MTL, you can copy the MTL folder to the current Project. If you want to set up a VC 's include directory, you should first change all #include "..." to #include <...>. However, I have just started using MTL and there are some things that are less likely to be accepted. For example, MTL :: Matrix This template class is not capable of generating a actual matrix object, and generating a type of corresponding template parameters through its Type member, reappearing objects through this type.

For example, Typedf mtl :: Matrix

,

Rectangle <>, Dense <>, Row_major

> :: Type Matrix; Matrix A;

The A here is a real matrix object, while Matrix is ​​a matrix class that is Float, rectangle, dense, and c-style.

Below I offer three simple entry examples to explain the use of MTL. An example of a matrix addition, multiplication, transposition, reverse, and a linear equation group solving.

In addition, MTL's Test and Contrib folders have a lot of good sample code available.

MTL uses example 1, addition, multiplication, and transposition of the matrix.

#include

#include

#include "mtl / mtl.h"

#include

Using namespace std;

Using namespace MTL;

Template

Void Print_Matrix (Matrix & Mat, Const String & Description)

{

Std :: Cout << Description;

Std :: cout << '[';

For (Matrix :: Iterator i = mat.begin (); i! = mat.end (); i)

{

For (Matrix :: One :: Iterator J = (* i) .begin (); J! = (* i) .end (); J)

{

Std :: cout << '/ t' << * j;

}

Std :: cout << ((i 1 == mat.end ())? "/ t] / n": "/ n");

}

}

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

{

Typedef Matrix , dense <>, row_major> :: type matrix; const map :: size_type max_row = 3, max_col = 3;

Matrix A (max_row, max_col), b (max_row, max_col), c (max_row, max_col);

// Fill Matrix a with the index syntax

For (Matrix :: Size_TYPE I = 0; i

{

For (Matrix :: size_type j = 0; j

{

A (i, j) = matrix :: value_type (rand ()% 50);

}

}

// Fill Matrix B with the iterator syntax

FOR (Matrix :: Iterator I = B.BEGIN (); i! = b.end (); i)

{

For (Matrix :: One :: Iterator J = (* i) .begin (); J! = (* i) .end (); J)

{

* j = matrix :: value_type (rand ()% 50);

}

}

Print_matrix (A, "a = / N");

Print_matrix (b, "b = / n");

// Matrix C = A B

Add (a, c);

Add (b, c);

Print_matrix (C, "C = A B / N");

// matrix c = a * b ^ t, b ^ T: Transpose of B

Transpose (b);

Print_matrix (b, "b ^ t = / n");

ZERO_MATRIX (C); // Because Mult (A, B, C): C = a * b MULT (A, B, C); Print_Matrix (C, "C = A * B ^ T / N");

Return 0;

}

2, below is the solution of a linear equation group

#include

#include

#include

#include "mtl / mtl.h"

#include "mtl / lu.h"

#include

Using namespace std;

Using namespace MTL;

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

{

Typedef Matrix , Dense , row_major> :: type matrix;

// Dense

: Data Copy from A Float Array, Not Generate Them with your yourself

Const Matrix :: size_type max_row = 3, MAX_COL = 3;

// Solve the equation ax = b

// {4x - y z = 7

// 4x - 8y z = -21

// -2x y 5z = 15}

// a = [4 -1 1

// 4 -8 1

// -2 1 5]

// b = [7 - 21 15] ^ tfloat a [] = {

4.0F

,

-

1.0F

,

1.0f

,

4.0F

,

-

8.0f

,

1.0f

,

-

2.0F

,

1.0f

,

5.0F

}

Matrix A (A, Max_Row, Max_col);

Typedef Matrix , dense <>, row_major> :: type lumatrix;

Lumatrix Lu (a.nrows (), a.ncols ());

MTL :: Copy (a, lu);

TypedEf Dense1D vector;

Vector PVector (a.nrows ());

LU_FACTOR (Lu, PVector);

VECTOR B (a.nrows ()), x (a.nrows ());

B [0] =

7.0f

, B [1] =

-

21.0F

, B [2] =

15.0F

;

LU_SOLVE (Lu, PVector, B, X);

For (Vector :: item = x.begin (); i! = x.end (); i)

COUT << * i << '/ t';

System ("pause");

Return 0;

}

3, the matrix is ​​reversed

#include

#include

#include

#include "mtl / mtl.h"

#include "mtl / lu.h"

#include

Using namespace std;

Using namespace MTL;

Template

Void Print_Matrix (Matrix & Mat, Const String & Description)

{

Std :: Cout << Description;

Std :: cout << '[';

For (Matrix :: Iterator i = mat.begin (); i! = mat.end (); i)

{

For (Matrix :: One :: Iterator J = (* i) .begin (); J! = (* i) .end (); J)

{

Std :: cout << '/ t' << * j;

}

Std :: cout << ((i 1 == mat.end ())? "/ t] / n": "/ n");

}

}

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

{

Typedef Matrix , Dense , row_major> :: type matrix;

// Dense

: Data Copy from A Float Array, Not Generate Them with your yourself

Const Matrix :: size_type max_row = 3, MAX_COL = 3;

// Inverse Matrix A

// a = [4 -1 1

// 4 -8 1

// -2 1 5]

Float a [] = {

4.0F

,

-

1.0f

,

1.0f

,

4.0F

,

-

8.0f

,

1.0f

,

-

2.0F,

1.0f

,

5.0f

}

Matrix A (A, Max_Row, Max_col);

TypedEf Matrix , Dense <>, Row_major> :: Type Cmatrix;

CMatrix Lu (a.nrows (), a.ncols ());

MTL :: Copy (a, lu);

TypedEf Dense1D vector;

Vector PVector (a.nrows ());

LU_FACTOR (Lu, PVector);

CMAMATRIX INVA (A.nRows (), A.ncols ());

Lu_INVERSE (Lu, PVector, INVA);

Print_matrix (A, "a = / N");

Print_matrix (INVA, "A ^ (- 1) = / N");

System ("pause");

Return 0;

}

Reference: 1, numerical method (MATLAB version) 3RD

John H. MatHews, Kurtis D.FINK, Chen Yu, Zhou Wei, Money Fang and other translations

2, MATLAB 6.5 Documentation The MathWorks, Inc.

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

New Post(0)