I've written a Mathlink program that is called from Mathematica 9. It works very well but currently uses double precision numbers in the C++ function.

I've already replaced them with floats in C++ and corrected the .tm files ArgumentTypes to

:ArgumentTypes: { Real32List, Integer, Real32 }

But when calling the function within Mathematica like

MyFunction[{1.,2.,3.},100,99.]

it just returns the input - therefor a data type missmatch occurs.

Can anyone tell me how to pass the numbers as Real32? It's quite important to use 32 bit floating point numbers since I am doing calculations on a GPU that has a significatly better single precision performance.

Thanks

//template file:

void calc P((float *, int, int, float));

:Begin:
:Function:       calc
:Pattern:        Calc[a_List, b_Integer, c_Real32]
:Arguments:      { a, b, c}
:ArgumentTypes:  { Real32List, Integer, Real32 }
:ReturnType:     Manual
:End:

• If you used something like f[x_Real32] in the :Pattern: part, it should instead be f[x_Real] as Real32 is not a datatype in Mathematica. The things you put in :ArgumentTypes: are not the same as the things you put in :Pattern:. The former refers to data types MathLink can transfer while the latter will be used to validate the function call in Mathematica and need to be a Mathematica pattern. – Szabolcs Mar 26 '14 at 15:13
• If you will please share a complete example (i.e. full source code, template file and C) of a minimal attempt to write a function that takes a single Real32 and returns it, I'll probably be able to spot the problem. – Szabolcs Mar 26 '14 at 15:19
• Thanks! That was the solution. In the pattern I now use F[x_List, y_Real] and in ArgumentTypes { Real32List, Real32 } and the C function is P((float *, int, float)); – user2224780 Mar 26 '14 at 16:14
• @Szabolcs will you please post an answer, or vote to close? Thank you! – Jacob Akkerboom Mar 26 '14 at 18:00
• @JacobAkkerboom I don't want to vote to close. MathLink is not that well covered, so it's useful to have this. user2224780, can you include some extra information in your post, in particular the complete template for this function, to show the mistake very clearly? Then I'll post an answer. We're trying to make questions on this site useful not only for the original asker but also for future visitors. – Szabolcs Mar 26 '14 at 18:15

What you are trying to do is described in the tutorial Handling Lists, Arrays, and Other Expressions. In the middle of the tutorial it is explained how you can transfer different types of numbers. It works the same if you want to transfer a real with 32 bit.

:Begin:
:Function:       f
:Pattern:        f[i_Integer, x_Real, y_Real]
:Arguments:      {i, x, y}
:ArgumentTypes:  {Integer, Manual}
:ReturnType:     Real
:End:


then your function f looks like

float f(int i) {
float x, y;