Message Boards Message Boards

How to generate this C code with Wolfram Language?

I want to generate a C-program that does the following. All the variables are real and the calculations are done as double precision, the reading and writing can be unformatted (READ( * , * ) and WRITE( * , * ) in FORTRAN):

  1. Read some coefficients from an input file:

    a1=first read value, ..., a8=last read value.
    
  2. Assign a set of known values (data) to some 'design' variables:

    x1=list1, ..., x12=list12
    

Note: The amount of 'columns' (lists of x's) is not related to the amount of coefficients, because this is a non-linear regression problem.

  1. Calculate a function value for each 'row'. The function f is non-linear but relatively easy to code (powers, exponentials, etc.)

    yi=f(a1, a2, ...a8, x1i, x2i,...,x12i)
    
  2. Print the yi's.

The program is easy to write in FORTRAN (the harder part is to automate the assignment of dataset values into the lists of x's, because the dataset is large). I'd rather not code in C if I can avoid it, so I was hoping Mathematica could do it for me.

The purpose is to couple the compiled C code to some external non linear regression solver and compare the results with the NonlinearModelFit in Mathematica. The reason for wanting something compiled is to avoid the overhead of launching the Mathematica kernel and evaluating not compiled code each time the external solver calls the code.

Any ideas will be appreciated.

OL.

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

Group Abstract Group Abstract