rational.utils

rational.utils.find_closest_equivalent(rational_func, new_func, x)[source]

Compute the parameters a, b, c, and d that minimizes distance between the rational function and the other function on the range x

Parameters
  • rational_func (callable) – The rational function to consider.

  • new_func (callable) – The function you want to fit to rational.

  • x (array) –

    The range on which the curves of the functions are fitted together.

    Default True

Returns

((a, b, c, d), dist) with:

a, b, c, d: the parameters to adjust the function (vertical and horizontal scales and bias)

dist: The final distance between the rational function and the fitted one

Return type

tuple

rational.utils.find_weights(function, function_name=None, degrees=None, bounds=None, version=None, plot=None, save=None, overwrite=None)[source]

Finds the weights of the numerator and the denominator of the rational function. Beside function, all parameters can be left to the default None.

In this case, user is asked to provide the params interactively.

Parameters
  • function (callable) – The function to approximate (e.g. from torch.functional).

  • function_name (str) – The name of this function (used at Rational initialisation)

  • degrees (tuple of int) –

    The degrees of the numerator (P) and denominator (Q).

    Default None

  • bounds (tuple of int) –

    The bounds to approximate on (e.g. (-3,3)).

    Default None

  • version (str) –

    Version of Rational to use. Rational(x) = P(x)/Q(x)

    A: Q(x) = 1 + |b_1.x| + |b_2.x| + … + |b_n.x|

    B: Q(x) = 1 + |b_1.x + b_2.x + … + b_n.x|

    C: Q(x) = 0.1 + |b_1.x + b_2.x + … + b_n.x|

    D: like B with noise

  • plot (bool) – If True, plots the fitted and target functions. Default None

  • save (bool) – If True, saves the weights in the config file. Default None

  • save – If True, if weights already exist for this configuration, they are overwritten. Default None

Returns

(numerator, denominator) if not save, otherwise None

Return type

tuple

class rational.utils.Snapshot(name, rational, fitted_function=True, other_func=None)[source]

Snapshot to save, display, and export images of rational functions. Makes it easy to generate animations of the function through time, … etc.

Parameters
  • name (str) – The name of Snapshot.

  • rational (Rational) – A rational function to save

  • fitted_function (bool) –

    If True, displays the best fitted function if searched. Otherwise, returns it.

    Default True

  • other_func (callable) – another function to be plotted or a list of other callable functions or a dictionary with the function name as key and the callable as value. Default None

Returns

Rational module

Return type

Module

borders(x=None, fitted_function=True, other_func=None, tolerance=0.001)[source]

Returns the borders x_min, x_max, y_min, y_max.

Parameters
  • x (range) –

    The range to print the function on.

    Default None

  • fitted_function (bool) –

    If True, displays the best fitted function if searched. Otherwise, returns it.

    Default True

  • other_func (callable) – another function to be plotted or a list of other callable functions or a dictionary with the function name as key and the callable as value. Default None

  • tolerance (float) –

    Tolerance the bins frequency. If tolerance is 0.001, every frequency smaller than 0.001 will be cutted out of the histogram.

    Default True

Returns

Rational module

Return type

Module

save(x=None, fitted_function=True, other_func=None, path=None, tolerance=0.001, title=None, format='svg')[source]

Saves an image of the snapshot.

Parameters
  • x (range) –

    The range to print the function on.

    Default None

  • fitted_function (bool) –

    If True, displays the best fitted function if searched. Otherwise, returns it.

    Default True

  • other_func (callable) –

    another function to be plotted or a list of other callable functions or a dictionary with the function name as key and the callable as value.

    Default None

  • tolerance (float) –

    Tolerance the bins frequency. If tolerance is 0.001, every frequency smaller than 0.001 will be cutted out of the histogram.

    Default True

  • title (str) –

    If not None, title to be displayed on the figure.

    Default None

  • format (str) –

    The format of the figure, if not in the title.

    Default svg

show(x=None, fitted_function=True, other_func=None, display=True, tolerance=0.001, title=None, axis=None)[source]

Show the function using matplotlib.

Parameters
  • x (range) –

    The range to print the function on.

    Default None

  • fitted_function (bool) –

    If True, displays the best fitted function if searched. Otherwise, returns it.

    Default True

  • display (bool) –

    If True, displays the graph. Otherwise, returns a dictionary with functions informations.

    Default True

  • other_func (callable) – another function to be plotted or a list of other callable functions or a dictionary with the function name as key and the callable as value. Default None

  • tolerance (float) –

    Tolerance the bins frequency. If tolerance is 0.001, every frequency smaller than 0.001 will be cutted out of the histogram.

    Default True

  • title (str) –

    If not None, title to be displayed on the figure.

    Default None

  • axis (matplotlib.pyplot.axis) – axis to be plotted on. If None, creates one automatically. Default None