Sine Distribution on a 2D Array
Hi, So I have a square 2D array, and I want to fill the array with sine values. The values need to be generated by their coordinates within the array. The center of the array should be treated as the angle 90 degrees. Each of the four edges should be 0 degrees. The corners, therefore, ought to be -sqrt(2)*90 degrees. The angle is equal to (distance_from_center/(dimension_of_array/2))*90 degrees. Then take the sine of this angle. To describe another way, if the array is treated like a height-field, a single mound of the sine wave should just fit inside the array. Right now, I'm having trouble because I don't know how to operate on an array's values based on the index of the values themselves. Help? Thanks, Ian
Am 25.07.10 06:38, schrieb Ian Mallett:
Hi,
So I have a square 2D array, and I want to fill the array with sine values. The values need to be generated by their coordinates within the array.
The center of the array should be treated as the angle 90 degrees. Each of the four edges should be 0 degrees. The corners, therefore, ought to be -sqrt(2)*90 degrees. The angle is equal to (distance_from_center/(dimension_of_array/2))*90 degrees. Then take the sine of this angle.
To describe another way, if the array is treated like a height-field, a single mound of the sine wave should just fit inside the array.
Right now, I'm having trouble because I don't know how to operate on an array's values based on the index of the values themselves.
Something like that? The mgrid thing returns two 2D arrays containing the x- and y-coordinates which are then used to calculate the height field. In [5]: import numpy as N In [6]: x,y = N.mgrid[-N.pi/2.0:N.pi/2.0:100j,-N.pi/2.0:N.pi/2.0:100j] In [7]: z = N.sin(N.sqrt(x**2+y**2)) Christian
Hi, After much deliberation, I found a passable solution: distances = np.abs(np.arange(0,resolution,1)+0.5-(resolution/2.0)) x_gradient = np.tile(distances,(resolution,1)) y_gradient = np.copy(x_gradient) y_gradient = np.swapaxes(y_gradient,0,1) distances_to_center = np.hypot(x_gradient,y_gradient) angles = np.radians( (distances_to_center/((resolution/2.0)-0.5)) * 90.0 ) lambert_weights = np.cos(angles) This seems like it could be improved. Ian PS, I meant "Sinusoidal" in the title. I carried that over into my description, which should have been "cosine", instead of "sine".
participants (2)
-
Christian K.
-
Ian Mallett