2

I'm trying to create an n x m numpy array populated by a set of cosines like cos(v_t), cos(2 * v_t), cos(3 * v_t), ..., which I've tried to do with the following;

v_t = np.linspace(0,tmax,tsteps)
m_psi = np.zeros([tsteps,m])
for i in xrange(m):
  for j in xrange(tsteps):
    m_psi[j,i] = np.cos(v_t * k * 2 * pi/T)
    k += 1

but this returns the error

ValueError: setting an array element with a sequence.

I need every element of the array to be one of these cosines, rather than a row or a column of the array being a cosine (because I'm going to perform some kind of operation like np.dot(R,m_psi), where R is another 2D array, and the result of this multiplication must also be a matrix).

EDIT: To clarify, I'm looking for something like

[[cos(v_t),     cos(2*v_t),   ...,cos(m*v_t)],
[cos((m+1)*v_t),cos((m+2)*v_t,...,cos(2*m*t)],
[etc.]]
1
  • I want k to vary from 1 to m*steps, but that's easy to fix Commented Feb 6, 2013 at 3:19

1 Answer 1

1

EDIT Based on you comment below, this non-working code:

v_t = np.linspace(0, tmax, tsteps)
m_psi = np.array([tsteps,m])
for j in range(m):
    m_psi[:,np.cos(v_t*m*2*pi/T)]

Could be translated into working numpy as:

v_t = np.linspace(0, tmax, tsteps)
m_psi = np.empty((tsteps, m))
for j in xrange(m) :
    m_psi[:, m] = np.cos(v_t * m * 2 * np.pi / T)

You can achieve the exact same thing in a more elegant and numpythonic way using broadcasting instead of a loop :

v_t = np.linspace(0, tmax, tsteps)
m_psi = np.cos(v_t[:, None] * np.arange(m)[None, :] * 2 * np.pi / T)  

You´ll have to figure out some of the details, but something like this may be what you are after:

v_t = np.linspace(0, m * tmax, m * tsteps)
m_psi = np.cos(v_t * 2 * np.pi / T).reshape(tsteps, m)

If we leave the cosine part out:

>>> m = 4
>>> tsteps = 5
>>> np.arange(m * tsteps).reshape(tsteps, m)
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19]])

and the values in these array would be what multiply the base value inside your cosine in the code above.

Sign up to request clarification or add additional context in comments.

5 Comments

This seems to reshape the cos(v_t) array.
@DanielBlay It does reshape the cos(v_t) array, but starting from a longer v_t array to achieve what I think you are after. If it isn't I'd suggest you rewrite your question giving the mathematical expression of m_psi[i, j], because we are having a hard time figuring what you are after.
I've edited the question a little. I'm sorry that I'm finding it hard to clarify - I've been coding for all of 3 weeks, and my supervisor has asked me to write a program along these lines, but his example code didn't work. All I really know is that I'm trying to do something with a continuous signal (sum of cosines), and part of the process involves multiplying the signal vector by a random matrix R and the matrix psi, which is the cosine basis matrix (i.e. matrix with elements cos(t) to cos(nt)).
The code he provided me with is; v_t = np.linspace(0, tmax, tsteps) \n m_psi=np.array([tsteps,m]) \n for j in range(m): \n m_psi[:,np.cos(v_t*m*2*pi/T] \n
Well, this certainly works in that it doesn't return an error. I don't think it's what my supervisor wanted (he said the matrix should be orthonormal, which this is not), but clearly I don't know what he wants either, so I think this is as close to an answer as I can get.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.