Re: What to return on symbolic evaluation of own heads ?
- To: mathgroup@smc.vnet.net
- Subject: [mg12590] Re: What to return on symbolic evaluation of own heads ?
- From: "Allan Hayes" <hay@haystack.demon.cc.uk>
- Date: Mon, 25 May 1998 14:25:01 -0400
- References: <6k7dfb$1la@smc.vnet.net>
Andreas Keese wrote in message <6k7dfb$1la@smc.vnet.net>... >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]... Andreas: We can use pattern matching to return unevaluated: MyCharFunc2[left_, right_, var_?NumberQ]/; left<=var<=right:=1; MyCharFunc2[left_, right_, var_?NumberQ]/; left<=right:=0 F[x_] := MyCharFunc2[0,1,x] F[-1] 0 F[.5] 1 F[2] 0 F[x] MyCharFunc2[0,1,x] F[x]/.x->3 0 Also: MyCharFunc2[a,b,2] MyCharFunc2[a,b,2] MyCharFunc2[E,Pi,3] 1 ------------------------------------------------------------- Allan Hayes Training and Consulting Leicester UK http://www.haystack.demon.co.uk hay@haystack.demon.co.uk voice: +44 (0)116 271 4198 fax: +44(0)116 271 8642