Re: clip

*To*: mathgroup at smc.vnet.net*Subject*: [mg115741] Re: clip*From*: Alexey <lehin.p at gmail.com>*Date*: Wed, 19 Jan 2011 05:30:30 -0500 (EST)*References*: <igpbfi$fcr$1@smc.vnet.net>

Hello, There is a better solution: clip[x_] := Min[Max[x, -1], 1] clip[x_, {min_, max_}] := Min[Max[x, min], max] SetAttributes[clip, {Listable, NumericFunction}] You can compare it with the standard Clip: list = {"1.00000000000000036", "-1.00000000000000036", "1.000000000000000360000", "0.9999999999999966", "0.9999999999999966000"}; TableForm[ InputForm /@ {#, v = ToExpression[#], Clip[v], clip[v]} & /@ list, TableHeadings -> {None, {"Input", "%//InputForm", "Clip//InputForm", "clip//InputForm"}}] But it is 10 times slower: In[4]:= l = Table[10^5*x, {x, -1., 1., 10^-6}]; l1 = clip[l]; // Timing l1 = clip[#] & /@ l; // Timing l1 = Thread[clip[l]]; // Timing l2 = Clip[l]; // Timing l1 == l2 l1 === l2 Out[5]= {9.954, Null} Out[6]= {11.337, Null} Out[7]= {10.054, Null} Out[8]= {0.721, Null} Out[9]= True Out[10]= True Is there a way to make it faster? On 14, 16:19, oliver <oliver.kn... at gmail.com> wrote: > Just add a few more 0 to the argument and the accuracy will increase. > If you still suspicious, build your own Clip function. > Clip1[x_]:=Sign[x]*Min[Abs[x],1] > will do and refers to basic functions and works better. > Strangely enough, Clip1 has no problem with your example, but > it has also hick-ups with numbers smaller than 1, if not more 0 are > given. Try this out: > > Clip[1.00000000000000036] > Clip[1.000000000000000360000] > Clip1[x_]:=Sign[x]*Min[Abs[x],1] > Clip1[1.00000000000000036] > Clip1[0.9999999999999966] > Clip1[0.9999999999999966000] > > But there are some fundamental problems with accuracy. In the second > last example, it should > render the result at least in the same accuracy. All involved function > Sign, Min and Abs are > unproblematic with respect to accuracy.