Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从C++;通过mex连接到Matlab(vpa?)?_Matlab_Arguments_Mex_Argument Passing_Gmp - Fatal编程技术网

如何从C++;通过mex连接到Matlab(vpa?)?

如何从C++;通过mex连接到Matlab(vpa?)?,matlab,arguments,mex,argument-passing,gmp,Matlab,Arguments,Mex,Argument Passing,Gmp,我是mex和Matlab的新手。我刚刚学习了如何将实变量和矩阵变量从mex函数传递到Matlab 安装GMP(其替代MPIR和MPFR库)后,现在的问题是:当我通过C++和GMP构建MEX函数时,如何传递一个“多精度”变量输出参数作为MATLAB的原生函数? 有没有简单的工作示例 下面是mex函数代码示例,我在其中添加了多个与精度相关的头。 如何将“多重精度”pi设置为第三个输出参数?还是将其作为字符串打印 为了正确构建,必须安装MPIR、MPFR和C++包装(MPPRA.H)。 #includ

我是mex和Matlab的新手。我刚刚学习了如何将实变量和矩阵变量从mex函数传递到Matlab

安装GMP(其替代MPIR和MPFR库)后,现在的问题是:当我通过C++和GMP构建MEX函数时,如何传递一个“多精度”变量输出参数作为MATLAB的原生函数? 有没有简单的工作示例

下面是mex函数代码示例,我在其中添加了多个与精度相关的头。 如何将“多重精度”pi设置为第三个输出参数?还是将其作为字符串打印

为了正确构建,必须安装MPIR、MPFR和C++包装(MPPRA.H)。
#include <iostream>
#include <math.h>
#include "mex.h"

// The multiple precision libraries
// #include "mpir.h"
#include "mpreal.h"


using namespace mpfr;
using namespace std;

extern void _main();

/****************************/
class MyData {

public:
  void display();
  void set_data(double v1, double v2);
  MyData(double v1 = 0, double v2 = 0);
  ~MyData() { }
private:
  double val1, val2;
};

MyData::MyData(double v1, double v2)
{
  val1 = v1;
  val2 = v2;
}

void MyData::display()
{
#ifdef _WIN32
    mexPrintf("Value1 = %g\n", val1);
    mexPrintf("Value2 = %g\n\n", val2);
#else
  cout << "Value1 = " << val1 << "\n";
  cout << "Value2 = " << val2 << "\n\n";
#endif
}

void MyData::set_data(double v1, double v2) { val1 = v1; val2 = v2; }

/*********************/

static
void mexcpp(
        double num1,
        double num2
        )
{
#ifdef _WIN32
    mexPrintf("\nThe initialized data in object:\n");
#else
  cout << "\nThe initialized data in object:\n";
#endif
  MyData *d = new MyData; // Create a  MyData object
  d->display();           // It should be initialized to
                          // zeros
  d->set_data(num1,num2); // Set data members to incoming
                          // values
#ifdef _WIN32
  mexPrintf("After setting the object's data to your input:\n");
#else
  cout << "After setting the object's data to your input:\n";
#endif
  d->display();           // Make sure the set_data() worked
  delete(d);
  flush(cout);
  return;
}

void mexFunction(
         int          nlhs,
         mxArray      *[],
         int          nrhs,
         const mxArray *prhs[]
         )
{
  double      *vin1, *vin2;

  /* Check for proper number of arguments */

  if (nrhs != 2) {
    mexErrMsgIdAndTxt("MATLAB:mexcpp:nargin", 
            "MEXCPP requires two input arguments.");
  } else if (nlhs >= 1) {
    mexErrMsgIdAndTxt("MATLAB:mexcpp:nargout",
            "MEXCPP requires no output argument.");
  }

  vin1 = (double *) mxGetPr(prhs[0]);
  vin2 = (double *) mxGetPr(prhs[1]);


  ////////////// Here are the multiple precision settings
  const int digits = 64;
  mpreal::set_default_prec(mpfr::digits2bits(digits));
  const mpreal pi          =    mpfr::const_pi();
  ////////////// How can I set the multiple precision pi as a third output argument?
  ///// say vpa? or something else?
  mexPrintf(" The multiple precision pi is %g \n",pi);

  mexcpp(*vin1, *vin2);
  return;
}
#包括
#包括
#包括“mex.h”
//多精度库
//#包括“mpir.h”
#包括“mpreal.h”
使用名称空间mpfr;
使用名称空间std;
外部无效(主);
/****************************/
类MyData{
公众:
void display();
无效集_数据(双v1,双v2);
MyData(双v1=0,双v2=0);
~MyData(){}
私人:
双val1,val2;
};
MyData::MyData(双v1,双v2)
{
val1=v1;
val2=v2;
}
void MyData::display()
{
#ifdef_WIN32
mexPrintf(“值1=%g\n”,值1);
mexPrintf(“Value2=%g\n\n”,val2);
#否则

我现在认为答案应该很简单。 例如,如果我想将获得的向量xmp传递给Matlab

VectorXmp x(n);
使用mxCreateCellMatrix创建相同的维度向量,将x的所有组件转换为字符,然后使用以下代码将其放入输出单元格:

plhs[0]=mxCreateCellMatrix(n, 1);
 for (int i = 0; i < n; i++) {
     mxSetCell(plhs[0],i,mxCreateString(_strdup(x(i).toString().c_str())));
 }
将输出转换为符号矩阵(矢量),以及


将其转换为具有精度损失的双矩阵(矢量)。

您能否在您使用的代码中显示GMP变量(
mpz\u t
mpf\u t
)的示例?我怀疑您需要转换为MATLAB支持的数据类型,以便将数据复制到
mxArray
。我通过以下方法解决了这个问题:(1)首先将多精度数值变量转换为字符串,通过使用帕维尔的C++包装器,它非常容易(V.ToString()),因为MpAlAl是一个类;(2)然后将字符串(CHAR *)变量传递给Matlab;(3)MATLAB将使用“VPA”将多精度(CHAR*)转换成符号变量;然而,因为MPIR和MPFR。我使用的库是其他人预编译的库,我只有“dll”版本;因此获得的mex函数必须与两个“mpir.dll,mpfr.dll”一起使用;这可能会影响Matlab自己的符号工具箱,它也基于另一个mpfr.dll。我必须使用“clear mex”每次调用处理冲突之前和之后,实际上我使用帕维尔的C++包装器,这在处理“MPZYT或MPFYT”时大大简化了编程工作。因此,我个人更倾向于使用“MPRAL”;看一下这里,您可能会理解为什么我不使用“MPZYT MPFYT”。。请随时发布答案,以便其他人可以获得您的发现。
 vpa(x) 
 double(vpa(x))