Author: bugman Date: Fri Jul 12 17:12:38 2013 New Revision: 20285 URL: http://svn.gna.org/viewcvs/relax?rev=20285&view=rev Log: Added the missing mpower() function as lib.linear_algebra.matrix_power.square_matrix_power(). This is needed by the lib.dispersion.ns_2site_star module. The function comes from the 'fitting_main_kex.py' file attached to comment 3 of task #7712 (https://gna.org/task/?7712#comment3, https://gna.org/support/download.php?file_id=18263). The mpower() function was copied and modified to suite relax's coding conventions. Added: branches/relax_disp/lib/linear_algebra/matrix_power.py Modified: branches/relax_disp/lib/dispersion/ns_2site_star.py branches/relax_disp/lib/linear_algebra/__init__.py Modified: branches/relax_disp/lib/dispersion/ns_2site_star.py URL: http://svn.gna.org/viewcvs/relax/branches/relax_disp/lib/dispersion/ns_2site_star.py?rev=20285&r1=20284&r2=20285&view=diff ============================================================================== --- branches/relax_disp/lib/dispersion/ns_2site_star.py (original) +++ branches/relax_disp/lib/dispersion/ns_2site_star.py Fri Jul 12 17:12:38 2013 @@ -36,6 +36,9 @@ from numpy import conj, dot, matrix if dep_check.scipy_module: from scipy.linalg import expm + +# relax module imports. +from lib.linear_algebra.matrix_power import square_matrix_power def r2eff_ns_2site_star(r20a=None, r20b=None, fg=None, kge=None, keg=None, tcpmg=None, cpmg_frqs=None, back_calc=None, num_points=None): @@ -87,7 +90,7 @@ prop_2 = dot(dot(expm_R_tcp, expm(cR2*tcp)), expm_R_tcp) - prop_total = mpower(prop_2, cpmg_frqs[i]*tcpmg) + prop_total = square_matrix_power(prop_2, cpmg_frqs[i]*tcpmg) Moft = prop_total * M0 Modified: branches/relax_disp/lib/linear_algebra/__init__.py URL: http://svn.gna.org/viewcvs/relax/branches/relax_disp/lib/linear_algebra/__init__.py?rev=20285&r1=20284&r2=20285&view=diff ============================================================================== --- branches/relax_disp/lib/linear_algebra/__init__.py (original) +++ branches/relax_disp/lib/linear_algebra/__init__.py Fri Jul 12 17:12:38 2013 @@ -23,5 +23,6 @@ """The relax-lib NMR package - a library of functions for advanced linear algebra not present in numpy.""" __all__ = [ - 'kronecker_product' + 'kronecker_product', + 'matrix_power' ] Added: branches/relax_disp/lib/linear_algebra/matrix_power.py URL: http://svn.gna.org/viewcvs/relax/branches/relax_disp/lib/linear_algebra/matrix_power.py?rev=20285&view=auto ============================================================================== --- branches/relax_disp/lib/linear_algebra/matrix_power.py (added) +++ branches/relax_disp/lib/linear_algebra/matrix_power.py Fri Jul 12 17:12:38 2013 @@ -1,0 +1,57 @@ +############################################################################### +# # +# Copyright (C) 2013 Edward d'Auvergne # +# # +# This file is part of the program relax (http://www.nmr-relax.com). # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <http://www.gnu.org/licenses/>. # +# # +############################################################################### + + +# Python module imports. +from numpy import diag, dot, eye +from numpy.linalg import eig, inv + +# relax module imports. +from lib.errors import RelaxError + + +def square_matrix_power(x, y): + """Compute x raised to the power y when x is a square matrix and y is a scalar. + + @param x: The square matrix. + @type x: numpy rank-2 array + @param y: The power. + @type y: float + @return: The matrix power of x. + @rtype: numpy rank-2 array + """ + + # Sanity check. + s = x.shape() + if len(s) != 2 or s[0] != s[1]: + raise RelaxError("The matrix '%s' must be square." % x) + + # Catch the zeroth power. + if y == 0: + return eye(s[0]) + + # The eigensystem of x. + e, v = eig(x) + d = diag(e) + + # Return the matrix power. + return dot(dot(v, d**y), inv(v)) +