Using numpyThe NumPy library is very helpful for solving linear systems in python. For how to install NumPy look here
Matrixes in Python
Code Block |
---|
language | py |
---|
title | Creating a matrix |
---|
|
a = np.array([[1,2],[3,4]]) |
In Python rows and columns start at 0. For example index the data at first row second column :
...
Linear algebra using NumPy
NumPy has several useful functions for linear algebra built-in. For full list look check the NumPy documentation.
Code Block |
---|
language | py |
---|
title | Solve() |
---|
collapse | true |
---|
|
np.linalg.solve(a,b) #where a and b is matrixs
#returns a array with solutions to the system |
Code Block |
---|
language | py |
---|
title | Eigenvalues and eigenvectors |
---|
collapse | true |
---|
|
np.linalg.eig(a) # where a is a square matrix
#returns two arrays [v,w]
#v containing the eigenvalues of a
#w containing the eigenvectors of a |
'
Code Block |
---|
language | py |
---|
title | Determinant |
---|
collapse | true |
---|
|
np.linalg.det(a) # where a is a square matrix
#returns the determinant of the matrix a |
Example
Code Block |
---|
language | py |
---|
title | Solving set of equations |
---|
|
import numpy as np
# Solving following system of linear equation
# 5a + 2b = 35
# 1a + 4b = 49
a = np.array([[5, 2],[1,4]]) # Lefthand-side of the equation
b = np.array([35, 94]) #Righthand-side
print(np.linalg.solve(a,b)) #Printing the solution |
Eigenvalues and Eigenvectors
...
Relaterte artikler
Content by Label |
---|
showLabels | false |
---|
max | 5 |
---|
spaces | imtsoftware |
---|
showSpace | false |
---|
sort | modified |
---|
reverse | true |
---|
type | page |
---|
cql | label = "python" and type = "page" and space = "imtsoftware" |
---|
labels | python |
---|
|
...