[Date Index] [Thread Index] [Author Index]
Re: small problem.
phpcp wrote: > > Hi! > > I want to choose a definition of a function by the vlaues of its > argument. So I can use : > > a0[t_] := t^2 - t^3/5./;t<0.001;a0[t_] := t^2/;t>=0.001 > > This works fine. But if the function definition (or form or whatever) is > a bit more complicated as follows : > > solution = Solve[s0^4 + s0 t + 1 == 0,s0]; chi0[x_] := > x/;x<0.01;chi0[x_] := (solution[[3,1,2]]/.t -> x)/;x>=0.01 > > It doesnt work. Any ideas? > > thanks in advance, > Sanjay. You can "overload" the definition of functions by specifying conditions on the arguments in the formal parameter list itself. For example: myfunction[x_,y_/;(y<2.5&&y>-3.)]:=definition1 myfunction[x_,y_]:=definition2 In this case, whenever y was within the range -3 to 2.5, the first function would execute and the second one would execute the rest of the time. You can also specify conditions on the dimensions, head, etc. of the parameter. For 2-D functions, I often put a condition like a/;TensorRank[a]==2 , so that 1-D lists aren't included. The only kind of condition that doesn't seem to work in this manner is where one parameter is compared against another. You have to use an conditional within the function to do that.