Re: Boolean 0/1 function -- Conditional definitions and Thread
- To: mathgroup <mathgroup at yoda.physics.unc.edu>
- Subject: Re: Boolean 0/1 function -- Conditional definitions and Thread
- From: HAY at leicester.ac.uk
- Date: Thu, 5 NOV 92 15:38:27 GMT
Following up Robby Villegas's observation about using Thread here is a a little
number featuring it. Many variations are possible.
Let
xlist = {a,b,c,d};
condns = Thread[LessEqual[Drop[xlist,-1], y, Drop[xlist,1]]]
{a <= y <= b, b <= y <= c, c <= y <= d}
fvalues = Thread[f[y,Drop[xlist,-1]]]
{f[y, a], f[y, b], f[y, c]}
Thread[If@@{condns, fvalues, 0}]
{If[a <= y <= b, f[y, a], 0], If[b <= y <= c, f[y, b], 0],
If[c <= y <= d, f[y, c], 0]}
Plus@@%
If[a <= y <= b, f[y, a], 0] + If[b <= y <= c, f[y, b], 0] +
If[c <= y <= d, f[y, c], 0]
The construction If@@{....} is needed since If has the attribute HoldAll; so we
get
Thread[If[condns, fvalues, 0]]
If[condns, fvalues, 0]
Re Robby's warning
> fvalues = Thread[ f[y, xlist] ]
> as long as 'y' isn't a list itself (if it's a number, say).
We can get round this problem by using the extra slots in Thread
Thread[ f[{1,4}, xlist], List, -1 ]
{f[{1, 4}, a], f[{1, 4}, b], f[{1, 4}, c], f[{1, 4}, d]}
Allan Hayes
hay at leicester.ac.uk