Re: efficiently testing a list
- To: mathgroup at smc.vnet.net
- Subject: [mg122539] Re: efficiently testing a list
- From: Ray Koopman <koopman at sfu.ca>
- Date: Mon, 31 Oct 2011 06:52:13 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <j8j5o2$fab$1@smc.vnet.net>
On Oct 30, 2:34 am, zb <zaeem.b... at gmail.com> wrote: > Consider the list of pairs: > > List = Table[ {a[i],b[i]} , {i,1,N} ]. > > or > > List = { {a[1],b[1]} , {a[2],b[2]} , {a[3],b[3]} , ... , > {a[N],b[N]} }, > > and a given function f[a]. > > What is the quickest way of testing if all elements of List satisfy > f[a[i]] < b[i]. "List" and "N" are protected symbols in Mathematica. Use names that start with lowercase letters, such as "abList" and "n". The answer to your question depends on how fast your function f is and how many terms in abList must be checked. The faster f is, and the more likely it is that f[a[i]] < b[i] will be true for all i, then the more likely it is that And @@ ( f@#1 < #2 & @@@ abList ) will be satisfactory (though there may be faster ways in this case, especially if f is Listable). On the other hand, the slower f is, and the more likely it is that f[a[i]] < b[i] will be false for some small value of i, then the more you will want something like David Bevan's mapAndF from https://groups.google.com/group/comp.soft-sys.math.mathematica/browse_frm/thread/7196783d6bf28c7e If[Scan[If[f@#[[1]]>=#[[2]],Return@False]&,abList],Null,False,True] The downside of that is that it is very slow if all the terms in abList must be checked. A compromise procedure would be something like the suggestions of Carl Woll and Chris Chiasson in https://groups.google.com/group/comp.soft-sys.math.mathematica/browse_frm/thread/966d232547bae196 f@#1 < #2 & @@@ And @@ abList