Re: What to return on symbolic evaluation of own heads ?
- To: mathgroup@smc.vnet.net
- Subject: [mg12596] Re: [mg12511] What to return on symbolic evaluation of own heads ?
- From: Wouter Meeussen <eu000949@pophost.eunet.be>
- Date: Mon, 25 May 1998 14:25:07 -0400
At 18:10 23-05-98 -0400, Andreas Keese wrote: >Hi, > >I'm trying to build my own Datatype - as an example, I'll try to >construct a characteristic function. I define a characteristic function >like this: > >F[x_] := MyCharFunc[0,1,x] >(* this is the char func of the interval [0,1] *) > >And I try to define an evaluation-rule like this: > >MyCharFunc[left_, right_, var_]:= > If[NumberQ[var], > If[var < left || var > right, > 0, > 1], (* else - symbolic variable *) > MyCharFunc[left, right, var] > ] > >Of course, this doesn't work - while I can write F[0.5] and get a >correct result, I can't write F[x] as this results in an infinite >recursion --- but If I want to manipulate my data-object, I somehow >need to return the original object. E.G. I want to be able to write > F[x] /. x-> 3 >and have this evaluated as F[3]... > >What should I return in my evaluation-rule when the parameter is >symbolic ? > >Bye, >Andreas > > what about putting the NumericQ[ ] constraint in the pattern? that way, a symbolic variable just bounces the input back to you: In[1]:= MyCharFunc[myleft_, myright_, var_?NumberQ]:= If[var < myleft || var > myright, 0, 1] *** test *** In[2]:= MyCharFunc[0,1,.5] Out[2]= 1 In[3]:= MyCharFunc[0,1,1.5] Out[3]= 0 In[4]:= MyCharFunc[0,1,x] Out[4]= MyCharFunc[0,1,x] In[5]:= MyCharFunc[0,1,x]/.x->.7 Out[5]= 1 Dr. Wouter L. J. MEEUSSEN w.meeussen.vdmcc@vandemoortele.be eu000949@pophost.eunet.be