numpy

Implementing tridiagonal matrix algorithm (TDMA) with NumPy

Hi. I'm implementing TDMA in Python using NumPy. The tridiagonal matrix is stored in three arrays: a = array([...]) b = array([...]) c = array([...]) I'd like to calculate alpha-coefficients efficiently. The algorithm is as follows: # n = size of the given matrix - 1 alpha = zeros(n) alpha[0] = b[0] / c[0] for i in range(n-1): a...

PyTables problem - different results when iterating over subset of table

I am new to PyTables, and am looking at using it to process data generated from an agent-based modeling simulation and stored in HDF5. I'm working with a 39 MB test file, and am experiencing some strangeness. Here's the layout of the table: /example/agt_coords (Table(2000000,)) '' description := { "agent": Int32Col(shape=(), d...

Python Datatype for a fixed-length FIFO

I would like to know if there is a native datatype in Python that acts like a fixed-length FIFO buffer. For example, I want do create a length-5 FIFO buffer that is initialized with all zeros. Then, it might look like this: [0,0,0,0,0] Then, when I call the put function on the object, it will shift off the last zero and put the new v...

Combine matrix in numpy

Suppose I have three "sheets" of matrix a,b and c, each with the same m*n*p dimension. And I want to combine them to get a new m*n*p*3 matrix whose (i,j,k) element is (a[i,j,k],b[i,j,k],c[i,j,k]). Which command should I use ? The dstack command seems not work here. Thanks. ...

Constructing a python set from a numpy matrix

I'm trying to execute the following >> from numpy import * >> x = array([[3,2,3],[4,4,4]]) >> y = set(x) TypeError: unhashable type: 'numpy.ndarray' How can I easily and efficiently create a set from a numpy array? ...

Extending numpy with cython

I am trying to wrap a header file which has lots of functions like this test.h void test(int N, int* data_in, int* data_out); so that I can use those from numpy. Right now I have the following cython code: test.pyx import numpy as np cimport numpy as np ctypedef np.int_t itype_t cdef extern from 'VolumeForm.h': void _test '...

"painting" one array onto another using python / numpy

I'm writing a library to process gaze tracking in Python, and I'm rather new to the whole numpy / scipy world. Essentially, I'm looking to take an array of (x,y) values in time and "paint" some shape onto a canvas at those coordinates. For example, the shape might be a blurred circle. The operation I have in mind is more or less identi...

Selecting rows from numpy ndarray

I want to select only certain rows from a numpy array based on the value in the second column. For example, this test array has integers from 1 to 10 in second column. >>> test = numpy.array([numpy.arange(100), numpy.random.randint(1, 11, 100)]).transpose() >>> test[:10, :] array([[ 0, 6], [ 1, 7], [ 2, 10], [ 3, ...

Converting NumPy array into Python array_like structure?

How do I convert a NumPy array to a Python array_like structure (for example [[1,2,3],[4,5,6]] ), and do it reasonably fast? ...

Converting arrays between NumPy and JPype?

Does a library or script exist to convert between NumPy and JPype arrays? ...

Integer overflow in numpy arrays

import numpy as np a = np.arange(1000000).reshape(1000,1000) print(a**2) With this code I get this answer. Why do I get negative values? [[ 0 1 4 ..., 994009 996004 998001] [ 1000000 1002001 1004004 ..., 3988009 3992004 3996001] [ 4000000 4004001 4008004 ..., 8982009 ...

Matlab's griddata3 for numpy?

Hi, I realize that there is a griddata for numpy via matplotlib, but is there a griddata3 (same has griddata, but for higher dimensions). In other words, I have (x,y,z,d(x,y,z)) where (x,y,z) form an irregular grid and d(x,y,z) is a scalar function of three variables. I need to generate d(xi,yi,zi) for a new set of (xi,yi,zi) points u...

Python - pickling fails for numpy.void objects

>>> idmapfile = open("idmap", mode="w") >>> pickle.dump(idMap, idmapfile) >>> idmapfile.close() >>> idmapfile = open("idmap") >>> unpickled = pickle.load(idmapfile) >>> unpickled == idMap False idMap[1] {1537: (552, 1, 1537, 17.793827056884766, 3), 1540: (4220, 1, 1540, 19.31205940246582, 3), 1544: (592, 1, 1544, ...

Python: print the full array

When I print an array i get the following stuff, but I want the full array. Is there any way to do this? Thanks in advance, and everybody a happy, lucky, successfull new year! [[0 0 0 ..., 0 0 0] [0 0 0 ..., 0 0 0] [0 0 0 ..., 0 0 0] ..., [0 0 0 ..., 0 0 0] [0 0 0 ..., 0 0 0] [0 0 0 ..., 0 0 0]] ...

Poor numpy.cross() performance

I've been doing some performance testing in order to improve the performance of a pet project I'm writing. It's a very number-crunching intensive application, so I've been playing with Numpy as a way of improving computational performance. However, the result from the following performance tests were quite surprising.... Test Source Co...

Increment Numpy array with repeated indices

I have a Numpy array and a list of indices whose values I would like to increment by one. This list may contain repeated indices, and I would like the increment to scale with the number of repeats of each index. Without repeats, the command is simple: a=np.zeros(6).astype('int') b=[3,2,5] a[b]+=1 With repeats, I've come up with the fo...

Selecting indices for a 2d array in numpy

Hi Folks, This works quite well in 1 dimension: # This will sort bar by the order of the values in foo (Pdb) bar = np.array([1,2,3]) (Pdb) foo = np.array([5,4,6]) (Pdb) bar[np.argsort(foo)] array([2, 1, 3]) But how do I do that in two dimensions? Argsort works nicely, but the select no longer works: (Pdb) foo = np.array([[5,4,6], [...

Project Euler #101 - how to work around numpy polynomial overflow?

Project Euler #101 I just started learning Numpy and it so far looks pretty straightforward to me. One thing I ran into is that when I evaluate the polynomial, the result is a int32, so an overflow would occur. u = numpy.poly1d([1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1]) for i in xrange(1, 11): print(i, u(i)) The results are: (1, 1...

Displaying a cvMatrix containing complex numbers (CV_64FC2)

Hi, I'm new to OpenCV, and I would like to compare the results of a python program with my calculations in OpenCV. My matrix contains complex numbers since its the result of a cvDFT. Python handles complex numbers well and displays it with scientific notation. My C++ program is not effective when trying to use std::cout. I tried to sto...

Is the order of a Python dictionary guaranteed over iterations?

I'm currently implementing a complex microbial food-web in Python using SciPy.integrate.ode. I need the ability to easily add species and reactions to the system, so I have to code up something quite general. My scheme looks something like this: class Reaction(object): def __init__(self): #stuff common to all reactions d...