青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

牽著老婆滿街逛

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

Matrix libraries for C and C++

form:http://www-128.ibm.com/developerworks/linux/library/l-matrix.html

Matrix libraries for C and C++

An evaluation and comparison of Meschach, Cooperware matrix, and Blitz

developerWorks
Document options
Set printer orientation to landscape mode

Print this page

Email this page

E-mail this page


Congratulations!

developerWorks wins Jolt Hall of Fame award


Rate this page

Help us improve this content


Level: Introductory

Andrew Blais (onlymice@attbi.com), Researcher and writer

01 Jul 2002

This article presents some of the currently available options for open source C/C++ matrix libraries employable within a Linux environment. Particular libraries discussed are Meschach, which provides routines for operating on matrices and vectors for projects coded in C, the Cooperware Matrix (CwMtx) for C++ coding, and Blitz, which provides an n-dimensional array class for C++ with integral, floating, complex, and well-behaved, user-defined types. Andrew Blais, who has contributed several articles to developerWorks, is a researcher and writer for Gnosis, Inc., and does work in neural nets.

The following article assumes some familiarity with C/C++ and a preoccupation with the fact that C/C++ per se lacks matrix functionality. You may be analyzing econometric data or modeling rain forests. As for me, I work with neural nets, the implementation of which is greatly simplified by a matrix or two. Although C/C++ includes containers that can be considered matrices (such as arrays and, in the Standard Library, vectors, lists, and maps), a container that actually is a matrix would make the tasks at hand far easier. So we're going to look at three open source options that don't require you to build your matrices from scratch, but do allow you to tweak your matrix library. This is especially good if you anticipate needing to tweak it in as yet unforeseen ways.

Meschach: a C option

For projects coded in C, Meschach (pronounced: me-shark) provides routines for operating on matrices and vectors. It has the virtue of compiling under Linux and most other operating systems, and is openly available under copyright, provided the customary acknowledgment is observed and errors are reported. Meschach was designed to solve systems of dense or sparse linear equations, compute eigenvalues and eigenvectors, and solve least squares problems, among other things. It provides nearly 400 functions for doubles and complex numbers. It also comes with a tutorial that introduces these functions in the context of illustrative mini case studies. David Stewart and Zbigniew Leyk introduce Meschach through a discussion of topics such as a generalized least square equation solver for over-determined equations, and problems involving sparse matrices. Their tutorial also includes the slightly more advanced topics of 3-dimensional matrices and error reporting.

Objects and class functions are often associated with code, and C structures may seem somewhat arcane, so C libraries are often dismissed as viable solutions. But in its defense, this library is very well organized and should not be dismissed by default. A quarter hour after downloading Meschach, I was making, filling, and displaying matrices (the moral equivalent of creating a Hello World! program). An affordable paper manual, "Meschach: Matrix Computations in C," is available for reference (see Resources later in this article). In particular, the test program "torture" contains a number of helpful clues.

Matrices can easily be sent to files or to the standard output. Meschach computes Fast Fourier Transforms, extracts columns and rows, and computes eigenvalues of symmetric matrices. You can fill a matrix with random ints and complexes. The library, believe it or not, even has facilities for adding matrices. One of the Meschach features that simplifies writing the obvious stepping stone programs is a function that returns a random double in [0,1). Although Meschach has a function that fills a matrix with 1.0s, it unfortunately lacks a function for filling a matrix with an arbitrary double or with random doubles. But these are easy to add.


Listing 1. Filling a matrix with an arbitrary double or with random doubles

                        MAT *m_fill( MAT *A, double x)
                        /* MAT *m_random_fill( MAT *A ) */
                        {
                        int i, j;
                        for ( i = 0; i < A->m; i++ ) for ( j = 0; j < A->n; j++ )
                        { A->me[i][j] = x ; }
                        /* { A->me[i][j] = m_random ; } */
                        return A;
                        }
                        

So, as we've seen, Meschach code is easily extensible, although in an ideal world more thoroughly commented code is preferable. But if you happen to be doing computational work in C that needs matrices, this is a highly useful library.



Back to top


Cooperware matrix: the basics

If you want to write object-oriented code in C++ and you prefer conceptual clarity to speed, Harry Kuiper's Cooperware Matrix (CwMtx) will work very well. Of the three matrix libraries discussed here, I found its conceptual architecture to be the most intuitive. In constructing a matrix, you use the obvious:


Listing 2. Constructing a matrix

                        CWMatrix A( rows, columns ) ;
                        

Of the three libraries considered here, CwMtx had the worst performance in terms of three evaluative tasks, which we'll look at in detail in the section on Speed. But when clarity is more important than performance (when, for example you want to be sure that your data is being crunched properly), CwMtx is a great option. Make it right first, then make it fast.

The matrices in CwMtx include vectors and square matrices, where vectors include space vectors and quaternions. A matrix can be mapped into a matrix, filled with some element, transposed, and subjected to the usual mathematical operations. Kuiper originally used CwMtx to simulate systems built from discrete and interactive state machines. In addition to the obligatory matrix class, a quaternion class also exists. The answer to the obvious question is that q is a quaternion just when q = r + xi + yi + zi, where r is a real number, i is the square root of -1, and x, y, and z are complex numbers. Quaternions make it possible to extend the concept of rotation in three dimensions to four dimensions (see Resources for links to quaternion references).

CwMtx has no built-in random number generator, and there is no class function that fills a matrix with random elements. But because it's free and released under the GNU LGPL license, you have the freedom to create these if you'd like. As far as filling a matrix with random elements, the following option is a good and easy way to go.


Listing 3. Filling a matrix with random elements

                        #include <stdlib.h>
                        ...
                        void random_fill( CWMatrix &M )
                        {
                        int SIZE = M.GetRows() ;
                        for ( int r=0; r<SIZE; r++ ) for ( int c=0; c<SIZE; c++ ) { M[r][c]= drand48(); }
                        }
                        

The documentation here is slight, but it is clear and well organized. It is easy to find such things as the class hierarchy and the constructor and member function options. Although there is no tutorial, you won't miss it; between the documentation and the test program, it won't be needed.



Back to top


Blitz: as fast as Fortran?

Blitz is another C++ library distributed under the GNU GPL, and with it you can freely create objects. It supports the KAI, Intel, gcc, Metroworks, and Cray 3.0.0.0 C++ compilers and provides an n-dimensional array class that can contain integral, floating, complex, and well-behaved user-defined types. Its constructor is more complex that the CwMtx constructor, as is evident in this example:


Listing 4. The Blitz constructor

                        Array<double,2> A(4,7) ;
                        

This creates a 4x7 rank 2 array that contains doubles. But because it's a bit unclear, Blitz makes you think of an array as a matrix. Also, it does not implement many matrix functions. For example, no function returns the eigenvalues of a matrix. Also, no function fills an array or matrix with random doubles. But, Blitz does have two basic virtues.

One virtue lies in its breadth. By employing its native capabilities, it is easy to implement and construct a random double filling function, as you can see from this example:


Listing 5. A random double filling function in Blitz

                        template <class Array, class Uniform>
                        void fill( Array &a, int size, Uniform &u )
                        {
                        for ( int i=0; i<size; i++ )
                        for ( int j=0; j<size; j++ )
                        { a(i,j) = u.random() ; }
                        }
                        

Blitz's Uniform class provides a member function that returns a double in [0,1). It also provides three methods for accessing array elements: standard indexing, creating subarrays, and slicing, which produces a less dimensional view of an array segment. Blitz also possesses the standard calculator type functions, so arrays can be displayed on the standard output, and can be read from and sent to files. Laplacian, gradient, and Jacobian operators are just three examples of Blitz's stencil functions.

The other virtue lies in Blitz's speed. Depending on which compiler is employed, C++ can deliver performances at or about that of Fortran, which is renowned for its high performance in scientific and engineering computing. Take a look at the comparisons in the table below, but see the section on Speed below for an analysis of this data and the performance claims based upon it.

Table 1. Blitz performance on different platforms
Platform Compiler Out-of-cache In-cache
HPC-160 KAI C++ 100.2% 97.5%
Pentium II egcs 98.4% 79.6%
Cray T3E KAI C++ 95.7% 98.1%
Origin 2000 KAI C++ 88.1% 79.8%

Blitz comes with a manual in HTML and Postscript, but unfortunately, there is no tutorial. There is, however, quite a bit of illustrative code from which the nuances of Blitz syntax can be gleaned. And the class reference provides the usual information. There are also several helpful mailing lists that are archived and searchable (see Resources).



Back to top


Speed

Libraries can be evaluated in terms of their functional resources, documentation, the quality of their tutorials, how easily they can be extended, and so on. They can also be evaluated in terms of performance and/or speed. But comparing them is sometimes difficult, because (as is the case with our examples) they aren't all written in the same language, and they don't have much native functionality in common. In our case, there is sufficient overlap among the libraries to permit comparison in terms of speed based on three relatively simple yet revealing tasks, which are displayed and discussed in the following three examples:


Listing 6. Task 1

                        For ( d=2; d<7; d++)
                        Construct 3 dxd matrices: A, B, C
                        Start Clock: Do the following one million times:
                        Fill A and B with 1.0s
                        Let C = A + B
                        Stop clock: Report elapsed time in seconds.
                        

Implementing and executing this algorithm using our libraries produced the following results:

Table 2. Results of task 1
Library 2x2 3x3 4x5 5x5 6x6
Blitz 0.40 0.48 0.62 0.75 0.91
CwMtx 2.64 3.57 4.58 5.60 6.60
Meschach 0.17 0.27 0.45 0.60 0.79

Given its intuitive architecture, it is unfortunate that CwMtx doesn't perform better here. And Although Blitz is outperformed by Meschach, it is remarkable that it so vastly out performs CwMtx, its object oriented competitor.

Recall that both Meschach and Blitz have functions (Random Number Generators) that deliver random doubles, and that CwMtx lacks native random generating capacity. Given the pivotal role of randomization in some matrix-based simulations, it is instructive to investigate how these libraries perform in situations that call on randomization.


Listing 7. Task 2

                        For ( d=2; d<7; d++)
                        Construct 3 dxd matrices: A, B, C
                        Start Clock: Do the following one million times:
                        Fill A and B with random doubles (using library RNG, if any)
                        Let C = A + B
                        Stop clock: Report elapsed time in seconds.
                        

Our libraries performed as follows:

Table 3. Results of task 2
Library 2x2 3x3 4x5 5x5 6x6
Blitz 0.87 1.71 2.83 4.34 6.13
CwMtx 3.67 5.87 8.59 11.93 15.61
Meschach 0.42 0.80 1.32 1.86 2.52

Once again, Blitz is outperformed by Meschach, but outperforms its object oriented competitor CwMtx by an astounding margin. Lest you think that this is due to the performance of RNGs, let's take a look at this third evaluative task:


Listing 8. Task 3

                        For ( d=2; d<7; d++)
                        Construct 3 dxd matrices: A, B, C
                        Start Clock: Do the following one million times:
                        Fill A and B with random doubles (using shared RNG)
                        Let C = A + B
                        Stop clock: Report elapsed time in seconds.
                        

Our libraries retain the performance rankings of the previous two tasks:

Table 4. Results of task 3
Library 2x2 3x3 4x5 5x5 6x6
Blitz 1.31 2.62 4.50 6.85 9.71
CwMtx 3.67 5.87 8.59 11.93 15.61
Meschach 1.17 2.45 4.28 6.63 9.40

As you would expect, CwMtx doesn't change its rank in performance capability. Moreover, Blitz and Meschach retain their relative rankings. If raw speed is the deciding factor, the order of rank among these libraries is now clear.



Back to top


Installing and compiling on Red Hat Linux 7.1

For convenience, I include the following notes on installing and compiling with these three libraries. Links to the downloads can be found in Resources.


Listing 9. Blitz

                        tar zxf blitz-0.5beta3.tar.gz
                        cd blitz-0.5beta3
                        ./configure --with-cxx=gcc
                        make all
                        cp -a blitz/ /usr/local/include/ ( or whatever you wish )
                        cp -a lib/ /usr/local/include/blitz/
                        Compile with: g++ -O2 pgm.cpp -o pgm
                        


Listing 10. Meschach

                        unzip -q mesch12b.zip -d mesch12b
                        cd mesch12b
                        ./configure
                        make basic
                        mkdir /usr/local/include/meschach ( or whatever you wish )
                        cp *.h meschach.a /usr/local/include/meschach/
                        Compile with: gcc -O2 pgm.c /usr/local/include/meschach/meschach.a -o pgm
                        


Listing 11. CwMtx

                        tar zxf cwmtx-0.3.0.tar.gz
                        cd cwmtx-0.3.0
                        Open Makefile, and set INSTALL_DIR to /usr/local/include/cwmtx ( or whatever you wish )
                        Execute: make install
                        Compile with: g++ -O2 pgm.cpp -o pgm
                        



Back to top


Summary

We've now looked at the features of three matrix libraries, and detailed their native capacities. We've also seen a number of functional shortcomings among them, and have looked at ways that these might be overcome. I have provided you with some simple tests that provide some crude quantitative data that might aid you in your coding choices, but in the end the decision is yours and should depend on the individual aspects of your project as well as the speed of the libraries in the given circumstances.



Back to top


Resources



Back to top


About the author

Andrew L. Blais divides his time between home schooling his son, A. Van Blais, being a husband to K.J. Krawczyk-Blais, researching and writing for Gnosis Software, Inc., and teaching philosophy and religion for Anna Maria College. On occasion, he breathes. You can contact Andrew at onlymice@attbi.com.

posted on 2007-04-05 12:22 楊粼波 閱讀(1645) 評論(0)  編輯 收藏 引用


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


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            欧美一区二粉嫩精品国产一线天| 亚洲欧美另类综合偷拍| 久久综合给合久久狠狠色| 国外成人在线| 毛片av中文字幕一区二区| 久久久www免费人成黑人精品| 国模私拍视频一区| 欧美多人爱爱视频网站| 欧美激情视频在线免费观看 欧美视频免费一 | 欧美日本在线观看| 亚洲在线观看免费| 午夜一区二区三区在线观看| 影音欧美亚洲| 亚洲伦理在线观看| 国产美女精品| 亚洲第一主播视频| 欧美婷婷六月丁香综合色| 午夜精品视频在线观看| 久久日韩粉嫩一区二区三区| 亚洲麻豆国产自偷在线| 亚洲视频专区在线| 亚洲国产精品视频| 亚洲特级毛片| 在线看片成人| 中文在线不卡视频| 亚洲大胆女人| 亚洲一区二区三区影院| 亚洲国产婷婷| 亚洲字幕一区二区| 日韩午夜免费视频| 久久福利视频导航| 亚洲资源av| 牛牛影视久久网| 久久久99国产精品免费| 欧美日韩xxxxx| 久久免费视频一区| 欧美性生交xxxxx久久久| 欧美11—12娇小xxxx| 国产精品成人免费精品自在线观看 | 国产精品一区亚洲| 亚洲国产视频a| 在线播放国产一区中文字幕剧情欧美 | 一本一本久久a久久精品牛牛影视| 亚洲欧美精品在线观看| 一本色道久久加勒比88综合| 久久久亚洲精品一区二区三区| 亚洲一区国产一区| 欧美电影在线| 欧美不卡一区| 国内精品伊人久久久久av一坑| 日韩小视频在线观看| 最新中文字幕亚洲| 欧美一级播放| 欧美在线观看一区二区| 欧美偷拍一区二区| 亚洲精品一区二区三区樱花| 91久久久久久| 免费日本视频一区| 欧美本精品男人aⅴ天堂| 国产亚洲精品久久飘花| 亚洲一区在线观看免费观看电影高清| 99精品久久久| 欧美美女日韩| 亚洲免费av电影| 一区二区三区四区五区视频 | 欧美日韩亚洲另类| 亚洲国产精品精华液网站| 激情六月婷婷久久| 久久精品综合| 欧美成人午夜免费视在线看片 | 国内欧美视频一区二区| 午夜亚洲性色福利视频| 亚洲欧美一区二区三区久久| 国产精品久久久一区二区三区| av成人老司机| 亚洲欧美日韩精品久久奇米色影视 | 欧美人与禽性xxxxx杂性| 亚洲人成在线观看| 亚洲私拍自拍| 国产午夜精品在线观看| 欧美一区永久视频免费观看| 久久精品国产综合| 亚洲电影免费观看高清| 欧美电影在线免费观看网站| 亚洲激情综合| 亚洲女人av| 国内欧美视频一区二区| 蜜臀av一级做a爰片久久| 亚洲精品字幕| 久久精品亚洲乱码伦伦中文| 在线播放豆国产99亚洲| 欧美日韩免费| 久久www成人_看片免费不卡| 欧美激情精品久久久久久久变态| 在线亚洲免费视频| 国产视频亚洲精品| 欧美精品播放| 久久国产欧美精品| 亚洲人妖在线| 久久久久亚洲综合| 9人人澡人人爽人人精品| 国产一区二区三区直播精品电影 | 久久裸体艺术| 一二美女精品欧洲| 美国成人毛片| 亚洲一区二区高清视频| 影音先锋另类| 国产老肥熟一区二区三区| 久久综合九色综合欧美狠狠| 一区二区不卡在线视频 午夜欧美不卡在| 久久国产精品色婷婷| 亚洲久久一区二区| 国内精品久久久久久久影视蜜臀| 欧美日本簧片| 免费成人你懂的| 午夜精品影院| 在线综合亚洲欧美在线视频| 亚洲第一黄色网| 久久久久国产一区二区| 亚洲一本大道在线| 99国产精品久久久久久久| 狠狠狠色丁香婷婷综合久久五月 | 欧美三级电影一区| 欧美激情视频在线免费观看 欧美视频免费一 | 欧美福利视频在线| 久久亚洲综合色| 欧美一区二区在线免费播放| 一区二区三区高清在线| 亚洲国产精品第一区二区三区| 国产亚洲欧美一区二区| 国产精品卡一卡二卡三| 欧美日韩精品伦理作品在线免费观看 | 亚洲国产日韩一级| 免费中文字幕日韩欧美| 久久久国产精品一区二区中文| 亚洲主播在线观看| 中文无字幕一区二区三区| 在线观看欧美日韩| 在线成人激情视频| 悠悠资源网亚洲青| 激情成人中文字幕| 一区二区三区在线免费播放| 国产一区二区日韩| 国内精品模特av私拍在线观看| 国产精品一香蕉国产线看观看| 国产精品美女久久久久久久| 国产精品久久久久久久一区探花| 国产精品国产三级国产a| 国产精品va在线| 国产毛片精品国产一区二区三区| 国产精品久久久久久久久久三级| 国产精品欧美经典| 国产伦精品一区二区三区免费| 国产酒店精品激情| 狠色狠色综合久久| 亚洲电影专区| 99精品视频免费观看视频| 中文欧美字幕免费| 欧美一区免费视频| 嫩草国产精品入口| 亚洲成人在线视频播放 | 亚洲国产高清aⅴ视频| 亚洲激情专区| 亚洲私人影院| 久久xxxx精品视频| 欧美成人a视频| 国产精品第2页| 国产一区二区三区免费在线观看| 在线观看视频一区二区欧美日韩| 亚洲国产日韩一区| 亚洲一区二区黄| 久久女同精品一区二区| 亚洲国产一区二区a毛片| 亚洲色诱最新| 久久综合伊人77777| 欧美日韩精品免费看| 国产日韩欧美另类| 最新日韩在线| 欧美一区二区三区日韩视频| 六月天综合网| 亚洲手机视频| 免费在线日韩av| 国产欧美一区在线| 亚洲精品资源| 久久久水蜜桃av免费网站| 亚洲国产你懂的| 亚洲自拍高清| 欧美黄网免费在线观看| 国产农村妇女精品| aa级大片欧美三级| 美女精品在线观看| 亚洲一区二区三区高清不卡| 亚洲午夜精品久久久久久浪潮 | 美女国产一区| 亚洲女女女同性video| 欧美黄色成人网| 伊人久久大香线蕉av超碰演员| 亚洲一区二区动漫| 91久久久久久| 久久亚洲综合色|