Popular Python recipes tagged "matrix"http://code.activestate.com/recipes/langs/python/tags/matrix/2012-08-07T08:45:23-07:00ActiveState Code RecipesA simple Matrix class (Python) 2012-05-14T13:34:31-07:00Anand B Pillaihttp://code.activestate.com/recipes/users/4169530/http://code.activestate.com/recipes/578131-a-simple-matrix-class/ <p style="color: grey"> Python recipe 578131 by <a href="/recipes/users/4169530/">Anand B Pillai</a> (<a href="/recipes/tags/algebra/">algebra</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/matrix/">matrix</a>). Revision 3. </p> <p>A simple class in Python representing a Matrix with basic operations, operator overloading and class factory methods to make Matrices from different sources.</p> Generate the parity or sign of a permutation (Python) 2012-07-28T07:21:49-07:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/578227-generate-the-parity-or-sign-of-a-permutation/ <p style="color: grey"> Python recipe 578227 by <a href="/recipes/users/398009/">Paddy McCarthy</a> (<a href="/recipes/tags/determinants/">determinants</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/matrix/">matrix</a>, <a href="/recipes/tags/permutations/">permutations</a>). </p> <p>The <a href="http://en.wikipedia.org/wiki/Parity_of_a_permutation">parity</a> of a given permutation is whether an odd or even number of swaps between any two elements are needed to transform the given permutation to the first permutation.</p> <p>The sign of the permutation is +1 for an even parity and -1 for an odd parity.</p> <p>This python function returns the sign of a permutation of all the integers 0..N. When the program is run it shows the sign of permutations as generated by the standard function itertools.permutations.</p> <p>The function uses a modified <a href="http://rosettacode.org/wiki/Sorting_algorithms/Selection_sort#Python">selection sort</a> to compute the parity.</p> Alternatve generation of the parity or sign of a permutation (Python) 2012-08-07T08:45:23-07:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/578236-alternatve-generation-of-the-parity-or-sign-of-a-p/ <p style="color: grey"> Python recipe 578236 by <a href="/recipes/users/398009/">Paddy McCarthy</a> (<a href="/recipes/tags/determinants/">determinants</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/matrix/">matrix</a>, <a href="/recipes/tags/permutations/">permutations</a>). </p> <p>Although <a href="http://code.activestate.com/recipes/578227/">Recipe 578227</a> was straight-forward to derive, I came across <a href="http://stackoverflow.com/questions/337664/counting-inversions-in-an-array/6424847#6424847">this Stack Overflow answer</a> that gives another method for generating the sign based on comparing the perm and the sorted perm.</p> Determinant of matrix of any order (Python) 2012-04-24T10:49:00-07:00Sachin Joglekarhttp://code.activestate.com/recipes/users/4181845/http://code.activestate.com/recipes/578108-determinant-of-matrix-of-any-order/ <p style="color: grey"> Python recipe 578108 by <a href="/recipes/users/4181845/">Sachin Joglekar</a> (<a href="/recipes/tags/algebra/">algebra</a>, <a href="/recipes/tags/determinant/">determinant</a>, <a href="/recipes/tags/matrix/">matrix</a>, <a href="/recipes/tags/order/">order</a>). </p> <p>A small snipet of code to find the determinant of a mtrix of any order.Input must be a list like [[1,2,3],[4,5,6],[7,8,9]] (for a matrix of order 3). Works fine.</p> A matrix generated by shifting the zeroth row (Python) 2012-06-05T12:38:36-07:00Andrej T.http://code.activestate.com/recipes/users/4182297/http://code.activestate.com/recipes/578149-a-matrix-generated-by-shifting-the-zeroth-row/ <p style="color: grey"> Python recipe 578149 by <a href="/recipes/users/4182297/">Andrej T.</a> (<a href="/recipes/tags/matrix/">matrix</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/shift/">shift</a>). Revision 7. </p> <p>ShiftMatrix is a class that generates matrix values by shifting the zeroth row left or right length_of_row times. For example: if you initialize it with sm = ShiftMatrix([1,2,3]), sm[1] will return [3,1,2]. Only the initial list is stored, the other values are generated at runtime.</p> Infix operators for numpy arrays (Python) 2010-06-07T05:57:07-07:00John Schulmanhttp://code.activestate.com/recipes/users/4171677/http://code.activestate.com/recipes/577201-infix-operators-for-numpy-arrays/ <p style="color: grey"> Python recipe 577201 by <a href="/recipes/users/4171677/">John Schulman</a> (<a href="/recipes/tags/array/">array</a>, <a href="/recipes/tags/infix/">infix</a>, <a href="/recipes/tags/matrix/">matrix</a>, <a href="/recipes/tags/multiplication/">multiplication</a>, <a href="/recipes/tags/numpy/">numpy</a>). Revision 3. </p> <p>This recipe adapts the infix operator trick from <a href="http://code.activestate.com/recipes/384122-infix-operators/" rel="nofollow">http://code.activestate.com/recipes/384122-infix-operators/</a> to give the appropriate behavior with numpy arrays, so you can write A *dot* B for np.dot(A,B)</p> <p>UPDATE A solution to the dot problem was recently added to the numpy trunk: the dot method was added to the ndarray class so you can write a.dot(b). See <a href="http://projects.scipy.org/numpy/ticket/1456" rel="nofollow">http://projects.scipy.org/numpy/ticket/1456</a></p>