Wednesday, November 24, 2010

MathLink Example 2: Sum a list of floating point numbers

This example considers a sum of of floating point numbers.
----------------------------------------------------------------
addRealList.c
----------------------------------------------------------------

#include "mathlink.h"
extern double addRealList( void );



double addRealList( void )
{
 int i,n;

 double *list; 

 MLGetReal64List(stdlink,&list,&n);

 double sum=0;
 for(i=0;i<n;i++) sum = sum + list[i];

 
  MLReleaseReal64List(stdlink,list,n);

 return sum;
}



#if WINDOWS_MATHLINK

#if __BORLANDC__
#pragma argsused
#endif

int PASCAL WinMain( HINSTANCE hinstCurrent, HINSTANCE hinstPrevious, LPSTR lpszCmdLine, int nCmdShow)
{

 char  buff[512];
 char FAR * buff_start = buff;

 char FAR * argv[32];
 char FAR * FAR * argv_end = argv + 32;

 hinstPrevious = hinstPrevious; /* suppress warning */

 if( !MLInitializeIcon( hinstCurrent, nCmdShow)) return 1;

 MLScanString( argv, &argv_end, &lpszCmdLine, &buff_start);
 return MLMain( (int)(argv_end - argv), argv);
}

#else

int main(int argc, char* argv[])
{

 return MLMain(argc, argv);
}

#endif


------------------------------------------------------
addRealList.tm
------------------------------------------------------

:Begin:
:Function:       addRealList

:Pattern:        addRealList[ L:{___Real}]
:Arguments:      { L  }
:ArgumentTypes:  { Manual  }
:ReturnType:     Real

:End:

:Evaluate: addRealList::usage = "addRealList[x] gives the sum of the elements on x, given that they are real (double floating numbers)"

------------------------------------------
Makefile
------------------------------------------
MPREP = /usr/bin/mprep
CXX = /usr/bin/c++

addRealList : addRealListtm.c
 ${CXX} addRealListtm.c addRealList.c -o addRealList -lML64i3 -lm -lpthread -lrt -lstdc++

addRealListtm.c : addRealList.tm
 ${MPREP} addRealList.tm -o $@

clean:

 rm addRealListtm.c
 rm addRealList
 

--------------------------------------------------------
addRealList.nb
--------------------------------------------------------

link = Install["./addRealList"]

?addRealList

addRealList[{1., 5., 6.}]

Tuesday, November 23, 2010

MathLink Example 1: Sum a List of Integers

This example passes a list of integers, performs the sum and returns an integer.
We must note that the length of the list is not type int but type long.

-------------------------------------------------------------------------
addList.c
-------------------------------------------------------------------------

#include "mathlink.h"
extern double addList( double *list, long n);



double addList( double *list, long n)
{

 int i;
 double sum=0;
 for(i=0;i<n;i++) sum = sum + list[i];

 
 return sum;
}



#if WINDOWS_MATHLINK

#if __BORLANDC__
#pragma argsused
#endif

int PASCAL WinMain( HINSTANCE hinstCurrent, HINSTANCE hinstPrevious, LPSTR lpszCmdLine, int nCmdShow)
{

 char  buff[512];
 char FAR * buff_start = buff;

 char FAR * argv[32];
 char FAR * FAR * argv_end = argv + 32;

 hinstPrevious = hinstPrevious; /* suppress warning */

 if( !MLInitializeIcon( hinstCurrent, nCmdShow)) return 1;

 MLScanString( argv, &argv_end, &lpszCmdLine, &buff_start);
 return MLMain( (int)(argv_end - argv), argv);
}

#else

int main(int argc, char* argv[])
{

 return MLMain(argc, argv);
}

#endif


---------------------------------------------------------------------
 addList.tm
---------------------------------------------------------------------
:Begin:
:Function:       addList

:Pattern:        addList[ L_List]
:Arguments:      { L  }
:ArgumentTypes:  { RealList  }
:ReturnType:     Real

:End:

:Evaluate: addList::usage = "addList[x] gives the sum of the elements of the list x"

---------------------------------------------------------------------
Makefile
---------------------------------------------------------------------
MPREP = /usr/bin/mprep
CXX = /usr/bin/c++

BINARIES = addList

all : $(BINARIES)

addList : addListtm.c addList.c
 ${CXX}  addList.c addListtm.c  -lML64i3 -lm -lpthread -lrt -lstdc++ -o $@

addListtm.c : addList.tm
 ${MPREP} addList.tm -o addListtm.c

-------------------------------------------
addList.nb
-------------------------------------------

link = Install["./addList"]

addList[{1, 5, 6}]

Monday, November 22, 2010

MathLink Example 0

MathLink is a protocol on how to communicate Mathematica with external programs. For example, one would like to run compiled code in C within the Mathematica environment.  I am going to post a sequence of MathLink programs for Linux with an increasing level of complexity, starting from a simple example already found in the Mathematica help (included for completeness) finishing with an example where I send and receive a matrix of complex numbers processed by the GPU using CUDA

Most examples are made of 4 files:
  • *.c             : where the computation occurs.
  • *.tm          : MathLink template where the input and output are specified and the documentation of the function is placed. 
  • Makefile   : where the compilation sequence is specified
  • *.nb          : Mathematica notebook that executes the whole program
These files have to be placed in the same folder and compiled with: $make , which may need editing in order to provide the path of the executable file mprep that comes as part of Mathematica. 
  
As seen in Makefile, mprep generates a proper *.c code from the *.tm file, which is compiled along with the *.c file that contains the computational process.

The first example adds two integers

---------------------------------------------------------------------------------
addtwo.c
---------------------------------------------------------------------------------


#include "mathlink.h"
extern int addtwo( int i, int j);



int addtwo( int i, int j)
{

 return i+j;
}



#if WINDOWS_MATHLINK

#if __BORLANDC__
#pragma argsused
#endif

int PASCAL WinMain( HINSTANCE hinstCurrent, HINSTANCE hinstPrevious, LPSTR lpszCmdLine, int nCmdShow)
{

 char  buff[512];
 char FAR * buff_start = buff;

 char FAR * argv[32];
 char FAR * FAR * argv_end = argv + 32;

 hinstPrevious = hinstPrevious; /* suppress warning */

 if( !MLInitializeIcon( hinstCurrent, nCmdShow)) return 1;

 MLScanString( argv, &argv_end, &lpszCmdLine, &buff_start);
 return MLMain( (int)(argv_end - argv), argv);
}

#else

int main(int argc, char* argv[])
{

 return MLMain(argc, argv);
}

#endif

-----------------------------------------------------------------
 addtwo.tm
-----------------------------------------------------------------

int addtwo P(( int, int));

:Begin:
:Function:       addtwo

:Pattern:        AddTwo[i_Integer, j_Integer]
:Arguments:      { i, j }
:ArgumentTypes:  { Integer, Integer }
:ReturnType:     Integer

:End:

:Evaluate: AddTwo::usage = "AddTwo[x, y] gives the sum of two machine integers x and y."

------------------------------------------------------------
Makefile
------------------------------------------------------------

MPREP = /usr/bin/mprep
CXX = /usr/bin/c++

BINARIES = addtwo

all : $(BINARIES)

addtwo : addtwotm.c addtwo.c
 ${CXX}  addtwotm.c addtwo.c  -lML64i3 -lm -lpthread -lrt -lstdc++ -o $@

addtwotm.c : addtwo.tm
 ${MPREP} addtwo.tm -o addtwotm.c

------------------------------------------------
addtwo.nb
------------------------------------------------
SetDirectory["/home/rcabrera/Documents/source/mathematica/MathLinkExamples/mytest2/alternative"]

link = Install["./addtwo"]

?AddTwo

AddTwo[2, 3]

Thursday, October 14, 2010

Subversion

Subversion is a version control software very useful for software developers
Here I found a nice video tutorial
http://showmedo.com/videotutorials/video?name=950000&fromSeriesID=95

Wednesday, October 13, 2010

Efimov states

I've heard about Efimov states some time ago and now I decided to put them in my shopping list

http://www.sciencedaily.com/releases/2009/12/091211131526.htm

Sunday, October 3, 2010

The Challenge to make quantum computers

The challenge to implement a completely scalable  quantum computer is tied with the understanding of the quantum/classical transition where quantum mechanics dominates the explanation of the microscopic world (up to the molecular scale more or less), but classical mechanics explains very well the ordinary macroscopic world. A completely scalable quantum computer would eventually give us a macroscopic taste of the imaginable strange quantum effects that so far have been seen only in the microscopic world.

"Because there are no known fundamental obstacles to such scalability (practical quantum computer with large number of qubits), it has been suggested that failure to achieve it would reveal new physics" -Emanuel Knill


I feel that the most recent papers are becoming more conservative about their predictions on the feasibility of quantum computing despite fact that there is work stating that fault tolerant quantum computation is possible with two ingredients:

  • Maximum Error/Gate about 10^-4 to 10^-5
  • Effective error correction codes with ancillary qubits.
The last ingredient seems to be more or less accomplished  and the former one does not seem to be fundamentally unattainable despite the fact that we are currently very far. A paper that summarizes these facts with a high degree of scepticism is

Is Fault-Tolerant Quantum Computation Really Possible?

which I also like for its entertaining and straightforward writing style. 

Tuesday, September 21, 2010

Humble opinion about God and science

For some reason many physicists in the past and present such as Einstein and Hawking used the term God when they wanted to describe something essential and fundamental such as the laws of physics or the Big Bang theory. For most people this was an indication of their religiosity. However, anybody who knows the context of those words can tell that truth is different. Here is an article explaining this issue.

My personal point of view is that science does not require God by definition and not because science can prove that God does not exist. Defining science as the rational understanding of nature based on fundamental laws (and axioms), the acknowledgement of the existence of God would contradict this premise. Using God to explain something in science would be equivalent to admit an irrational element in the theory. In other words, admitting something that is not based on fundamental laws or axioms.

However, I think there is still the inconsistent possibility to put God outside science as the something beyond the laws of physics and axioms. Some would place God as the creator of the laws of physics and axioms. I do not see any consistent and rational way to put God as the explanation of absolutely anything. Having said that, I do not consider myself as completely consistent and rational being and I do not think anybody was, is or will ever be, so, here it lies a window for the endless debate for the existence of God.

Will we ever find a rational and consistent explanation for our consciousness and existence? Something tells me that the most likely answer is NOT.