MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: What to return on symbolic evaluation of own heads ?




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




  • Prev by Date: Re: What to return on symbolic evaluation of own heads ?
  • Next by Date: Re: About plotting a surface
  • Prev by thread: Re: What to return on symbolic evaluation of own heads ?
  • Next by thread: Re: What to return on symbolic evaluation of own heads ?