Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Mxarray elements sum problem

Subject: Mxarray elements sum problem

From: Hazem

Date: 27 Jul, 2014 02:03:10

Message: 1 of 4

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.
Thanks so much in advance.
Hazem

Subject: Mxarray elements sum problem

From: James Tursa

Date: 27 Jul, 2014 05:53:07

Message: 2 of 4

"Hazem " <nutlove22@hotmail.com> wrote in message <lr1mku$206$1@newscl01ah.mathworks.com>...
> 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.
> Thanks so much in advance.
> Hazem

Well, R is a double and mxSetPr is a function that attaches a (double *) to an mxArray. So passing R to mxSetPr simply isn't going to work, as you found out. I think what you are trying to do is save each iterative result into one of the elements of img_mat. So do this instead:

#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; // ADDED
    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) ;
    rptr = mxGetPr(img_mat); // ADDED
    iptr = mxGetPi(img_mat); // ADDED
// plhs[0] = mxCreateDoubleMatrix(1, 5, mxREAL); // DELETED

    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; // DELETED
// I_ptr=&I; // DELETED

// mxSetPr(img_mat, R); //--------------(1) // DELETED
// mxSetPi(img_mat, I);//----------------(2) // DELETED
        rptr[o] = R; // ADDED
        iptr[o] = I; // ADDED
    }
 
    plhs[0]=img_mat;
        
// mexPrintf(" % ",plhs[0]); // DELETED
   
}


mxMalloc is the MATLAB Memory Manager version of malloc. It simply allocated a block of memory of the size you request from the heap, and puts that address on the Garbage Collection list. If it doesn't get attached to a plhs[ ] mxArray or have mexMakeMemoryPersistent called on it, then it will automatically get free'd when the mex function returns control to the caller. The memory can be used for any type of data you want, not just double. To free this memory manually you would call mxFree on the address.

mxCreateDoubleMatrix creates an mxArray from the heap. This consists of the mxArray structure header itself (type of variable, size of variable, data pointers, etc), and separate data memory for the real and imaginary data. So it is NOT at all the same as mxMalloc ... it has a lot of extra stuff involved in the header part. The header address is on the Garbage Collection list, so if it is not one of the plhs[ ] variables and if you have not called mexMakeVariablePersistent on it, then it will be automatically free'd when the mex function returns control to the caller. To free this memory manually one would call mxDestroyArray on the address (frees the header stuff and the data memory also).

James Tursa

Subject: Mxarray elements sum problem

From: Hazem

Date: 27 Jul, 2014 14:26:11

Message: 3 of 4

Thank you very much James .. I really appreciate it ..

What I want to do is to sum all iterations in (img_mat, at the very end) to get at the end a scalar value (for real and imaginary part of course)
To make myself clear:
I need to sum all the values in (img_mat) into one value of real and imaginary part.

Your solution worked perfectly (apart from the above mentioned points .. it gives me the values of each iteration separately) ..

On a second note (completely different from above) I tried the following modification

mat_1 [o] = mul_j_p[o]*j_sphere_bessel[o];
mat_2 [o] = mul_j_p[o]*n_sphere_bessel[o];

mxSetPr(img_mat, mat_1);
mxSetPi(img_mat, mat_2);

it seems to work also but I still need the sum of all values in img_mat
The difference between my solution and yours is that you summed then put the result in img_mat and I put the values in img_mat and then (want to) sum ..
Do you think that there's gonna be a difference between the two?
Thanks a lot
Hazem

Subject: Mxarray elements sum problem

From: Hazem

Date: 28 Jul, 2014 18:50:11

Message: 4 of 4

up

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us