Documentation

Smolyak_one.View()

View of project

Returns

None

Smolyak_one.calculate_stat_data(function_string_stat_calc: str, var_string: str, option_list_quad: str, filename: str, degree_of_app_range: list, repetitions=1024, damping=True, damping_exp=1.5)

Calculate the estimation many times

This function calculates and saves the result, error and runtime Smolyak approximation for the quadratures given in option_list several times.

Parameters
  • function_string_stat_calc (string) – function for which the integral should be approximated.

  • var_string (sting) – variables of function for which the integral should be approximated.

  • option_list_quad (list) – list with the quadratures that should be used.

  • filename (string) – name of the file in which you want to save the data

  • degree_of_app_range (list) – list with degrees of approximation, for which data should be generated repetitions: Times the results are generated.

  • repetitions (int) – Times the results are generated.

  • damping (boolean, optional) – boolean saying, whether the number of repetition should decrease according to the degree of approximation. Depending on the range of degrees of approximation and the computer recommendable, default is True.

  • damping_exp (float) – damping exponent of the degree of approximation, if damping is true.

Raises
  • Exception – If one of the quadrature is not defined.

  • Exception – If the filename is not valid

  • Exception – If degree_of_app_range does not have the right length.

Returns

None

Smolyak_one.controller_smolyak(function_string_control: str, variables_string_control: str, quadrature: str, q: int, no_error=False, function_given=False, example_2=False, a=0, b=1)

Controller of project

This function gets a function and a few settings and return an approximation, an error-estimation and a cost-estimation of the integral of this function.

Parameters
  • function_string_control (str) – string in which the function is defined

  • variables_string_control (str) – string for safe use, to be sure that right variables are used in function

  • quadrature (str) – variable with 4 options to choose quadrature that should be used for approx

  • q (int) – degree of approximation

  • no_error (boolean, optional) – Decides, if error should be calculated. (Sometimes the time can be reduced relevantly.) Default is set to False.

  • function_given (boolean, optional) – Needs to be set to true, if a callable function already is given. In this case the input function_string_control needs to be a callable function and variable_string_control need to be a list of sympy.variables, default set to False.

  • example_2 (boolean, optional) – Defined for example 2. In this case the quadrature is open, default set to False.

  • a (float, optional) – lower limit of interval, default set to 0.

  • b (float, optional) – upper limit of interval, default set to 1.

Raises

Exception – If the type of quadrature chosen is not defined.

Returns

result of approximation, upper bound of error of approximation and upper bound of cost.

Return type

[float, float, float]

Smolyak_one.cost_smolyak(q: int, dim: int, nested=True, is_rand=False, example_2=False, give_param=False)

Cost calculation

Function giving back an estimation of the cardinality of the information needed for the algorithm. Due to the definition of the. sequences of the one dimensional algorithms used, the cost could be calculated relatively quickly

Parameters
  • q (int) – Degree of approximation

  • dim (int) – Dimension

  • nested (boolean, optional) – Decides if information is nested, default True.

  • is_rand (boolean, optional) – Decides if quadrature is probabilistic, default is False.

  • example_2 (boolean, optional) – Mainly defined for example 2. Decides, if the quadrature is open, default is False.

  • give_param (boolean, optional) – Decides, if the cost estimation or the parameters are returned, default is False.

Returns

estimated cost of approximation.

Return type

float

Smolyak_one.epsilon_cost(function_string: str, variable_string: str, quadrature: str, epsilon: <module 'numbers' from '/home/docs/.pyenv/versions/3.7.9/lib/python3.7/numbers.py'>, nested=False, open_quad=False)

Epsilon cost

Function calculating the epsilon error.

Parameters
  • function_string (str) – string of function

  • variable_string (str) – string of variables

  • quadrature (str) – string of one-dimensional quadrature to use

  • epsilon (float) – upper bound of error

  • nested (boolean, optional) – Decides if the equation for nested or not nested information is used, default is

  • False.

  • open_quad (boolean, optional) – Decides if a open or closed quadrature is used, default is

  • False.

Raises

Exception – Exception: If the quadrature chosen is not implemented.

Returns

The estimated error and cost estimation.

Return type

[float, float]

Smolyak_one.error_smolyak(f, quadrature: str, variables, q: int, function_string_error: str, a=0, b=1, give_parameters=False)

Error estimation

This function returns error-estimation for Smolyak algorithm.

Parameters
  • f (function) – callable sympy function

  • quadrature (str) – quadrature used

  • variables (sympy.variables) – sympy variables of function

  • q (int) – degree of approximation

  • function_string_error (str) – string of function. (Needed, because analytically deriving strings is easier than

  • functions) (deriving) –

  • a (float, optional) – lower limit of interval, default to 0.

  • b (float, optional) – upper limit of interval, default to 1.

  • give_parameters (boolean, optional) – Boolean defined to get the parameters of the error estimation instead of

  • error estimation (the) –

  • set to False. (default) –

Raises
  • Exception – If the quadrature chosen is not implemented

  • Exception – If deterministic quadratures are used and the function can not be derived sufficiently many times.

Returns

error of estimation

Return type

float

Smolyak_one.fast_eps_cost(dim: int, B: float, C: float, D: float, F_0: float, F: float, epsilon: float, q=0, is_nested=True)

Epsilon cost (predefined constants)

Function for the fast calc. of eps cost, if all parameters are given

Parameters
  • dim (int) – dimension

  • B (float) – Estim. of norm of operator

  • C (float) – Estim. of linear part of approx error

  • D (float) – Estim. of exp. part of approx error

  • F_0 (float) – Estim. of linear part of costs

  • F (float) – Estim. of exp. part of cost

  • epsilon (float) – epsilon error chosen

  • q (int, optional) – degree of approximation

  • is_nested (boolean, optional) – Decides if the equation for nested or not nested information is used, default is

  • True.

Returns

The epsilon cost

Smolyak_one.find_all_sensible_combinations(dim: int, q: int)

Faster combinatorics

Function finding all tuples of combinations of one dim approximations necessary to calc. Smol. alg..

Parameters
  • dim (int) – dimension of vectors

  • q (int) – maximal cumulative sum

Returns

List of all combination of degrees of approximation needed for later calculations.

Return type

list

Smolyak_one.find_next_tuples_index(current_tuple: tuple, tuple_list: list)

Determine index of the matrix with the most reusable information

Function to find the function evaluation matrix with the most reusable function values

Parameters
  • current_tuple (tuple) – Tuple saving the degree of approximation currently used for the approximation

  • tuple_list (list) – list of tuples for which the evaluations are stored.

Returns

The index of the evaluations with the most reusable evaluations.

Return type

integer

Smolyak_one.find_right_position(current_vector: numpy.array, pointer_current_position: int, max_value: int)

Faster combinatorics

Decides, where to add one.

Parameters
  • current_vector (np.array) – Current combination of degrees of approximation.

  • pointer_current_position (int) – index of dimension, in which needs to be changed next.

  • max_value (int) – upper bound of degrees of approximation needed in set.

Returns

highest position that needs to be changed in next step

Return type

pointer_current_position (int)

Smolyak_one.load_stat_data(filename: str)

Load data calculated by calculate_stat_data

Function loading the formerly saved data to the memory. !! This method only should be used for the calculate_stat_data, because the data structure is relatively specific. :param filename: string of name of file that should be loaded.

Raises

Exception – If the file name has the wrong format.

Returns

Contains the results of the approximation.

Return type

List

Smolyak_one.modified_scatter_plot(x, y, title='Gridpoints used for Smolyak-algorithm', input_list=False, a=0, b=1)

Scatter plot

Function for quick application some basic modifications to a scatter plot of the gridpoints used for the Smolyak algorithm :param x: list of x-values of points :type x: list :param y: list of y-values of points :type y: list :param title: title of plot :type title: str :param input_list: Boolean saying, whether input is list. If True, the points in different

lists are plotted in different colours, default is False.

Parameters
  • a (float, optional) – lower border of interval

  • b (float, optional) – upper border of interval

Returns

None

Smolyak_one.monte_carlo_quad(q: int, a=0, b=1)

Monte Carlo quadrature

This function computes the nodes and weights for the 1D Monte carlo quadrature.

Parameters
  • q (int) – degree of approximation. Number of nodes, m = 2^(q-1)+1.

  • a (int, optional) – Lower boundary of integral. Defaults to 0.

  • b (int, optional) – Upper boundary of integral. Defaults to 1.

Raises
  • Exception – a and b need to be real numbers.

  • Exception – b as the upper boundary has to be bigger than a.

  • Exception – q needs to be an integer.

Returns

nodes and weights for the 1D Monte carlo quadrature of degree q.

Return type

[array, array]

Smolyak_one.next_step(current_vector: numpy.array, pointer_current_position: int, max_value: int)

Faster combinatorics

Function returning, were and what to change in the vectors determining, what one dimensional degree of approximation should be applied. (Internal function)

Parameters
  • current_vector (np.array) – full actual vector. (Could be reduced in most cases)

  • pointer_current_position (int) – position currently worked at

  • max_value (int) – necessary to decide when to skip to the next position

Returns

The position at which the value needs to be incremented, the next value and a pointer showing, in which dimension the next action needs to be performed.

Return type

[int, int, int]

Smolyak_one.one_dim_newton_cotes(q: int, a=0, b=1)

Newton-Cotes quadrature

This function computes the nodes and weights for the 1D Newton-Cotes quadrature. This is a program helping me getting into programming. On the internet better implementation of this very basic and not very precise quadrature. Apart from this, the quadrature will be used for the testing of the viewer at the start. !! Effectively we have the open Newton-cotes formula of the degree (2^q - 1)!!

Parameters
  • q (int) – Degree of quadrature.

  • a (int, optional) – Lower boundary of integral. Defaults to 0.

  • b (int, optional) – Upper boundary of integral. Defaults to 1.

Raises
  • Exception – a and b need to be real numbers.

  • Exception – b as the upper boundary has to be bigger than a.

  • Exception – q needs to be an integer.

  • Exception – Please choose q < 6 for the Newton-Cotes approximation to avoid numerical errors.

Returns

nodes and weights for the 1D Newton-Cotes quadrature of degree q.

Return type

[array, array]

Smolyak_one.one_dim_trapezoidal(q: int, a=0, b=1, example_2=False)

Trapezoidal quadrature

This function computes the nodes and the weights for the 1D trapezoidal quadrature.

Parameters
  • q (int) – Degree of quadrature. The number of points used for the quadrature then is 2^q

  • a (int, optional) – Lower boundary of integral. Defaults to 0.

  • b (int, optional) – Upper boundary of integral. Defaults to 1.

  • example_2 (bool, optional) – Boolean only defined for the example with the periodic, derivable functions. If this variable is true, the borders of the interval are not used.

Raises
  • Exception – a and b need to be real numbers.

  • Exception – b as the upper boundary has to be bigger than a.

  • Exception – q needs to be an integer.

Returns

nodes and the weights for the 1D trapezoidal quadrature of degree q.

Return type

[array, array]

Smolyak_one.qmc_quad(q: int, a=0, b=1)

Quasi-Monte Carlo quadrature

This function computes the nodes and weights for the 1D Quasi Monte carlo quadrature using lattice rules.

Parameters
  • q (int) – degree of approximation. Number of nodes, m = 2^q.

  • a (int, optional) – Lower boundary of integral. Defaults to 0.

  • b (int, optional) – Upper boundary of integral. Defaults to 1.

Raises
  • Exception – a and b need to be real numbers.

  • Exception – b as the upper boundary has to be bigger than a.

  • Exception – q needs to be an integer.

Returns

nodes and weights for the 1D QMC quadrature of degree q.

Return type

[array, array]

Smolyak_one.rewrite_function(function_string_rewrite: str, variables_string_rewrite: str, a=0, b=1)list

Rewrite string to function

This function converts strings to Sympy objects.

Parameters
  • function_string_rewrite (str) – function to be approximated

  • variables_string_rewrite (str) – variables used in function

  • a (float) – lower border of interval

  • b (float) – upper border of interval

Raises
  • Exception – If no variable defined.

  • Exception – If the set of variables used in function and defined in variable string do not coincide.

Returns

function and variable used for approximation. String rewritten

with standardized variables.

Return type

[sympy.function, sympy.variables, string]

Smolyak_one.slice_find(current_shape: tuple, nearest_shape: tuple, open_quad=False)

Determine the position of the reusable information in the matrix.

Function returning slices that make it possible to find old evaluations can be used for the calculation of the current summand and the points for that further evaluation are needed. :param current_shape: Tuple storing the number of evaluations used for the current approximation. :type current_shape: tuple :param nearest_shape: Tuple storing the number of evaluations used for a former evaluation :type nearest_shape: tuple :param open_quad: Only defined for example 2. If this is true, the borders of the interval are not

used, default to False.

Raises

Exception – A unforeseen combination of values occurred.

Returns

Three d dimensional lists of slices are returned. In these the slice at the i-th position

coincide with the points used in this dimension.

list1: Positions at which a new evaluation is needed. list2: Positions at which old evaluations can be used. list3: Positions of the weights multiplied with the old evaluations.

Return type

[list1, list2, list3]

Smolyak_one.smolyak(f, weights: list, points: list, q: int, dim: int, is_nested=False, example_2=False)list

The Smolyak algorithm

Method to compute an approximation of the integral of a function using the Smolyak algorithm.

Parameters
  • f (function) – Callable sympy function.

  • dim (int) – Dimension of the function f.

  • weights (list) – Numpy array with saved 1D weights corresponding to the points.

  • points (list) – Points on which the function is evaluated for the 1D integral approx.

  • q (int) – Degree of approximation.

  • is_nested (bool, optional) – Boolean defined for the case that the information used is nested. Defaults to False.

  • example_2 (bool, optional) – Boolean only defined for the example with the periodic, derivable functions. If this variable is true, the borders of the interval are not used.

Raises
  • Exception – The list of points and weights given for the Smolyak have incorrect lengths.

  • Exception – The dimensions of the 1D gridpoints and weights do not match.

  • Exception – Something went wrong with the usage of the nested information.

  • Exception – Up to now the input vectors should be one dimensional but are not.

Returns

result of approximation

Return type

float

Smolyak_one.sort_tuples(list_of_tuples: list, q: int)

Sort tuples

We need this function to use the advantages of nested information, for this it is easier to first need to sort the list of tuples in a way, that we first evaluate the function in a systematical way. This sorting is done here. Starting with the first dimension we sort the list in a rising order.

This means: input = [(2,1,4),(1,2,2),(1,1,1),(2,1,2)] –> [(1,1,1),(1,2,2),(2,1,2),(2,1,4)]

Parameters
  • list_of_tuples (list) – List of tuples that needs to be sorted in a specific order.

  • q (int) – Degree of approximation

Returns

Sorted list of tuples.

Return type

list

Smolyak_one.sum_tuple_shape(old_shape: tuple, new_shape: tuple)

Sum up tuples

Function summing up the dimension of the old information used in the way necessary here. This means that if the dimension is the same, this value is saved. In other case, the values are summed up. Example: (33,3) + (32,3) = (65,3).

Parameters
  • old_shape (tuple) – Dimension of the used information.

  • new_shape (tuple) – Dimension of the used information.

Raises

Exception – The input data needs to have the same lengths.

Returns

Combined shape of information.

Return type

tuple