当代明月分享 http://blog.sciencenet.cn/u/chenfanglin

博文

CLAPACK-Windows的安装与简单使用

已有 21789 次阅读 2007-12-26 21:22 |个人分类:Math

CLAPACK-Windows的安装与简单使用 

 

   

CLAPACKLAPACKC语言接口。LAPACK的全称是Linear Algebra PACKage,是非常著名的线性代数库。LAPACK是用Fortran写的,为了方便C/C++程序的使用,就有了LAPACKC接口库CLAPACKLAPACK的主页是http://www.netlib.org/lapack/CLAPACK的主页http://www.netlib.org/clapack/

 

CLAPACK有Linux和Window版。

 

安装CLAPACK-Windows首先从其主页上下载CLAPACK-Windows,解压,然后用VC(我用的是VS2005)打开clapack.dsw,编绎既可。

 

CLAPACK下的clapack.h是所需要的头文件,除此之外还需要的一个头文件是F2CLIBS/f2c.h

 

现在通过使用CLAPACK中的一个函数sgesv_解线性方程组来学习一下使用的方法。

 

包括此函数在内的所有函数可以在CLAPACK/SRC下找到源代码,并在代码中有函数参数的说明信息。sgesv_的代码文件就是sgesv.c

 

int sgesv_(integer *n, integer *nrhs, real *a, integer *lda, integer *ipiv, real *b, integer *ldb, integer *info)

 

sgesv_的功能是使用LU分解法解线性方程组AX=B,其中A是一个n*n的方阵。

 

integer *n, 方程的个数,也既A的行数和列数

integer *nrhs, B的列数

real *a, 存储矩阵A数据的一维数组,在fortran中,数组是列主序存储,在此a中的二维数据也必须是列主序

integer *lda, 等于n

integer *ipiv, 一个输出数据数组,数组大小是n,具体什么功能不太明白,但是似乎不影响最后结果,谁明白请告诉我

real *b,存储矩阵B数据的一维数组,在fortran中,数组是列主序存储,在此b中的二维数据也必须是列主序

integer *ldb, 等于n

integer *info,输出参数,如果返回此参数为0,表示函数正常退出,否则表示出错

以下是代码:

 

// mytest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>

using namespace std;

#include <F2CLIBS/f2c.h>

//因为程序是C++,而CLAPACK是C语言写的,所以在此处用extern关键字
extern "C"
{
#include <clapack.h>
}


int _tmain(int argc, _TCHAR* argv[])
{
 integer  M=3 ;
 integer  N=1;
 real a[9]={4,3,11,2,-1,0,-1,2,3};
 real b[3]={2,10,8};
 integer lda;
 integer ldb;
 integer INFO;

 lda=M;
 ldb=M; 
 integer *ipiv;
 ipiv = (integer *)new integer[M];

 sgesv_(&M, &N, a, &lda,ipiv, b, &ldb, &INFO);

 if(INFO==0)
 {
  for(int i=0; i<M; i++)
  {     
   cout<<b[i]<<endl;
  } 
 }
 else
 {
  cout<<"Failed."<<endl;
 }  

 return 0;
}

 

编译链接时要将clapack.lib加进链接库里。



https://blog.sciencenet.cn/blog-3199-13343.html

上一篇:数值运算包
下一篇:#pragma once与 #ifndef的区别
收藏 IP: .*| 热度|

0

发表评论 评论 (8 个评论)

数据加载中...

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

GMT+8, 2024-3-29 04:05

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部