-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[Term Entry] Python - NumPy Linear-algebra: eig() #6416 #6540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @dancikmad, thank you for contributing to Codecademy Docs, the entry is nicely written! 😄
I've suggested a few changes, could you please review and modify those at your earliest convenience? Thank you! 😃
|
||
**Parameters:** | ||
|
||
- `a`: array_like of shape (M, M). A square matrix whose eigenvalues and eigenvectors will be computed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- `a`: array_like of shape (M, M). A square matrix whose eigenvalues and eigenvectors will be computed. | |
- `a`: A square matrix of shape (M, M) used as input to compute its eigenvalues and corresponding right eigenvectors. |
- `w`: ndarray of shape (M,). The eigenvalues of the matrix. | ||
- `v`: ndarray of shape (M, M). The eigenvectors of the matrix. The column `v[:, i]` corresponds to the eigenvalue `w[i]`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- `w`: ndarray of shape (M,). The eigenvalues of the matrix. | |
- `v`: ndarray of shape (M, M). The eigenvectors of the matrix. The column `v[:, i]` corresponds to the eigenvalue `w[i]`. | |
- `w`: An ndarray of shape (M,), containing the eigenvalues of the matrix. | |
- `v`: An ndarray of shape (M, M), containing the eigenvectors of the matrix, where each column `v[:, i]` corresponds to the eigenvalue `w[i]`. |
Are they equal? True | ||
``` | ||
|
||
This example demonstrates how the `eig()` function handles matrices with complex eigenvalues. The rotation matrix B has eigenvalues 1j and -1j, illustrating how complex eigenvalues often indicate rotational transformations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example demonstrates how the `eig()` function handles matrices with complex eigenvalues. The rotation matrix B has eigenvalues 1j and -1j, illustrating how complex eigenvalues often indicate rotational transformations. | |
This example demonstrates how the `eig()` function handles matrices with complex eigenvalues. The rotation matrix B has eigenvalues `1j` and `-1j`, illustrating how complex eigenvalues often indicate rotational transformations. |
## Codebyte Example: Using Eigendecomposition for System Analysis | ||
|
||
This example uses eigenvalues to analyze a dynamical system: | ||
|
||
```codebyte/python | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
# Define a system matrix (represents a linear dynamical system) | ||
A = np.array([[0.5, 0.1], | ||
[0.2, 0.8]]) | ||
|
||
# Compute eigenvalues and eigenvectors | ||
eigenvalues, eigenvectors = np.linalg.eig(A) | ||
|
||
print("System matrix:") | ||
print(A) | ||
print("\nEigenvalues:", eigenvalues) | ||
|
||
# Check system stability (all eigenvalues must have magnitude < 1) | ||
stable = np.all(np.abs(eigenvalues) < 1) | ||
print("\nIs the system stable?", stable) | ||
|
||
# Simulate the system evolution | ||
x0 = np.array([1, 0]) # Initial state | ||
steps = 20 | ||
states = [x0] | ||
|
||
for i in range(steps): | ||
x0 = A @ x0 | ||
states.append(x0) | ||
|
||
states = np.array(states) | ||
|
||
# Plot the system trajectory | ||
plt.figure(figsize=(10, 5)) | ||
plt.plot(range(steps+1), states[:, 0], 'b-', label='x1') | ||
plt.plot(range(steps+1), states[:, 1], 'r-', label='x2') | ||
plt.xlabel('Time Step') | ||
plt.ylabel('State Value') | ||
plt.title('System Evolution') | ||
plt.legend() | ||
plt.grid(True) | ||
plt.tight_layout() | ||
plt.show() | ||
|
||
print("\nFinal state after", steps, "steps:", states[-1]) | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this section can be removed as we have 3 examples above
print("Are they equal?", np.allclose(np.dot(A, eigenvectors[:, 0]), | ||
eigenvalues[0] * eigenvectors[:, 0])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
print("Are they equal?", np.allclose(np.dot(A, eigenvectors[:, 0]), | |
eigenvalues[0] * eigenvectors[:, 0])) | |
print("Are they equal?", np.allclose(np.dot(A, eigenvectors[:, 0]), eigenvalues[0] * eigenvectors[:, 0])) |
print("Are they equal?", np.allclose(np.dot(B, eigenvectors[:, 0]), | ||
eigenvalues[0] * eigenvectors[:, 0])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
print("Are they equal?", np.allclose(np.dot(B, eigenvectors[:, 0]), | |
eigenvalues[0] * eigenvectors[:, 0])) | |
print("Are they equal?", np.allclose(np.dot(B, eigenvectors[:, 0]), eigenvalues[0] * eigenvectors[:, 0])) |
Are they equal? True | ||
``` | ||
|
||
This example demonstrates how the `eig()` function handles matrices with complex eigenvalues. The rotation matrix B has eigenvalues 1j and -1j, illustrating how complex eigenvalues often indicate rotational transformations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example demonstrates how the `eig()` function handles matrices with complex eigenvalues. The rotation matrix B has eigenvalues 1j and -1j, illustrating how complex eigenvalues often indicate rotational transformations. | |
This example demonstrates how the `eig()` function handles matrices with complex eigenvalues. The rotation matrix B has eigenvalues `1j` and `-1j`, illustrating how complex eigenvalues often indicate rotational transformations. |
@mamtawardhani thank you for your feedback! I will work on this changes today |
* Make changes after first PR review * Delete codebyte/example - 3 examples already provided * refactor print statements in ```shell:tag``` codes
@mamtawardhani hi :) i trust you are doing well |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good for a second round of review! 🚀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The entry looks good to be merged, @dancikmad!
👋 @dancikmad 🎉 Your contribution(s) can be seen here: https://www.codecademy.com/resources/docs/numpy/linear-algebra/eig Please note it may take a little while for changes to become visible. |
Description
Issue Solved
Type of Change
Checklist
main
branch.Issues Solved
section.