This repository demonstrates a set of numerical methods implemented in Python, using high-precision arithmetic provided by GMPY2. It covers several mathematical problems including root-finding for equations, numerical integration, solving systems of linear equations, and polynomial interpolation/approximation.
Below is an overview of each file and its purpose, followed by instructions on how to install dependencies and run the examples.
-
algorithm.py
• Defines the base abstract classAlgorithm
.
• Provides a precision setup (using GMPY2) and basic iteration tracking (accuracy, iteration counts).
• Acts as a backbone for all other numerical methods. -
rootequation.py
• Implements root-finding classes, inheriting fromAlgorithm
.RootEquation
: Abstract extension specialized for root-finding tasks.Bisection
: Standard Bisection method implementation.RegulaFalsi
: False Position (Regula Falsi) method.Newton
: Newton–Raphson method.
• Each class tracks precision, iterations, and provides aprint_result()
method for convenient output.
-
integral.py
• Implements numerical integration classes (extendingAlgorithm
):Integral
: Base class for integration tasks.RectangularMethod
: Uses the midpoint (rectangular) rule with adaptive refinement.TrapezoidalMethod
: Uses the trapezoidal rule with adaptive refinement.
-
interpolation_approximation.py
• Defines classes to solve interpolation and approximation problems (extendingAlgorithm
):InterpolationApproximation
: Base class providing coordinate sorting, result storage, etc.LagrangePolynomial
: Implements Lagrange polynomials for interpolation.Approximation
: Performs a least squares linear fit.LinearInterpolationSpline
: Framework for piecewise-linear interpolation (placeholder example).
-
linearequations.py
• ExtendsAlgorithm
with classes that solve systems of linear equations:LinearSolver
: Base class that reads matrixA
and vectorb
, checks convergence, etc.JacobiMethod
&GaussSeidelMethod
: Iterative methods for solving systems of linear equations.
-
requirements.txt
• Lists all Python dependencies needed to run the project (numpy, gmpy2, sympy, matplotlib, scipy).
Several Jupyter notebooks showcase how to use these core modules. Each notebook demonstrates an example numerical problem and how to apply the corresponding method:
-
rootequation.ipynb
• UsesBisection
,RegulaFalsi
, andNewton
to find roots of a sample function (e.g.,x^(1/2) - 2.5
). -
integer.ipynb
• Demonstrates numerical integration methods, callingRectangularMethod
andTrapezoidalMethod
, and showing howIntegral
classes refine the computation until desired precision is reached. -
interpolation_aproximacion.ipynb
• Shows howLagrangePolynomial
,Approximation
, andLinearInterpolationSpline
can be used for interpolating or approximating a set of points. -
linearequations.ipynb
• Demonstrates solving linear systems withJacobiMethod
andGaussSeidelMethod
.
- Clone the Repository
• Run:git clone https://github.com/your_username/your_repository.git
cd your_repository
- Install Dependencies
• From the project’s root directory:pip install -r requirements.txt
- Run a Notebook
• Launch JupyterLab or Jupyter Notebook:jupyter notebook
• Open one of the supplied .ipynb files (e.g.,
rootequation.ipynb
) to experiment with the numerical methods.
- Using the Python Modules Directly
• You can also import the modules in your own Python scripts or notebooks.
• For example:from rootequation import Bisection, RegulaFalsi, Newton
my_function = lambda x: x**2 - 4
solver = Bisection(my_function, 10, [[1, 3]])
solver.compute()
solver.print_result()
All external libraries are listed in requirements.txt
:
- numpy
- matplotlib
- sympy
- gmpy2
- scipy
These handle numerical arrays (numpy
), big-number arithmetic (gmpy2
), symbolic operations (sympy
), plotting (matplotlib
), and advanced computations (scipy
).