Beispiel #1
0
def get_finite_difference_coefficients(derivative_order, accuracy_order,
                                       grid_type):

    if derivative_order < 1:
        raise ValueError("Wrong derivative order")

    if odd(accuracy_order) or accuracy_order < 2:
        raise ValueError("Wrong accuracy order")

    if grid_type == 'centred':
        window_size = accuracy_order / 2
        grid = list(range(-window_size, window_size + 1))
    elif grid_type == 'forward':
        grid = list(range(derivative_order + accuracy_order))
    elif grid_type == 'backward':
        grid = list(range(-(derivative_order + accuracy_order) + 1, 1))
        grid = list(reversed(grid))  # Fixme: why ?
    else:
        raise ValueError("Wrong grid type")

    key = '{}-{}'.format(derivative_order, accuracy_order)
    coefficients = _coefficient_cache[grid_type].get(key, None)
    if coefficients is None:
        coefficients = compute_finite_difference_coefficients(
            derivative_order, grid)
        _coefficient_cache[grid_type][key] = coefficients

    return grid, coefficients
Beispiel #2
0
def get_finite_difference_coefficients(derivative_order, accuracy_order, grid_type):

    if derivative_order < 1:
        raise ValueError("Wrong derivative order")

    if odd(accuracy_order) or accuracy_order < 2:
        raise ValueError("Wrong accuracy order")

    if grid_type == 'centred':
        window_size = accuracy_order / 2
        grid = list(range(-window_size, window_size +1))
    elif grid_type == 'forward':
        grid = list(range(derivative_order + accuracy_order))
    elif grid_type == 'backward':
        grid = list(range(-(derivative_order + accuracy_order) +1, 1))
        grid = list(reversed(grid)) # Fixme: why ?
    else:
        raise ValueError("Wrong grid type")

    key = '{}-{}'.format(derivative_order, accuracy_order)
    coefficients = _coefficient_cache[grid_type].get(key, None)
    if coefficients is None:
        coefficients = compute_finite_difference_coefficients(derivative_order, grid)
        _coefficient_cache[grid_type][key] = coefficients

    return grid, coefficients
Beispiel #3
0
        8: [1/280.,   -4/105.,   1/5.,   -4/5.,   0],
   },
    2: {
        2: [1,  -2],
        4: [-1/12.,  4/3.,  -5/2.],
        6: [1/90.,  -3/20.,  3/2.,  -49/18.],
        8: [-1/560.,  8/315.,  -1/5.,  8/5.,  -205/72.],
   },
}
# complete right coefficients
for derivative_order, derivative_order_dict in centred_coefficients.items():
    for coefficients in derivative_order_dict.values():
        if len(coefficients) > 1:
            for i in range(len(coefficients) -2, -1, -1):
                coefficient = coefficients[i]
                if odd(derivative_order):
                    coefficient *= -1
                coefficients.append(coefficient)

forward_coefficients = {
    # on a grid 0...8
    # [order of derivative][order of accuracy] = (coefficients)
    0: { None:(1,) },
    1: {
        1: (-1,  1),
        2: (-3/2.,  2,  -1/2.),
        3: (-11/6.,  3,  -3/2.,  1/3.),
        4: (-25/12.,  4,  -3,  4/3.,  -1/4.),
        5: (-137/60.,  5,  -5,  10/3.,  -5/4.,  1/5.),
        6: (-49/20.,  6,  -15/2.,  20/3.,  -15/4.,  6/5.,  -1/6.),
        7: (-363/140.,  7,  -21/2.,  35/3.,  -35/4.,  21/5.,  -7/6.,  1/7.),
Beispiel #4
0
        8: [1/280.,   -4/105.,   1/5.,   -4/5.,   0],
   },
    2: {
        2: [1,  -2],
        4: [-1/12.,  4/3.,  -5/2.],
        6: [1/90.,  -3/20.,  3/2.,  -49/18.],
        8: [-1/560.,  8/315.,  -1/5.,  8/5.,  -205/72.],
   },
}
# complete right coefficients
for derivative_order, derivative_order_dict in centred_coefficients.items():
    for coefficients in derivative_order_dict.values():
        if len(coefficients) > 1:
            for i in range(len(coefficients) -2, -1, -1):
                coefficient = coefficients[i]
                if odd(derivative_order):
                    coefficient *= -1
                coefficients.append(coefficient)

forward_coefficients = {
    # on a grid 0...8
    # [order of derivative][order of accuracy] = (coefficients)
    0: { None:(1,) },
    1: {
        1: (-1,  1),
        2: (-3/2.,  2,  -1/2.),
        3: (-11/6.,  3,  -3/2.,  1/3.),
        4: (-25/12.,  4,  -3,  4/3.,  -1/4.),
        5: (-137/60.,  5,  -5,  10/3.,  -5/4.,  1/5.),
        6: (-49/20.,  6,  -15/2.,  20/3.,  -15/4.,  6/5.,  -1/6.),
        7: (-363/140.,  7,  -21/2.,  35/3.,  -35/4.,  21/5.,  -7/6.,  1/7.),