Author: bugman Date: Thu May 2 12:15:04 2013 New Revision: 19613 URL: http://svn.gna.org/viewcvs/relax?rev=19613&view=rev Log: Created the lib.mathematics relax library module. This currently contains two functions, order_of_magnitude() and round_to_next_order(). Added: trunk/lib/mathematics.py Modified: trunk/lib/__init__.py Modified: trunk/lib/__init__.py URL: http://svn.gna.org/viewcvs/relax/trunk/lib/__init__.py?rev=19613&r1=19612&r2=19613&view=diff ============================================================================== --- trunk/lib/__init__.py (original) +++ trunk/lib/__init__.py Thu May 2 12:15:04 2013 @@ -38,6 +38,7 @@ 'io', 'linear_algebra', 'list', + 'mathematics', 'model_selection', 'order', 'physical_constants', Added: trunk/lib/mathematics.py URL: http://svn.gna.org/viewcvs/relax/trunk/lib/mathematics.py?rev=19613&view=auto ============================================================================== --- trunk/lib/mathematics.py (added) +++ trunk/lib/mathematics.py Thu May 2 12:15:04 2013 @@ -1,0 +1,55 @@ +############################################################################### +# # +# 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/>. # +# # +############################################################################### + +# Module docstring. +"""Module for basic mathematical operations.""" + +# Python module imports. +from math import floor, log10 + + +def order_of_magnitude(value): + """Determine the order of magnitude of the given number. + + @param value: The value to determine the order of magnitude of. + @type value: float or int + @return: The order of magnitude. + @rtype: int + """ + + # Calculate and return the value. + return floor(log10(x)) + + +def round_to_next_order(value): + """Round the given value up to the next order of magnitude. + + For example, the number 1,234 will be rounded up to 10,000. + + + @param value: The value to determine the order of magnitude of. + @type value: float or int + @return: The new value rounded up to the next order of magnitude. + @rtype: float + """ + + # Calculate and return the value. + return 10**(order_of_magnitude(value) + 1)