How To Solve Linear Equations Using Sympy In Python
Sympy is a great library for symbolic mathematics.
import sympy as sp
from sympy import *
Before delve deeper in to solving linear equations, let us see how we can print actual mathematical symbols easily using Sympy.
Pretty print in ipython notebook
init_printing()
var('x y z a')
Let us print Integration symbol.
Integral(sqrt(1/x))
Solve Linear Equations Using Sympy
Let us solve a simple linear equation, x^2-a=0
Let us solve for x.
solve(x**2 - a, x)
Let us do an another one, x^2 - 4a - 4 = 0. Let us solve for x.
solve(x**2 - 4*a - 4, x)
Let us solve the above equation for "a" now.
solve(x**2 - 4*a - 4, a)
Solve System Of Linear Equations
Let us solve following two equations...
x + 5*y - 2 = 0
-3x + 6y - 15 = 0
solve((x + 5*y - 2, -3*x + 6*y - 15), x, y)
Note the syntax x,y above, since we are solving for both x and y.
Solve Linear Equations Using linsolve
Sympy has another library which is called livsolve which can be used to solve the linear equations.
from sympy.solvers.solveset import linsolve
Let us solve below equations again using linsolve.
x + 5*y - 2 = 0
-3x + 6y - 15 = 0
x, y = symbols('x, y')
linsolve([x + 5*y + -2, -3*x + 6*y - 15], (x, y))
Related Notebooks
- How To Solve Error Numpy Has No Attribute Float In Python
- Regularization Techniques in Linear Regression With Python
- How to Visualize Data Using Python - Matplotlib
- How To Read CSV File Using Python PySpark
- How To Read JSON Data Using Python Pandas
- How to Create DataFrame in R Using Examples
- How To Analyze Wikipedia Data Tables Using Python Pandas
- How To Plot Unix Directory Structure Using Python Graphviz
- How to do SQL Select and Where Using Python Pandas