[Date Index]
[Thread Index]
[Author Index]
Re: C-pointers from Mathematica
*To*: mathgroup at smc.vnet.net
*Subject*: [mg112974] Re: C-pointers from Mathematica
*From*: Albert Retey <awnl at gmx-topmail.de>
*Date*: Sat, 9 Oct 2010 06:33:01 -0400 (EDT)
*References*: <i8mlt4$h87$1@smc.vnet.net>
Hi,
> 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?
I have done similar things, and for everything where the syntax is just
not available in Mathematica, I found it easiest to work with string
pattern matching of the resulting code. Here is an example:
expr = T[5] - T[4]
StringReplace[
ToString[CForm[expr]],
{
"T(" ~~ x : (Except[")"] ..) ~~ ")" :> "*(profile + " <> x <> ")"
}
]
On the other hand I don't really see that why you want to address your
array with pointer arithmetic, from all I remember from C you could just
as well use array syntax, which would probably be easier generated from
a Mathematica expression -- and create C code that is easier to
read/debug. Here is an example of that approach (you need to change T[x]
to T[[x]] to get T[x] instead of T(x) in CForm):
CForm[expr /. T[x_] :> T[[x]]]
You might want to suppress the warning messages with Quiet or by
preventing the evaluation of the expression with something like this:
CForm[Apply[HoldForm,{expr}] /. T[x_] :> T[[x]]]
hth,
albert
Prev by Date:
**Re: How to run Mathematica nb file in command line in windows?**
Next by Date:
**Re: Astronomical Data Accuracy**
Previous by thread:
**Re: C-pointers from Mathematica**
Next by thread:
**Re: C-pointers from Mathematica**
| |