I was surprised to see that an in-place modification of a 2-d array
turns out to be slower from the respective non-mutating operation on 1-
d arrays, although the latter creates new array objects. Here is the
for n in 10,100,1000,10000:
setup = 'from numpy.random import random;' \
'u2=u1.reshape((u1.size,1))' % (n,n)
timers = [timeit.Timer(stmt,setup) for stmt in
# 1-d operations; create new arrays
'a0 = m[:,0]-u1; a1 = m[:,1]-u1',
# 2-d in place operation
'm -= u2'
print n, [min(timer.repeat(3,1000)) for timer in timers]
And some results (Python 2.5, WinXP):
10 [0.010832382327921563, 0.0045706926438974782]
100 [0.010882668048592767, 0.021704993232380093]
1000 [0.018272154701226007, 0.19477587235249172]
10000 [0.073787590322233698, 1.9234369172618306]
So the 2-d in-place modification time grows linearly with the array
size but the 1-d operations are much more efficient, despite
allocating new arrays while doing so. What gives ?
| Date: Wed, 05 Sep 2007 21:19:58 +0200
| From: G?nter Dannoritzer <dannoritzer(a)web.de>
| Subject: Re: [Numpy-discussion] Use my own data type with NumPy
| The purpose of my (Python) class is to model a fixed point data
| type. So I can specify how many bits are used for integer and how
| many bits are used for fractional representation. Then it should be
| possible to assign a value and do basic arithmetic with an instance
| of that class. The idea is that based on fixed point arithmetic
| rules, each operation tracks changes of bit width.
You may already be aware of this, but there is a package available
that does exactly what you describe:
It is not currently actively aintained, and it's quite slow, but it
does work reliably, and we use it daily at my site. It might be best
for all concerned if you simply took over that project, making it
faster and well supported, rather than creating a competing one...
I am trying to use my own data type with NumPy, but get some funny
result when creating a NumPy array with it.
My data type is indexable and sliceable and what happens now is when I
create an array, NumPy is adding the instance as a list of the indexed
values. How can I force NumPy to handle my data type as an 'Object' and
use the value of __repr__ to display in the array?
Maybe I am handling that too simple, as I had another data type before,
-- not indexable though --, that just works fine with NumPy. Do I have
to worry about a dtype object with my new data type or how can I use my
new data type with NumPy?
Thanks for your help.
I have a scalar value S. I want to perform the following math on vectors A
and B (both of type array):
A + B * S
By order of operations, B * S should be done first. This is a vector
multiplied by a real number and should be valid. However, the interpreter
"ValueError: shape mismatch: objects cannot be broadcast to a single
I am not sure how I am supposed to multiply a vector with a scalar value.
array([5,2]) * 2 = [10,4]
The above should happen. However, I get the error message instead. Any
numpy.rot90 (from twodim_base.by) says it works only on the first two
axes of an array, but due to its use of the transpose method (which
reverses the shape tuple), can affect other axes.
a = numpy.ones((50,40,3))
b = numpy.rot90(a)
assert(b.shape == (3,40,50))
# desired result is b.shape == (40,50,3)
I believe that the fix is to replace the two calls to 'transpose()'
in the function with 'swapaxes(0,1)'. This would allow rotating an
array along just the first two axes.
Does this fix look right? Should I file a bug or can someone just
check that in?
I have two questions:
1) Is there any way in numpy to represent vectors? Currently I'm using
'array' for vectors.
2) Is there a way to calculate the magnitude (length) of a vector in numpy?
I am personnally a bit annoyed by the way trac handle bug reports,
and would like to know if there is space for improvement. I do not know
much about bug tracking systems, so maybe I just don't know how to use
it, though. The main thing I dislike is the status of tickets and
reports. In particular:
- I don't know how other feel, but I am rather confused by the meta
data of a ticket, and do not find them really useful from a developer
point of view. It would be quite helpful to have information whether the
bug is confirmed or not, another one which says wether a patch is
available or not. This would make bug triage much easier, IMHO. This
should be possible, since I have seen some trac installation with such
features (wordpad trac, for example).
- This one maybe a bit more difficult to implement I guess (I don't
know anything about trac internals): I find the general view of bugs for
a given repository really helpful in launchpad, in perticular, you can
easily view the percentage of bugs wrt their status (eg 30 % bugs
unconfirmed, etc...); see for example https://bugs.launchpad.net/+about.
This gives a pretty good idea of what needs to be done for a particular
How do other people feel about those suggestions ?
I want to change the "status" of a numpy array.
I mean this array was created by a server application using PyArray_FromDimsAndData that sets the NPY_OWNDATA flag to False.
The server application believes the client would free the memory.
But there are more than one client application and none knows who is in charge of freeing this memory.
Then I want to set the flag NPY_OWNDATA to True to tell the server to do the job when it finishes the script.
How can I do that, I mean at the Python interface level, not the C API.
>>> print a.flags.owndata
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: attribute 'owndata' of 'numpy.flagsobj' objects is not writable
Or should I have to write my own function set/get for OWNDATA ?
The get is there but I can't get the set...
Marc POINOT [ONERA/DSNA] Tel:+220.127.116.11.42.84 Fax:+18.104.22.168.41.66
> Would you be willing to help get the config.h file set up correctly?
Yes. I thought it was automatic, tho.
What to do?
> Its kernel is 2.4.19-Asmp tailored by the vendor.
> Which vendor?
Curtiss-Wright Controls, back when they were called Synergy.
> Ancient.... Curtiss-Wright now supports Linux and kernel 2.6.16 on
> some of their newer hardware
The military ends up supporting ancient systems for a long time.
I don't think I have the option of upgrading the OS on these beasts,
especially when it involves a new binary format.
That would be nice, tho, especially if it made it possible to run subversion.
> Any more detail on these? What causes the conflict. I've got to wonder about
> the the libc/libm versions also. Does the include file math.h say anything
> about the prototypes for these functions? I expect cosl et.al. to be
> potential problems on the PPC anyway due to the way long doubles were
"grep -r sinl /usr/include" finds nothing.
math.h defines sin and sinf with various underscores attached,
using token pasting, but no mention of sinl.
Similarly for fabs and cos which I checked.
> What is the PPC model number?
The CWC VSS4 contains four powerpc G4's, the 7400.