我正在尝试使用复杂的mxArray元素的总和(我必须分别对实部和虚部进行处理)..这是我的代码
#include <boost/math/special_functions/bessel.hpp>
#include <boost/math/special_functions/legendre.hpp>
#include <iostream>
#include <cmath>
#include <complex>
#include "mex.h"
#include "math.h"
#include "matrix.h"
using namespace std;
void mexFunction (int nlhs,mxArray *plhs[],int nrhs, mxArray *prhs[])
{
mxArray *wave_num,*rho,*phi, *r,*data3,*img_mat,*out;
int k;
double *vr,*vi,*j_sphere_bessel, *n_sphere_bessel, *mul_j_p,*mat_1,*mat_2,*R_ptr,*I_ptr;
double rho1,phi1,r1,R,I;
double *rptr, *iptr;
wave_num = prhs[0];
k = (int)(mxGetScalar(wave_num));
vr = mxGetPr(prhs[1]);
vi = mxGetPi(prhs[1]);
r = prhs[2];
r1 = (double)(mxGetScalar(r));
rho = prhs[3];
rho1 = (double)(mxGetScalar(rho));
phi = prhs[4];
phi1 = (double)(mxGetScalar(phi));
j_sphere_bessel = (double *) mxMalloc(1*5 * sizeof(double));
n_sphere_bessel = (double *) mxMalloc(1*5 * sizeof(double));
mul_j_p = (double *) mxMalloc(1*5 * sizeof(double));
mat_1 = (double *) mxMalloc(1*5 * sizeof(double));
mat_2 = (double *) mxMalloc(1*5 * sizeof(double));
rptr = mxGetPr(img_mat); // ADDED
iptr = mxGetPi(img_mat); // ADDED
img_mat = mxCreateDoubleMatrix(1, 1, mxCOMPLEX);
plhs[0] = mxCreateDoubleMatrix(1, 5, mxREAL);
for (int o=0;o<5;o++)
{
j_sphere_bessel[o] = boost::math::sph_bessel(o, k*rho1);
n_sphere_bessel[o] = -boost::math::sph_neumann(o, k*rho1);
mul_j_p [o] = ((2*o)+1)*(boost::math::sph_bessel(o, k*r1))*(boost::math::legendre_p(o, cos(phi1*3.14159265/180)));
mat_1 [o] = mul_j_p[o]*j_sphere_bessel[o];
mat_2 [o] = mul_j_p[o]*n_sphere_bessel[o];
R = R + mat_1[o];
I = I + mat_2[o];
rptr[o] = R; // ADDED
iptr[o] = I; // ADDED // Here I want R to be the real part of the already created matrix img_mat (also I as imaginary part)
}
plhs[0]=img_mat;
mexPrintf(" % ",plhs[0]);
}
现在将mat_1和mat_2的所有元素分别归入(R和I)后,我试图将(R)设置为已经创建的矩阵img_mat的真实部分,并且(I)作为虚部,但似乎没有工作。
有人能帮帮我吗?
提前谢谢。