Re: Re: Manipulating Slot objects in Compile

• To: mathgroup at smc.vnet.net
• Subject: [mg24685] Re: [mg24604] Re: Manipulating Slot objects in Compile
• From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
• Date: Fri, 4 Aug 2000 01:19:08 -0400 (EDT)
• Organization: Universitaet Leipzig
• Sender: owner-wri-mathgroup at wolfram.com

```Hello,

first of all there should be parenthesis arround  the replacements

c = Compile @@ ({{{cArg, _Real, 1}}, Hold @@ {grad /. rules}} /.
Hold[a_Part] :> a /. Hold[a_] :> Unevaluated[a])

c[[3]]

{0, 4, 11, 0, 2}

and

c[{2, 3}]

{108., 108.}

the parenthesis are lost while collecting the steps. :-(
But my original answer had a assigment step.

Here is my first reply:

>>Here is the code that take a system of differential equations
>>and make a compiled function and a compiled jacobian. It is not
>>exactly the same but it may give you  a hint.
>>
>>Clear[dirkMakeFun]
>>
>>(* We must wrap a Hold[] around the Part[new, _] to
>>   prevent that the part of a symbol is serached *)
>>
>>dirkMakeVars[y_List, new_Symbol] :=
>>  Module[{x, i},
>>    Thread[y -> Table[Hold[new[[x]]] /. x -> i, {i, Length[y]}]]
>>                ]
>>
>>DIRKNDSolve::noexpl = "No explicit solution found for equations `1`."
>>
>>
>>dirkMakeFun[eqn : {_Equal ..}, y_List, t_Symbol, compile_:True] :=
>>  Module[{sol, deqn, f, jac, rules, newsym, fj},
>>    deqn = Select[eqn, ! FreeQ[#, t] &];
>>    sol = Solve[deqn, D[#, t] & /@ y];
>>   If[sol == {} || Head[sol] === Solve,
>>     Message[DIRKNDSolve::noexpl, eqn];
>>     Return[\$Failed];
>>    ];
>>   f = D[y, t] /. sol; (* explicit solution *)
>>   jac = Outer[D[#1, #2] &, #, y] & /@ f;
>>   newsym = Unique[];
>>   rules = dirkMakeVars[y, newsym];
>>   fj = Transpose[{f, jac}] /.  rules;
>>   If[compile,
>>    (* at first we add the function arguments for compile
>>       with an evaluated newsym, than Hold[] is removed
>>       and the Hold[part[_]] are removed with the last Replace[] *)
>>    fj = Map[Hold,
>>           Map[{{{t, _Real}, {newsym, _Real, 1}}, #} & , fj, {2}] , {2}]
>>/.
>>             Hold[a_Part] :> a;
>>    (* Now we can start with compile. To prevent a early
>>       evaluation of the Part[] function in the Apply[] call an
>>Unevaluated[]
>>       is needed *)
>>    fj /. Hold[l_] :> Apply[Compile, Unevaluated[l]]
>>   (* Else *),
>>    ReleaseHold[Map[Function[Evaluate[{t, newsym}], #]  &, fj, {2}]]
>>  ]
>>
>>dirkMakeFun[] generate for the variables in y a vector newsym so that
>>for
>>y={x[t],q[t],z[t]} the new vector ist newsym[[1]], newsym[[2]] and
>>newsym[[3]].
>>The tricky part is to prevent  the evaluation of newsym[[i]]  before
>>it is in the Compile[] command.
>>dirkMakeVars will return something like
>>
>>{x[t]->Hold[newsym[[1]]],q[t]->Hold[newsym[[2]]],z[t]->Hold[newsym[[3]]]}
>>
>>than I make the argument for Compile[]  but with a hold arround the full
>>arguments and get
>>Hold[{{{newsym, _Real,1}},theExpressionWithManyHoldParts}}
>>
>>since a Hold[] is around the expression I can remove the Hold[a_Part]
>>inside the expression. Finaly I have to Apply[] the Compile command
>>*and* to remove the Hold[]
>>
>>    fj /. Hold[l_] :> Apply[Compile, Unevaluated[l]]
>>
>>and I have my pair of function/jacobian compiled functions for
>>the right hand side of my systems of differential equations.
>>
>>Hope that helps
>>  Jens

Since I have exchanged several mails with the poster of this question
he got a solution two weeks ago with my first mail.

Your solution must explicit switch off the
error messages of Part[cArg,i].
One task was just to avoid the explicit Off[Part::"partd"]
because there is no save way to switch it on again.
Since there is no way to find out if the message was prevoiusly on or
off,
you cant just say On[Part::"partd"]. That's  why my solution
was not so simple as yours.

Regards
Jens

```

• Prev by Date: RE: Equation of a "potato"
• Next by Date: RE: Re: Manipulating Slot objects in Compile
• Previous by thread: RE: Re: Manipulating Slot objects in Compile
• Next by thread: RE: Re: Manipulating Slot objects in Compile