在lapacke的dgels函数中使用malloc [英] using malloc in dgels function of lapacke

查看:248
本文介绍了在lapacke的dgels函数中使用malloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用lapacke的dgels功能: 当我将其与malloc功能一起使用时.它没有给出正确的值. 有人可以告诉我使用malloc并创建矩阵时出现什么错误吗? 谢谢

i am trying to use dgels function of lapacke: when i use it with malloc fucntion. it doesnot give correct value. can anybody tell me please what is the mistake when i use malloc and create a matrix? thankyou

 /* Calling DGELS using row-major order */

#include <stdio.h>
#include <lapacke.h>
#include <conio.h>
#include <malloc.h>

int main ()
{
double a[3][2] = {{1,0},{1,1},{1,2}};

double **outputArray;
int designs=3;
int i,j,d,i_mal;
lapack_int info,m,n,lda,ldb,nrhs;
double outputArray[3][1] = {{6},{0},{0}};*/



outputArray = (double**) malloc(3* sizeof(double*)); 
for(i_mal=0;i_mal<3;i_mal++)
{
outputArray[i_mal] = (double*) malloc(1* sizeof(double));  
}
for (i=0;i<designs;i++)
{
printf("put first value");
scanf("%lf",&outputArray[i][0]);
}

m = 3;
n = 2;
nrhs = 1;
lda = 2;
ldb = 1;




info = LAPACKE_dgels(LAPACK_ROW_MAJOR,'N',m,n,nrhs,*a,lda,*outputArray,ldb);

for(i=0;i<m;i++)
{
for(j=0;j<nrhs;j++)
{
printf("%lf ",outputArray[i][j]);
}
printf("\n");
}
getch();

返回(信息); }

推荐答案

问题可能来自outputArray在内存中不连续.您可以改用这样的东西:

The problem may come from outputArray not being contiguous in memory. You may use something like this instead :

 outputArray = (double**) malloc(3* sizeof(double*)); 
 outputArray[0]=(double*) malloc(3* sizeof(double));
 for (i=0;i<designs;i++){
     outputArray[i]=&outputArray[0][i];
 }

别忘了释放内存!

 free(outputArray[0]);
 free(outputArray);

Edit:连续意味着您必须一次为所有值分配内存.参见 http://www.fftw.org/doc/Dynamic-Arrays-in-C_002dThe-Wrong-Way.html#Dynamic-Arrays-in-C_002dThe-Wrong-Way :某些软件包(例如fftw或lapack)需要此功能进行了优化.三次调用malloc时,您创建了三个部分,但出现了问题.

Edit : Contiguous means that you have to allocate the memory for all values at once. See http://www.fftw.org/doc/Dynamic-Arrays-in-C_002dThe-Wrong-Way.html#Dynamic-Arrays-in-C_002dThe-Wrong-Way : some packages, like fftw or lapack require this feature for optimization. As you were calling malloc three times, you created three parts and things went wrong.

如果只有一个右侧,则不需要2D数组(double**). outputArray[i]double*,即第i行(主行)的开始.如果您有很多RHS,则右行可能是outputArray[i]=&outputArray[0][i*nrhs];.

If you have a single right hand side, there is no need for a 2D array (double**). outputArray[i] is a double*, that is, the start of the i-th row ( row major). The right line may be outputArray[i]=&outputArray[0][i*nrhs]; if you have many RHS.

通过在代码中执行此操作,您将构建3行1列,即1个RHS.解决方案的大小为n=2.它应该是outputArray[0][0] , outputArray[1][0].希望我不太错,请在简单的情况下检查一下!

By doing this in your code, you are building a 3 rows, one column, that is one RHS. The solution, is of size n=2. It should be outputArray[0][0] , outputArray[1][0]. I hope i am not too wrong, check this on simple cases !

再见

这篇关于在lapacke的dgels函数中使用malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆