Appending a Numpy array to a list
Question: I am new to Python and even newer to Numpy so apologies if I've made a blunder
somewhere.

Essentially I am taking a list of angles (of indeterminate length) calculating
an array based on trig functions of those values, and then creating a new list
where at each index is a "trig array" corresponding to the index of the value
that created it in the angles list.

Currently the loop calculates the correct arrays and prints them one at a time
as expected however, the final output of the function is a single array rather
than a list of each array.

Any help would be greatly appreciated!

    
    
                def tmatrices(angles):
                    for angle in angles:
                        tmatrices = []
                        cos = math.cos(angle)
                        cos2 = (math.cos(angle)) ** 2)
                        sin = math.sin(angle)
                        sin2 = (math.sin(angle)) ** 2)
                        T = np.array( (((cos2), (sin2), (sin*cos)), ((sin2), (cos2), ((-sin) * cos)), ((-2 * sin * cos), (2 * sin * cos), (cos2 - sin2))) )
                        print (T)
                        tmatrices.append(T)
                    return tmatrices
    


Answer: I think you have made a bit of a blunder and

    
    
    tmatrices = []
    

should probably be outside the loop?

That said, you might prefer to use numpy more numpythonically and go for

    
    
    import numpy
    
    
    def tmatrix(angles):
        cos  = numpy.cos(angles)[numpy.newaxis, :]
        cos2 = cos**2
        sin  = numpy.sin(angles)[numpy.newaxis, :]
        sin2 = sin**2
        T = numpy.concatenate( (cos2, sin2, sin*cos, sin2, cos2, -sin * cos, -2 * sin * cos, 2 * sin * cos, cos2 - sin2 ), axis=0)
        return T
    
    
    angles = numpy.arange(0, 2*numpy.pi, numpy.pi/10)
    print tmatrix(angles)
    

which will return a matrix whose rows represent the different functions and
whose columns determined by the angles.

