MATLAB Newsgroup

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

"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

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

up

You can think of your watch list as threads that you have bookmarked.

You can add tags, authors, threads, and even search results to your watch list. This way you can easily keep track of topics that you're interested in. To view your watch list, click on the "My Newsreader" link.

To add items to your watch list, click the "add to watch list" link at the bottom of any page.

To add search criteria to your watch list, search for the desired term in the search box. Click on the "Add this search to my watch list" link on the search results page.

You can also add a tag to your watch list by searching for the tag with the directive "tag:tag_name" where tag_name is the name of the tag you would like to watch.

To add an author to your watch list, go to the author's profile page and click on the "Add this author to my watch list" link at the top of the page. You can also add an author to your watch list by going to a thread that the author has posted to and clicking on the "Add this author to my watch list" link. You will be notified whenever the author makes a post.

To add a thread to your watch list, go to the thread page and click the "Add this thread to my watch list" link at the top of the page.

*No tags are associated with this thread.*

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.

Got questions?

Get answers.

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

Learn moreDiscover what MATLAB ^{®} can do for your career.

Opportunities for recent engineering grads.

Apply TodayThe newsgroups are a worldwide forum that is open to everyone. Newsgroups are used to discuss a huge range of topics, make announcements, and trade files.

Discussions are threaded, or grouped in a way that allows you to read a posted message and all of its replies in chronological order. This makes it easy to follow the thread of the conversation, and to see what’s already been said before you post your own reply or make a new posting.

Newsgroup content is distributed by servers hosted by various organizations on the Internet. Messages are exchanged and managed using open-standard protocols. No single entity “owns” the newsgroups.

There are thousands of newsgroups, each addressing a single topic or area of interest. The MATLAB Central Newsreader posts and displays messages in the comp.soft-sys.matlab newsgroup.

**MATLAB Central**

You can use the integrated newsreader at the MATLAB Central website to read and post messages in this newsgroup. MATLAB Central is hosted by MathWorks.

Messages posted through the MATLAB Central Newsreader are seen by everyone using the newsgroups, regardless of how they access the newsgroups. There are several advantages to using MATLAB Central.

**One Account**

Your MATLAB Central account is tied to your MathWorks Account for easy access.

**Use the Email Address of Your Choice**

The MATLAB Central Newsreader allows you to define an alternative email address as your posting address, avoiding clutter in your primary mailbox and reducing spam.

**Spam Control**

Most newsgroup spam is filtered out by the MATLAB Central Newsreader.

**Tagging**

Messages can be tagged with a relevant label by any signed-in user. Tags can be used as keywords to find particular files of interest, or as a way to categorize your bookmarked postings. You may choose to allow others to view your tags, and you can view or search others’ tags as well as those of the community at large. Tagging provides a way to see both the big trends and the smaller, more obscure ideas and applications.

**Watch lists**

Setting up watch lists allows you to be notified of updates made to postings selected by author, thread, or any search variable. Your watch list notifications can be sent by email (daily digest or immediate), displayed in My Newsreader, or sent via RSS feed.

- Use a newsreader through your school, employer, or internet service provider
- Pay for newsgroup access from a commercial provider
- Use Google Groups
- Mathforum.org provides a newsreader with access to the comp.soft sys.matlab newsgroup
- Run your own server. For typical instructions, see: http://www.slyck.com/ng.php?page=2