Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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:
share|improve this question
1  
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 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 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 at 16:14
    
@Szabolcs will you please post an answer, or vote to close? Thank you! –  Jacob Akkerboom Mar 26 at 18:00
1  
@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 at 18:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.