Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #13796 > unrolled thread

CForm Question

Started byjakalof@gmail.com
First post2013-03-10 05:47 +0000
Last post2013-03-15 05:46 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  CForm Question jakalof@gmail.com - 2013-03-10 05:47 +0000
    Re: CForm Question Peter Pein <petsie@dordos.net> - 2013-03-12 04:34 +0000
    Re: CForm Question Jak Alof <spamaddress.giveaway@yahoo.com> - 2013-03-15 05:46 +0000

#13796 — CForm Question

Fromjakalof@gmail.com
Date2013-03-10 05:47 +0000
SubjectCForm Question
Message-ID<khh6p1$akm$1@smc.vnet.net>
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[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]}

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[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])


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

[toc] | [next] | [standalone]


#13815

FromPeter Pein <petsie@dordos.net>
Date2013-03-12 04:34 +0000
Message-ID<khmb7q$ngl$1@smc.vnet.net>
In reply to#13796
Am 10.03.2013 06:47, schrieb jakalof@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[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]}
>
> 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[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])
>
>
> 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]:= (1/12)*(
    (-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]) /. (f : x | y | z)[i_] :> Part[f, i] //
   CForm // Quiet
Out[1]//CForm=
(-(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.

hth,
Peter

[toc] | [prev] | [next] | [standalone]


#13844

FromJak Alof <spamaddress.giveaway@yahoo.com>
Date2013-03-15 05:46 +0000
Message-ID<khucj9$8i$1@smc.vnet.net>
In reply to#13796
On Saturday, March 9, 2013 10:47:13 PM UTC-7, Jak Alof wrote:
> 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[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]}
> 
> 
> 
> 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[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])
> 
> 
> 
> 
> 
> 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

Thanks. That's helpful.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web