       Maintaining Multiple Forms of a Function For Different Purposes

• To: mathgroup at smc.vnet.net
• Subject: [mg36745] Maintaining Multiple Forms of a Function For Different Purposes
• From: "A.M. Sauer-Budge" <ambudge at MIT.EDU>
• Date: Sun, 22 Sep 2002 04:32:25 -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:= t[x_] := {x[]-x[], x[]-x[]}
In:= f[x_,y_] := {y, 1}.t[x]

In:= f[{1,2,3,4},2]
Out= 3

In:= f[Array[x,4],2]
Out= -x + x + 2 (-x + x)

In:= CAssign[fcnval, f[Array[x,4],2], AssignToArray->{x}]
Out//OutputForm= fcnval=-x+x+2.*(-x+x);
--------------------------------------------------

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:= tc[x_] := Array[tt,2]
In:= fc[x_,y_] := {y, 1}.tc[x]

In:= CAssign[tt, t[Array[x,4]], AssignToArray->{x}]
Out//OutputForm= tt=-x+x;
tt=-x+x;

In:= CAssign[fcnval, fc[Array[x,4],2], AssignToArray->{tt}]
Out//OutputForm= fcnval=2.*tt+tt;
--------------------------------------------------

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:= t[x_] := {x[]-x[], x[]-x[]}
In:= DefineTemporaryForm[t[x_]] := Array[tt,Length[x]/2]

then be able to write

In:= CAssign[tt, t[Array[x,4]], AssignToArray->{x}]
Out//OutputForm= tt=-x+x;
tt=-x+x;

In:= CAssign[fcnval, UseTemporaryForm[f[Array[x,4],2]], AssignToArray-
>{tt}]
Out//OutputForm= fcnval=2.*tt+tt;

I would appreciate any pointers on a good, clean and hopefully simple
way to do this within Mathematica!

Thanks,
Alex

```

• Prev by Date: Defining Multiple Forms of a Function for Different Purposes
• Next by Date: RE: Re: Ball Rolling down on Cosh[t] Path
• Previous by thread: Defining Multiple Forms of a Function for Different Purposes
• Next by thread: how to plot with BOLD LINE with mathematica?