MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / rayleigh_quotient

Function rayleigh_quotient

linear_algebra/src/rayleigh_quotient.py:30–51  ·  view source on GitHub ↗

Returns the Rayleigh quotient of a Hermitian matrix A and vector v. >>> import numpy as np >>> A = np.array([ ... [1, 2, 4], ... [2, 3, -1], ... [4, -1, 1] ... ]) >>> v = np.array([ ... [1], ... [2], ... [3] ... ]) >>> rayleigh_quotient(A,

(a: np.ndarray, v: np.ndarray)

Source from the content-addressed store, hash-verified

28
29
30def rayleigh_quotient(a: np.ndarray, v: np.ndarray) -> Any:
31 """
32 Returns the Rayleigh quotient of a Hermitian matrix A and
33 vector v.
34 >>> import numpy as np
35 >>> A = np.array([
36 ... [1, 2, 4],
37 ... [2, 3, -1],
38 ... [4, -1, 1]
39 ... ])
40 >>> v = np.array([
41 ... [1],
42 ... [2],
43 ... [3]
44 ... ])
45 >>> rayleigh_quotient(A, v)
46 array([[3.]])
47 """
48 v_star = v.conjugate().T
49 v_star_dot = v_star.dot(a)
50 assert isinstance(v_star_dot, np.ndarray)
51 return (v_star_dot.dot(v)) / (v_star.dot(v))
52
53
54def tests() -> None:

Callers 1

testsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected