MathGroup Archive 2010

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

Search the Archive

Re: C-pointers from Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113045] Re: C-pointers from Mathematica
  • From: Daniel Lichtblau <danl at wolfram.com>
  • Date: Tue, 12 Oct 2010 04:24:59 -0400 (EDT)

Daniel Lichtblau wrote:
> 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

What a mess. No guarantees I'll get any of it correct this time, but let 
me try again.

For the first approach, the code below is probably the same as that from 
a response by Albert Retey.

In[3]:= Quiet[CForm[T[x] /. T[x] -> profile[[i-1]]]]
Out[3]//CForm= profile[-1 + i]

For adding a macro to a header file, this version might work.

#define pointer(a)   (*(a))

Daniel Lichtblau
Wolfram Research


  • Prev by Date: Re: GeneralizedLinearModelFit and offsert for poisson
  • Next by Date: Re: local variables - Module, For loop
  • Previous by thread: Re: C-pointers from Mathematica
  • Next by thread: Re: C-pointers from Mathematica