Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left Matrix Division and Numpy Solve

I am trying to convert code that contains the \ operator from Matlab (Octave) to Python. Sample code

B = [2;4]
b = [4;4]
B \ b

This works and produces 1.2 as an answer. Using this web page

http://mathesaurus.sourceforge.net/matlab-numpy.html

I translated that as:

import numpy as np
import numpy.linalg as lin
B = np.array([[2],[4]])
b = np.array([[4],[4]])
print lin.solve(B,b)

This gave me an error:

numpy.linalg.linalg.LinAlgError: Array must be square

How come Matlab \ works with non square matrix for B?

Any solutions for this?

like image 830
BBSysDyn Avatar asked Aug 23 '11 11:08

BBSysDyn


People also ask

How do you divide a matrix in NumPy?

divide(arr1, arr2, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : Array element from first array is divided by elements from second element (all happens element-wise). Both arr1 and arr2 must have same shape and element in arr2 must not be zero; otherwise it will raise an error.

What is left matrix division?

Left division is used to solve the matrix equation AX=B . In this equation X and B. are column vectors. This equation can be solved by multiplying, on the left, both.

How do you combine two matrices with NumPy?

You can use the numpy. concatenate() function to concat, merge, or join a sequence of two or multiple arrays into a single NumPy array. Concatenation refers to putting the contents of two or more arrays in a single array.

How do I divide every element in a NumPy array?

How do you divide every element in a NumPy array? Dividing a NumPy array by a constant is as easy as dividing two numbers. To divide each and every element of an array by a constant, use division arithmetic operator / . Pass array and constant as operands to the division operator as shown below.


2 Answers

From MathWorks documentation for left matrix division:

If A is an m-by-n matrix with m ~= n and B is a column vector with m components, or a matrix with several such columns, then X = A\B is the solution in the least squares sense to the under- or overdetermined system of equations AX = B. In other words, X minimizes norm(A*X - B), the length of the vector AX - B.

The equivalent in numpy is np.linalg.lstsq:

In [15]: B = np.array([[2],[4]])

In [16]: b = np.array([[4],[4]])

In [18]: x,resid,rank,s = np.linalg.lstsq(B,b)

In [19]: x
Out[19]: array([[ 1.2]])
like image 135
unutbu Avatar answered Sep 17 '22 03:09

unutbu


Matlab will actually do a number of different operations when the \ operator is used, depending on the shape of the matrices involved (see here for more details). In you example, Matlab is returning a least squares solution, rather than solving the linear equation directly, as would happen with a square matrix. To get the same behaviour in numpy, do this:

import numpy as np
import numpy.linalg as lin
B = np.array([[2],[4]])
b = np.array([[4],[4]])
print np.linalg.lstsq(B,b)[0]

which should give you the same solution as Matlab.

like image 38
talonmies Avatar answered Sep 18 '22 03:09

talonmies