473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

efficent test for array with only one value?

I'm trying to discover if there's an efficient way to determine
if all of the values of a Numeric array are the same. In C, I
would search from the second value, checking each against the
first value. The first one that doesn't match would trigger a
False return value. If the loop completes, True would be
returned.

Looking through array functions, I'm not finding anything like
that. I'm imagining that there should be something like an
equal function (Is that Lisp I'm recalling?) that performs
a[0,0] == a[0,1] == a[0,2] == ...
and returns False as soon as it is known. I don't see that.

I can, of course, iterate through all of the values, but it
seems like there should be an efficient built-in function to do
it.

Thank you.

--kyler
Jul 18 '05 #1
6 1616
Kyler Laird wrote:
I'm trying to discover if there's an efficient way to determine
if all of the values of a Numeric array are the same. In C, I
would search from the second value, checking each against the
first value. The first one that doesn't match would trigger a
False return value. If the loop completes, True would be
returned.

Looking through array functions, I'm not finding anything like
that. I'm imagining that there should be something like an
equal function (Is that Lisp I'm recalling?) that performs
a[0,0] == a[0,1] == a[0,2] == ...
and returns False as soon as it is known. I don't see that.

I can, of course, iterate through all of the values, but it
seems like there should be an efficient built-in function to do
it.
Python 2.3.3 (#1, Dec 28 2003, 00:16:29)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
from Numeric import *
a = ones((3,5))
equal.reduce(a.flat) 1 a[0,3] = 0
equal.reduce(a.flat) 0


Ufuncs are wonderful things.
Thank you.

--kyler

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Jul 18 '05 #2
Robert Kern <rk***@ucsd.edu> writes:
from Numeric import *
a = ones((3,5))
equal.reduce(a.flat)1 a[0,3] = 0
equal.reduce(a.flat)0
Ufuncs are wonderful things.


Yeah, but they also don't work for this. First, I'm guessing that reduce
will not stop immediately when equal encounters a False. (It doesn't do
"and".)

Also, it just plain doesn't work. import Numeric
a = Numeric.zeros((3,5))
Numeric.equal.reduce(a.flat) 0 Numeric.equal.reduce([0,0,0,0,0,0,1])

1

--kyler
Jul 18 '05 #3
Kyler Laird wrote:
Robert Kern <rk***@ucsd.edu> writes:

>from Numeric import *
>a = ones((3,5))
>equal.reduce(a.flat)
1
>a[0,3] = 0
>equal.reduce(a.flat)


0

Ufuncs are wonderful things.

Yeah, but they also don't work for this. First, I'm guessing that reduce
will not stop immediately when equal encounters a False. (It doesn't do
"and".)

Also, it just plain doesn't work.
>>> import Numeric
>>> a = Numeric.zeros((3,5))
>>> Numeric.equal.reduce(a.flat)
0
>>> Numeric.equal.reduce([0,0,0,0,0,0,1])
1


Yeah, I'm sorry. I had a brainfart (one of several today, alas). The
reduce method is completely the wrong thing to use since it uses the
*result* of the last comparison as the first argument for the following
comparison.

However, alltrue(a.flat == a.flat[0]) will work but won't short-circuit.
Fast, though, if the array isn't huge.
--kyler


--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Jul 18 '05 #4
Kyler Laird wrote:
Robert Kern <rk***@ucsd.edu> writes:
... I'm guessing that reduce
will not stop immediately when equal encounters a False.


If the early stop is what you want, how about:
if vector:
d = {}
d[vector[0]] = 0
try:
for el in vector:
d[el]
except KeyError:
return False
return True

-Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #5
Robert Kern <rk***@ucsd.edu> writes:
However, alltrue(a.flat == a.flat[0]) will work but won't short-circuit.
How did I miss that?! Yes, that's the kind of operation I sought.
I'm surprised that it doesn't short-circuit. It *should*, right?

It's a shame that it only works along a single axis. The use of
"flat" is a problem for me. I'm looking at sections of an image
(building a quadtree) so the arrays I'm checking are not contiguous.
I think that means I have to resort to something ugly like this.
Numeric.alltrue(Numeric.alltrue(a == a[0,0]))
That eliminates many opportunities for short-circuiting.

I can also flatten the array using reshape() before checking it. I
assume that also means a lot of possibly unnecessary operations.
(Does reshape() return a copy of the array or just an array with
the original data and a new shape?)
Fast, though, if the array isn't huge.


Indeed, I think I'll use it. I can always write a clean short-
circuiting C version later.

Thank you!

--kyler
Jul 18 '05 #6
At some point, Kyler Laird <Ky***@news.Lairds.org> wrote:
Robert Kern <rk***@ucsd.edu> writes:
However, alltrue(a.flat == a.flat[0]) will work but won't short-circuit.


How did I miss that?! Yes, that's the kind of operation I sought.
I'm surprised that it doesn't short-circuit. It *should*, right?

It's a shame that it only works along a single axis. The use of
"flat" is a problem for me. I'm looking at sections of an image
(building a quadtree) so the arrays I'm checking are not contiguous.
I think that means I have to resort to something ugly like this.
Numeric.alltrue(Numeric.alltrue(a == a[0,0]))
That eliminates many opportunities for short-circuiting.

I can also flatten the array using reshape() before checking it. I
assume that also means a lot of possibly unnecessary operations.
(Does reshape() return a copy of the array or just an array with
the original data and a new shape?)


In general, use ravel() instead of .flat.
ravel() is a simple wrapper around reshape(), which will make a
contiguous only if necessary. .flat will fail for non-contiguous arrays
(like views into another array).

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Randell D. | last post by:
Folks, Ever since reading an interesting article in Linux Format on PHP whereby suggested code writing was made that could enhance performance on a server, I've started testing various bits of...
5
by: Bosconian | last post by:
I need to make an associated array (all with the same key name) using values from another array. Is there a more efficient way to doing this in one pass (instead of looping)? I'm always looking...
6
by: Shelly | last post by:
OK, I am at a toral loss after having programmed for forty years. Here are my results: selHint_2=1 It is clear that for the second time through the loop ($i=1) that $index=1 and $selHint_2=1,...
40
by: Allan M. Bruce | last post by:
I am applying for my first jobs after completing my PhD. I have been asked by a company to go and take a C programming test to see how my C skills are. Apparantly this test is mostly finding...
9
by: hendedav | last post by:
Gang, I am trying to get a regular expression test to work and can't figure out why. I will give you the code below: for (var j=0; j<document.getElementById('cmbList').options.length; j+...
2
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
13
by: Daniel Klein | last post by:
I have a class constructor that accepts an array as the only argument. The catch is that the array MUST be an 'integer-indexed' array, not an 'associative' array, because the index position has...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.