title: Python - NumPy - tuples as elements of an array
body: <p>I'm a CS major in university working on a programming project for my Calc III course involving singular-value decomposition. The idea is basically to convert an image of m x n dimensions into an m x n matrix wherein each element is a tuple representing the color channels (r, g, b) of the pixel at point (m, n). I'm using Python because it's the only language I've really been (well-)taught so far.</p>
  
  <p>From what I can tell, Python generally doesn't like tuples as elements of an array. I did a little research of my own and found a workaround, namely, pre-allocating the array as follows:</p>
  
  <pre><code>def image_to_array(): #converts an image to an array  
      aPic = loadPicture("zorak_color.gif")  
      ph = getHeight(aPic)  
      pw = getWidth(aPic)  
      anArray = zeros((ph,pw), dtype='O')  
      for h in range(ph):  
           for w in range(pw):             
              p = getPixel(aPic, w, h)  
              anArray[h][w] = (getRGB(p))  
      return anArray
  </code></pre>
  
  <p>This worked correctly for the first part of the assignment, which was simply to convert an image to a matrix (no linear algebra involved).</p>
  
  <p>The part with SVD, though, is where it gets trickier. When I call the built-in numPy svd function, using the array I built from my image (where each element is a tuple), I get the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;pyshell#5&gt;", line 1, in -toplevel-
      svd(x)
    File "C:\Python24\Lib\site-packages\numpy\linalg\linalg.py", line 724, in svd
      a = _fastCopyAndTranspose(t, a)
    File "C:\Python24\Lib\site-packages\numpy\linalg\linalg.py", line 107, in _fastCopyAndTranspose
      cast_arrays = cast_arrays + (_fastCT(a.astype(type)),)
  ValueError: setting an array element with a sequence.
  </code></pre>
  
  <p>This is the same error I was getting initially, before I did some research and found that I could pre-allocate my arrays to allow tuples as elements.</p>
  
  <p>The issue now is that I am only in my first semester of (college-level) programming, and these numPy functions written by and for professional programmers are a little too black-box for me (though I'm sure they're much clearer to those with experience). So editing these functions to allow for tuples is a bit more complicated than when I did it on my own function. Where do I need to go from here? I assume I should copy the relevant numPy functions into my own program, and modify them accordingly?</p>
  
  <p>Thanks in advance.</p>