[Date Index] [Thread Index] [Author Index]
Re: Problems with easy simplifications
Antonio_González <gonferh2o at esi.us.es> wrote: > I have found some weird behaviors of Mathematica (5.0) related to the > simplifications of easy expressions. > > Take, for instance the function > > H[x_, a_, b_] = If[x < a, a + b, a - b] > > If I try now to evaluate > > H[x,0,0] > > the result > > If[x < 0, 0 + 0, 0 - 0] I have two very different suggestions. Other people may have better ideas. First suggestion: Use Evaluate and delayed assignment as shown below. In:= H[x_, a_, b_]:= If[x < a, Evaluate[a + b], Evaluate[a - b]] In:= H[x, 1, 2] Out= If[x<1, 3, -1] Second suggestion: Write H in a different way such as In:= H[x_, a_, b_]:= a + (1 - 2 UnitStep[x-a])b In:= Simplify[H[x, 1, 2]] Out= 3 - 4 UnitStep[-1+x] David Cantrell > (the values of a and b are irrelevat, a similar result is obtained, for > instance, with a=1, b= 2, or any integer, real or complex values). I > cannot force (at least in a simple way) Mathematica to make the addition > or the substraction. The action of Simplify or FullSimplify leaves the > expression unchanged while a Map of Simplify > > MapAll[Simplify, H[x, 0, 0]] > > produces > > If[x < 0, > Simplify[Simplify + Simplify], Simplify[Simplify[ > 0] + Simplify[Simplify[-1] Simplify]]] > > Something even worse happens if a use values already defined. For > instance > > c = 0; d = 0; H[x_] = If[x < c, c + d, c - d] > > produces > > If[x < 0, c + d, c - d] > > In this case, the addition is not necessary. > > c = 0; d = 0; H[x_] = If[x < c, c, d] > > leads to > > If[x < 0, c, d] > > Similar problems arise using the function Which. > > Any explanation or help with this malfunction would be welcome.