Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Mxarray elements sum problem
Replies: 3   Last Post: Jul 28, 2014 2:50 PM

 Messages: [ Previous | Next ]
 Hazem Posts: 4 Registered: 7/26/14
Mxarray elements sum problem
Posted: Jul 26, 2014 10:03 PM

Hello,
I am trying to implement a certain formula in Mex file but I have a problem.
The idea of the code is to compute a sum of several spherical Bessel function computations of several orders for the same value.
The code also contains some complex terms.

#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;
R=0;
I=0;
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));
R_ptr = (double *) mxMalloc(1*1 * sizeof(double));
I_ptr = (double *) mxMalloc(1*1 * sizeof(double));

img_mat = mxCreateDoubleMatrix(1, 5, 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];

R_ptr=&R;
I_ptr=&I;

mxSetPr(img_mat, R); //--------------(1)
mxSetPi(img_mat, I);//----------------(2)
}

plhs[0]=img_mat;

mexPrintf(" % ",plhs[0]);

}

What I am trying to do here is to claculate the sum of all elements of mat_1 and put it into R (same as to mat_2: put it into I) and then set R to be the real part of the final matrix img_mat and also set I to be the imaginary part of it. However I keep getting the error messages:
error C2664: 'mxSetPr' : cannot convert parameter 2 from 'double' to 'double *'
error C2664: 'mxSetPi' : cannot convert parameter 2 from 'double' to 'double *'

Which indicate to lines (1 and 2) at the end of the code.
I believe that I am confusing the usage of mxArray with the usage of double array so can somebody please help me with this code? and Also some explanation on the difference between (mxCreateDoubleMatrix) and (mxMalloc) would be great.