Why does numpy.linalg.solve() offer more precise matrix inversions than numpy.linalg.inv()?

np.linalg.solve(A, b) does not compute the inverse of A. Instead it calls one of the gesv LAPACK routines, which first factorizes A using LU decomposition, then solves for x using forward and backward substitution (see here). np.linalg.inv uses the same method to compute the inverse of A by solving for A-1 in A·A-1 = I … Read more

Convert matrix to 3-column table (‘reverse pivot’, ‘unpivot’, ‘flatten’, ‘normalize’)

To “reverse pivot”, “unpivot” or “flatten”: For Excel 2003: Activate any cell in your summary table and choose Data – PivotTable and PivotChart Report: For later versions access the Wizard with Alt+D, P. For Excel for Mac 2011, it’s ⌘+Alt+P (See here). Select Multiple consolidation ranges and click Next. In “Step 2a of 3”, choose … Read more

Flip a Bitmap image horizontally or vertically

Given cx,cy is the centre of the image: Flip in x: matrix.postScale(-1, 1, cx, cy); Flip in y: matrix.postScale(1, -1, cx, cy); Altogether: public static Bitmap createFlippedBitmap(Bitmap source, boolean xFlip, boolean yFlip) { Matrix matrix = new Matrix(); matrix.postScale(xFlip ? -1 : 1, yFlip ? -1 : 1, source.getWidth() / 2f, source.getHeight() / 2f); return … Read more

Matrix inversion without Numpy

Here is a more elegant and scalable solution, imo. It’ll work for any nxn matrix and you may find use for the other methods. Note that getMatrixInverse(m) takes in an array of arrays as input. Please feel free to ask any questions. def transposeMatrix(m): return map(list,zip(*m)) def getMatrixMinor(m,i,j): return [row[:j] + row[j+1:] for row in … Read more

Accessing a matrix element in the “Mat” object (not the CvMat object) in OpenCV C++

On the documentation: http://docs.opencv.org/2.4/modules/core/doc/basic_structures.html#mat It says: (…) if you know the matrix element type, e.g. it is float, then you can use at<>() method That is, you can use: Mat M(100, 100, CV_64F); cout << M.at<double>(0,0); Maybe it is easier to use the Mat_ class. It is a template wrapper for Mat. Mat_ has the … Read more

Array of Matrices in MATLAB

Use cell arrays. This has an advantage over 3D arrays in that it does not require a contiguous memory space to store all the matrices. In fact, each matrix can be stored in a different space in memory, which will save you from Out-of-Memory errors if your free memory is fragmented. Here is a sample … Read more

Generating Symmetric Matrices in Numpy

You could just do something like: import numpy as np N = 100 b = np.random.random_integers(-2000,2000,size=(N,N)) b_symm = (b + b.T)/2 Where you can choose from whatever distribution you want in the np.random or equivalent scipy module. Update: If you are trying to build graph-like structures, definitely check out the networkx package: http://networkx.lanl.gov which has … Read more