schrodinger.math.mathutils module

Contains math-related utility functions

Copyright Schrodinger, LLC. All rights reserved.

schrodinger.math.mathutils.round_value(val, precision=3, significant_figures=None)

Return val as a string with the required precision or significant figures.

Either precision or significant_figures should be provided. Uses scientific notation for very large or small values, if the precision allows.

  • val (float) – The value to round

  • precision (int) – The precision needed after rounding. A precision of 2 means two decimal places should be kept after rounding. A negative precision indicates how many digits can be replaced with zeros before the decimal point. -5 means that 123456 can be shown as 1e5, -3 means it can be shown as 1.23e5.

  • significant_figures (int) – The number of significant figures that should remain after rounding. If provided, determines the rounding precision.

Return type

str or None


A string with the required precision, or None if the input is a string

schrodinger.math.mathutils.deduplicate_xy_data(x_vals, y_vals)

Remove duplicate x values by averaging the y values for them.

  • x_vals (list) – The x values

  • y_vals (list) – The y values

Return type

list, list


The deduplicated xy data

class schrodinger.math.mathutils.Interpolate1D(source_vals, target_vals, log_interp=False)

Bases: object

Creates a map between values in a source and a target axis, allowing to get the equivalent target point for each source point.

Wrapper around scipy.interpolate.interp1d to allow logarithmic interpolation or extrapolation.

__init__(source_vals, target_vals, log_interp=False)

Create an instance.

  • source_vals (tuple) – The values of points in the source range

  • target_vals (tuple) – The values of points in the target range

  • log_interp (bool) – Whether the interpolation is logarithmic. If False, linear interpolation will be used.

class schrodinger.math.mathutils.Interpolate2D(x_source_vals, x_target_vals, y_source_vals, y_target_vals, x_log_interp=False, y_log_interp=False)

Bases: object

Creates two instances of Interpolate1D to map values between two source axes and two target axes. Example use case is mapping QGraphicsScene/QChart XY coordinates to a XY coordinate system being displayed in the scene/chart.

The two axes need to be independent.

__init__(x_source_vals, x_target_vals, y_source_vals, y_target_vals, x_log_interp=False, y_log_interp=False)

Create an instance.

  • x_source_vals (tuple) – The values of points in the X source range

  • x_target_vals (tuple) – The values of points in the X target range

  • y_source_vals (tuple) – The values of points in the Y source range

  • y_target_vals (tuple) – The values of points in the Y target range

  • x_log_interp (bool) – Whether the X axis interpolation is logarithmic

  • y_log_interp (bool) – Whether the Y axis interpolation is logarithmic


Round away from zero (to +/-infinity).


inp (float or numpy.array) – value to be rounded

Return type

float or numpy.array


Rounded value

schrodinger.math.mathutils.sig_fig_round(value, significant_figure=5)
  • value (float) – change the significant figures of this value

  • significant_figure (int) – significant figure of the displayed value

Return type



str representation of a number with proper significant figures


Get significant digits of a number


number (float or int or str) – number to find significant digits

Return type



number of significant digits

schrodinger.math.mathutils.set_significant_figures(number, sig_fig)

Set significant digits of a number

  • number (float or int) – number to set significant digits

  • sig_fig (int) – number of significant digits

Return type



number with desired significant digits

schrodinger.math.mathutils.polyfit(xdata, ydata, degree)

Fit a polynomial of rang degree to data.

  • xdata (numpy.array) – X data

  • ydata (numpy.array) – Y data

  • degree (int) – Degree of the fitting polynomial

Return type

numpy.array, float


Fitting coefficients, coefficient of determination (R^2)

schrodinger.math.mathutils.dbscan_cluster(objects, eps, key=None)

Cluster the given objects by numbers obtained via the given key function using a specified precision. Uses simple interface to sklearn.cluster.DBSCAN (density based spatial clustering).

  • objects (list) – the objects to cluster

  • eps (float) – the precision that controls the size of the clusters, for example if objects is [102, 104, 307, 309, 99, 919, 918] an eps of 6 will produce 3 clusters, see sklearn.cluster.DBSCAN documentation for more details

  • key (function or None) – the function to get a number from the object, if None is the identity function


ValueError – if there is an issue

Return type



clustered objects, keys are arbitrary cluster indices, values are lists of objects for the given cluster