RE: How to operate on strictly numerical functions ?
- To: mathgroup at smc.vnet.net
- Subject: [mg24118] RE: [mg24102] How to operate on strictly numerical functions ?
- From: Wolf Hartmut <hwolf at debis.com>
- Date: Wed, 28 Jun 2000 02:11:40 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
My answer see below > -----Original Message----- > From: Mark Harder [SMTP:harderm at ucs.orst.edu] To: mathgroup at smc.vnet.net > Sent: Tuesday, June 27, 2000 6:52 AM > To: mathgroup at smc.vnet.net > Subject: [mg24102] How to operate on strictly numerical functions ? > > > Some of the Mathematica functions made for numerical procedures > appear to actually evaluate by substitution of symbolic variables with > replacement rules. An example of this, which has blocked me for more > than a week now, is NonlinearRegress, which seems to evaluate the > user-supplied model function symbolically, then evaluate the resulting > expression through replacement of the independent variables and the > current set of adjustable parameters through replacement rules that it > constructs. My model function requires evaluation of the SVD (with > SingularValues[] ) of a matrix computed from the independent variables > and the parameters of the model, and so NonlinearRegress fails, since > SingularValues[] can't accept a non-numeric matrix. A simpler example I > have encountered is the numeric derivative function, which I'll use to > illustrate my problem. > First, construct a simple test function which, through an If[] test, > won't evaluate for non-numeric arguments: > In[804]:= > ClearAll[x, tstFn1] > tstFn1[x_] := If[NumericQ[x ], Return[x^2];, Print["NonNumeric x > and/or y."];] > > In[808]:=tstFn1[2] > Out[808]=4 > In[809]:=tstFn1[u] > > & try to use ND[] on this function: > > <<NumericalMath`NLimit` > > In[806]:=ClearAll[u, v] > ND[tstFn1[u ], u, 1.] > Out[807]= 0 > > So, I'm looking for some means of modifying testFn1 to cause ND to > evaluate it only after numeric substitution for u. Is there some way of > Hold-ing evaluation until numeric values are assigned to arguments of a > function? Is this sort of thing impossible in Mathematica? Do I have > to write my own numeric routines for finding derivatives, gradients, > Jacobians, least-squares fits, etc. by procedural routines ala FORTRAN? > Thanks for any help you can offer. > > mark e. harder > > harderm at ucs.orst.edu > [Wolf Hartmut] Dear Mark, perhaps this observation might help you: In[20]:= << NumericalMath`NLimit` In[23]:= Attributes[ND] Out[23]= {Protected, ReadProtected} so there are no Hold Attributes here, this will evaluate tstFn1[u] in first place. However there might be more layers of evaluation lurking inside. So let's test that: In[44]:= uu = NestList[Unevaluated, Hold[tstFn1[u]], 5] This contains up to five 'protection layers' agains evaluation. In[51]:= Print[Block[{u = 1.}, ReleaseHold[#]]] & /@ uu >From In[51]:= 1. >From In[51]:= 1. >From In[51]:= 1. >From In[51]:= Unevaluated[tstFn1[u]] >From In[51]:= Unevaluated[Unevaluated[tstFn1[u]]] >From In[51]:= Unevaluated[Unevaluated[Unevaluated[tstFn1[u]]]] Out[51]= {Null, Null, Null, Null, Null, Null} This was for getting familiar with the test, now apply it: In[47]:= (Print["---"]; ND[ReleaseHold[#], u, 1.]) & /@ uu >From In[47]:= "---" >From In[47]:= "NonNumeric x and/or y." >From In[47]:= "---" >From In[47]:= "NonNumeric x and/or y." >From In[47]:= "---" >From In[47]:= "NonNumeric x and/or y." >From In[47]:= "---" >From In[47]:= "NonNumeric x and/or y." >From In[47]:= "---" >From In[47]:= "NonNumeric x and/or y." >From In[47]:= "NonNumeric x and/or y." >From In[47]:= "NonNumeric x and/or y." >From In[47]:= "NonNumeric x and/or y." >From In[47]:= "NonNumeric x and/or y." >From In[47]:= "NonNumeric x and/or y." >From In[47]:= "NonNumeric x and/or y." >From In[47]:= "NonNumeric x and/or y." >From In[47]:= "---" Out[47]= {0, 0, 0, 0, 0, 2.} The last result looks very pleasing, not so pleasing would be the next layer (I avoided here). So if you need to have your function unevaluated up to a certain -- generally unknown -- point in computation with supplied packages you certainly live on the knife's edge. However perhaps this might be a 'cheating device' against unwanted evaluation: In[59]:= tstFn2[x_] := If[NumericQ[x], x^2, Unevaluated[Unevaluated[tstFn2[x]]]] In[60]:= tstFn2[u] Out[60]= Unevaluated[tstFn2[u]] In[61]:= ND[tstFn2[u], u, 1.] Out[61]= 2. Kind regards, Hartmut