Re: Is there any efficient easy way to compare two lists with the same length with Mathematica?
- To: mathgroup at smc.vnet.net
- Subject: [mg124451] Re: Is there any efficient easy way to compare two lists with the same length with Mathematica?
- From: Murray Eisenberg <murray at math.umass.edu>
- Date: Fri, 20 Jan 2012 01:49:59 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
Yes, you are of course correct, and I used "thread" and "listable" very loosely rather than in their strict technical senses. Perhaps there's some better term to describe what the behavior seems to be. I was thinking, more generally, of the evolution of Mathematica to a point where things like Solve[{2 x + 3 y, 5 x - y} == {0, 1}, {x, y}] now work in Mathematica (and have through several versions) but, as I recall, did not in earlier versions. And I admit I'm spoiled by my experience with the array-processing languages APL and J, where the scalar right-hand-side of the equivalent of {1, 2} == 1 would be coerced into a length-2 list, so that the expression would be interpreted as {1, 2} == {1, 1}, and then parallel processed to give the equivalent of a {True, False} result. On 1/19/12 11:40 AM, Andrzej Kozlowski wrote: > Note however that == does not "thread" over lists. Equal is, of course, not Listable so {1,1}==1 is unevaluated > > Thread[{1, 1} == 1] > > {True, True} > > Equality of lists (including matrices etc) is indeed implemented (but not through Listability) because the advantage of being able to use the same function Equal for all these different situations overrides the flexibility that you loose (like the one you point out). So I agree, but I simply don't think and equally good case can be made for Greater etc. > > Andrzej > > > On 19 Jan 2012, at 17:24, Murray Eisenberg wrote: > >> >> >> Yes, I understand the danger of having greater, etc., threading automatically over lists. >> >> But one might make the same argument as to whether == should thread automatically. For example, suppose you are modeling rational numbers as pairs of integers. Then you would want {a,b} == {c, d} to be True exacgly when a d == b c. >> >> On 1/19/12 5:10 AM, Andrzej Kozlowski wrote: >>> I do not think I would like Mathematica to automatically thread Greater >>> etc over lists or automatically return False or True. Comparisons such >>> as {3, 4}>= {2, 5} do not have a canonical meaning and they may arise >>> in programs where the fact that they are kept unevaluated can be >>> convenient e.g this >>> Min /@ ({3, 4}>= {2, 5}) is a convenient way to compare minima >>> (Min@{3,5}>=Min@{2,5} is quite a lot longer...). >>> In general, there has to be a balance between the sort of things >>> Mathematica does automatically and the sort of things that Mathematica >>> leaves unevaluated until you make your intention more clear (by using >>> Thread, for instance). In this particular case I think the second choice >>> is the right one. >>> >>> Andrzej Kozlowski >>> >>> >>> >>> On 18 Jan 2012, at 11:57, Murray Eisenberg wrote: >>> >>>> Over time more and more things like this have been extended to work >>>> automatically on lists. But so far, as you discovered, not GreaterEqual >>>> (nor Greater, etc.). Here's one way without explicitly using Table: >>>> >>>> a = RandomInteger[{0, 20}, 5]; >>>> b = RandomInteger[{0, 20}, 5]; >>>> And @@ MapThread[Greater, {a, b}] >>>> >>>> The key there is MapThread, which does what you (and I) would evidently >>>> like Mathematica to do automatically -- in effect, to make GreaterEqual >>>> have Listable as an Attribute. >>>> >>>> On 1/17/12 3:34 AM, Rex wrote: >>>>> Given two lists `A={a1,a2,a3,...an}` and `B={b1,b2,b3,...bn}`, I would >>>>> say `A>=B` if and only if all `ai>=bi`. >>>>> >>>>> There is a built-in logical comparison of two lists, `A==B`, but no >>>>> `A>B`. >>>>> Do we need to compare each element like this >>>>> >>>>> And@@Table[A[[i]]>=B[[i]],{i,n}] >>>>> >>>>> Any better tricks to do this? >>>>> >>>> >>>> -- >>>> Murray Eisenberg murray at math.umass.edu >>>> Mathematics& Statistics Dept. >>>> Lederle Graduate Research Tower phone 413 549-1020 (H) >>>> University of Massachusetts 413 545-2859 (W) >>>> 710 North Pleasant Street fax 413 545-1801 >>>> Amherst, MA 01003-9305 >>>> >>> >>> >> >> -- >> Murray Eisenberg murray at math.umass.edu >> Mathematics& Statistics Dept. >> Lederle Graduate Research Tower phone 413 549-1020 (H) >> University of Massachusetts 413 545-2859 (W) >> 710 North Pleasant Street fax 413 545-1801 >> Amherst, MA 01003-9305 > -- Murray Eisenberg murray at math.umass.edu Mathematics & Statistics Dept. Lederle Graduate Research Tower phone 413 549-1020 (H) University of Massachusetts 413 545-2859 (W) 710 North Pleasant Street fax 413 545-1801 Amherst, MA 01003-9305