linear-algebra
Row-major vs Column-major confusion
I think you are mixing up an implementation detail with usage, if you will. Let’s start with a two-dimensional array, or matrix: | 1 2 3 | | 4 5 6 | | 7 8 9 | The problem is that computer memory is a one-dimensional array of bytes. To make our discussion easier, lets … Read more
Most efficient way to solve a system of linear equations
The best way to solve a system of linear equations of the form Ax = b is to do the following. decompose A into the format A = M1 * M2 (where M1 and M2 are triangular) Solve M1 * y = b for y using back substitution Solve M2 * x = y for … Read more
Equivalent of `polyfit` for a 2D polynomial in Python
Here is an example showing how you can use numpy.linalg.lstsq for this task: import numpy as np x = np.linspace(0, 1, 20) y = np.linspace(0, 1, 20) X, Y = np.meshgrid(x, y, copy=False) Z = X**2 + Y**2 + np.random.rand(*X.shape)*0.01 X = X.flatten() Y = Y.flatten() A = np.array([X*0+1, X, Y, X**2, X**2*Y, X**2*Y**2, Y**2, … Read more
How to find linearly independent rows from a matrix
First, your 3rd row is linearly dependent with 1t and 2nd row. However, your 1st and 4th column are linearly dependent. Two methods you could use: Eigenvalue If one eigenvalue of the matrix is zero, its corresponding eigenvector is linearly dependent. The documentation eig states the returned eigenvalues are repeated according to their multiplicity and … Read more
What is SVD(singular value decomposition)
SVD can be understood from a geometric sense for square matrices as a transformation on a vector. Consider a square n x n matrix M multiplying a vector v to produce an output vector w: w = M*v The singular value decomposition M is the product of three matrices M=U*S*V, so w=U*S*V*v. U and V … Read more
Efficient dot products of large memory-mapped arrays
I’ve implemented a function for applying np.dot to blocks that are explicitly read into core memory from the memory-mapped array: import numpy as np def _block_slices(dim_size, block_size): “””Generator that yields slice objects for indexing into sequential blocks of an array along a particular axis “”” count = 0 while True: yield slice(count, count + block_size, … Read more
Efficient element-wise multiplication of a matrix and a vector in TensorFlow
The simplest code to do this relies on the broadcasting behavior of tf.multiply()*, which is based on numpy’s broadcasting behavior: x = tf.constant(5.0, shape=[5, 6]) w = tf.constant([0.0, 1.0, 2.0, 3.0, 4.0, 5.0]) xw = tf.multiply(x, w) max_in_rows = tf.reduce_max(xw, 1) sess = tf.Session() print sess.run(xw) # ==> [[0.0, 5.0, 10.0, 15.0, 20.0, 25.0], # … Read more
How to create random orthonormal matrix in python numpy
Version 0.18 of scipy has scipy.stats.ortho_group and scipy.stats.special_ortho_group. The pull request where it was added is https://github.com/scipy/scipy/pull/5622 For example, In [24]: from scipy.stats import ortho_group # Requires version 0.18 of scipy In [25]: m = ortho_group.rvs(dim=3) In [26]: m Out[26]: array([[-0.23939017, 0.58743526, -0.77305379], [ 0.81921268, -0.30515101, -0.48556508], [-0.52113619, -0.74953498, -0.40818426]]) In [27]: np.set_printoptions(suppress=True) In [28]: … Read more