88from mpmath import whitw
99from scipy .special import gamma
1010
11- from rydstate .angular .utils import is_unknown
11+ from rydstate .angular .utils import is_unknown , lru_cache
1212from rydstate .metaclass_cache import CachedABCMeta
1313from rydstate .radial .numerov import _run_numerov_integration_python , run_numerov_integration
1414from rydstate .radial .radial_matrix_element import calc_radial_matrix_element_from_w_z
@@ -554,9 +554,12 @@ def calc_matrix_element(
554554 The radial matrix element in the desired unit.
555555
556556 """
557- # Ensure wavefunctions are integrated before accessing the grid
558- radial_matrix_element_au = calc_radial_matrix_element_from_w_z (
559- self .z_list , self .w_list , other .z_list , other .w_list , k_radial , integration_method
557+ ket1 , ket2 = self , other
558+ if hash (self ) > hash (other ):
559+ # we sort the kets by their hash to increase the cache hit rate of calc_matrix_element_au_cached
560+ ket1 , ket2 = other , self
561+ radial_matrix_element_au = calc_matrix_element_au_cached (
562+ ket1 , ket2 , k_radial , integration_method = integration_method
560563 )
561564
562565 if unit == "a.u." :
@@ -565,3 +568,16 @@ def calc_matrix_element(
565568 if unit is None :
566569 return radial_matrix_element
567570 return radial_matrix_element .to (unit ).magnitude
571+
572+
573+ @lru_cache (maxsize = 100_000 )
574+ def calc_matrix_element_au_cached (
575+ ket1 : RadialKet ,
576+ ket2 : RadialKet ,
577+ k_radial : int ,
578+ * ,
579+ integration_method : INTEGRATION_METHODS = "sum" ,
580+ ) -> float :
581+ return calc_radial_matrix_element_from_w_z (
582+ ket1 .z_list , ket1 .w_list , ket2 .z_list , ket2 .w_list , k_radial , integration_method
583+ )
0 commit comments