Re: Boolean 0/1 function

*To*: MathGroup at yoda.physics.unc.edu*Subject*: Re: Boolean 0/1 function*From*: villegas*Date*: Tue, 3 Nov 92 08:07:55 CST

Joseph McWilliams asked how to define a Boolean function which returns 1 or 0 (instead of the Mathematica Booleans 'True' and 'False'): > I am trying to recursively define a piecewise function in the > following manner: > > ======================== > s=0 > For[i=0,i<=(n-1), i++, > s = s + f[y,x[i]]*Indicator[y,x[i],x[i+1]] > ] > ======================== > > I would like Indicator[y,x[i],x[i+1]] to equal 1 if x[i]<= y <=x[i+1] > and 0 otherwise. You could define a function which maps the symbol 'True' to 1, and 'False' to 0: BooleanBit[True] = 1 BooleanBit[False] = 0 Then apply attach this function to any expression whose output you expect to be 'True' or 'False'. For instance, the inequality you wrote: s=0 For[i=0,i<=(n-1), i++, s = s + f[y,x[i]] * BooleanBit[ x[i] <= y <= x[i+1] ] ] If you wished, you could define BooleanBit to return some flag for cases where a test returns neither 'True' nor 'False', which could happen if you ever had symbols involved in the comparison instead of numbers. An alternative way to code this computation, more Mathematica-esque in that it is structural and functional, could go something like: (* Initialization of values *) xlist = {x1, x2, ..., xn} (where any 'xi' stands for the value of 'x[i]' in your code) y = <whatever> (the value you had for 'y') (* Computation *) fvalues = Map[f[y, #]&, Drop[xlist, -1] ] consecutivepairs = Partition[xlist, 2, 1] boolvalues = Apply[BooleanBit[#1 <= y <= #2]&, consecutivepairs, {1}] total = Apply[Plus, fvalues * boolvalues] The 'fvalues' could also be without using any pure functions, with a structural command: fvalues = Thread[ f[y, xlist] ] as long as 'y' isn't a list itself (if it's a number, say). Robby Villegas Technical Support Wolfram Research