MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: C-pointers from Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg112985] Re: C-pointers from Mathematica
  • From: Daniel Lichtblau <danl at wolfram.com>
  • Date: Sat, 9 Oct 2010 06:35:07 -0400 (EDT)

Bruno Silva wrote:
> Dear all,
> 
> I want to use Mathematica to transform a nonlinear PDE to its equivalent
> finite difference expression under a particular discretization scheme, and
> finally export the result to a C source file.
> I manage to do the first part of the job but the problem arrives when
> translating Mathematica variable to C-program pointers.
> 
> I want to transform T[x] -> * (profile + i), meaning that the value of "T"
> at point "x" (inside Mathematica) is equivalent to the "i-th" value in
> pointer "profile" (inside file.c).
> Because Mathematica doesn't allow me to use the C-pointer sign * that way, I
> thought replacing T[x] by a string would solve the problem (
> T[x]->"*(profile+i)" ) but this does not work because, as soon as I apply
> CForm to the result, the string symbol "..." is kept inside the expression I
> would like to write in file.c :
> 
> T[x]/.T[x] -> "*(profile+i)"
> CForm[%]
> 
> Does anyone know how to overcome this difficulty?
> Thanks in advance,
> Bruno

Could work around via explicit array reference.

In[220]:= CForm[T[x] /. T[x] -> profile[i - 1]]

Out[220]//CForm=
profile(-1 + i)

Alternatively, could do:

SetAttributes[pointer, HoldAll]
CForm[T[x] /. T[x] -> pointer[(profile + i)]]

and in a C header file define a macro for this notation:

#define pointer (*(a))


Daniel Lichtblau
Wolfram Research


  • Prev by Date: Select, from table data
  • Next by Date: Re: How to run Mathematica nb file in command line in windows?
  • Previous by thread: C-pointers from Mathematica
  • Next by thread: Re: C-pointers from Mathematica