Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there any efficient easy way to compare two lists with the same length with Mathematica?

1,617 views
Skip to first unread message

Rex

unread,
Jan 17, 2012, 3:52:37 AM1/17/12
to
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?

DrMajorBob

unread,
Jan 17, 2012, 6:03:54 AM1/17/12
to
And@@Thread[A > B]

Bobby
--
DrMaj...@yahoo.com

Bill Rowe

unread,
Jan 17, 2012, 7:02:59 AM1/17/12
to
On 1/17/12 at 3:34 AM, aoi...@gmail.com (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}]

I think you do need to do an element by element comparison. But
you don't need to specifically select each element using Part as
you are doing. That is:

In[13]:= {a, b} = RandomInteger[100, {2, 5}];
And @@ Thread[a > b]

Out[14]= False

In[15]:= c = RandomInteger[{150, 200}, 5];
And @@ Thread[c > a]

Out[16]= True


Murray Eisenberg

unread,
Jan 18, 2012, 5:58:02 AM1/18/12
to
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 mur...@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

Andy Ross

unread,
Jan 18, 2012, 5:58:33 AM1/18/12
to
On 1/17/2012 2: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?
>

If the vectors are very long and there isn't a good reason to expect
that most false cases will occur early you might consider UnitStep.

a=RandomReal[{0,1},10^6];
b=a+1;
c=RandomReal[{0,1},10^6];

In[311]:= And@@Thread[b>a]//AbsoluteTiming
Out[311]= {0.858011,True}

In[312]:= Total[UnitStep[b-a]]==Length[b]//AbsoluteTiming
Out[312]= {0.046801,True}

In[313]:= And@@Thread[c>a]//AbsoluteTiming
Out[313]= {0.858011,False}

In[314]:= Total[UnitStep[c-a]]==Length[c]//AbsoluteTiming
Out[314]= {0.046801,False}

Andy Ross
Wolfram Research

James Stein

unread,
Jan 18, 2012, 5:59:34 AM1/18/12
to
I learned something from the answer(s) on this thread, leading to a
question of my own: Why do 'Equal' and 'SameQ' respond differently
from 'Greater' and 'Less' ? e.g.:

a = {1, 2, 3};
b = {3, 2, 1};
Thread[a < b]
Thread[a == b]
Thread[a === b]
Thread[b < a]

On Tue, Jan 17, 2012 at 4:01 AM, Bill Rowe <read...@sbcglobal.net> wrote:

> On 1/17/12 at 3:34 AM, aoi...@gmail.com (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}]
>

Alexei Boulbitch

unread,
Jan 18, 2012, 6:06:13 AM1/18/12
to

What about this?



B = Range[1000000];

A = Range[1000001, 2000000];

Positive[A - B] /. List -> And // Timing



{0.281, True}



A = Range[1000000];

B = Range[1000001, 2000000];

Positive[A - B] /. List -> And // Timing



{0.281, False}











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?





Alexei BOULBITCH, Dr., habil.

IEE S.A.

ZAE Weiergewan,

11, rue Edmond Reuter,

L-5326 Contern, LUXEMBOURG



Office phone : +352-2454-2566

Office fax: +352-2454-3566

mobile phone: +49 151 52 40 66 44



e-mail: alexei.b...@iee.lu<mailto:alexei.b...@iee.lu>






DrMajorBob

unread,
Jan 19, 2012, 5:07:24 AM1/19/12
to
a == b and a === b evaluate before Thread has a chance to act. The others
do not.

a < b
a > b
a == b
a === b

{1, 2, 3} < {3, 2, 1}

{1, 2, 3} > {3, 2, 1}

False

False

Bobby

On Wed, 18 Jan 2012 04:58:07 -0600, James Stein <math...@stein.org>
wrote:

> I learned something from the answer(s) on this thread, leading to a
> question of my own: Why do 'Equal' and 'SameQ' respond differently
> from 'Greater' and 'Less' ? e.g.:
>
> a = {1, 2, 3};
> b = {3, 2, 1};
> Thread[a < b]
> Thread[a == b]
> Thread[a === b]
> Thread[b < a]
>
> On Tue, Jan 17, 2012 at 4:01 AM, Bill Rowe <read...@sbcglobal.net>
> wrote:
>
>> On 1/17/12 at 3:34 AM, aoi...@gmail.com (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}]
>>
>> I think you do need to do an element by element comparison. But
>> you don't need to specifically select each element using Part as
>> you are doing. That is:
>>
>> In[13]:= {a, b} = RandomInteger[100, {2, 5}];
>> And @@ Thread[a > b]
>>
>> Out[14]= False
>>
>> In[15]:= c = RandomInteger[{150, 200}, 5];
>> And @@ Thread[c > a]
>>
>> Out[16]= True
>>
>>
>>


--
DrMaj...@yahoo.com

Andrzej Kozlowski

unread,
Jan 19, 2012, 5:13:22 AM1/19/12
to
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?
>>
>

Armand Tamzarian

unread,
Jan 19, 2012, 5:17:40 AM1/19/12
to
On Jan 18, 9:58 pm, Andy Ross <an...@wolfram.com> wrote:
> On 1/17/2012 2: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?
>
> If the vectors are very long and there isn't a good reason to expect
> that most false cases will occur early you might consider UnitStep.
>
> a=RandomReal[{0,1},10^6];
> b=a+1;
> c=RandomReal[{0,1},10^6];
>
> In[311]:= And@@Thread[b>a]//AbsoluteTiming
> Out[311]= {0.858011,True}
>
> In[312]:= Total[UnitStep[b-a]]==Length[b]//AbsoluteTiming
> Out[312]= {0.046801,True}
>
> In[313]:= And@@Thread[c>a]//AbsoluteTiming
> Out[313]= {0.858011,False}
>
> In[314]:= Total[UnitStep[c-a]]==Length[c]//AbsoluteTiming
> Out[314]= {0.046801,False}
>
> Andy Ross
> Wolfram Research


Min[UnitStep[c - a]] == 1

and

Min[Sign[c - a]] == 1

are also pretty quick.

Mike

Murray Eisenberg

unread,
Jan 20, 2012, 1:48:57 AM1/20/12
to


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 mur...@math.umass.edu
>> Mathematics& Statistics Dept.

Andrzej Kozlowski

unread,
Jan 20, 2012, 1:49:28 AM1/20/12
to
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

Murray Eisenberg

unread,
Jan 20, 2012, 1:52:02 AM1/20/12
to
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.

Andrzej Kozlowski

unread,
Jan 20, 2012, 1:53:34 AM1/20/12
to
Well, actually both. The vector spaces R^n for n>1 have lots of
different orders arising from the natural total order on R (the real
numbers) and none of them is in any sense "canonical". In fact, of
course, mathematica does have a default ordering, which is
lexicographical, as you can see here:

OrderedQ[{1, 4}, {3, 3}]

True

and not the product ordering which is only a partial order. In any case,
I don't think any of these ordering should by assumed when one applies
the symbol <= etc.

Andrzej


On 19 Jan 2012, at 18:25, Ralph Dratman wrote:

> "Comparisons such as {3, 4} >= {2, 5} do not have a canonical meaning..."
>
> Andrej,
>
> Are you referring to a canonical meaning within Mathematica, or to
> the canonical meaning in some wider realm of mathematics?
>
> I ask because I have seen the expression "canonical" used at Wolfram
> in the past, and I am not sure what is intended.
>
> How do we determine what the canonical meaning (if any) of an expression is?
>
> Ralph

Rex

unread,
Jan 21, 2012, 5:11:24 AM1/21/12
to
Great. Thanks a lot for all of you.

Hope you all have a nice day.


On Tue, Jan 17, 2012 at 7:37 AM, Andy Ross <an...@wolfram.com> wrote:
> On 1/17/2012 2: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?
>>
>
0 new messages