NumPy Library Advanced Tutorial (ii)

Source: Internet
Author: User

The first article is here: NumPy Library Advanced Tutorial (i) solving a linear equation group

Solving eigenvalues and eigenvectors

For an introduction to eigenvalues and eigenvectors, click here
First create a matrix

In [1]: A=mat("3 -2;1 0")In [2]: AOut[2]: matrix([[ 3, -2],        [ 1,  0]])

In the Numpy.linalg module, the Eigvals function calculates the eigenvalues of the Matrix, and the EIG function can return a tuple containing the eigenvalues and corresponding eigenvectors.

Using the Eigvals function to solve eigenvalue values

In[3]linalg.eigvals(A)Out[3]array([ 2.,  1.])

Use the EIG function to solve eigenvalues and eigenvectors. The function returns a tuple, which discharges the eigenvalues and corresponding eigenvectors, where the first column is the eigenvalues and the second column is the eigenvector.

In [5]: B=eig(A)In [6]: BOut[62.,  1.]), matrix([[ 0.89442719,  0.70710678],         [ 0.4472136 ,  0.70710678]]))

Using the DOT function to verify that the solution is correct by multiplying the matrix

eigenvalues, eigenvectors = np.linalg.eig(A)print"First tuple of eig", eigenvaluesprint"Second tuple of eig\n", eigenvectorsforin range(len(eigenvalues)):      print"Left", np.dot(A, eigenvectors[:,i])      print"Right", eigenvalues[i] * eigenvectors[:,i]      print

Output to

[[ 1.78885438] [ 0.89442719]][[ 1.78885438] [ 0.89442719]][[ 0.70710678] [ 0.70710678]][[ 0.70710678] [ 0.70710678]]
Calculation Matrix determinant
In [51]: A = np.mat("3 4;5 6")In [52print"A\n", AA[[3 4] [5 6]]

Use the DET function to calculate the determinant

In [53"Determinant", np.linalg.det(A)Determinant -2.0

NumPy Library Advanced Tutorial (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.