• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            牽著老婆滿街逛

            嚴(yán)以律己,寬以待人. 三思而后行.
            GMail/GTalk: yanglinbo#google.com;
            MSN/Email: tx7do#yahoo.com.cn;
            QQ: 3 0 3 3 9 6 9 2 0 .

            Blitz++與MTL兩大數(shù)值計(jì)算程序庫(kù)(C++)的簡(jiǎn)介

            Blitz++ MTL 兩大數(shù)值計(jì)算程序庫(kù) (C++) 的簡(jiǎn)介

            Blitz++ MTL 都是基于 C++ template 高效數(shù)值計(jì)算程序庫(kù),不過(guò)他們專(zhuān)注于不同的方向。

            Blitz++ 提供了一個(gè) N 維( 1—10 )的 Array 類(lèi) , 這個(gè) Array 類(lèi)以 reference counting 技術(shù)實(shí)現(xiàn),支持任意的存儲(chǔ)序 (row-major C-style 數(shù)組, column-major Fortran-style 數(shù)組 ) ,數(shù)組的切割 (slicing), 子數(shù)組的提取 (subarray), 靈活的 Array 相關(guān)表達(dá)式處理。另外提供了可以產(chǎn)生不同分布的隨機(jī)數(shù) (F,Beta,Chi-Square ,正態(tài),均勻分布等 ) 的類(lèi)也是很有特色的。

            MTL 專(zhuān)注于線性代數(shù)相關(guān)的計(jì)算任務(wù),如各種形式矩陣的生成 ( 對(duì)角,共軛,稀疏,對(duì)稱(chēng)等 ) ,相關(guān)的計(jì)算,變換,以及與一維向量的運(yùn)算。

            兩個(gè)程序庫(kù)對(duì)于從 Matlab 導(dǎo)入導(dǎo)出數(shù)據(jù)都有不錯(cuò)的支持。

            本文主要介紹如何在 Visual C++7.1 編譯器下運(yùn)用這兩個(gè)程序庫(kù)。

            以前的 VC6 編譯器由于對(duì) ISO C++98 標(biāo)準(zhǔn)的支持不夠,特別是在 template 方面,以至于很難編譯這種完全用 template 技術(shù)構(gòu)造起來(lái)的程序庫(kù)。 Blitz++ 是完全不支持 VC6 的。

            到了 VC7.1 ,由于對(duì)于 ISO 標(biāo)準(zhǔn)的支持達(dá)到了 98% ,使得我們可以很輕松的編譯使用這兩個(gè)程序庫(kù)。

            不過(guò)這兩個(gè)程序庫(kù)的文檔不是那么友好,特別是 MTL ,僅僅提供了類(lèi)似于 reference 的文檔,對(duì)于具體的使用方法則不作介紹。 Blitz++ 相對(duì)來(lái)說(shuō)好一些,還提供一份介紹性的入門(mén)文檔 。所以使用這兩個(gè)程序庫(kù)閱讀其源代碼往往是必要的。當(dāng)然了,兩個(gè)程序庫(kù)都是 template 代碼,源代碼必定是全開(kāi)放的。

            先來(lái)介紹一下配置吧

            1,? Blitz++, 目前最高版本是 0.7 Blitz++ 已經(jīng)成為 SourceForge 的項(xiàng)目了,所以可以在 SourceForge.net 下載到。下載后解壓縮,你會(huì)看到 \Blitz++-0.7\blitz \Blitz++-0.7\random 兩個(gè)文件夾,這是 blitz 的源代碼所在處。 \Blitz++-0.7\manual 是文檔所在文件夾。 \Blitz++-0.7\benchmarks,\Blitz++-0.7\examples \Blitz++-0.7\testsuite 中都有很多好的使用實(shí)例可供參考。

            現(xiàn)在將 VC++ IDE Include 設(shè)置為 \Blitz++-0.7 ,因?yàn)?/span> blitz 源碼中都有這樣形式的 #include ,#include 。或者就干脆把兩個(gè)源碼文件夾整個(gè)得 copy include 文件夾內(nèi)。然后將 blitz 文件夾下的 config.h 改為其它名字,而將 config-VS.NET2003.h 的名字改為 config.h OK, 現(xiàn)在你就可以編譯所有的 testsuite benchmarks 了。

            1,? MTL 的配置相對(duì)來(lái)說(shuō)麻煩一點(diǎn),現(xiàn)在 http://www.osl.iu.edu/research/mtl/ 這里下載一個(gè) VC++7 的,不過(guò)還不能馬上用。由于 VC++7.1 對(duì)標(biāo)準(zhǔn)的支持更近了一步,同時(shí)對(duì)于某些語(yǔ)法細(xì)節(jié)的檢查更為嚴(yán)格 ( 主要是對(duì)于 typename template partial specialization ),我們要對(duì)代碼做一些小小地修改,特別是 mtl/mtl_config.h 這個(gè)文件。有一些地方要加入 typename 。另外有兩個(gè)模板偏特化的情況需要修改,加上 template <> 。在這里 http://newsuppy.go.nease.net/mtl.zip 我提供了一個(gè)修改完成的版本,不過(guò)我不保證我的修改可能引入的新的 bugs ,所以請(qǐng)謹(jǐn)慎使用。 MTL 的內(nèi)部使用一定數(shù)量的 STL 組件和算法。 MTL 的源代碼都在 mtl 文件夾內(nèi),由于 mtl 內(nèi)部的 include 都是 #include “…” 的形式,使用時(shí)把 mtl 文件夾復(fù)制到當(dāng)前 project 下就可以。如果要設(shè) VC++ Include 目錄,則應(yīng)該先把所有的 #include “…” 改為 #include? <…> 這樣的形式。

            不過(guò)剛開(kāi)始使用 MTL 還是有一些不太容易讓人接受的地方。比如 mtl::matrix 這個(gè)模板類(lèi)并不能夠產(chǎn)生實(shí)際的矩陣對(duì)象,而要通過(guò)它的 type 成員產(chǎn)生一個(gè)對(duì)應(yīng)模板參數(shù)的類(lèi)型,再通過(guò)這個(gè)類(lèi)型來(lái)實(shí)例化對(duì)象。

            比如 typedef mtl::matrix< SPAN>, rectangle<>, dense<>, row_major >::type Matrix; Matrix A;

            這里的 A 才是真正的矩陣對(duì)象,而 Matrix 則是一個(gè)元素為 float ,矩形,密集,行主 (C-style) 的矩陣類(lèi)。

            ?????? 下面我提供三個(gè)簡(jiǎn)單的入門(mén)例子解釋 MTL 的使用。分別有矩陣的加法,乘法,轉(zhuǎn)置,求逆以及一個(gè)線性方程組求解的例子。

            ?????? 另外 mtl test contrib 文件夾下也有很多不錯(cuò)的示例代碼可以查閱。

            MTL 使用示例 1 ,矩陣的加法,乘法和轉(zhuǎn)置。

            ?

            #include

            #include

            #include "mtl/mtl.h"???????????????????????????????????????????????????????????????????????????????????????

            #include

            using namespace std;

            using namespace mtl;

            ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

            template < class Matrix>

            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::OneD::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<float, rectangle<>, dense<>, row_major>::type Matrix;

            ?

            ? const Matrix::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<MAX_ROW;++i)

            ? {

            ?????? ? for (Matrix::size_type j=0; j<MAX_COL;++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::OneD::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 ,下面是一個(gè)線性方程組的解法

            #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<float, rectangle<>, dense<external>, row_major>::type Matrix;

            ? // dense : data copy from a float array,not generate them with 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]^T

            ? 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<float, rectangle<>, dense<>, row_major>::type LUMatrix;

            ? LUMatrix LU(A.nrows(), A.ncols());

            ? mtl::copy(A, LU);

            ?

            ? typedef dense1D<float> 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::iterator i=x.begin(); i!=x.end();++i)

            ?????? ? cout <<*i <<'\t';

            ?

            ? system("pause");

            ? return 0 ;

            }

            ?

            3 ,矩陣求逆

            #include

            #include

            #include

            #include "mtl/mtl.h"

            #include "mtl/lu.h"

            #include

            using namespace std;

            using namespace mtl;

            ?

            template < class Matrix>

            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::OneD::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<float, rectangle<>, dense<external>, row_major>::type Matrix;

            ? // dense : data copy from a float array,not generate them with 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<float, rectangle<>, dense<>, row_major>::type CMatrix;

            ? CMatrix LU(A.nrows(), A.ncols());

            ? mtl::copy(A, LU);

            ?

            ? typedef dense1D<float> Vector;

            ? Vector pvector(A.nrows());

            ? lu_factor(LU, pvector);

            ?

            ? CMatrix 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 ;

            }

            ?

            ?

            參考: 1 ,數(shù)值方法 (Matlab ) 3rd

            ?????????? John H.Mathews, Kurtis D.Fink 著, 陳渝,周璐,錢(qián)方 等譯

            ?????? 2 Matlab 6.5 的文檔 ??? The MathWorks, Inc.


            作者Blog: http://blog.csdn.net/newsuppy/

            posted on 2006-07-01 13:26 楊粼波 閱讀(3096) 評(píng)論(1)  編輯 收藏 引用

            評(píng)論

            # re: Blitz++與MTL兩大數(shù)值計(jì)算程序庫(kù)(C++)的簡(jiǎn)介 2008-08-20 16:20 littlewater

            不知道MTL71版本能不能更新一下,下載不能t.T  回復(fù)  更多評(píng)論   


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            久久精品这里只有精99品| 91久久精品国产成人久久| 亚洲AV无码久久| 久久97精品久久久久久久不卡| 国产成人久久777777| 久久受www免费人成_看片中文| 国产婷婷成人久久Av免费高清 | 青青青青久久精品国产| 久久精品这里只有精99品| 久久亚洲精品人成综合网| 国产ww久久久久久久久久| 蜜臀av性久久久久蜜臀aⅴ麻豆| 亚洲伊人久久大香线蕉苏妲己| 久久久久久精品久久久久| 久久精品男人影院| 色欲av伊人久久大香线蕉影院| 亚洲综合精品香蕉久久网97| 三上悠亚久久精品| 亚洲国产成人久久精品99| 国产L精品国产亚洲区久久| 久久国产精品成人影院| 久久99精品国产麻豆宅宅| 久久综合久久鬼色| 国产亚洲成人久久| 欧美一区二区精品久久| 久久久久久亚洲精品成人 | 国产亚洲精品自在久久| 狠狠综合久久综合88亚洲| 久久久午夜精品| 婷婷久久综合九色综合九七| 亚洲精品无码专区久久同性男| 91久久成人免费| 很黄很污的网站久久mimi色| 色综合久久中文综合网| 国产精品亚洲综合专区片高清久久久| 狠狠狠色丁香婷婷综合久久俺| 久久99国内精品自在现线| 99re这里只有精品热久久| 99久久精品国内| 久久93精品国产91久久综合| 欧美国产精品久久高清|