Note that there are some explanatory texts on larger screens.

plurals
  1. POMatrix View in Function Doesn't Have Side Effects
    primarykey
    data
    text
    <p><strong>Edit</strong>: I've found what the problem boils down to:</p> <p>If you run this code:</p> <pre><code>A = ones((10,4)) view = A[:,1] view.fill(7) A </code></pre> <p>or </p> <pre><code>A = ones((10,4)) view = A[:,1:3] view.fill(7) A </code></pre> <p>You'll see that the columns of A change</p> <p>If you run this:</p> <pre><code>A = ones((10,4)) view = A[:,(1,2)] view.fill(7) A </code></pre> <p>There's no side effects on A. Is this behavior on purpose or a bug?</p> <p>I have a function that calculates the amount I have to rotate certain columns of x,y points in a matrix. The function only takes one input - a matrix mat:</p> <pre><code>def rotate(mat): </code></pre> <p>In the function, I create views to make working with each section easier:</p> <pre><code>rot_mat = mat[:,(col,col+1)] </code></pre> <p>Then, I calculate a rotation angle and apply it back on the view that I had created before:</p> <pre><code>rot_mat[row,0] = cos(rot)*x - sin(rot)*y rot_mat[row,1] = sin(rot)*x + cos(rot)*y </code></pre> <p>If I perform this in the main body of my program, the changes to my rot_mat view would propagate to the original matrix mat. When I turned it into a function, the views stopped having side effects on the original matrix. What's the reasoning for this and is there any way to get around it? I should also note that it isn't changing mat within the function itself. At the end, I just try to return mat but no changes have been made.</p> <p>Full code for function:</p> <pre><code>def rotate(mat): # Get a reference shape ref_sh = 2*random.choice(range(len(filelist))) print 'Reference shape is ' print (ref_sh/2) # Create a copy of the reference point matrix ref_mat = mat.take([ref_sh,ref_sh+1],axis=1) # Calculate rotation for each set of points for col in range(len(filelist)): col = col * 2 # Account for the two point columns rot_mat = mat[:,(col,col+1)] # Numerator = sum of wi*yi - zi*xi numer = inner(ref_mat[:,0],rot_mat[:,1]) - inner(ref_mat[:,1],rot_mat[:,0]) # Denominator = sum of wi*xi + zi*yi denom = inner(ref_mat[:,0],rot_mat[:,0]) + inner(ref_mat[:,1],rot_mat[:,1]) rot = arctan(numer/denom) # Rotate the points in rot_mat. As it's a view of mat, the effects are # propagated. for row in range(num_points): x = rot_mat[row,0] y = rot_mat[row,1] rot_mat[row,0] = cos(rot)*x - sin(rot)*y rot_mat[row,1] = sin(rot)*x + cos(rot)*y return mat </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload