[Numpy-discussion] Flattening an array
Hello, I have a function -- call it f() -- which takes a length-N 1D numpy array as an argument, and returns a length-N 1D array. I want to pass it the data in an N-D array, and obtain the N-D array of the result. I've thought about wrapping it as such: #python code: from my_module import f # takes a 1D array, raises an exception otherwise def f_wrap(A): A_1D = A.ravel() B = f(A_1D) return B.reshape(A.shape) #end code I expect A to be contiguous in memory, but I don't know if it will be C_CONTIGUOUS or F_CONTIGUOUS. Is there a way to implement this such that 1) the data in the arrays A and B_1D are not copied (memory issues) 2) the function f is only called once (speed issues)? The above implementation appears to copy data if A is fortran-ordered. Thanks for the help -Jake ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] NumPy-Discussion Digest, Vol 38, Issue 52
I'm rapidly losing interest here. Perhaps you could supply some code implementing this new array? Why not just a class using an array that doubles the array size when an index is out of bounds and copies over the old data. That is pretty much what realloc does. As to python lists, do you have any benchmarks showing how bad python lists are compared to arrays? Chuck It sounds like all of this could be done very simply without going to C, using a class based on numpy.ndarray. The following works for 1D arrays, behaves like a regular 1D numpy array, and could be easily improved with a little care. Is this what you had in mind? import numpy #easy scalable array class class scarray: def __init__(self,*args,**kwargs): self.__data = numpy.ndarray(*args,**kwargs) def append(self,val): tmp = self.__data self.__data = numpy.ndarray(tmp.size+1) self.__data[:-1] = tmp self.__data[-1] = val del tmp def __getattr__(self,attr): return getattr(self.__data,attr) x = scarray(5) x[:] = numpy.arange(5) print x x.append(5) print x ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Accessing LAPACK and BLAS from the numpy C API
The safe way to access them, since they are not exposed, is to call the function at the python level in your C code, but I don't think that's what you want, I want to avoid calling functions at the python level, because of the overhead for multiple calls within nested loops. I may have a solution: I've managed to get access to the BLAS fortran library by using in the setup.py file: from distutils.core import Extension from numpy.distutils import system_info myextension = Extension(... library_dirs = system_info.blas_opt_info().get_lib_dirs(), ...) Then in my C++ code I can declare, e.g. extern C double ddot_(const int *N, const double *DX, const int *INCX, const double *DY, const int *INCY); ...and directly call the BLAS fortran library. This pattern works on my system (linux, using the system BLAS/LAPACK libraries). Is this a form that will work across different OSs and different installs? -Jake ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] Accessing LAPACK and BLAS from the numpy C API
Hello, I'm working on wrapping a set of C++ routines for manifold learning (LLE, Isomap, LTSA, etc) in python. In the LLE routine, it is necessary to loop through the input points and perform an svd of each local covariance matrix. Presently I have my own C-LAPACK wrapper that I call within a C loop, but this seems non-ideal because numpy already wraps LAPACK/ATLAS for linear algebra. Does anybody know a way to directly access the numpy.linalg routines from a C extension, without the overhead of a python callback? Thanks for the help. -Jake ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion