Defining Multiple Forms of a Function for Different Purposes
- To: mathgroup at smc.vnet.net
- Subject: [mg36748] Defining Multiple Forms of a Function for Different Purposes
- From: "A.M. Sauer-Budge" <ambudge at MIT.EDU>
- Date: Sun, 22 Sep 2002 04:32:30 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
I would like to build my Mathematica notebooks in manner which allows me to carry out two overlapping purposes: 1) prototype an algorithm completely within Mathematica, 2) use Mathematica as a partial evaluator to splice derived expressions into a C language version of the algorithm (with the aid of the Format package from MathSource). The key complication in doing this is that for the Mathematica prototype I want everything to be evaluated, while for splicing I want the results from some of the functions to be retained as data in temporary variables. A first step towards these purposes is simple: write my component functions so they operate on lists (the natural form of the prototype data), then provide lists of arrays for arguments which I would like to supply as data in the resulting C language output forms. An example: -------------------------------------------------- In[1]:= t[x_] := {x[[4]]-x[[3]], x[[2]]-x[[1]]} In[2]:= f[x_,y_] := {y, 1}.t[x] In[3]:= f[{1,2,3,4},2] Out[3]= 3 In[4]:= f[Array[x,4],2] Out[4]= -x[1] + x[2] + 2 (-x[3] + x[4]) In[5]:= CAssign[fcnval, f[Array[x,4],2], AssignToArray->{x}] Out[5]//OutputForm= fcnval=-x[1]+x[2]+2.*(-x[3]+x[4]); -------------------------------------------------- For various reasons, however, I would like certain expressions of my symbolic derivation to be treated as data for the C language version. In the above example, for instance, I would like the array that function t produces to be a data array. The modification of the above example: -------------------------------------------------- In[6]:= tc[x_] := Array[tt,2] In[7]:= fc[x_,y_] := {y, 1}.tc[x] In[8]:= CAssign[tt, t[Array[x,4]], AssignToArray->{x}] Out[8]//OutputForm= tt[0]=-x[3]+x[4]; tt[1]=-x[1]+x[2]; In[9]:= CAssign[fcnval, fc[Array[x,4],2], AssignToArray->{tt}] Out[9]//OutputForm= fcnval=2.*tt[1]+tt[2]; -------------------------------------------------- But I don't want to carry around two versions of everything, nor do I really want to thread all of the supporting functions through my definitions in order to choose the correct function for the purpose I would rather define the C language alternatives only for functions like t and tc in the example, with f using the appropriate one depending on some evaluation flag that I set at the highest level. For example, it would be nice to be able to sprinkle in the C language alternatives with a construct like In[10]:= t[x_] := {x[[4]]-x[[3]], x[[2]]-x[[1]]} In[11]:= DefineTemporaryForm[t[x_]] := Array[tt,Length[x]/2] then be able to write In[8]:= CAssign[tt, t[Array[x,4]], AssignToArray->{x}] Out[8]//OutputForm= tt[0]=-x[3]+x[4]; tt[1]=-x[1]+x[2]; In[9]:= CAssign[fcnval, UseTemporaryForm[f[Array[x,4],2]], AssignToArray->{tt}] Out[9]//OutputForm= fcnval=2.*tt[1]+tt[2]; I would appreciate any pointers on a good, clean and hopefully simple way to do this within Mathematica! Thanks, Alex