使用lapack库调用我的solve_chol()函数时,我的代码崩溃了

时间:2016-01-27 13:59:37

标签: c matlab

#include "mex.h"
#include <math.h>
#include <string.h>

extern int dpotrs(char *, int *, int *, double *, int *, double *, int *, int *);

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  double *C;
  int n, m, q;

  if (nrhs != 2 || nlhs > 1)                              /* check the         input */
    mexErrMsgTxt("Usage: X = solve_chol(R, B)");

  n = mxGetN(prhs[0]);

  if (n != mxGetM(prhs[0]))
    mexErrMsgTxt("Error: First argument matrix must be square");

  if (n != mxGetM(prhs[1]))
    mexErrMsgTxt("Error: First and second argument matrices must have same     number of rows");

  m = mxGetN(prhs[1]);
  plhs[0] = mxCreateDoubleMatrix(n, m, mxREAL); /* allocate space for output */
  C = mxGetPr(plhs[0]);

  if (n==0) return;              /* if argument was empty matrix, do no more */
    memcpy(C,mxGetPr(prhs[1]),n*m*sizeof(double));   /* copy argument matrix */
    dpotrs("U", &n, &m, mxGetPr(prhs[0]), &n, C, &n, &q);   /* solve system */

  if (q > 0)
    mexErrMsgTxt("Error: illegal input to solve_chol");
}

大家好,

我遇到了这个C代码的问题。它应该解决一个矩阵系统(Ax = B)A是对称的,正定的并且已经是上三角形?

我创建了这个片段来提升matlab的性能,但遗憾的是matlab崩溃,一些调试显示问题是在脚本末尾调用dpotrs()。 你能看出问题所在吗?

有关dpotrs()的信息,请访问:http://physics.oregonstate.edu/~rubin/nacphy/lapack/routines/dpotrs.html

如果能提供帮助会很棒

0 个答案:

没有答案