assignment1.dg1_high_prec module

Helpers to use assignment1.dg1 with high-precision numbers.

High-precision is achieved by using mpmath.

class assignment1.dg1_high_prec.HighPrecProvider[source]

Bases: object

High-precision replacement for assignment1.dg1.MathProvider.

Implements interfaces that are essentially identical (at least up to the usage in dg1) as those provided by NumPy.

All matrices returned are numpy.ndarray with mpmath.mpf as the data type and all matrix inputs are assumed to be of the same form.

static exp_func(value)[source]

Vectorized exponential function.

static linspace(start, stop, num=50)[source]

Linearly spaced points.

Points are computed with mpmath.linspace() but the output (a list) is converted back to a numpy.ndarray.

static mat_inv(mat)[source]

Matrix inversion, using mpmath.

static num_type(value)[source]

The high-precision numerical type: mpmath.mpf.

classmethod solve(left_mat, right_mat)[source]

Solve Ax = b for x.

A is given by left_mat and b by right_mat.

This method seeks to mirror mpmath.matrices.linalg.LinearAlgebraMethods.lu_solve(), which uses mpmath.matrices.linalg.LinearAlgebraMethods.LU_decomp(), mpmath.matrices.linalg.LinearAlgebraMethods.L_solve() and mpmath.matrices.linalg.LinearAlgebraMethods.U_solve(). Due to limitations of mpmath we use modified helpers to accomplish the upper- and lower-triangular solves. We also cache the LU-factorization for future uses.

It’s worth pointing out that numpy.linalg.solve() works in exactly this fashion. From the C source there is a lapack_func that gets defined and is eventually used in Python as gufunc. Notice that the lapack_func is dgesv for doubles. Checking the LAPACK docs verifies the dgesv does an LU and then two triangular solves.

static zeros(shape, **kwargs)[source]

Produce a matrix of zeros of a given shape.

assignment1.dg1_high_prec.gauss_lobatto_points(start, stop, num_points)[source]

Get the node points for Gauss-Lobatto quadrature.

Rather than using the optimizations in dg1.gauss_lobatto_points(), this uses mpmath utilities directly to find the roots of \(P_n'(x)\) (where \(n\) is equal to num_points - 1).

Parameters:
  • start (mpmath.mpf (or float)) – The beginning of the interval.
  • stop (mpmath.mpf (or float)) – The end of the interval.
  • num_points (int) – The number of points to use.
Return type:

numpy.ndarray

Returns:

1D array, the interior quadrature nodes.