Is there a python module to solve linear equations?

You can use least square method in python to solve system of equations for example for solving equations 3x+4y=7 and 5x+6y=8

>>> import numpy
>>> a=[[3,4],[5,6]]
>>> b=[7,8]
>>> numpy.linalg.lstsq(a,b)
(array([-5. ,  5.5]), array([], dtype=float64), 2, array([ 9.27110906,  0.21572392]))

Yes, the very-popular NumPy package has a function to do this. Their example:

Solve the system of equations 3 * x0 + x1 = 9 and x0 + 2 * x1 = 8:

>>> import numpy as np
>>> a = np.array([[3,1], [1,2]])
>>> b = np.array([9,8])
>>> x = np.linalg.solve(a, b)
>>> x
array([ 2.,  3.]) 

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.linalg.solve.html


See http://sympy.org/ and http://numpy.scipy.org/.

Specifically, http://docs.scipy.org/doc/numpy/reference/routines.linalg.html

And http://docs.sympy.org/0.7.0/tutorial.html#algebra, http://docs.sympy.org/dev/modules/solvers/solvers.html

Edit: Added solvers link from the comment.

Tags:

Python