Skip to content

Changed the deprecated np.matrix to np.ndarray #1923

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 1 commit into from
May 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Changed the deprecated np.matrix to np.ndarray
  • Loading branch information
QuantumNovice authored May 1, 2020
commit 8fed10bbc42fd7119415ba3d09ad96278c63d109
27 changes: 13 additions & 14 deletions linear_algebra/src/rayleigh_quotient.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,55 @@
import numpy as np


def is_hermitian(matrix: np.matrix) -> bool:
def is_hermitian(matrix: np.array) -> bool:
"""
Checks if a matrix is Hermitian.

>>> import numpy as np
>>> A = np.matrix([
>>> A = np.array([
... [2, 2+1j, 4],
... [2-1j, 3, 1j],
... [4, -1j, 1]])
>>> is_hermitian(A)
True
>>> A = np.matrix([
>>> A = np.array([
... [2, 2+1j, 4+1j],
... [2-1j, 3, 1j],
... [4, -1j, 1]])
>>> is_hermitian(A)
False
"""
return np.array_equal(matrix, matrix.H)
return np.array_equal(matrix, matrix.conjugate().T)


def rayleigh_quotient(A: np.matrix, v: np.matrix) -> float:
def rayleigh_quotient(A: np.array, v: np.array) -> float:
"""
Returns the Rayleigh quotient of a Hermitian matrix A and
vector v.
>>> import numpy as np
>>> A = np.matrix([
>>> A = np.array([
... [1, 2, 4],
... [2, 3, -1],
... [4, -1, 1]
... ])
>>> v = np.matrix([
>>> v = np.array([
... [1],
... [2],
... [3]
... ])
>>> rayleigh_quotient(A, v)
matrix([[3.]])
array([[3.]])
"""
v_star = v.H
return (v_star * A * v) / (v_star * v)
v_star = v.conjugate().T
return (v_star.dot(A).dot(v)) / (v_star.dot(v))


def tests() -> None:
A = np.matrix([[2, 2 + 1j, 4], [2 - 1j, 3, 1j], [4, -1j, 1]])
v = np.matrix([[1], [2], [3]])
A = np.array([[2, 2 + 1j, 4], [2 - 1j, 3, 1j], [4, -1j, 1]])
v = np.array([[1], [2], [3]])
assert is_hermitian(A), f"{A} is not hermitian."
print(rayleigh_quotient(A, v))

A = np.matrix([[1, 2, 4], [2, 3, -1], [4, -1, 1]])
A = np.array([[1, 2, 4], [2, 3, -1], [4, -1, 1]])
assert is_hermitian(A), f"{A} is not hermitian."
assert rayleigh_quotient(A, v) == float(3)

Expand Down