       Re: CForm Question

• To: mathgroup at smc.vnet.net
• Subject: [mg130124] Re: CForm Question
• From: Peter Pein <petsie at dordos.net>
• Date: Tue, 12 Mar 2013 00:34:51 -0400 (EDT)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• Delivered-to: l-mathgroup@wolfram.com
• Delivered-to: mathgroup-newout@smc.vnet.net
• Delivered-to: mathgroup-newsend@smc.vnet.net
• References: <khh6p1\$akm\$1@smc.vnet.net>

```Am 10.03.2013 06:47, schrieb jakalof at gmail.com:
> Hi,
>
> I have a fairly complicated expression that involves arrays; I want to export the expression.
>
> First, I generate 3 lists; entries in the list represent entries in a C Array.
>
> X = Table[x[i], {i, 0, 7}];
> Y = Table[y[i], {i, 0, 7}];
> Z = Table[z[i], {i, 0, 7}];
>
> Here is what X looks like:
>
> {x, x, x, x, x, x, x, x}
>
> Then I do some other work using X, Y, Z and form expressions that I need in C.  Here is a typical result that I need to export to C:
>
> 1/12 (-y z - y z + y (z - z) + y z +
>     y z + y z - y z - y z +
>     y (-z - z + z + z) +
>     y (z + z - z - z) + y z)
>
>
> The above expression involves values from the X,Y,Z tables created above which is exactly what I want.  However, when I use CForm on this expression I get:
>
> (-(y(4)*z(1)) - y(5)*z(1) + y(2)*(z(1) - z(3)) +
>       y(4)*z(3) + y(7)*z(3) + y(5)*z(4) - y(7)*z(4) -
>       y(4)*z(5) + y(1)*(-z(2) - z(3) + z(4) + z(5)) +
>       y(3)*(z(1) + z(2) - z(4) - z(7)) + y(4)*z(7))/12.
>
>
> This is extremely close to what I want except that the array values are using 'parentheses' rather than the C square brackets.    I understand that this stems from my original creation of the Tables X, Y,Z  but I don't know how to get around that.
>
> Is there a good way to fix this?
>
> Thanks,
> Jak
>
>

Hi Jak,

you can change the calls to the functions x, y, z to Part[f, index]:

In:= (1/12)*(
(-y)*z - y*z + y*(z - z) +
y*z + y*z + y*z - y*z - y*z +
y*(-z - z + z + z) +
y*(z + z - z - z) +
y*z) /. (f : x | y | z)[i_] :> Part[f, i] //
CForm // Quiet
Out//CForm=
(-(y*z) - y*z + y*(z - z) + y*z + y*z +
y*z - y*z - y*z +
y*(-z - z + z + z) +
y*(z + z - z - z) + y*z)/12.

hth,
Peter

```

• Prev by Date: Re: MixedRadixQuantity
• Next by Date: Re: Greek symbols mangled by InputForm
• Previous by thread: CForm Question
• Next by thread: Re: CForm Question