# [SciPy-User] Numpy/Scipy: Avoiding nested loops to operate on matrix-valued images

tyldurd dhondt.olivier@gmail....
Mon Mar 19 04:23:57 CDT 2012

```Dan,

However, this solution does not work for me.

First, it returns an array with dtype=object which is not the original type
of the data.  Besides, the values in the array are not equal to the ones
given by the 'traditional' nested loops. I think the problem comes from the
fact that ufuncs are functions that act over each element of an array, not
over slices.

I have done a lot of research on this topic but it seems it is not feasible
in terms of slicing or vectorizing. The only solution I found would be with
generalized ufuncs but from what I understand, they require to write C
code, which I would like to avoid :-)

Therefore, I am going to stick to nested loops at least for now.

Regards,

Olivier

On Thursday, March 15, 2012 9:23:49 PM UTC+1, Dan Lussier wrote:
>
> Have you tried numpy.frompyfunc?
>
> http://docs.scipy.org/doc/numpy/reference/generated/numpy.frompyfunc.html
>
> http://stackoverflow.com/questions/6126233/can-i-create-a-python-numpy-ufunc-from-an-unbound-member-method
>
> With this approach you may be able create a function which acts
> elementwise over your array to compute the matrix logarithm at each entry
> using Numpy's ufuncs.  This would avoid the explicit iteration over the
> array using the for loops.
>
> As a rough outline try:
>
> from scipy import linalg
> import numpy as np
>
> # Assume im is the container array containing a 3x3 matrix at each pixel.
>
> # Composite function so get matrix log of array A as a matrix in one step
> def log_matrix(A):
>     return linalg.logm(np.asmatrix(A))
>
>
> # Creating function to operate over container array.  Takes one argument
> and returns the result.
> log_ufunc = np.frompyfunc(log_matrix, 1, 1)
>
> # Using log_ufunc on container array, im
> res = log_ufunc(im)
>
> Dan
>
>
> On 2012-03-15, at 1:59 AM, tyldurd wrote:
>
> Hello,
>
> I am a beginner at python and numpy and I need to compute the matrix
> logarithm for each "pixel" (i.e. x,y position) of a matrix-valued image of
> dimension MxNx3x3. 3x3 is the dimensions of the matrix at each pixel.
>
> The function I have written so far is the following:
>
> def logm_img(im):
>     from scipy import linalg
>     dimx = im.shape[0]
>     dimy = im.shape[1]
>     res = zeros_like(im)
>     for x in range(dimx):
>         for y in range(dimy):
>             res[x, y, :, :] = linalg.logm(asmatrix(im[x,y,:,:]))
>     return res
>
> Is it ok? Is there a way to avoid the two nested loops ?
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120319/2dbd037b/attachment-0001.html
```