Jsoftware
High-Performance Development Platform

Checking if all elements are the same

View: New views
9 Messages — Rating Filter:   Alert me  

Checking if all elements are the same

by unigee :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi everyone,

This is slightly related my other thread on how to check if the first
and last element were the same.

If I have
a=. 2 2 2 2 2 2 2 2
b=. 10 4 $ 2
c=. 2 2 2 2 2 2 4
d=. (10 4 $ 2) , 2 2 2 4

It can be seen that the list 'a' contains all the same number
It can be seen that all the rows in matrix 'b'  are the same
It can be seen that the list 'c' _doesn't_ contain all the same number
It can be seen that the last row of the matrix 'd' is different of the
result, thus d doesn't contain all the same rows.

I am looking for a way to perform a check to see if all the elements
are the same or not.

I have managed to come up with a solution, but it is very ugly and inefficient

Thanks
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Checking if all elements are the same

by Roger Hui :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

*./@~:@,

I believe *./@~: is supported by special code
(if not, it should be).

   6!:2 '*./@~: y' [ y=: 1e6$4
0.0114199
   6!:2 '*./@~: y' [ y=: 1e6$1 4
0.0100949

Hmm, the times are the same.  No special code.



----- Original Message -----
From: Ian Gorse <unigee@...>
Date: Wednesday, November 11, 2009 11:43
Subject: [Jprogramming] Checking if all elements are the same
To: Programming forum <programming@...>

> Hi everyone,
>
> This is slightly related my other thread on how to check if the first
> and last element were the same.
>
> If I have
> a=. 2 2 2 2 2 2 2 2
> b=. 10 4 $ 2
> c=. 2 2 2 2 2 2 4
> d=. (10 4 $ 2) , 2 2 2 4
>
> It can be seen that the list 'a' contains all the same number
> It can be seen that all the rows in matrix 'b'  are the same
> It can be seen that the list 'c' _doesn't_ contain all the same number
> It can be seen that the last row of the matrix 'd' is different
> of the
> result, thus d doesn't contain all the same rows.
>
> I am looking for a way to perform a check to see if all the elements
> are the same or not.
>
> I have managed to come up with a solution, but it is very ugly
> and inefficient
>
> Thanks
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Checking if all elements are the same

by Sherlock, Ric :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

How about this?
   *./@(-:"_1 _ {.) a
1
   *./@(-:"_1 _ {.) b
1
   *./@(-:"_1 _ {.) c
0
   *./@(-:"_1 _ {.) d
0

> -----Original Message-----
> From: programming-bounces@... [mailto:programming-
> bounces@...] On Behalf Of Ian Gorse
> Sent: Thursday, 12 November 2009 08:43
> To: Programming forum
> Subject: [Jprogramming] Checking if all elements are the same
>
> Hi everyone,
>
> This is slightly related my other thread on how to check if the first
> and last element were the same.
>
> If I have
> a=. 2 2 2 2 2 2 2 2
> b=. 10 4 $ 2
> c=. 2 2 2 2 2 2 4
> d=. (10 4 $ 2) , 2 2 2 4
>
> It can be seen that the list 'a' contains all the same number
> It can be seen that all the rows in matrix 'b'  are the same
> It can be seen that the list 'c' _doesn't_ contain all the same number
> It can be seen that the last row of the matrix 'd' is different of the
> result, thus d doesn't contain all the same rows.
>
> I am looking for a way to perform a check to see if all the elements
> are the same or not.
>
> I have managed to come up with a solution, but it is very ugly and
> inefficient
>
> Thanks
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Checking if all elements are the same

by Dan Bron :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ian Gorse wrote:
>  I am looking for a way to perform a check to see if all the
>  elements are the same or not.

           allSame  =:  -: 1&|.
           allSame&> a;b;c;d
        1 1 0 0

-Dan
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Checking if all elements are the same

by Sherlock, Ric :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> From: Dan Bron
>
> Ian Gorse wrote:
> >  I am looking for a way to perform a check to see if all the
> >  elements are the same or not.
>
>   allSame  =:  -: 1&|.
>   allSame&> a;b;c;d
> 1 1 0 0
>

Nice!
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Checking if all elements are the same

by Sherlock, Ric :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> > From: Ian Gorse
> >
> > I am looking for a way to perform a check to see if all the elements
> > are the same or not.
> >
> From: Sherlock, Ric
>
> How about this?
>    *./@(-:"_1 _ {.) a
> 1

Here is another one:
   (1 = #@~.) &> a;b;c;d
1 1 0 0

isAllSame0=: -: 1&|.
isAllSame1=: *./@(-:"_1 _ {.)
isAllSame2=: 1 = #@~.

   ts=: 6!:2 , 7!:2@]
   f=:  30 (# ,:) i.10 20 30

   20 ts 'isAllSame0 f'
0.00212922717994 1049728
   20 ts 'isAllSame1 f'
0.000538559346748 33920
   20 ts 'isAllSame2 f'
0.00608321959755 1082944

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Checking if all elements are the same

by Dan Bron :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ric wrote:
>     20 ts 'isAllSame0 f'
>  0.00212922717994 1049728
>     20 ts 'isAllSame1 f'
>  0.000538559346748 33920
>     20 ts 'isAllSame2 f'
>  0.00608321959755 1082944

This surprised me, until I remembered that your phrase  -:"r  is supported
by special code.  

>  isAllSame2=: 1 = #@~.

Ah, one must be careful with constants, even the special constants  0  and
1  .  Note that  allSame2  doesn't agree with the other formulations on
zero-item arrays:

           (isAllSame0, isAllSame1, isAllSame2) ''
        1 1 0
           
Though I guess you could say  (2 > #@~.)  .

-Dan
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Checking if all elements are the same

by Sherlock, Ric :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> From: Dan Bron
>
> Ah, one must be careful with constants, even the special constants  0
> and
> 1  .  Note that  allSame2  doesn't agree with the other formulations on
> zero-item arrays:
>
>   (isAllSame0, isAllSame1, isAllSame2) ''
> 1 1 0
>
> Though I guess you could say  (2 > #@~.)  .
>

Something to remember!
Although if there are no items then are all the items equal, or are they not equal because there are none!!! ;-)

Ric
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Checking if all elements are the same

by Dan Bron :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ric wrote:
>  Although if there are no items then are all the items equal,
>  or are they not equal because there are none!!! ;-)

I would say no two items differ.  This is consistent with the answer for a
single item (1 = allSame single_item_array) and with similar results in J,
e.g.  1 = *./''  .

-Dan
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm