Chinaunix首页 | 论坛 | 博客
  • 博客访问: 605682
  • 博文数量: 796
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 5095
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-10 09:43
文章分类

全部博文(796)

文章存档

2011年(1)

2008年(795)

我的朋友

分类:

2008-09-10 09:45:18

#include <iostream>
#include 
"boost/multi_array.hpp"
using namespace std;

typedef boost::multi_array
<int2> matrix; 

matrix matrix_multiply(matrix
& a,matrix& b)
{
    matrix::index row
=a.shape()[0];
    matrix::index col
=b.shape()[1];
    matrix c(boost::extents[row][col]);

    
for (matrix::index i=0; i!=a.shape()[0]; ++i)
        
for (matrix::index j=0; j!=b.shape()[1]; ++j)
            
for (matrix::index k=0; k!=a.shape()[1]; ++k)
                c[i][j]
+=a[i][k]*b[k][j];

    
return c;
}


void print(const matrix& m)
{
    
for (matrix::index i=0; i!=m.shape()[0]; cout<<endl,++i)
        
for (matrix::index j=0; j!=m.shape()[1]; ++j)
                cout
<<m[i][j]<<" ";    
}


int main() {   

    
int values[] = {   
        
012,   
        
345    
    }
;   
    
const int values_size = 6;   
    matrix A(boost::extents[
2][3]);  
    matrix B(boost::extents[
3][2]); 
    A.assign(values,values 
+ values_size);
    B.assign(values,values 
+ values_size);

        cout
<<"matrix A"<<endl;
        print(A);   
    cout
<<endl;cout<<"*"<<endl;cout<<"matrix B"<<endl;
        print(B);   
    cout
<<endl;cout<<"="<<endl;cout<<"matrix C"<<endl;
    print(matrix_multiply(A,B));
    cout
<<endl;  

    
return 0;
}
 
--------------------next---------------------

阅读(527) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:rivest

给主人留下些什么吧!~~