SymPy is a great Python library for symbolic computation. It can be used e.g. for solving systems of linear equations. So if you are manually solving problems from a linear algebra book, and you want to verify your solutions, you can check them against the solutions that SymPy provides. You just need to know how to code the linear system for SymPy.
Here is an example which illustrates this very well.
import sympy as sp
x, y, z, t = sp.symbols(['x', 'y', 'z', 't'])
solution = solve(
[
2 * x + 3 * y - 5*z + 1 * t - 2 ,
2 * x + 3 * y - 1*z + 3 * t - 8 ,
6 * x + 9 * y - 7*z + 7 * t - 18 ,
4 * x + 6 * y - 12*z + 1 * t - 1
]
,
[x,y,z,t]
)
print(solution)
The code above solves the following system.
$$\begin{bmatrix}2 & 3 & -5 & 1 \\2 & 3 & -1 & 3 \\6 & 9 & -7 & 7 \\4 & 6 & -12 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ z \\ t \end{bmatrix} = \begin{bmatrix} 2 \\ 8 \\ 18 \\ 1 \end{bmatrix} $$
The solution which SymPy provides is this one
{z: 3/2 - t/2, x: -7*t/4 - 3*y/2 + 19/4}
The form of this solution implies that $y$ and $t$ are free variables, and can thus be given any values (i.e. they can be taken as free parameters e.g. $y=a, t=b$). The system is then solved (in terms of $a,b$) for the leading variables which happen to be $x$ and $z$.
No comments:
Post a Comment