From 4863c26c1d4705a5870984ca3a6ba5329cba0400 Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Tue, 11 Feb 2020 11:35:29 +0500 Subject: [PATCH 1/6] Create RayleighQuotient.py https://en.wikipedia.org/wiki/Rayleigh_quotient --- linear_algebra/src/RayleighQuotient.py | 70 ++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 linear_algebra/src/RayleighQuotient.py diff --git a/linear_algebra/src/RayleighQuotient.py b/linear_algebra/src/RayleighQuotient.py new file mode 100644 index 000000000000..6df7836dc3f8 --- /dev/null +++ b/linear_algebra/src/RayleighQuotient.py @@ -0,0 +1,70 @@ +''' +https://en.wikipedia.org/wiki/Rayleigh_quotient +''' +import numpy as np + + +def isHermitian(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]]) + >>> isHermitian(A) + True + ''' + 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.]]) + ''' + 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 isHermitian(A) == True + print( rayleigh_quotient(A, v)) + + A = np.matrix([ + [1, 2, 4], + [2, 3, -1], + [4, -1, 1] + ]) + assert isHermitian(A) == True + assert rayleigh_quotient(A, v) == float(3) + +if __name__=='__main__': + import doctest + doctest.testmod() + tests() From da15729827ab24648719095b8dc5f576a9d6972e Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Tue, 11 Feb 2020 11:41:24 +0500 Subject: [PATCH 2/6] Update RayleighQuotient.py --- linear_algebra/src/RayleighQuotient.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/linear_algebra/src/RayleighQuotient.py b/linear_algebra/src/RayleighQuotient.py index 6df7836dc3f8..a34c767c38c5 100644 --- a/linear_algebra/src/RayleighQuotient.py +++ b/linear_algebra/src/RayleighQuotient.py @@ -4,7 +4,7 @@ import numpy as np -def isHermitian(matrix) -> bool: +def is_hermitian(matrix) -> bool: ''' Checks if a matrix is Hermitian. @@ -13,7 +13,7 @@ def isHermitian(matrix) -> bool: ... [2, 2+1j, 4], ... [2-1j, 3, 1j], ... [4, -1j, 1]]) - >>> isHermitian(A) + >>> is_hermitian(A) True ''' return np.array_equal(matrix, matrix.H) @@ -53,7 +53,7 @@ def tests() -> None: [3] ]) - assert isHermitian(A) == True + assert is_hermitian(A) == True print( rayleigh_quotient(A, v)) A = np.matrix([ @@ -61,7 +61,7 @@ def tests() -> None: [2, 3, -1], [4, -1, 1] ]) - assert isHermitian(A) == True + assert is_hermitian(A) == True assert rayleigh_quotient(A, v) == float(3) if __name__=='__main__': From 786079ef93af7415ae742a6d647da0f219d32336 Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Tue, 11 Feb 2020 11:51:48 +0500 Subject: [PATCH 3/6] Update and rename RayleighQuotient.py to rayleigh_quotient.py --- ...yleighQuotient.py => rayleigh_quotient.py} | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 deletions(-) rename linear_algebra/src/{RayleighQuotient.py => rayleigh_quotient.py} (67%) diff --git a/linear_algebra/src/RayleighQuotient.py b/linear_algebra/src/rayleigh_quotient.py similarity index 67% rename from linear_algebra/src/RayleighQuotient.py rename to linear_algebra/src/rayleigh_quotient.py index a34c767c38c5..434d1a55ff5f 100644 --- a/linear_algebra/src/RayleighQuotient.py +++ b/linear_algebra/src/rayleigh_quotient.py @@ -1,11 +1,11 @@ -''' +""" 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 @@ -15,11 +15,18 @@ def is_hermitian(matrix) -> bool: ... [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 @@ -35,36 +42,26 @@ def rayleigh_quotient(A, v) -> float: ... ]) >>> rayleigh_quotient(A, v) matrix([[3.]]) - ''' + """ v_star = v.H - return (v_star*A*v)/(v_star*v) + 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] - ]) + A = np.matrix([[2, 2 + 1j, 4], [2 - 1j, 3, 1j], [4, -1j, 1]]) - v = np.matrix([ - [1], - [2], - [3] - ]) + v = np.matrix([[1], [2], [3]]) assert is_hermitian(A) == True - print( rayleigh_quotient(A, v)) + print(rayleigh_quotient(A, v)) - A = np.matrix([ - [1, 2, 4], - [2, 3, -1], - [4, -1, 1] - ]) + 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__': + +if __name__ == "__main__": import doctest + doctest.testmod() tests() From f80b4b2ce112d783987f182c48a525625ace10a9 Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Tue, 11 Feb 2020 11:58:58 +0500 Subject: [PATCH 4/6] Update rayleigh_quotient.py --- linear_algebra/src/rayleigh_quotient.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/linear_algebra/src/rayleigh_quotient.py b/linear_algebra/src/rayleigh_quotient.py index 434d1a55ff5f..b00536178f20 100644 --- a/linear_algebra/src/rayleigh_quotient.py +++ b/linear_algebra/src/rayleigh_quotient.py @@ -49,9 +49,7 @@ def rayleigh_quotient(A, v) -> float: 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 print(rayleigh_quotient(A, v)) From 438dc6b8dbfb5a04d0de7ecbc3705f42b0cb4e05 Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Tue, 11 Feb 2020 12:08:57 +0500 Subject: [PATCH 5/6] Update rayleigh_quotient.py python/black --- linear_algebra/src/rayleigh_quotient.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/linear_algebra/src/rayleigh_quotient.py b/linear_algebra/src/rayleigh_quotient.py index b00536178f20..b98364bdcf02 100644 --- a/linear_algebra/src/rayleigh_quotient.py +++ b/linear_algebra/src/rayleigh_quotient.py @@ -50,11 +50,11 @@ def rayleigh_quotient(A, v) -> float: 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 + assert is_hermitian(A) , True print(rayleigh_quotient(A, v)) A = np.matrix([[1, 2, 4], [2, 3, -1], [4, -1, 1]]) - assert is_hermitian(A) == True + assert is_hermitian(A) , True assert rayleigh_quotient(A, v) == float(3) From be2f1126c03a808beebfa1ffcd5c503e62d2e430 Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Tue, 11 Feb 2020 12:38:41 +0500 Subject: [PATCH 6/6] Update rayleigh_quotient.py --- linear_algebra/src/rayleigh_quotient.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/linear_algebra/src/rayleigh_quotient.py b/linear_algebra/src/rayleigh_quotient.py index b98364bdcf02..46551749febd 100644 --- a/linear_algebra/src/rayleigh_quotient.py +++ b/linear_algebra/src/rayleigh_quotient.py @@ -4,7 +4,7 @@ import numpy as np -def is_hermitian(matrix) -> bool: +def is_hermitian(matrix:np.matrix) -> bool: """ Checks if a matrix is Hermitian. @@ -25,7 +25,7 @@ def is_hermitian(matrix) -> bool: return np.array_equal(matrix, matrix.H) -def rayleigh_quotient(A, v) -> float: +def rayleigh_quotient(A:np.matrix, v:np.matrix) -> float: """ Returns the Rayleigh quotient of a Hermitian matrix A and vector v. @@ -50,11 +50,11 @@ def rayleigh_quotient(A, v) -> float: 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 + 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]]) - assert is_hermitian(A) , True + assert is_hermitian(A), f"{A} is not hermitian." assert rayleigh_quotient(A, v) == float(3)