Skip to content

[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

Merged
merged 7 commits into from
Apr 25, 2025

Conversation

dancikmad
Copy link
Contributor

Description

Issue Solved

Type of Change

  • Adding a new. Term Entry for Python - Numpy Linear-algebra: eig()

Checklist

  • All writings are my own.
  • My entry follows the Codecademy Docs style guide.
  • My changes generate no new warnings.
  • I have performed a self-review of my own writing and code.
  • I have checked my entry and corrected any misspellings.
  • I have made corresponding changes to the documentation if needed.
  • I have confirmed my changes are not being pushed from my forked main branch.
  • I have confirmed that I'm pushing from a new branch named after the changes I'm making.
  • I have linked any issues that are relevant to this PR in the Issues Solved section.

@mamtawardhani mamtawardhani self-assigned this Apr 13, 2025
@mamtawardhani mamtawardhani added new entry New entry or entries numpy NumPy entries status: under review Issue or PR is currently being reviewed labels Apr 13, 2025
Copy link
Collaborator

@mamtawardhani mamtawardhani left a 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `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.

Comment on lines 33 to 34
- `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]`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Comment on lines 206 to 253
## 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])
```
Copy link
Collaborator

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

Comment on lines 69 to 70
print("Are they equal?", np.allclose(np.dot(A, eigenvectors[:, 0]),
eigenvalues[0] * eigenvectors[:, 0]))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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]))

Comment on lines 121 to 122
print("Are they equal?", np.allclose(np.dot(B, eigenvectors[:, 0]),
eigenvalues[0] * eigenvectors[:, 0]))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

@dancikmad
Copy link
Contributor Author

@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
@dancikmad
Copy link
Contributor Author

@mamtawardhani hi :) i trust you are doing well
I made the changes that you requested

Copy link
Collaborator

@mamtawardhani mamtawardhani left a 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! 🚀

@Sriparno08 Sriparno08 self-assigned this Apr 20, 2025
@Sriparno08 Sriparno08 added status: under review Issue or PR is currently being reviewed and removed status: ready for next review labels Apr 20, 2025
Copy link
Collaborator

@Sriparno08 Sriparno08 left a 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!

@Sriparno08 Sriparno08 merged commit a4622a9 into Codecademy:main Apr 25, 2025
7 checks passed
Copy link

👋 @dancikmad
You have contributed to Codecademy Docs, and we would like to know more about you and your experience.
Please take a minute to fill out this four question survey to help us better understand Docs contributions and how we can improve the experience for you and our learners.
Thank you for your help!

🎉 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.
If you're appearing as anonymous and want to be credited, visit the linked accounts page and ensure that your GitHub account is linked.

@Sriparno08 Sriparno08 added status: review 2️⃣ completed and removed status: under review Issue or PR is currently being reviewed labels Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Term Entry] Python - NumPy Linear-algebra: .eig()
3 participants