-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Create RayleighQuotient.py #1749
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
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4863c26
Create RayleighQuotient.py
QuantumNovice da15729
Update RayleighQuotient.py
QuantumNovice 786079e
Update and rename RayleighQuotient.py to rayleigh_quotient.py
QuantumNovice f80b4b2
Update rayleigh_quotient.py
QuantumNovice 438dc6b
Update rayleigh_quotient.py
QuantumNovice be2f112
Update rayleigh_quotient.py
QuantumNovice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
https://en.wikipedia.org/wiki/Rayleigh_quotient | ||
""" | ||
import numpy as np | ||
|
||
|
||
def is_hermitian(matrix) -> bool: | ||
""" | ||
Checks if a matrix is Hermitian. | ||
|
||
>>> import numpy as np | ||
>>> A = np.matrix([ | ||
... [2, 2+1j, 4], | ||
... [2-1j, 3, 1j], | ||
... [4, -1j, 1]]) | ||
>>> is_hermitian(A) | ||
True | ||
>>> A = np.matrix([ | ||
... [2, 2+1j, 4+1j], | ||
... [2-1j, 3, 1j], | ||
... [4, -1j, 1]]) | ||
>>> is_hermitian(A) | ||
False | ||
""" | ||
return np.array_equal(matrix, matrix.H) | ||
|
||
|
||
def rayleigh_quotient(A, v) -> float: | ||
""" | ||
Returns the Rayleigh quotient of a Hermitian matrix A and | ||
vector v. | ||
>>> import numpy as np | ||
>>> A = np.matrix([ | ||
... [1, 2, 4], | ||
... [2, 3, -1], | ||
... [4, -1, 1] | ||
... ]) | ||
>>> v = np.matrix([ | ||
... [1], | ||
... [2], | ||
... [3] | ||
... ]) | ||
>>> rayleigh_quotient(A, v) | ||
matrix([[3.]]) | ||
QuantumNovice marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
v_star = v.H | ||
return (v_star * A * v) / (v_star * v) | ||
|
||
|
||
def tests() -> None: | ||
A = np.matrix([[2, 2 + 1j, 4], [2 - 1j, 3, 1j], [4, -1j, 1]]) | ||
v = np.matrix([[1], [2], [3]]) | ||
assert is_hermitian(A) , True | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print(rayleigh_quotient(A, v)) | ||
|
||
A = np.matrix([[1, 2, 4], [2, 3, -1], [4, -1, 1]]) | ||
assert is_hermitian(A) , True | ||
assert rayleigh_quotient(A, v) == float(3) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
tests() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.