Understanding numpy array operations
Hello, I am a novice python/numpy user, using the language for computational physical science research. I have been modifying scripts developed by another student who worked on the project before me. However, I require a good understanding of what is actually happening in the code for when I go back to modify them. I was wondering if anyone could tell me what the bit of code does: n = 5 A = reshape(zeros(5*3*n), (n*n,3))*1.0 for j in range(n): for i in range(n): A[i+n*j] = i*u + j*v I understand there are some matrix operations happening but I cannot tell what they do. I would appreciate any help you could offer. Thanks, BT
OK, here's a shot: n=5 # Pretty obvious, set n=5 A= reshape(zeros(5*3*n), (n*n,3))*1.0 # Start from the inside out. zeros(5*3*n) give you a 1D array # (not a matrix) of zeros 0.0 that has 75 0.0's. The reshape changes # it to an array that is 25 rows by 3 columns (a 2D array). It is all # multipled by 1.0 (which doesn't really change things in this case) # and given the name A as a reference for j in range(n): for i in range(n): A[i+n*j] = i*u + j*v # This is a double loop, but I suspect there is something wrong or missing # What are u and v? The indexing on A is for returning an entire row, but # it will go beyond the bounds of A. I can't figure this one out. Sorry. -- Lou Pecora, my views are my own. --- On Tue, 11/4/08, Evan Wilson <bandtheory@rocketmail.com> wrote:
From: Evan Wilson <bandtheory@rocketmail.com> Subject: [SciPy-user] Understanding numpy array operations To: scipy-user@scipy.org Date: Tuesday, November 4, 2008, 3:59 PM Hello,
I am a novice python/numpy user, using the language for computational physical science research. I have been modifying scripts developed by another student who worked on the project before me. However, I require a good understanding of what is actually happening in the code for when I go back to modify them. I was wondering if anyone could tell me what the bit of code does:
n = 5 A = reshape(zeros(5*3*n), (n*n,3))*1.0 for j in range(n): for i in range(n): A[i+n*j] = i*u + j*v
I understand there are some matrix operations happening but I cannot tell what they do. I would appreciate any help you could offer.
Thanks,
BT
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
Tue, 04 Nov 2008 13:39:36 -0800, Lou Pecora wrote: [clip]
for j in range(n): for i in range(n): A[i+n*j] = i*u + j*v
# This is a double loop, but I suspect there is something wrong or # missing What are u and v? The indexing on A is for returning an # entire row, but it will go beyond the bounds of A. I can't figure # this one out. Sorry.
It actually won't go out-of-bounds, the loops go through i=0, 1, ..., n-1 and same for j -- which is in the range >= 0 and < n*n. If 'u' and 'v' are vectors of length 3, the row "(i+n*j)" will after the loops contain the vector "i*u + j*v". -- Pauli Virtanen
participants (3)
-
Evan Wilson
-
Lou Pecora
-
Pauli Virtanen