Group Abstract Group Abstract

Message Boards Message Boards

How to generate this C code with Wolfram Language?

POSTED BY: Otto Linsuain
2 Replies

Hello Bianca,

Thanks for your reply. There is certainly nothing wrong with doing this in FORTRAN; I have tried it and it works. However, my FORTRAN code needs to read the coefficients (a1, a2,...) from a file, and it also needs to read the dataset from a file (READ( *, * ) x1, x2, ...).

This last thing is what I was trying to avoid, because the dataset is large and does not change, so reading it each time is a big overhead. I wanted the dataset to be already built into the executable, but the only way I know to do that is to type many very long DATA statements myself (or write a script to do it for me).

I was hoping Mathematica could create the code with the data statements already in it. I think I can do this with the CCodeGenerate and functions like CAssign, but I haven't gotten that to work yet.

I have tried simple things, such as:

Export["trycode.c", Compile[{x1, x2}, 3.0*x1^2 - 4.0*Exp[-2.5*x2^3]]]

Mathematica creates the source files, but I still need to figure out how to do the rest. I have tried the CCompilerDriver following the examples in the documentation. I guess I need to look into CCodeGenerate too.

Thanks,

OL.

POSTED BY: Otto Linsuain

Have you looked at the CCodeGenerate function? You need to make sure that Compile can handle your function, but if you know how to write it in Fortran that shouldn't be an issue.

(Also, if the program will always only communicate through files, and you find it easy to write in Fortran, there may not actually be anything wrong with doing just that.)

POSTED BY: Bianca Eifert
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard