joby_m_anthony_iii package¶
Welcome to the ‘joby_m_anthony_iii’ package!
References
Submodules¶
joby_m_anthony_iii.numerical_methods module¶
- class joby_m_anthony_iii.numerical_methods.BVP(function: Tuple[function], a: float, b: float, alpha: float, beta: float, variables: Optional[Tuple[str]] = ('x', 'y', 'yp'), steps: int = 100)¶
Bases:
__ode
Class containing Boundary Value Problem (BVP) methods.
- Parameters:
function (lambda) – Time derivative of `function`(s) to approximate.
a (float) – Initial and final time with initial values in function`(s) at `a and b.
b (float) – Initial and final time with initial values in function`(s) at `a and b.
alpha (float) – Initial and final time with initial values in function`(s) at `a and b.
beta (float) – Initial and final time with initial values in function`(s) at `a and b.
variables (tuple, optional) – Collection of string variables to respect in `function`(s).
steps (int, optional) – Maximum number of time steps to discretize domain.
- a, b, alpha, beta
Initial and final time with initial values in function`(s) at `a and b.
- Type:
float
- steps¶
Maximum number of time steps to discretize domain.
- Type:
int
- finite_difference_method(solver_method='gauss_seidel')¶
- linear_shooting_method()¶
- Raises:
ValueError – If steps constraint is not an integer greater than zero.
TypeError – If input function cannot be understood as lambda expression.
Notes
Make sure the independent variable is the first element of variables!
- finite_difference_method(solver_method='gauss_seidel') Tuple[DataFrame, DataFrame] ¶
Solve a BVP differential equation with Dirichlet boundary conditions by 2 IVP differential equations.
- Parameters:
solver_method ({"jacobi", "gauss_seidel", "successive_relaxation"}, optional) – Unless specified, SOE will be solved by the “gauss_seidel” method.
- Returns:
pandas.DataFrame() (DataFrame) – Dataframe of method iterations and time domains, range of approximations for input function, and iterative increments.
pandas.DataFrame() (DataFrame) – Dataframe of cumulative errors through the required number of iterations according to solver_method.
- step_size¶
Domain step size.
- Type:
float
- iterations¶
Collection of steps through method.
- Type:
tuple
- domain¶
Discretized domain between endpoints a and b for so many steps.
- Type:
tuple
- range¶
Range mapped from method through discretized domain between endpoints a and b for so many steps.
- Type:
tuple
- derivatives¶
Collection of derivatives at each step.
- Type:
tuple
- Raises:
TypeError – If input expression cannot be understood as lambda or sympy expression nor as string.
ValueError – Prescribed method is not an available option.
See also
MultiVariableIteration.jacobi
,MultiVariableIteration.gauss_seidel
,MultiVariableIteration.successive_relaxation
Notes
Uses a Taylor polynomial with a first-order and a second-order IVP equations.
Converges \(\mathcal{O}(h^{2})\).
- linear_shooting_method() DataFrame ¶
Solve a BVP differential equation with 2 IVP differential equations.
- Returns:
pandas.DataFrame() – Dataframe of method iterations and time domains, range of approximations for input function, and iterative increments.
- Return type:
DataFrame
- step_size¶
Domain step size.
- Type:
float
- iterations, domain, range, derivatives
Collection of steps through, domain used in, range from, and derivatives at each step in method.
- Type:
tuple
- Raises:
TypeError – If input `function`(s) cannot be understood as lambda expression.
- class joby_m_anthony_iii.numerical_methods.CubicSpline(domain: tuple, function: tuple, variable: Optional[str] = 'x')¶
Bases:
object
Given a domain and range, construct a piecewise-polynomial approximation (spline) within each interval by some condition.
- Parameters:
domain (tuple) – Input domain and range from which to build interpolating splines.
function (tuple) – Input domain and range from which to build interpolating splines.
variable (string) – Respected variable in derivative of equation. Assumed to be “x” if not stated.
- domain, function, mapped
Input domain, function, and mapped range.
- Type:
np.ndarray
- variable¶
Variable to respect in derivation.
- Type:
string
- clamped(function_derivative: Optional[tuple])¶
Use the derivative at the endpoints of the domain.
- natural()¶
Assume the derivative at either end of domain is one.
- Raises:
IndexError – If domain is not a one-dimensional array.
TypeError – If function is not an expression or function and is not an one-dimensional array.
IndexError – If domain and function are of unequal length.
See also
make_array
Translates function to mapped array from given domain.
endpoint
Find derivatives at endpoints if not explicitly provided by data, function_derivative nor a lambda expression.
midpoint
Finds the derivatives at points within the bounds given domain and function.
Notes
Method uses many, low-ordered polynomials to fit larger datasets. This minimizes computational load, which, conversely, greatly increases for larger datasets that yield high-ordered polynomials.
General form
\[S_{j}(x) = a_{j} + b_{j}(x - x_{j}) + c_{j}(x - x_{j})^{2} + dj(x - x_{j})^{3}\]clamped() splines fit the constructed polynomial to the given data and its derivatives at either endpoint.
natural() splines set the derivative at either endpoint to be 1.
- Definitions of cubic spline conditions:
\(S(x)\) is a cubic polynomial, \(S_{j}(x)\) on sub-interval \([x_{j}, x_(j+1)]\) for each \(j \in [0, 1, ..., n-1]\);
\(S_{j}(x_{j}) = f(x_{j})\) and \(S_{j}(x_{j+1}) = f(x_{j+1})\) for each \(j \in [0, 1, ..., n-1]\);
\(S_{j+1}(x_{j+1}) = S_{j}(x_{j+1})\) for each \(j \in [0, 1, ..., n-2]\);
\(S_{j+1}'(x_{j+1}) = S_{j}'(x_{j+1})\) for each \(j \in [0, 1, ..., n-2]\);
- One of the following conditions is satisfied:
\(S''(x_{0}) = S''(x_{n}) = 0\) -> ‘natural’
\(S'(x_{0}) = f'(x_{0})\) and \(S'(x_{n}) = f'(x_{n})\) -> ‘clamped’
- clamped(function_derivative: Optional[tuple] = None) Tuple[ndarray, Tuple[function]] ¶
The bookend polynomials will have the same slope entering and exiting the interval as the derivative at the respective endpoint.
- Parameters:
function_derivative (tuple, optional) – Derivative at each point in function.
- Returns:
Y (np.ndarray) – Finally evaluated solutions.
splines (list) – Aggregate of lambda expressions for the spline in each interval.
- Raises:
ValueError – If function_derivative is not an expression or function and is not an one-dimensional array.
IndexError – If domain, function, and function_derivative are not the same length.
ValueError – Output message that derivative data or expression is missing.
See also
midpoint
Calculates derivative of points within dataset.
endpoint
Calculates derivatives at either end of dataset.
Notes
function_derivative will be calculated from domain and mapped if not specified.
- natural() Tuple[ndarray, Tuple[function]] ¶
The endpoint derivatives entering and exiting the interval are assumed to be 1.
- Returns:
Y (np.ndarray) – Finally evaluated solutions.
splines (list) – Aggregate of lambda expressions for each spline on each interval.
- class joby_m_anthony_iii.numerical_methods.Derivative(domain: tuple, function: Union[tuple, function], h: float, point_type: Optional[str] = 'three')¶
Bases:
object
Find the numeric derivative at some point within dataset.
- Parameters:
domain (tuple) – Domain of collected data.
function (tuple or lambda) – Range of collected data.
h (float) – Step-size through interval.
point_type ({"three", "five", "2nd_derivative"}, optional) – Determines if 3 or 5 point stencil method is used. “2nd_derivative” possible for midpoint() method. Defaults to 3 point.
- domain, function
Domain and mapped range from data.
- Type:
np.ndarray
- h¶
Step-size through interval.
- Type:
float
- point_type¶
Stencil method used.
- Type:
string
- endpoint(point: int)¶
Find the derivative at endpoint of dataset.
- midpoint(point: int)¶
Find the derivative at some point within dataset.
- Raises:
IndexError – If domain is not a one-dimensional array.
IndexError – If function is function and not a one-dimensional array
ValueError – If function is neither a lambda expression nor one-dimensional array.
IndexError – If domain and function are of unequal length.
ValueError – If point_type is not ‘three’, ‘five’, nor ‘2nd_derivative’.
Warning
point_type=”2nd_derivative” only available for midpoint method.
See also
make_array
Maps input lambda expression, function to range from domain.
Notes
5 point, \(\mathcal{O}(h^{4})\) is more accurate than 3 point, \(\mathcal{O}(h^{2})\); however, round-off error increases. midpoint has half the error of endpoint because of using more information and performing fewer calculations.
- endpoint(point: int) float ¶
Find the derivative of a bookend point at either end of a dataset.
- Parameters:
point (int) – Index location in domain to evaluate derivative.
- Returns:
derivative – Evaluated derivative at point.
- Return type:
float
- midpoint(point: int) float ¶
Find the derivative of some point within a dataset.
- Parameters:
point (int) – Index location in domain to evaluate derivative.
- Returns:
derivative – Evaluated derivative at point.
- Return type:
float
- class joby_m_anthony_iii.numerical_methods.EigenValues(A: tuple, power: float = -6, max_iter: int = 100)¶
Bases:
object
Find the characteristic (eigen) values of matrix. Typically thought as roots of polynomial from determinant.
- Parameters:
A (tuple) – Characteristic matrix.
power (float, optional) – Signed power to which function error must be within.
max_iter (int, optional) – Maximum iterations for which function may loop.
- A¶
Input characteristic matrix.
- Type:
np.ndarray
- tol¶
Specified tolerance to which method terminates.
- Type:
float
- max_iter¶
Maximum iterations allowed for method.
- Type:
int
- is_diagonal, is_tridiagonal
Truth value of whether matrix is diagonal and tridiagonal, respectively.
- Type:
bool
- power_method(x: tuple)¶
Determine the dominating eigenvalue and resulting eigenvector from initial guess, x.
- inverse_power_method(x: tuple, q: float)¶
Determine the eigenvalue close to q and resulting eigenvector from initial guess, x.
- qr_algorithm()¶
Directly determine eigenvector of matrix, A.
- Raises:
IndexError – Matrix of interest must be square.
ValueError – If iterations constraint is not an integer.
Notes
Specified tolerance evaluated by 10**power.
If is_diagonal is True, then matrix, A is strictly, diagonally dominant. Else, not strictly, diagonally dominant. Similar for is_tridiagonal.
- inverse_power_method(x: tuple, q: float) DataFrame ¶
Approximate eigenvalue closest to target, q and associated eigenvector of matrix, A given some non-zero vector, x.
- Parameters:
x (tuple) – Initial guess for eigenvector.
q (float) – Target to which the closest eigenvalue of matrix will be found.
- Returns:
pandas.DataFrame – Summarized dataframe from iterations.
- Return type:
DataFrame
- x¶
Initial guess at eigenvector.
- Type:
np.ndarray
- iterations, mu, lambdas, errors
Collection of iterations, eigenvalues, eigenvectors, and propogated errors through method.
- Type:
np.ndarray
- Raises:
IndexError – If x is not a one-dimensional array.
Notes
Supposed to converge faster than power_method [burdenNumericalAnalysis2016].
- power_method(x: tuple) DataFrame ¶
Approximate the dominant eigenvalue and associated eigenvector of matrix, A given some non-zero vector, x.
- Parameters:
vector (tuple) – Initial guess for eigenvector.
- Returns:
pandas.DataFrame – Summarized dataframe from iterations.
- Return type:
DataFrame
- vector¶
Initial guess for eigenvector.
- Type:
np.ndarray
- iterations, mu, lambdas, errors
Collection of iterations, eigenvalues, eigenvectors, and propogated errors through method.
- Type:
np.ndarray
- Raises:
IndexError – If x is not a one-dimensional array.
- qr_algorithm() DataFrame ¶
Approximate dominant eigenvalue and associated eigenvector of matrix, A by decomposition [burdenNumericalAnalysis2016].
- Returns:
pandas.DataFrame – Summarized dataframe from iterations.
- Return type:
DataFrame
- iterations, lambdas, errors
Collection of iterations, eigenvectors, and propogated errors through method.
- Type:
np.ndarray
Warning
Matrix, A must be symmetric and tridiagonal!
Notes
This method is preferred over power_method and inverse_power_method by keeping round-off error to a minimum [burdenNumericalAnalysis2016].
Examples
Refer to this example for an explanation and demonstration [thebrightsideofmathematicsQRDecompositionSquare2020].
- class joby_m_anthony_iii.numerical_methods.IVP(function: Tuple[function], a: float, b: float, alpha: float, variables: Optional[Tuple[str]] = ('t', 'y'), steps: Optional[int] = 100)¶
Bases:
__ode
Class containing Initial Value Problem (IVP) methods.
- Parameters:
function (expression) – Time derivative of function(s) to approximate.
a (float) – Initial and final time with initial value in function at a.
b (float) – Initial and final time with initial value in function at a.
alpha (float) – Initial and final time with initial value in function at a.
variables (tuple, optional) – Collection of string variables to respect in `function`(s).
steps (int, optional) – Maximum number of time steps to discretize domain.
- function¶
Time derivative of function to approximate.
- Type:
expression
- a, b, alpha
Initial and final time with initial value in function at a.
- Type:
float
- variables¶
Collection of symbolic or string variables to respect in function.
- Type:
tuple, optional
- steps¶
Maximum number of time steps to discretize domain.
- Type:
int, optional
- backward_euler()¶
Implicit, first-order accuracy from Taylor approximation. Best suited to stiff equations.
- forward_euler()¶
Explicit, first-order accuracy from Taylor approximation. Not recommended.
- improved_euler()¶
Explicit, first-order accuracy from Taylor approximation.
- runge_kutta()¶
Explicit, fourth-order accuracy bypassing higher-order derivatives as in Taylor-based methods.
- trapezoidal(power=-6, max_iter=100)¶
Implicit, second-order accuracy derived from Trapezoidal Integration.
- Raises:
ValueError – If steps constraint is not an integer greater than zero.
TypeError – If input `function`(s) cannot be understood as lambda expression.
Notes
Make sure the independent variable is the first element of variables!
- backward_euler(power: float = -6, max_iter: int = 100) DataFrame ¶
Implicitly use information at next time step to approximate IVP differential equation at mesh points between a and b.
- Parameters:
power (int, optional) – Signed power to which function error must be within.
max_iter (int, optional) – Maximum iterations for Newton-Raphson loop.
- Returns:
pandas.DataFrame() – Dataframe of method iterations and time domains, range of approximations for input function, and iterative increments.
- Return type:
dataframe
- step_size¶
Domain step size.
- Type:
float
- iterations, domain, range, increments
Collection of steps through, domain used in, range from, and increments between steps in method.
- Type:
tuple
- Raises:
TypeError – If input `function`(s) cannot be understood as lambda expression.
See also
SingleVariableIteration.newton_raphson
Root-finding method of single variable equation given an initial guess.
trapezoidal
Another implicit method but with second-order accuracy.
Notes
Implicit time stepping scheme which is generally suited for stiff equations being conditionally stable.
Gives first-order accuracy, \(\mathcal{O}(h)\).
- crank_nicholson(power: float = -6, max_iter: int = 100) DataFrame ¶
Implicit method derived from Trapezoidal integration and sometimes called Crank-Nicholson.
- Parameters:
power (int, optional) – Signed power to which function error must be within.
max_iter (int, optional) – Maximum iterations for Newton-Raphson loop.
- Returns:
pandas.DataFrame() – Dataframe of method iterations and time domains, range of approximations for input function, and iterative increments.
- Return type:
dataframe
- step_size¶
Domain step size.
- Type:
float
- iterations, domain, range, increments
Collection of steps through, domain used in, range from, and increments between steps in method.
- Type:
tuple
- Raises:
TypeError – If input `function`(s) cannot be understood as lambda expression.
See also
SingleVariableIteration.newton_raphson
Root-finding method of single variable equation given an initial guess.
Notes
Preferred method to backward_euler because of \(\mathcal{O}(h^{2})\) accuracy.
Tolerance of SingleVariableIteration.newton_raphson defined by 10**power.
- forward_euler() DataFrame ¶
Explicitly march forward through time to approximate IVP differential equation at mesh points between a and b.
- Returns:
pandas.DataFrame() – Dataframe of method iterations and time domains, range of approximations for input function, and iterative increments.
- Return type:
DataFrame
- step_size¶
Domain step size.
- Type:
float
- iterations, domain, range, increments
Collection of steps through, domain used in, range from, and increments between steps in method.
- Type:
tuple
- Raises:
TypeError – If input `function`(s) cannot be understood as lambda expression.
Warning
Error grows linearly; therefore, method is a good place to start, but others should be explored.
Notes
Explicit time stepping scheme via first-order Taylor approximations.
- improved_euler() DataFrame ¶
Explicit implementation of second-order, Runge-Kutta Method. Also known as Modified Euler Method.
- Returns:
pandas.DataFrame() – Dataframe of method iterations and time domains, range of approximations for input function, and iterative increments.
- Return type:
dataframe
- step_size¶
Domain step size.
- Type:
float
- iterations, domain, range, increments
Collection of steps through, domain used in, range from, and increments between steps in method.
- Type:
tuple
- Raises:
TypeError – If input `function`(s) cannot be understood as lambda expression.
See also
Notes
Is equivalent to \(2^{\text{nd}}\)-Order Runge-Kutta (Midpoint) Method where endpoints a and b are 0.5 and \(\lambda\) = 1 with \(\mathcal{O}(h^{2})\).
Explicit time stepping scheme.
- modified_euler() DataFrame ¶
Explicit implementation of second-order, Runge-Kutta Method. Also known as Modified Euler Method.
- Returns:
pandas.DataFrame() – Dataframe of method iterations and time domains, range of approximations for input function, and iterative increments.
- Return type:
dataframe
- step_size¶
Domain step size.
- Type:
float
- iterations, domain, range, increments
Collection of steps through, domain used in, range from, and increments between steps in method.
- Type:
tuple
- Raises:
TypeError – If input `function`(s) cannot be understood as lambda expression.
See also
Notes
Is equivalent to \(2^{\text{nd}}\)-Order Runge-Kutta (Midpoint) Method where endpoints a and b are 0.5 and \(\lambda\) = 1 with \(\mathcal{O}(h^{2})\).
Explicit time stepping scheme.
- runge_kutta() DataFrame ¶
Explicit, fourth-order method.
- Returns:
pandas.DataFrame() – Dataframe of method iterations and time domains, range of approximations for input function, and iterative increments.
- Return type:
dataframe
- step_size¶
Domain step size.
- Type:
float
- iterations, domain, range, increments
Collection of steps through, domain used in, range from, and increments between steps in method.
- Type:
tuple
- Raises:
TypeError – If input `function`(s) cannot be understood as lambda expression.
Notes
Achieves higher-order, local truncation error, \(\mathcal{O}(h^{4})\) like Taylor-based methods–such as forward_euler–but without the need to compute the higher-order derivatives.
Explicit time stepping scheme.
- trapezoidal(power: float = -6, max_iter: int = 100) DataFrame ¶
Implicit method derived from Trapezoidal integration and sometimes called Crank-Nicholson.
- Parameters:
power (int, optional) – Signed power to which function error must be within.
max_iter (int, optional) – Maximum iterations for Newton-Raphson loop.
- Returns:
pandas.DataFrame() – Dataframe of method iterations and time domains, range of approximations for input function, and iterative increments.
- Return type:
dataframe
- step_size¶
Domain step size.
- Type:
float
- iterations, domain, range, increments
Collection of steps through, domain used in, range from, and increments between steps in method.
- Type:
tuple
- Raises:
TypeError – If input `function`(s) cannot be understood as lambda expression.
See also
SingleVariableIteration.newton_raphson
Root-finding method of single variable equation given an initial guess.
Notes
Preferred method to backward_euler because of \(\mathcal{O}(h^{2})\) accuracy.
Tolerance of SingleVariableIteration.newton_raphson defined by 10**power.
- class joby_m_anthony_iii.numerical_methods.Integrate(function: Union[tuple, function], domain: Optional[tuple] = None, a: Union[None, float] = None, b: Union[None, float] = None, h: Union[None, float] = None, scheme: Optional[str] = 'open')¶
Bases:
object
Find the definite integral by some composite numeric quadrature.
- Parameters:
function (tuple or lambda) – Either mapped range from or mapping function for domain.
domain (tuple) – Domain over which function is evaluated.
a (float) – Left and right-hand bound of interval and step-size through that interval.
b (float) – Left and right-hand bound of interval and step-size through that interval.
h (float) – Left and right-hand bound of interval and step-size through that interval.
scheme ({"open", "closed"}, optional) – “open” excludes the endpoints of dataset; conversely, “closed” includes endpoints. Defaults to “open”.
- simpson()¶
Apply Simpson’s Rule.
- trapezoidal()¶
Apply Trapezoidal Quadrature.
- function, domain
Domain and range defining integration bounds.
- Type:
np.ndarray
- a, b, h
Left and right-hand bound of interval and step-size through that interval.
- Type:
float
- scheme¶
Defines integration scheme.
- Type:
string
- Raises:
IndexError – If domain is not a one-dimensional array.
TypeError – If function is not an expression.
Notes
Dataset may contain unevenly spaces points.
Unless specified and if domain is defined, a and b will be the left and right-hand bounds, respectively, of domain. If domain is not defined and a, b, and h are, then domain is constructed by np.arange(a, b, h).
- simpson() Tuple[ndarray, ndarray, float] ¶
Gives exact result for polynomials of degree < 3 because error function utilizes the fourth derivative.
Notes
- Theorem:
Let \(f\) be in \(C^{4}[a,b]\), \(n\) be even, \(h = (b-a)/n\), and \(x_{j} = a + jh\) for \(j = 0, 1, ..., n\). There exists a \(\mu\) in (a,b)` for which the quadrature for \(n\) sub-intervals can be written with its error term as:
\[\int_{a}^{b}f(x)dx = \frac{h}{3}\bigl[ f(a) + 2*[\sum_{j=1}^{n/2 - 1}{f(x_{2j})}] + 4[\sum_{j=1}^{n/2}{f(x_{2j-1})}] + f(b) \bigr] - (b-a)h^{4}f^{\text{iv}}(\mu)/180.\]Where: \((b-a)h^{4}f^{\text{iv}}(\mu)/180 \rightarrow \mathcal{O}(h^{4})\)
- Returns:
X, Y (np.ndarray) – Domain and range used to calculate numeric integral.
F (float) – Numeric integral.
- trapezoidal() Tuple[ndarray, ndarray, float] ¶
Gives exact result for polynomials of degree < 2 because error function utilizes the second derivative.
Notes
- Theorem:
Let \(f\) be in \(C^{2}[a,b]\), \(h = (b-a)/n\), and \(x_{j} = a + jh\) for \(j = 0, 1, ..., n\). There exists a \(\mu \in (a,b)\) for which the quadrature for \(n\) sub-intervals can be written with its error term as:
\[\int_{a}^{b}f(x)dx = \frac{h}{2}\bigl[ f(a) + 2[\sum_{j=1}^{n - 1}{f(x_{j})}] + f(b) \bigr] - (b-a)*(h^{2})f''(\mu)/12.\]Where: \((b-a)*(h^{2})f''(\mu)/12 \rightarrow \mathcal{O}(h^{2})\)
- Returns:
X, Y (np.ndarray) – Domain and range used to calculate numeric integral.
F (float) – Numeric integral.
- class joby_m_anthony_iii.numerical_methods.LeastSquares(domain: tuple, function: tuple)¶
Bases:
object
Interpolate across all points in dataset to minimize error according to rule of fit.
- Parameters:
domain (tuple) – Input domain and range from which to build interpolating polynomial.
function (tuple) – Input domain and range from which to build interpolating polynomial.
- linear(n
`n`th-degree polynomial to fit data.
- Type:
int)
- power¶
Fit power law to data.
- Raises:
IndexError – If domain is not a one-dimensional array.
IndexError – If function is not a one-dimensional array.
IndexError – If domain and function are of unequal length.
- linear(degree: int, variable: Optional[str] = 'x') Tuple[function, float] ¶
Construct a polynomial of some degree while minimizing the least squares error.
- Parameters:
degree (int) – Degree of polynomial.
- Returns:
polynomial (lambda) – Lambdified linear least square polynomial.
error (float) – Total error.
- Raises:
ValueError – If prescribed degree is not an integer greater than zero.
See also
SystemOfEquations.conjugate_gradient
Utilize the Conjugate Gradient Method to solve SOE (if positive definite).
SystemOfEquations.steepest_descent
Utilize the Steepest Descent Method to solve SOE (if positive not definite).
Notes
Least squares error := \(E = \sum_{i=1}^{m}(y_{i} - P_{n}(x_{i}))^{2}\)
Constructed polynomial of the form: \(P(x) = a_{n}x^{n} + a_{n - 1}x^{n - 1} + \dots + a_{1}x + a_{0}\)
- power() Tuple[function, float, float, float] ¶
Given a domain and range, yield the coefficients for an equation and the equation of the form \(y = ax^{b}\) [weissteinLeastSquaresFitting2022].
- Returns:
expression (lambda) – Lambda expression of curve-fit with calculated leading coefficient, a and exponent, b.
error, a, b (float) – Total error, leading coefficient, and exponent of fit equation.
Notes
Least squares error := \(E = \sum_{i=1}^{m}(y_{i} - P_{n}(x_{i}))^{2}\)
Constructed polynomial of the form: \(P(x) = ax^{b}\)
- class joby_m_anthony_iii.numerical_methods.MultiVariableIteration(A: tuple, x: tuple, b: tuple, power: Optional[float] = -6, max_iter: Optional[int] = 100, norm_type: Optional[str] = 'l_infinity')¶
Bases:
object
Iteratively find the solution to a system of equations (SOE): \(\mathbf{A}\vec{x} = \vec{b}\). Ideal for large, sparse systems.
- Parameters:
A (tuple) – Either one-dimensional vector of input functions or matrix of characteristic values.
x (tuple) – Either one-dimensional vector of variables or initial guesses for SOE.
b (tuple) – Solution vector.
power (float, optional) – Signed, specified power of tolerance until satisfying method.
max_iter (int, optional) – Number of iterations.
norm_type ({'l_infinity', 'l_two'}, optional) – String representation of desired norm function. ‘l_infinity’ by default.
- A¶
Either one-dimensional vector of input functions or matrix of characteristic values.
- Type:
np.ndarray
- x¶
Either one-dimensional vector of variables or initial guesses for SOE.
- Type:
np.ndarray
- b¶
Solution vector.
- Type:
np.ndarray
- tol¶
Specified tolerance to which method terminates.
- Type:
float
- max_iter¶
Maximum iterations allowed for method.
- Type:
int
- norm_type¶
String representation of desired norm function.
- Type:
string
- is_diagonal, is_symmetric, is_tridiagonal
Truth value of whether matrix is diagonal, symmetric, and tridiagonal, respectively if not lambda expressions.
- Type:
bool
- eigen_values¶
Eigenvalues of characteristic matrix, A if not lambda expressions.
- Type:
np.ndarray
- spectral_radius, condition_number
Spectral radius and condition number of characteristic matrix, A, respectively if not lambda expressions.
- Type:
float
- find_omega(omega=0)¶
Suggests optimum \(\omega\) over input.
- gauss_seidel()¶
Improves on jacobi() for faster solution.
- jacobi()¶
Iteratively find solution until within tolerance.
- newton_raphson(variables: Tuple[str])¶
Given one-dimensional array of equations respect input variables to build gradient (Jacobian) matrix.
- successive_relaxation(omega=None)¶
Adjusts solution rate of gauss_seidel() by scalar \(\omega\) which is None by default to find the most optimum.
- Raises:
TypeError – Not all elements in matrix of interest (if one-dimensional) are lambda expressions.
IndexError – Matrix of interest must be square.
IndexError – If x is not a one-dimensional array.
IndexError – If b is not a one-dimensional array.
ValueError – If iterations constraint is not an integer greater than zero.
ValueError – If desired norm method was neither ‘l_infinity’ nor ‘l_two’.
See also
diagonality
Determines if matrix, A is strictly, diagonally dominant.
symmetry
Dtermines if matrix, A is symmetric.
tridiagonality
Determines if matrix, A is tridiagonal.
EigenValues.qr_algorithm
Function to find eigenvalues of matrix, A given initial vector, x and solution vector, b..
spectral_radius
Function to find the spectral radius of characteristic matrix, A.
condition_number
Finds the condition number of matrix, A.
SystemOfEquations
Alternative techniques to solve smaller SOE.
Notes
Specified tolerance evaluated by: 10**power.
norm_type may be either ‘l_infinity’ or ‘l_two’. Is ‘l_infinity’ by default.
- find_omega(omega: Optional[float] = 0) float ¶
Given the characteristic matrix and solution vector, determine if prescribed omega is the optimum choice. Will find optimum if not prescribed and possible
- Parameters:
omega (float, optional) – Relaxation parameter.
- Returns:
omega – The \(\omega\) used for Successive Relaxation method.
- Return type:
float
- user_omega¶
Supplied/default omega.
- Type:
float
- is_positive_definite¶
Whether or not matrix, A is positive definite.
- Type:
bool
- best_omega¶
If found, is the optimum choice of omega.
- Type:
float
Warning
If 0 < omega < 2, then method will converge regardless of choice for x.
If an optimal omega cannot be found, then self.best_omega assigned from supplied/default omega.
Will write to logfile that matrix, A is not tridiagonal, but will proceed with calculation all the same.
If matrix, A is poorly defined and not found to be positive definite, then this is written to logfile but calculation proceeds.
See also
tridiagonality
Determines if matrix, A is tridiagonal or not.
spectral_radius
Uses the spectral radius of Gauss-Seidel’s T-matrix to calculate \(\omega\).
Notes
Unless specified and changed from the default, omega=0 \(\omega\) will be chosen if possible.
- gauss_seidel() DataFrame ¶
Given \(\mathbf{A}\vec{x} = \vec{b}\), use norm_type to find \(\vec{x}\) via the Gauss-Seidel Method.
- Returns:
pandas.DataFrame – Summarized dataframe from iterations.
- Return type:
DataFrame
- iterations, approximations, errors
Collection of iterations, approximations, and normative errors through method.
- Type:
np.ndarray
Warning
Writes to logfile whether or not a solution was found within the specified tolerance with the supplied, initial guess.
See also
Norm.l_infinity
Will find \(||x_{i} - x_{0}||_{\infty}\)
Norm.l_two
Will find \(||x_{i} - x_{0}||_{2}\)
Notes
This improves on jacobi by using the most recently calculated entries in the approximation vector, x after each iteration.
The primary algorithm by which method marches approximation vector, x
\[\vec{x}^{(k)} = \bigl( (\mathbf{D} - \mathbf{L})^{-1} * \mathbf{U} \bigr) \cdot \vec{x}^{(k - 1)} + \bigl( (\mathbf{D} - \mathbf{L})^{-1} \bigr) \cdot \vec{b}\]
- jacobi() DataFrame ¶
Given \(\mathbf{A}\vec{x} = \vec{b}\), use norm_type to find \(\vec{x}\) via the Jacobi Method.
- Returns:
pandas.DataFrame – Summarized dataframe from iterations.
- Return type:
DataFrame
- iterations, approximations, errors
Collection of iterations, approximations, and normative errors through method.
- Type:
np.ndarray
Warning
Writes to logfile whether or not a solution was found within the specified tolerance with the supplied, initial guess.
See also
Norm.l_infinity
Will find \(||x_{i} - x_{0}||_{\infty}\)
Norm.l_two
Will find \(||x_{i} - x_{0}||_{2}\)
Notes
The primary algorithm by which method marches approximation vector, x
\[\vec{x}^{(k)} = \bigl( \mathbf{D}^{-1} * (\mathbf{L} + \mathbf{U}) \bigr) \cdot \vec{x}^{(k - 1)} + ( \mathbf{D}^{-1} ) \cdot \vec{b}\]
- newton_raphson(variables: Tuple[str]) DataFrame ¶
Employ the Newton-Raphson Method to find solution of non-linear systems of equations within tolerance.
- Parameters:
variables (tuple) – Collection of string representations for variables to respect in derivations.
- iterations, approximations, errors
Collection of iterations, approximations, and normative errors through method.
- Type:
np.ndarray
- Raises:
TypeError – If an element of variables is not of type string.
Notes
Modified form of MultiVariableIteration to analyze a one-dimensional array of non-linear SOE. Each element should be a lambda expression wherein each variable is represented.
Examples
>>> A = [lambda x1, x2, x3: 3*x1 - sympy.cos(x2*x3) - 1/2, lambda x1, x2, x3: x1**2 - 81*(x2 + 0.1)**2 + sympy.sin(x3) + 1.06, lambda x1, x2, x3: sympy.exp(-x1*x2) + 20*x3 + (10*math.pi - 3)/3 ] >>> x, b = (0.1, 0.1, -0.1), (0, 0, 0) >>> variables = ("x1", "x2", "x3") >>> MultiVariableIteration(A, x, b).newton_raphson(variables)["Approximations"].values[-1] [0.5, 0., -0.52359877]
- successive_relaxation(omega: Union[None, float] = None) DataFrame ¶
Given \(\mathbf{A}\vec{x} = \vec{b}\), use norm_type to find \(\vec{x}\) via the Successive Relaxation Method. Is Successive Over-Relaxation (SOR) if omega > 1, Successive Under-Relaxation (SUR) if omega < 1, and is Gauss-Seidel if omega = 1.
- Parameters:
omega (None or float, optional) – Relaxation parameter.
- iterations, approximations, errors
Collection of iterations, approximations, and normative errors through method.
- Type:
np.ndarray
- Returns:
pandas.DataFrame – Summarized dataframe from iterations.
- Return type:
DataFrame
Warning
Writes to logfile optimal choice of omega, regardless of assignment, and whether or not a solution was found within the specified tolerance with the supplied, initial guess.
See also
find_omega
Will analyze SOE to find an optimal \(\omega\), if possible.
gauss_seidel
Gauss-Seidel Method modified by omega.
Norm.l_infinity
Will find \(||x_{i} - x_{0}||_{\infty}\)
Norm.l_two
Will find \(||x_{i} - x_{0}||_{2}\)
Notes
SOR and SUR modify, respectively, on gauss_seidel by decreasing or increasing, respectively, the spectral radius of A to accelerate or deccelerate convergence, respectively.
The primary algorithm by which method marches approximation vector, x
\[\vec{x}^{(k)} = \bigl( (\mathbf{D} - \omega\mathbf{L})^{-1} * ((1 - \omega)*\mathbf{D} + \omega\mathbf{U}) \bigr) \cdot \vec{x}^{(k - 1)} + \omega( (\mathbf{D} - \omega\mathbf{L})^{-1} ) \cdot \vec{b}\]which is similar to gauss_seidel
\[\vec{x}^{(k)} = \bigl( (\mathbf{D} - \mathbf{L})^{-1} * \mathbf{U} \bigr) \cdot \vec{x}^{(k - 1)} + \bigl( (\mathbf{D} - \mathbf{L})^{-1} \bigr) \cdot \vec{b}\]omega will be analyzed independent of assigned value which will be used if not specified in assignment and if possible.
- class joby_m_anthony_iii.numerical_methods.Norm(x: tuple, x0: Optional[tuple] = None)¶
Bases:
object
Find the natural norm of a vector or between two vectors.
- Parameters:
x (tuple) – Newly and previously (optional) approximated array.
x0 (tuple) – Newly and previously (optional) approximated array.
- x, x0
Newly and previously (if given) approximated array.
- Type:
np.ndarray
- l_infinity(), l_two()¶
Evaluates the \(l_{\infty}\) or \(l_{2}\) norm, respectively.
- Raises:
IndexError – If the input vectors are not the same length.
Notes
- Definition [burdenNumericalAnalysis2016]:
- A matrix norm on the set of all \(n \times n\) matrices is a real-valued function, \(||\cdot||\), defined on this set, satisfying for all \(n \times n\) matrices \(\mathbf{A}\) and \(\mathbf{B}\) and all real numbers \(\alpha\):
\(||\mathbf{A}|| \geq 0\);
\(||\mathbf{A}|| = 0\) iff \(\mathbf{A}\) is a matrix with all zero entries;
\(||\alpha\mathbf{A}|| = |\alpha|||\mathbf{A}||\);
\(||\mathbf{A} + \mathbf{B}|| \leq ||\mathbf{A}|| + ||\mathbf{B}||\);
\(||\mathbf{A}\mathbf{B}|| \leq ||\mathbf{A}||||\mathbf{B}||\)
- Theorem [burdenNumericalAnalysis2016]:
If \(||\cdot||\) is a vector norm on \(\mathbb{R}^{n}\), then
\[||\mathbf{A}|| = \underset{||\vec{x}|| = 1}{\max}||\mathbf{A}\vec{x}||\]is a matrix norm.
- l_infinity() float ¶
Maximum difference between absolute sum of i’th rows.
- Returns:
norm – Scalar value.
- Return type:
float
- norm¶
Scalar value.
- Type:
float
Notes
Best thought as “actual” distance between vectors.
Also calculates infinity norm of matri(x/ces).
Examples
\[\begin{split}\vec{x0} &= (1, 1, 1)^{(t)} \\ \vec{x} &= (1.2001, 0.99991, 0.92538)^{(t)} \\ \implies ||x - x0|| &= max{|1.2001 - 1|, |0.99991 - 1|, |0.92538 - 1|} \\ &= 0.2001\end{split}\]
- l_two() float ¶
Square root of sum of differences squared along i’th row.
- Returns:
norm – Scalar value.
- Return type:
float
- norm¶
Scalar value.
- Type:
float
See also
spectral_radius
Function to find the spectral radius of vector.
Examples
\[\begin{split}\vec{x0} &= (1, 1, 1)^{(t)} \\ \vec{x} &= (1.2001, 0.99991, 0.92538)^{(t)} \\ \implies ||x - x0|| &= \sqrt{(1.2001 - 1)^{2} + (0.99991 - 1)^{2} + (0.92538 - 1)^{2}} \\ &= 0.21356\end{split}\]
- class joby_m_anthony_iii.numerical_methods.SingleVariableIteration(function: function, a: float, b: float, power: Optional[float] = -6, variable: Optional[str] = 'x', iter_guess: Optional[Union[bool, int]] = True, function_slope: Optional[float] = 0)¶
Bases:
object
Given \(f(x)\) such that \(x \in [a, b]\), find the root of a single-variable, equation within tolerance.
- Parameters:
function (lambda) – Input function.
a (float) – Left and right-hand bound of interval, respectively.
b (float) – Left and right-hand bound of interval, respectively.
power (float, optional) – Signed, specified power of tolerance until satisfying method.
variable (string, optional) – Respected variable in derivative. Assumed to be ‘x’ if not stated.
iter_guess (bool or integer, optional) – Boolean value of True by default. If integer, iterate for that integer.
function_slope (float, optional) – Absolute maximum slope of function.
- function¶
Input function.
- Type:
expression
- variable¶
Respected variable in derivative. Assumed to be ‘x’ if not stated.
- Type:
string, optional
- a, b
Left and right-hand bound of interval, respectively.
- Type:
float
- tol¶
Tolerance to satisfy method.
- Type:
float
- iter_guess¶
Boolean value of True by default. If integer, iterate for that integer.
- Type:
bool or integer
- function_slope¶
Absolute maximum slope of functon. Assumed 0 if not defined.
- Type:
float
- find_k()¶
Find the greatest value for first derivative of function.
- max_iterations()¶
Find maximum number of iterations for method if not provided.
- bisection()¶
Search for solution by halving the bounds wherein a and b initially yield opposite signs in function.
- false_position(p0: float, p1: float)¶
solution bounded by a and b wherein initial guesses p0 and p1 yield opposite signs in function.
- fixed_point(p0: float)¶
Root-finding method to find solution near initial guess.
- newton_raphson(p0: float)¶
Root-finding method to find solution near initial guess.
- secant_method(p0: float, p1: float)¶
Initial guesses p0 and p1 must yield opposite signs in function. Solution is NOT bounded by a and b.
- Raises:
TypeError – If input function cannot be understood as lambda expression.
Notes
self.tol evaluated by: 10**power.
- Convergence Rates:
newton_raphson > secant_method > false_position > fixed_point > bisection
- bisection() DataFrame ¶
Root-finding method: \(f(x) = 0\) [burdenNumericalAnalysis2016].
- Returns:
pandas.DataFrame – Summarized dataframe from iterations.
- Return type:
DataFrame
- iterations, approximations, errors
Collection of iterations, evaluated points, p and propogated errors through method.
- Type:
np.ndarray
- Raises:
ValueError – If input for desired iterations was assigned not an integer greater than zero.
ValueError – If bounds did not evaluate to have opposite signs from function.
TypeError – If input function cannot be understood as lambda expression.
Warning
Writes to logfile if solution was found, or state that solution did not converge with given guess or prescribed tolerance.
Notes
Relying on the Intermediate Value Theorem (IVT), this is a bracketed, root-finding method. Generates a sequence \({p_{n}}_{n=1}^{\infty}\) such \(f(x=p_{n}) = 0\) and converges by \(\mathcal{O}(1 / (2^{N}))\) [burdenNumericalAnalysis2016]. This method is rather slow to converge but will always converge to a solution; therefore, is a good starter method.
Examples
If f(x) = x**3 + 4*x**2 = 10
=> f(x) = x**3 + 4*x**2 - 10 = 0
- false_position(p0: float, p1: float) DataFrame ¶
Attempt method with initial guesses, p0 and p1 in [a, b].
Is root-finding method by solving the equation \(g(p) = p\) via \(f(p) - p = 0\).
!!! Use function with lowest slope !!!
- Parameters:
p0 (float) – Initial guesses.
p1 (float) – Initial guesses.
- Returns:
pandas.DataFrame – Summarized dataframe from iterations.
- Return type:
DataFrame
- iterations, approximations, errors
Collection of iterations, evaluated points, p and propogated errors through method.
- Type:
np.ndarray
- Raises:
ValueError – If input for desired iterations was assigned not an integer greater than zero.
ValueError – If initial guesses did not evaluate to have opposite signs from function.
TypeError – If input function cannot be understood as lambda expression.
Warning
Writes to logfile if solution was found, or state that solution did not converge with given guess or prescribed tolerance.
Notes
Similar to secant_method but includes a test to ensure solution is root-bracketed and is therefore slower to converge than the secant_method.
Check that \(|g'(x)| \leq (\text{leading coefficient of g'(x)})\) for all \(x \in [a, b]\).
Theorem: 1) Existence of a fixed-point:
If \(g \in C[a,b]\) and \(g(x) \in C[a, b]\) for all \(x \in [a, b]\), then function, \(g\) has a fixed point, \(p \in [a, b]\).
- Uniqueness of a fixed point:
If \(g'(x)\) exists on \([a, b]\) and a positive constant, k < 1 exist with \(\{|g'(x)| \leq k | x \in (a, b)\}\), then there is exactly one fixed-point, \(p \in [a, b]\).
Converges by \(\mathcal{O}(\text{linear})\) if \(g'(p) \neq 0\), and \(\mathcal{O}(\text{quadratic})\) if \(g'(p) = 0\) and \(g''(p) < M\), where \(M = g''(\xi)\) that is the error function.
Examples
If g(x) = x**2 - 2
Then p = g(p) = p**2 - 2
=> p**2 - p - 2 = 0
- find_k() float ¶
Find greatest integer for maximum iterations for tolerance.
- Returns:
k – Maximum possible slope of input function.
- Return type:
float
- self.function_slope¶
Maximum possible slope of input function.
- Type:
float
- fixed_point(p0: float) DataFrame ¶
Attempt method with initial guess, p0 in [a, b].
Is root-finding method by solving the equation \(g(p) = p\) via \(f(p) - p = 0\).
!!! Use function with lowest slope !!!
- Parameters:
p0 (float) – Initial guess.
- Returns:
pandas.DataFrame – Summarized dataframe from iterations.
- Return type:
DataFrame
- iterations, approximations, errors
Collection of iterations, evaluated points, p and propogated errors through method.
- Type:
np.ndarray
- Raises:
ValueError – If input for desired iterations was assigned not an integer greater than zero.
TypeError – If input function cannot be understood as lambda expression.
Warning
Writes to logfile if solution was found, or state that solution did not converge with given guess or prescribed tolerance.
Notes
Not root-bracketed!
Check that \(|g'(x)| \leq (\text{leading coefficient of g'(x)})\) for all \(x \in [a, b]\).
Theorem: 1) Existence of a fixed-point:
If \(g \in C[a,b]\) and \(g(x) \in C[a, b]\) for all \(x \in [a, b]\), then function, \(g\) has a fixed point, \(p \in [a, b]\).
- Uniqueness of a fixed point:
If \(g'(x)\) exists on \([a, b]\) and a positive constant, k < 1 exist with \(\{|g'(x)| \leq k | x \in (a, b)\}\), then there is exactly one fixed-point, \(p \in [a, b]\).
Converges by \(\mathcal{O}(\text{linear})\) if \(g'(p) \neq 0\), and \(\mathcal{O}(\text{quadratic})\) if \(g'(p) = 0\) and \(g''(p) < M\), where \(M = g''(\xi)\) that is the error function.
Examples
If g(x) = x**2 - 2
Then p = g(p) = p**2 - 2
=> p**2 - p - 2 = 0
- max_iterations(method: str, p0: Optional[float] = 0) int ¶
Find greatest integer for maximum iterations within tolerance.
- Parameters:
method ({"bisection", "fixed_point", "newton_raphson", "secant_method", "false_position"}) – Selection of iterative method for iterations are needed.
p0 (float, optional) – Initial guess for function solution. Not needed for “bisection” method.
- Returns:
max_iter – Maximum number of iterations required for specified tolerance.
- Return type:
int
- max_iter¶
Maximum number of iterations required for specified tolerance.
- Type:
int
- Raises:
ValueError – Prescribed method is not an available option.
Warning
Will round away from zero to higher integers.
Notes
Informs user in logfile the maximum number of iterations for method.
Examples
If method == “bisection” & `a`=1, `b`=2, and `power`=-3, then:
\[\begin{split}N &\geq -\log(`tol`/(`b` - `a`))/\log(2) \\ &\geq -\log((10^{-3}/(2 - 1))/\log(2) \\ &\geq 9.96 \\ \implies N = 10\end{split}\]Else, if `a`=1, `b`=2, `power`=-3, `p0`=1.5, nd `k`=0.9, then:
\[\begin{split}N &\geq \log(`tol`/max('p0' - `a`, `b` - `p0`))/log(`k`) \\ &\geq \log(10^{-3}/max(1.5 - 1, 2 - 1.5))/\log(0.9) \\ &\geq \log(10^{-3}/0.5)/\log(0.9) \\ &\geq 58.98 \\ \implies N >= 59\end{split}\]
- newton_raphson(p0: float) DataFrame ¶
Attempt method with initial guess, p0 in [a, b].
Is root-finding method by solving the equation \(g(p) = p\) via \(f(p) - p = 0\).
!!! Use function with lowest slope !!!
- Parameters:
p0 (float) – Initial guess.
- Returns:
pandas.DataFrame – Summarized dataframe from iterations.
- Return type:
DataFrame
- iterations, approximations, errors
Collection of iterations, evaluated points, p and propogated errors through method.
- Type:
np.ndarray
- Raises:
ValueError – If input for desired iterations was assigned not an integer greater than zero.
TypeError – If input function cannot be understood as lambda expression.
Warning
Write to logfile if solution was found, or state that solution did not converge with given guess or prescribed tolerance.
Not root-bracketed and has trouble with symmetric functions!
\(f'(x) \neq 0\)
Notes
Initial guess, p0 must be close to real solution; else, will converge to different root or oscillate (if symmetric).
Newton-Raphson has quickest convergence rate.
This method can be viewed as fixed-point iteration.
Check that \(|g'(x)| \leq (\text{leading coefficient of g'(x)})\) for all \(x \in [a, b]\).
Technique based on first Taylor polynomial expansion of \(f\) about \(p_{0}\) (that is p0) and evaluated at \(x = p\). \(|p - p_{0}|\) is assumed small; therefore, \(2^{\text{nd}}\)-order Taylor term, the error, is small.
Theorem: 1) Existence of a fixed-point:
If \(g \in C[a,b]\) and \(g(x) \in C[a, b]\) for all \(x \in [a, b]\), then function, \(g\) has a fixed point, \(p \in [a, b]\).
- Uniqueness of a fixed point:
If \(g'(x)\) exists on \([a, b]\) and a positive constant, k < 1 exist with \(\{|g'(x)| \leq k | x \in (a, b)\}\), then there is exactly one fixed-point, \(p \in [a, b]\).
Converges by \(\mathcal{O}(\text{linear})\) if \(g'(p) \neq 0\), and \(\mathcal{O}(\text{quadratic})\) if \(g'(p) = 0\) and \(g''(p) < M\), where \(M = g''(\xi)\) that is the error function.
Examples
If g(x) = x**2 - 2
Then p = g(p) = p**2 - 2
=> p**2 - p - 2 = 0
- secant_method(p0: float, p1: float) DataFrame ¶
Attempt method with initial guesses, p0 and p1 in [a, b].
Is root-finding method by solving the equation \(g(p) = p\) via \(f(p) - p = 0\).
!!! Use function with lowest slope !!!
- Parameters:
p0 (float) – Initial guesses.
p1 (float) – Initial guesses.
- Returns:
pandas.DataFrame – Summarized dataframe from iterations.
- Return type:
DataFrame
- iterations, approximations, errors
Collection of iterations, evaluated points, p and propogated errors through method.
- Type:
np.ndarray
- Raises:
ValueError – If input for desired iterations was assigned not an integer greater than zero.
ValueError – If initial guesses did not evaluate to have opposite signs from function.
TypeError – If input function cannot be understood as lambda expression.
Warning
Write to logfile if solution was found, or state that solution did not converge with given guess or prescribed tolerance.
Notes
Not root-bracketed.
Method is less computationally expensive than newton_raphson may converge at lower rate by circumventing need to calculate derivative.
Check that \(|g'(x)| \leq (\text{leading coefficient of g'(x)})\) for all \(x \in [a, b]\).
Theorem: 1) Existence of a fixed-point:
If \(g \in C[a,b]\) and \(g(x) \in C[a, b]\) for all \(x \in [a, b]\), then function, \(g\) has a fixed point, \(p \in [a, b]\).
- Uniqueness of a fixed point:
If \(g'(x)\) exists on \([a, b]\) and a positive constant, k < 1 exist with \(\{|g'(x)| \leq k | x \in (a, b)\}\), then there is exactly one fixed-point, \(p \in [a, b]\).
Converges by \(\mathcal{O}(\text{linear})\) if \(g'(p) \neq 0\), and \(\mathcal{O}(\text{quadratic})\) if \(g'(p) = 0\) and \(g''(p) < M\), where \(M = g''(\xi)\) that is the error function.
Examples
If g(x) = x**2 - 2
Then p = g(p) = p**2 - 2
=> p**2 - p - 2 = 0
- class joby_m_anthony_iii.numerical_methods.SystemOfEquations(A: tuple, b: tuple, power: Optional[float] = -6, max_iter: Optional[int] = 100)¶
Bases:
object
Solve a linear system of equations (SOE): \(\mathbf{A}\vec{x} = \vec{b}\).
- Parameters:
A (tuple) – Characteristic matrix of coefficients and vector that is particular solution from SOE.
b (tuple) – Characteristic matrix of coefficients and vector that is particular solution from SOE.
power (float, optional) – Signed power to which function error must be within.
max_iter (int, optional) – Maximum iterations for which function may loop.
- A, b
Characteristic matrix of coefficients and particular solution from SOE.
- Type:
np.ndarray
- tol¶
Specified tolerance to which method terminates.
- Type:
float
- max_iter¶
Maximum iterations allowed for method.
- Type:
int
- is_diagonal, is_positive_definite, is_symmetric, is_tridiagonal
Truth value of whether matrix is diagonal, positive definite, symmetric, and tridiagonal.
- Type:
bool
- conjugate_gradient(x: tuple, C=None)¶
Iteratively solves SOE within as many iterations as number of equations. Pre-conditions by default to solve with \(\sqrt{n}\) iterations.
- gaussian_elimination()¶
Perform Gaussian Elimination with Back-Substitution on SOE.
- steepest_descent(x: tuple)¶
Iteratively solve SOE.
- Raises:
IndexError – Matrix of interest must be square.
IndexError – If b is not a one-dimensional array.
ValueError – If iterations constraint is not an integer.
Notes
Specified tolerance evaluated by 10**power.
If is_diagonal is True, then matrix, A is strictly, diagonally dominant. Else, not strictly, diagonally dominant. Similar for is_tridiagonal.
- conjugate_gradient(x: tuple, C: Optional[Union[tuple, bool]] = None) DataFrame ¶
Use initial guess vector, x and (if desired) pre-conditioning matrix, C to solve SOE: \(\mathbf{A}\vec{x} = \vec{b}\).
- Parameters:
x (tuple) – Vector that is initial guess to solution for SOE.
C (tuple or bool, optional) – Pre-conditioning matrix. Will pre-condition by default. If set to True, will use the diagonal of matrix, A.
- Returns:
pandas.DataFrame – Summarized dataframe from iterations.
- Return type:
DataFrame
- x¶
Initial guess for solution.
- Type:
np.ndarray
- C¶
Stores matrix used for pre-conditioning if not None.
- Type:
None or np.ndarray
- iterations, approximations, errors
Collection of iterations, approximations, and normative errors through method.
- Type:
np.ndarray
- Raises:
ValueError – If A is not positive definite.
IndexError – If x is not a one-dimensional array.
Notes
More computationally expensive than gaussian_elimination for smaller systems and is best suited for large, sparse matrices. If pre-conditioned, can solve in \(\sqrt{n}\) iterations.
- gaussian_elimination() ndarray ¶
Directly find the solution to \(\mathbf{A}\vec{x} = \vec{b}\) by Gaussian Elimination with Back Substitution.
- Returns:
x – Input vector to SOE.
- Return type:
np.ndarray
- Aug¶
Augmented matrix representation of SOE.
- Type:
np.ndarray
- Raises:
ValueError – If a unique solution could not be find which indicates linearly dependent SOE.
- steepest_descent(x: tuple) DataFrame ¶
Approximate solution vector, x given matrix, A initial guess vector, x, and vector, b.
- Parameters:
x (tuple) – Initial guess for input vector to SOE.
- Returns:
pandas.DataFrame – Summarized dataframe from iterations.
- Return type:
DataFrame
- x¶
Initial guess for solution.
- Type:
np.ndarray
- iterations, approximations, errors
Collection of iterations, approximations, and normative errors through method.
- Type:
np.ndarray
- Raises:
IndexError – If x is not a one-dimensional array.
- joby_m_anthony_iii.numerical_methods.condition_number(A: tuple, norm_type: Optional[str] = 'l_two') float ¶
Find the condition number of a given matrix and norm type.
- Parameters:
A (tuple) – Input matrix for analysis.
norm_type ({'l_two', 'l_infinity'}, optional) – Selects norm comparison which is ‘l_two’ by default.
- Returns:
K – Condition number of matrix, A.
- Return type:
float
- Raises:
ValueError – If input norm_type is not understood as neither ‘l_infinity’ nor ‘l_two’.
See also
Norm.l_infinity
Yields the \(l_{\infty}\) norm.
Norm.l_two
Yields the \(l_{2}\) norm.
Notes
Will write evaluation of condition number to logfile.
- Definition [burdenNumericalAnalysis2016]:
The condition number of the non-singular matrix, \(\mathbf{A}\) relative to a norm, \(||\cdot||\) is
\[K(\mathbf{A}) = ||\mathbf{A}|| \cdot ||\mathbf{A}^{-1}||\]
A matrix is well-conditioned if \(K(\mathbf{A})\) is close to 1 and is ill-conditioned if significantly greater than 1.
- joby_m_anthony_iii.numerical_methods.diagonality(A: tuple) bool ¶
Determines if matrix, A is strictly, diagonally dominant.
- Parameters:
A (tuple) – Input matrix to be tested.
- Returns:
is_strictly_diagonal – Truth value whether matrix is strictly, diagonally dominant.
- Return type:
bool
- Raises:
IndexError – Matrix of interest must be square.
Notes
Will write to logfile either if strictly, diagonally dominant, or if matrix, A is not strictly, diagonally dominant which could lead to poor solution of \(\mathbf{A}\vec{x} = \vec{b}\).
- joby_m_anthony_iii.numerical_methods.eigen_values(A: tuple) ndarray ¶
Directly finds eigenvalues of matrix by its determinant. Not recommended for large, sparse matrices.
- Parameters:
A (tuple) – Matrix of interest.
- Returns:
lambdas – Eigenvector containing roots.
- Return type:
np.ndarray
- Raises:
IndexError – Matrix of interest must be square.
Warning
Not recommended to ever use because main logic is sympy.solve(sympy.det(A - I)) wherein I is the identity matrix to A.
- joby_m_anthony_iii.numerical_methods.gaussian_legendre(function, a, b)¶
- joby_m_anthony_iii.numerical_methods.hermite(domain: tuple, function: tuple, variable: Optional[str] = 'x', function_derivative: Optional[tuple] = None) function ¶
Given a domain and function, construct a Hermetic polynomial.
- Parameters:
domain (tuple) – Input domain, range, and derivative (optional) from which to build Hermetic polynomial.
function (tuple) – Input domain, range, and derivative (optional) from which to build Hermetic polynomial.
function_derivative (tuple) – Input domain, range, and derivative (optional) from which to build Hermetic polynomial.
variable (string) – Respected variable in derivative of equation. Defaults to “x”.
- Returns:
polynomial – Lambdified Hermetic polynomial.
- Return type:
lambda
- Raises:
IndexError – If domain is not a one-dimensional array.
IndexError – If function is not a one-dimensional array.
IndexError – If domain and function are of unequal length.
TypeError – If function_derivative is not an expression or function and is not an one-dimensional array.
IndexError – If domain, function, or function_derivative are of unequal lengths.
TypeError – If function_derivative is not given and function is not an expression, then missing derivative data or expression.
- Warns:
MadePolyInformation (string) – Displays the string form of the equation.
Warning
Slow computation time for larger datasets.
Notes
function_derivative calculated if not specified.
Osculating curve incorporates Taylor and Lagrangian polynomials to kiss the data and match each data point’s derivatives which fits the curve to the shape of the data and its trend.
- joby_m_anthony_iii.numerical_methods.lagrange(domain: tuple, function: tuple, degree: Optional[int] = None, variable: Optional[str] = 'x') Tuple[function, ndarray] ¶
Given a domain and range, construct a Lagrangian polynomial.
- Parameters:
domain (tuple) – Input domain and range from which to build Lagrangian polynomial.
function (tuple) – Input domain and range from which to build Lagrangian polynomial.
degree (int) – Degree of polynomial.
variable (string) – Respected variable in derivative of equation. Defaults to “x”.
- Returns:
polynomial (lambda) – Lambdified Lagrangian polynomial.
errors (np.ndarray) – Propogation of bounding error through construction.
- Raises:
IndexError – If domain is not a one-dimensional array.
IndexError – If function is not a one-dimensional array.
IndexError – If domain and function are of unequal length.
Notes
Polynomial will quickly begin to oscillate for larger datasets.
Polynomial is of the following form
\[\begin{split}P(x) &= f(x_{0})L_{n,0}(x) + ... + f(x_{n})L_{n,n}(x) \text{, where} \\ L_{n,k} &= \prod_{i=0, i \neq k}^{n} (x - x_{i})/(x_{k} - x_{i})\end{split}\]Examples
A Lagrange polynomial between (2,4) and (5,1) would be found as follows
\[\begin{split}L_{0}(x) &= (x - 5)/(2 - 5) = -(x - 5)/3 \\ L_{1}(x) &= (x - 2)/(5 - 2) = (x - 2)/3 \\ \implies P(x) &= (4)*(-(x - 5)/3) + (1)*((x - 2)/3) \\ &= -x + 6\end{split}\]
- joby_m_anthony_iii.numerical_methods.linear_interpolation(x0: float, y0: float, x1: float, y1: float, x: float) float ¶
y = y0 + (x - x0)*(y1 - y0)/(x1 - x0)
- joby_m_anthony_iii.numerical_methods.make_array(domain: tuple, function: function) ndarray ¶
Maps domain to range.
- Parameters:
domain (tuple) – Collection if input data.
function (lambda) – Function that maps the domain to range.
variable (string, optional) – String representation of variable to respect in function.
- Returns:
mapped – Mapped range from function.
- Return type:
np.ndarray
Notes
Writes to logfile the input expression, and that the expression was in fact used.
If the input function happens to already be a NumPy array, then that array will simply be returned without processing.
- joby_m_anthony_iii.numerical_methods.newton_difference(domain: tuple, function: Union[tuple, function], center_point: float, variable: Optional[str] = 'x', direction: Optional[str] = 'auto') function ¶
Given a domain and range, construct some polynomial by Newton’s Divided Difference.
- Parameters:
domain (tuple) – Input domain.
function (tuple or lambda) – Desired/Found range of interest.
center_point (float) – Point about which polynomial is evaluated.
variable (string, optional) – Dependent variable to respect in polynomial construction. Defaults to “x”.
direction ({"auto", "forward", "backward"}, optional) – ‘forward’ or ‘backward’ construction. Will be chosen automatically if not specified.
- Returns:
polynomial – Lambdified constructed polynomial.
- Return type:
lambda
- Raises:
IndexError – If domain is not a one-dimensional array.
IndexError – If function is not a one-dimensional array.
IndexError – If domain and function are of unequal length.
ValueError – If direction is neither ‘forward’ nor ‘backward’.
- Warns:
MadePolynomialInformation (string) – Displays the string form of the equation.
See also
make_array
Maps inputs function, if lambda expression, to range from domain.
Notes
Direction will be chosen if not specified.
Polynomials best made with even spacing in domain; although, this is not completely necessary.
- joby_m_anthony_iii.numerical_methods.positive_definite(A: tuple) bool ¶
Determines boolean truth value whether given matrix is positive definite.
- Parameters:
A (tuple) – Matrix of interest.
- Returns:
is_positive_definite – True if positive definite, else False.
- Return type:
bool
- Raises:
IndexError – Matrix of interest must be square.
Notes
Writes to logfile that A is either positive definite or not.
- joby_m_anthony_iii.numerical_methods.richard_extrapolation(function: Union[function, tuple], center_point: float, h: float, order: int, direction: Optional[str] = 'auto', variable: Optional[str] = 'x') function ¶
Results in higher-accuracy of derivative at point in function with lower-order formulas to minimize round-off error and increase \(\mathcal{O}(h)\) of truncation error.
- Parameters:
function (lambda or tuple) – Polynomial over which derivative must be calculated.
center_point (float) – Point about and step-size through which extrapolation centers.
h (float) – Point about and step-size through which extrapolation centers.
order (int) – Order for rate of convergence.
direction ({"auto", "forward", "backward"}, optional) – ‘forward’ or ‘backward’ construction. Will choose direction if not specified.
variable (string, optional) – Dependent variable to respect in polynomial construction. Defaults to “x”.
- Returns:
polynomial – Lambdified constructed polynomial.
- Return type:
lambda
- Raises:
ValueError – order must be an integer greater than zero.
ValueError – If direction is neither ‘forward’ nor ‘backward’.
TypeError – If function is not an expression.
See also
newton_difference
Newton Difference method to build extrapolation for function’s derivative and order of error.
- joby_m_anthony_iii.numerical_methods.spectral_radius(A: tuple) float ¶
Finds the spectral radius of matrix.
- Parameters:
A (tuple) – Matrix of interest.
- Returns:
rho – Spectral radius.
- Return type:
float
- Raises:
IndexError – Matrix of interest must be square.
See also
EigenValues.qr_algorithm
Function to find eigenvector of matrix, A according to QR Algorithm.
Notes
\(\rho(\mathbf{A}) = \max|\lambda|\), where \(\lambda\) is the set of eigenvalues for A [burdenNumericalAnalysis2016].
- joby_m_anthony_iii.numerical_methods.symmetry(A: tuple) bool ¶
Determines boolean truth value whether given matrix is symmetric.
- Parameters:
A (tuple) – Matrix of interest.
- Returns:
is_symmetric – True if symmetric, else False.
- Return type:
bool
- Raises:
IndexError – Matrix of interest must be square.
Notes
Writes to logfile that A is either symmetric or asymmetric.
- class joby_m_anthony_iii.numerical_methods.test¶
Bases:
object
- test()¶
Was the module loaded correctly?
:raises success : string: Prints a message of successful function call.
- joby_m_anthony_iii.numerical_methods.tridiagonality(A: tuple) bool ¶
Determine boolean truth value whether given matrix is tridiagonal.
- Parameters:
A (tuple) – Matrix of interest.
- Returns:
is_tridiagonal – True if tridiagonal, else False.
- Return type:
bool
- Raises:
IndexError – Matrix of interest must be square.
Notes
Writes to logfile that matrix is either tridiagonal or not.