weihuayi的个人博客分享 http://blog.sciencenet.cn/u/weihuayi

博文

boost::ublas中的稀疏矩阵

已有 7612 次阅读 2010-11-11 10:43 |个人分类: C++|系统分类:科研笔记| ublas, sparse

1, boost::ublas 中稀疏矩阵中随着元素插入过程,m.value_data()的变化。
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/storage.hpp>
namespace ublas = boost::numeric::ublas;
void show_array(const ublas::unbounded_array<double>& a)
{
for(size_t i=0; i<a.size(); ++i)
std::cout << a[i] << ' ';
std::cout << '
';
}
int main()
{
ublas::compressed_matrix<double> m (10, 10, 3 * 10);
m(0, 5) = 1; // underlying array is {1, 0, 0, 0, ...}
show_array(m.value_data());
m(0, 6) = 2; // underlying array is {1, 2, 0, 0, ...}
show_array(m.value_data());
m(0, 4) = 3; // underlying array is {3, 1, 2, 0, ...}
show_array(m.value_data());
m(0, 4) = 7; // underlying array is {7, 1, 2, 0, ...}
show_array(m.value_data());
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int main () {
{
compressed_matrix<double,row_major> m (4, 4, 2*2);
cout << sizeof(m) << "
"; // prints 56
cout << m << std::endl;
}

{
matrix<double> m (4, 4);
cout << sizeof(m) << "
"; // prints 20
cout << m << std::endl;
}

return 0;
}
2,sizeof 是编绎期运行的操作算子:
sizeof() is a compile time operator that only looks at
the direct size of the object and not any allocations that the object
might make at runtime. For example:
class SizeTest
{
public:

char* m_pData;

SizeTest()
{
m_pData = new char[1000];
}
};

int main (void)
{
SizeTest Test;
cout << sizeof(Test) << "
"; //Probably prints 4 depending on your system
return 0;
}
3,使用标准的STL transform 算法:

using namespace boost::numeric::ublas;

// Create a 30 element vector of doubles
vector
<double> vec(30);

// Assign 8.0 to each element.
std
::fill(vec.begin(), vec.end(), 8.0);

// Perform the "Gamma" function on each element and assign the result back
// to the original element in the vector.
std
::transform(vec.begin(), vec.end(), vec.begin(), boost::math::tgamma);



4, 提供了boost和lapack等数值代数软件的接口(Dowload):

下载后最新的tar.gz文件,解压到一个位置,解压后的文件里面有两个文件夹:

boost/: 头文件,把其中 numeric/bindings 文件夹 拷贝到/usr/include/boost/numeric/
(boost的通常安装目录)下, 就可以使用了

libs/: 一些测试的例子



https://blog.sciencenet.cn/blog-284809-382622.html

上一篇:Makefile.rule
下一篇:GCC 4.4 related build problems: missing #include
收藏 IP: .*| 热度|

0

发表评论 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-7-10 14:06

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部