The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 Returning a complex number using a DLL file
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

shafei

USA
49 Posts

Posted - 03/31/2012 :  9:19:29 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): 8.1 & 8.6
Operating System:8
Hi, I am trying to generate a DLL code so that my Origin C code runs faster. The code generates a complex number which is being used in another code. Let's assume my origin code is something like this:

complex Xnm(double x1, double y1)
{
complex eye(0.0, 1.0);
complex xnm = x1 + eye*y1;
return xnm;
}
This code is handled easily in OriginC. However the problem arises when I use visual studio (VC) to generate a DLL file out of the code above. The problem is that, if I am not wrong, C++ does not have complex class like Origin, such that VC can not return a complex number. I can only explore the real or imaginary parts of the complex number. If I want to do this, it doubles the time the code needs to be executed.
Do you have any suggestions on how to convert my Origin C code to a dll file using VC such that the returned value is a complex?
Thank you!

eparent

118 Posts

Posted - 04/03/2012 :  10:31:25 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
C++ has a complex class but it is not the same as Origin C's complex. In VC you will have to do this:

#include <complex>

typedef struct {
	double real;
	double imag;
} complex_t;


extern "C" complex_t Xnm(double x1, double y1)
{
	std::complex<double> eye(0.0, 1.0);
	std::complex<double> xnm = x1 + eye*y1;

	complex_t ret;
	ret.real = xnm.real();
	ret.imag = xnm.imag();
	return ret;
}
Go to Top of Page

shafei

USA
49 Posts

Posted - 04/04/2012 :  03:46:46 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I used it in VC and then made a Dll file. It works flawlessly. Thank you eparent!
Go to Top of Page

shafei

USA
49 Posts

Posted - 04/05/2012 :  02:56:09 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Following up with the past discussion, I have the following code in Origin C:
double BetaIntXYZ(int& N, matrix<complex>& xMoments, matrix<complex>& yMoments,
matrix<complex>& zMoments, vector& Energy, double& R)
{
int i,j;
complex value(0.0, 0.0);

for(i = 0; i< 2*N+1; i++)
{
for(j = 0; j< 2*N+1; j++)
{
if (i != N && j != N)
{
value = value + (0.75)^(0.75) * ((2*pi/R)^3) *
( xMoments[N][i] * yMoments[i][j] * zMoments[j][N] +
xMoments[N][i] * zMoments[i][j] * yMoments[j][N] +
zMoments[N][i] * xMoments[i][j] * yMoments[j][N] )
/Energy[i]/Energy[j]/3;
}
}
}

double realValue = value.m_re;
//printf("%f\n", realValue);

return realValue;
}

I want to take this code to VC, make a dll of it, and then use the dll in Origin code. Can you please let me know how the code should change, as the definition of complex and vectors are different in Origin C and VC?
Thank you!
Go to Top of Page

shafei

USA
49 Posts

Posted - 04/06/2012 :  3:03:32 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I asked this question because of the following reasons:
1. In Origin C 2-D vectors are matrices and C++ they are defined as vectors. Does Origin C recognize vector in a dll file made by C++?
2. The second problem is that in the argument of the function, I have complex numbers. Origi C handles them very well, however, I am not sure how I can use complex variables in an argument of a function in C++.
Thanks
Go to Top of Page

eparent

118 Posts

Posted - 04/17/2012 :  5:17:35 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
1. Origin C passes a vector<type> to a DLL function as a pointer to the type. The DLL function would not know the size of the vector unless you also passed in the size as another argument. Have you tried expanding the code I gave you earlier to accept a pointer to that struct?

2. The code I gave you earlier showed you how to pass a complex from Origin C to a DLL and you agreed it was working. It is not clear what you are asking now.

quote:
Originally posted by shafei

I asked this question because of the following reasons:
1. In Origin C 2-D vectors are matrices and C++ they are defined as vectors. Does Origin C recognize vector in a dll file made by C++?
2. The second problem is that in the argument of the function, I have complex numbers. Origi C handles them very well, however, I am not sure how I can use complex variables in an argument of a function in C++.
Thanks


Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000