Welcome to the Prac_Codes repository! This collection of Python scripts is designed to assist with solving a variety of mathematical and computational problems, particularly in the fields of linear and non-linear equations, numerical integration, and mathematical physics.
- Lineq: Tools for solving linear equations.
- Nonlineq: Methods for solving non-linear equations. (Also includes tools for interpolation.)
- Numintegrate: Numerical integration techniques.
- MathPhysEqs: Solutions for mathematical physics equations.
- Equation Solvers: Efficient algorithms for both linear and non-linear systems.
- Matrix Operations: Comprehensive functions for matrix manipulations.
- Polynomial Manipulation: Utilities for handling polynomials, including interpolation and fitting.
- Numerical Methods: Advanced numerical integration and differential equation solvers.
- Clone the repository:
git clone https://github.com/VIA-s-acc/Prac_Codes.git
- Navigate to the directory:
cd Prac_Codes
- Run the examples:
Explore the folders for example scripts and usage demonstrations.
- Python 3.x
- Required libraries: numpy, scipy, matplotlib (install via pip if not already available)
Each module is designed to be user-friendly. Refer to the example scripts in each folder for guidance on how to utilize the provided functions and classes.
For any inquiries or support, please contact VIA-s-acc.
This classes is a linear equation solver system that provides methods for performing various operations related to solving systems of linear equations. Here's a brief summary of each class method:
This `Checker` class contains three methods:-
_signum(num)
:- Calculates the signum of the given number.
-
_check_solve_web(matrix, b, size, dig, solution, epsilon)
:- Solves a system of linear equations using a web-based matrix calculator.
-
_sylvesters_criterion(matrix)
:- Checks if the given matrix satisfies Sylvester's criterion for positive definiteness.
-
_diagonal_domination(matrix)
:- Checks if the given matrix is diagonal dominant.
-
_symmetric_check(matrix)
:- Checks if the given matrix is symmetric.
This class Generator contains two static methods:
-
generate_random_matrix(size, rng: int = 10, mode: str = None)
:- Generates a random matrix of the given size, with an optional mode for symmetric or 3-diagonal matrix.
-
generate_random_vector(size, rng: int = 10)
:- Generates a random vector of the specified size and range.
This class contains methods for various matrix operations:
-
det(matrix)
:- Calculates the determinant of a square matrix.
-
LU_decomposition(matrix)
:- Performs LU decomposition on the given matrix and returns the lower and upper triangular matrices.
-
cholesky_decomposition_v1(matrix)
:- Performs Cholesky decomposition and returns the lower and upper triangular matrices.
-
cholesky_decomposition_v2(matrix)
:- Performs Cholesky decomposition and returns the lower triangular matrix, diagonal matrix, and upper triangular matrix.
-
_matrix_multiply(*matrices)
:- Performs matrix multiplication on the input matrices and returns the resulting matrix.
-
euclidean_norm(vec)
:- Calculates the Euclidean norm of a vector.
-
_scalar_matrix_multiply(scalar, matrix)
:- Performs matrix multiplication on the input matrices and returns the resulting matrix.
-
_vector_matrix_multiply(matrix, vector)
:- Multiply a matrix by a vector and return the resulting vector.
-
_vector_approximation(v1, v2, tol=1e-6)
:- Check if two vectors are approximately equal within a tolerance.
-
_inverse_matrix(matrix)
:- Calculate the inverse of a matrix using Gauss-Jordan elimination.
-
eigen_get(matrix, max_iter=100, eps=1e-6)
:- Calculate the max_min eigenvalues and eigenvectors of the given matrix using the power method algorithm.
-
power_method(matrix, max_iter=100, eps=1e-6)
:- Perform the power method to find the dominant eigenvalue and eigenvector of the given matrix.
This Reader
class has two methods:
-
read_matrix_from_file(filename)
:- Reads a matrix from a file and returns a 2D list representing the matrix.
-
read_vector_from_file(filename)
:- Reads a vector from a file and returns a list containing the integers read from the file.
This class, Saver
, provides methods to save matrices and vectors to files, and to combine text files into a single output file.
-
save_matrix_to_file
:- Saves the given matrix to a file, with an optional 'prettier' mode for formatting.
-
save_vector_to_file
:- Saves the given vector to the specified file.
-
_combine_txt
:- Combines text files in a specified folder into a single output file, with an option to delete the input text files after combining.
This class, Prettier
, contains a method _pretty_matrix
that generates a pretty matrix representation with column labels, row numbers, and proper spacing.
_pretty_matrix(matrix)
:- Generates a pretty matrix representation with column labels, row numbers, and proper spacing.
This class is a linear equation solver that provides methods for performing various operations related to solving systems of linear equations. Here's a brief summary of each class method:
-
gauss_elimination(matrix, vec, dig)
:- Performs Gaussian elimination to solve a system of linear equations.
-
tridiagonal_elimination(matrix, vec, dig)
:- Performs Tridiagonal elimination to solve a system of linear equations.
-
_select_omega(matrix, eigen_max_iter, eigen_eps)
:- Selects the relaxation parameter for the relaxation method.
-
simple_iteration(matrix, vec, max_iter, eigen_max_iter, eigen_eps, eps, dig)
:- Performs simple iteration to solve a system of linear equations.
-
seidel_iteration(matrix, vec, dig)
:- Performs Seidel iteration to solve a system of linear equations.
-
jacobi_iteration(matrix, vec, max_iter, eps, dig)
:- Performs Jacobi iteration to solve a system of linear equations.
-
relaxation_method(matrix, vec, dig, omega)
:- Performs relaxation method to solve a system of linear equations.
-
explicit_iteration(matrix, vec, dig)
:- Performs explicit iteration to solve a system of linear equations.
-
min_res_iteration(matrix, vec, max_iter, eps, dig)
:- Performs minimum residual iteration to solve a system of linear equations.
-
min_chg_iteration(matrix, vec, max_iter, eps, dig, matrix_choose_mode)
:- Performs minimum change iteration to solve a system of linear equations.
-
step_desc_iteration(matrix, vec, max_iter, eps, dig)
:- Performs method of steepest descent to solve a system of linear equations.
-
step_desc_iteration_imp(matrix, vec, max_iter, eps, dig, matrix_choose_mode)
:- Performs implicit method of steepest descent to solve a system of linear equations.
-
_chol_solver(matrix, vec, dig, mode)
:- Solves a linear system using Cholesky decomposition.
-
_lu_solver(matrix, vec, dig):
- Solves a linear system of equations using LU decomposition.
-
_forward_substitution(matrix, vec, dig)
:- Solves a system of linear equations using forward substitution.
-
_backward_substitution(matrix, vec, dig)
:- Solves a system of linear equations using backward substitution.
-
generate_and_solve_linear_equations(size, matrix_file, vector_file, solution_file, ext_file, dig, check, epsilon, m_v_range, mode, random, prettier_path, prettier, logger, **kwargs)
:- Generates and solves a system of linear equations, with various options for customization and output.
-
__init__(self, Poly)
:- Initialize the polynomials
-
__str__(self)
:- Return a string representation of the Degree and Polynomial attributes with special character replacements.
-
__eq__(self, other)
:- Check if two polynomials are equal.
-
__neq__(self, other)
:- Check if two polynomials are not equal.
-
__add__(self, other)
:- Add two polynomials.
-
__sub__(self, other)
:- Subtract two polynomials.
-
__mul__(self, other)
:- Multiply two polynomials.
-
__rmul__(self, other)
:- Multiply a polynomial by a constant.
-
__neg__(self)
:- Negate the polynomial.
-
__call__(self, value)
:- Evaluate the polynomial expression with the given value for the variable.
-
polynomial_degree(poly_str)
:- A method to calculate the degree of a polynomial expression.
-
eval(self, value)
:- Evaluate the polynomial expression with the given value for the variable.
-
get_degree(self)
:- A method to retrieve the degree attribute from the object.
-
get_variable(self)
:- Get the value of the Variable attribute.
-
get_coeffs(self)
:- Get the coefficients of the polynomial.
-
copy(self)
:- Create a copy of the polynomial.
-
get_diff(self)
:- Get the derivative of the polynomial.
-
plot(self, range, step, colors, legend, **kwargs)
:- Plot the polynomial.
-
solve(self, eps, mode, step)
:- Solve the non-linear equation
-
bisect_solver(self, eps, step)
:- Use Bisection method for solving the equation P(n) = 0
-
find_intervals_with_opposite_signs(self, start, end, step)
:- Find the intervals with opposite signs in the domain
-
newton_solver(self, eps, step)
:- Use Newton method for solving the equation P(n) = 0
-
iter_sim_solver(self, eps, step, max_iter)
:- Use simple Iterative method for solving the equation P(n) = 0
pretty_polynom(Poly)
:- Pretty string representation of the polynomial
-
generate_vandermonde(x)
:- generates the Vandermonde matrix
-
interpolate(func, mode, points, intervals=(0, 1, 10), **kwargs)
:- interpolates the function
-
lagrange_interpolation(x, y, var)
:- lagrange interpolation
-
newton_interpolation(x, y, var)
:- newton interpolation
-
divided_diff(x, y)
:- calculates the divided difference table
-
cspline_interpolation(x, y, x_y_intervals, var)
:- cubic spline interpolation
Class for numerical integration
-
squares(mode)
:- Calculate the integral using the sum of squares
-
Simpson()
:- Calculate the integral using Simpson method
-
Trapeze()
:- Calculate the integrals using trapeze method
MPE:
dy/dx = f(x, y)
y(x0) = y0
def f(x,y): return -x+y
-
__init__(self, Area, f, f0)
:- initialization of MPE solver
-
eiler(self, n)
:- Eiler method
-
heun(self, n)
:- Heun method
-
k_step_Adams_explicit(self, k)
:- K step Adams explicit method
-
rk4(self, n)
:- Runge-Kutta 4 order method
-
rk2(self, n)
:- Runge-Kutta 2 order method
-
solve_Adams_KSE(self, k, n, preliminary_k_method)
:- Solve using K step Adams explicit method
EVP: class for boundary value problem solving for the equation of thermal conduction
du/dt = d^2u/dx^2 + f(t, x)
u(t, a) = m1
u(t, b) = m2
u(0, x) = v0(x)
0 < x < L
0 < t < T
-
__init__(self, T, L, m1, m2, v0, f, alpha)
:- Initialize the boundary value problem solver for the equation of thermal conduction.
-
solve_heat_equation(self, N, M, scheme, omega)
:- Solve the boundary value problem for the equation of thermal conduction. (scheme list is [explicit, implicit, weighted])
-
_solve_heat_equation_explicit(self, N, M)
:- Solve the boundary value problem for the equation of thermal conduction. (explicit scheme)
-
_solve_heat_equation_implicit(self, N, M)
:- Solve the boundary value problem for the equation of thermal conduction. (implicit scheme)
-
_solve_heat_equation_weighted(self, N, M, omega)
:- Solve the boundary value problem for the equation of thermal conduction. (weighted scheme)
-
finit_diff(f, a, b, n, d, m1, m2)
:- function to solve the equation
d^2u/dx^2 + q(x)u(x) = f(x)
u(a) = m1(x)
u(b) = m2(x)
q(x) >= q(a) >= 0
- function to solve the equation
-
ritz(f, a, b, k, q, n, integrate_n)
:- Ritz method to solve the equation
(k(x)u (x)) + q(x)u(x)=f(x)
u(a) = 0
u(b) = 0
- Ritz method to solve the equation
-
plot(self, **kwargs)
:- Plot the graph for MPE
-
plot_solution(self, T, L, **kwargs)
:- Plot the solution for EPV (3D)
use with MPE.plotter.plot
, not with MPE.plotter.plot_solution
-
return_error(func)
:- This function processes the input arguments to extract 'func' and 'pts' information. It calculates the maximum error between the provided function values and the actual values at given points. The error information is stored in the function's 'error_map'. Finally, it returns the result of the input function with the processed arguments.
-
re_map_pretty(error_map, sort, sort_key)
:- This function takes a dictionary 'error_map' as input and returns a pretty string representation of the dictionary. ( sorting is based on 'sort_key' and 'sort' )
-
plot_histogram(map, digits)
:- Plot histogram from map. Map must be have keys, values, keys used as labels, values must be numeric