Matlab to C++ using boost.ublas and boost.bindings

 博士阶段的研究应用了大量矩阵计算,现在要转到C++上来,一般的数值线性代数库都是采用Fortran编写的,用C++调用起来比较麻烦,boost.ublas和boost.bindings结合可以实现方便的调用。

boost.ublas是使用纯C++实现的BLAS的替代品,但不依赖于BLAS,接口友好,当然C++实现在效率上比BLAS就要差很远了。boost.bindings还没有正式纳入boost,现在在boost sandbox中,它提供了通过C++调用BLAS, LAPACK, ATLAS和UMFPACK的接口,通过Type Traits技术,这些接口可以接收boost.ublas的数据如ublas::matrix进行计算,使用起来十分方便。

boost.bindings源码下载地址,svn checkout
http://svn.boost.org/svn/boost/sandbox/boost/numeric/bindings/
boost.bindings文档和example
http://www.cs.colostate.edu/~nate/tutorial/bs-nb.tgz


GotoBlas编译
据说gotoblas是现在最快的BLAS,下载了最新的1.22版进行了编译,我的系统是windows,编译平台用的cygwin,备忘一点编译经验:
  1. 编译:解压后根据说明修改Makefile.rule中的STATIC_ALLOCATION = 1,make编译,编译时间长啊
  2. 生成dll:编译完成后进入exports目录,make dll
  3. 生成lib:根据def生成lib,打开vc9控制台,进入exports目录,lib /machine:i386 /def:文件名.def,即可生成vc可以链接的lib。
我这里的文件名是libgoto_core2-r1.22.lib和libgoto_core2-r1.22.dll
#pragma  comment(lib, "libgoto_core2-r1.22.lib")

#include 
< iostream >
#include 
< memory >
#include 
< boost / numeric / ublas / matrix.hpp >    
#include 
< boost / numeric / ublas / vector.hpp >   
#include 
< boost / numeric / ublas / io.hpp >

#include 
< boost / numeric / bindings / blas / blas.hpp >
#include 
< boost / numeric / bindings / traits / ublas_matrix.hpp >
#include 
< boost / numeric / bindings / traits / ublas_vector.hpp >
#include 
< boost / numeric / bindings / traits / std_vector.hpp >

namespace  ublas  =  boost::numeric::ublas;
namespace  blas  =  boost::numeric::bindings::blas;

int  main()  {
    std::size_t n 
= 3;
    ublas::matrix
<double> A (n, n);
    
// fill matrix A
    A(00= 3; A(01= 2; A(02= 1;
    A(
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值