CLAPACK for Windows [转]

下载和教程(光看这两个大概就会安装和是使用了)

  clapack : http://icl.cs.utk.edu/lapack-for-windows/clapack/index.html

  lapack : http://icl.cs.utk.edu/lapack-for-windows/lapack/#libraries_mingw

  (把下载的文件放在VS的合适目录下即可)

----

  

CLAPACK是LAPACK的C语言接口。LAPACK的全称是Linear Algebra PACKage,是非常著名的线性代数库。LAPACK是用Fortran写的,为了方便C/C++程序的使用,就有了LAPACK的C接口库CLAPACK。LAPACK的主页是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,表示函数正常退出,否则表示出错

 

以下是代码:

 1 // mytest.cpp : Defines the entry point for the console application.
 2 //
 3 
 4 #include "stdafx.h"
 5 
 6 #include <iostream>
 7 
 8 using namespace std;
 9 
10 #include <F2CLIBS/f2c.h>
11 
12 //因为程序是C++,而CLAPACK是C语言写的,所以在此处用extern关键字
13 extern "C"
14 {
15 #include <clapack.h> 
16 }
17 
18 
19 int _tmain(int argc, _TCHAR* argv[])
20 {
21 integer M=3 ;
22 integer N=1;
23 real a[9]={4,3,11,2,-1,0,-1,2,3};
24 real b[3]={2,10,8};
25 integer lda;
26 integer ldb;
27 integer INFO;
28 
29 lda=M;
30 ldb=M; 
31 integer *ipiv;
32 ipiv = (integer *)new integer[M];
33 
34 sgesv_(&M, &N, a, &lda,ipiv, b, &ldb, &INFO);
35 
36 if(INFO==0)
37 {
38    for(int i=0; i<M; i++)
39    {      
40     cout<<b[i]<<endl;
41    } 
42 }
43 else
44 {
45    cout<<"Failed."<<endl;
46 }  
47 
48 return 0;
49 }
50 
51  

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

 

[转]http://hi.baidu.com/xuyungui/blog/item/c43ef9026e8cff713912bbd3.html

原文地址:https://www.cnblogs.com/longdouhzt/p/2518549.html