Wednesday, April 21, 2010

How to convert _TCHAR* to char* in C++

_TCHAR is a typedef of wide characters or wchar_t. Hence, we use wcstombs function to convert it to multibyte characters. This function should help you with that.

char * wchar_to_string(_TCHAR* widechar)
{
int size=0;
while( (char)widechar[size] != '\0'){
size++;
}
size++;
char * charpointer = new char[size];
wcstombs(charpointer, widechar, size );
return charpointer;
}

Wednesday, April 7, 2010

How to do Polynomial Fitting in Visual Studio

1. Install the GSL library as explained below in the previous post.

2. Follow the example in this link to do polynomial fitting with polynomial regression.

http://rosettacode.org/wiki/Polynomial_regression

3. Let me know if you have any questions. Good luck! :)

Tuesday, April 6, 2010

Getting Gnu Scientific Library working on Visual Studio 2005, 2008 express and 2010

I just tested on visual studio 2010 there is a problem with the _hypot not being included in the 2010 version. So for the code down below it will work but if you want to do something like polynomial fitting, it won't work on 2010 unless you get a work around. Hopefully they will fix this soon. I got it working on VS 2008 express and 2005 though. Here are the steps:
1. Download and install the complete GSL package for windows from:
http://gnuwin32.sourceforge.net/packages/gsl.htm
Just choose the "Complete package, except sources" link.


2. Within Visual Studio, go to:
File->New->Project,
then "Visual C++ Projects,"
then "Win32,"
then "Win32 Console Application."
Enter a name and click "OK."
On the next screen click "Finish."

3. Go to the menu Project and choose properties

4. Under Configuration Properties->C/C++->General->Additional Include Directories, type in "C:\Program Files\GnuWin32\include"


5. Under Configuration Properties->Linker->General->Additional Library Directories, type in "C:\Program Files\GnuWin32\lib"

6. Under Configuration Properties->Linker->Input->Additional Dependencies, type in: libgslcblas.a;libgsl.a;


7. Under Configuration Properties->C/C++->Code Generation->Runtime Library, select "Multi-threaded DLL(/MD)" or "Multi-threaded debug DLL(/MD)" depending on your code.

8. Replace the main .cpp file with something like the following:

#include "stdafx.h"
#include <gsl/gsl_poly.h>


int _tmain(int argc, _TCHAR* argv[])
{
int i;
double a[6] = { -1, 0, 0, 0, 0, 1 };
double z[10];

gsl_poly_complex_workspace * w
= gsl_poly_complex_workspace_alloc (6);

gsl_poly_complex_solve (a, 6, w, z);

gsl_poly_complex_workspace_free (w);

for (i = 0; i < 5; i++)
{
printf ("z%d = %+.18f %+.18f\n",
i, z[2*i], z[2*i+1]);
}

return 0;
}

-----------------------------------------------------------------

9. Hit F7 to build your program. You should now be able to run it from the command line. You'll get a result like the following:

z0 = -0.809016994374947670 +0.587785252292473360
z1 = -0.809016994374947670 -0.587785252292473360
z2 = +0.309016994374947510 +0.951056516295152980
z3 = +0.309016994374947510 -0.951056516295152980
z4 = +0.999999999999999890 +0.000000000000000000