Skip to content

Repository files navigation

Numerical Methods in C/C++

A collection of fundamental numerical analysis algorithms implemented in C and C++, organized by topic. Each module is self-contained and built around a shared arithmetic utility layer.


Project Structure

numeric_methods/
├── ArchivosComunes/ # Shared utilities (arithmetic, linear algebra, matrix I/O)
├── AritmeticaFinita/ # Finite arithmetic and precision experiments
├── CalculoDeCeros/ # Root-finding methods
├── CondicionamientoMatriz/ # Matrix conditioning
├── FactorizacionCholesky/ # Cholesky factorization
├── Integrales/ # Numerical integration
├── Interpolacion/ # Polynomial interpolation and splines
└── Mascaras/ # 2D convolution masks

Modules

Shared Utilities (ArchivosComunes/)

Core building blocks used across all modules.

  • mn_aritmeticasMachine epsilon detection, smallest/largest representable positive numbers, and a relative distance function for floating-point comparison.
  • mn_lapack — Dense linear algebra: Gaussian elimination with partial pivoting (mn_gauss), matrix inversion (mn_inversa), system error calculation, and matrix/vector file I/O. Also includes a set of macros for common matrix-vector operations (products, transposes, norms, copies).
  • mn_jacobiJacobi eigenvalue algorithm for symmetric matrices. Returns eigenvalues and eigenvectors, with error metrics for validation.
  • mn_raices_polinomios — Polynomial evaluation (direct and Horner's method) and Newton-Raphson root-finding for polynomials.

All modules use double via the real typedef and rely on the TNT (Template Numerical Toolkit)Array1D/Array2D types.


Finite Arithmetic (AritmeticaFinita/)

Experiments illustrating the limits of floating-point representation.

FileDescription
calculo_del_numero_e.cppComputes e by summing the Taylor series until convergence stalls
calculo_del_numero_pi.cppComputes π via the Leibniz formula
calculo_del_numero_pi_metodo_montecarlo.cppEstimates π using Monte Carlo sampling (1,000,000 trials)

Root Finding (CalculoDeCeros/)

Several methods for computing zeros of scalar functions and polynomials.

MethodFilesNotes
Bisectionbiseccion/biseccion.cppBracketing method; guaranteed convergence when sign change exists
Newton-Raphsonnewton_raphson/main.cppQuadratic convergence; uses Horner's method for polynomial derivatives
Regula Falsiregula_falsi/regula_falsi.cppFalse-position bracketing method
SOR RelaxationRelajacion/mn_relajacion.cSOR (Successive Over-Relaxation) for linear systems Au = b
Halley / 2nd-order NRcalculo_raiz.cppUses the quadratic Taylor approximation at each step

Test functions used throughout: f(x) = x - tan(x) + 1 and f(x) = x - sin(x) - 1.


Matrix Conditioning (CondicionamientoMatriz/)

Computes the condition number κ₂(A) of a matrix using its largest and smallest singular values (via Jacobi eigendecomposition). Demonstrates sensitivity of linear systems to perturbations.


Cholesky Factorization (FactorizacionCholesky/)

Factorizes a symmetric positive-definite matrix A into LLᵀ. Provides:

  • mn_cholesky_factorization — returns the lower-triangular factor L
  • mn_descenso / mn_remonteforward and back substitution
  • mn_cholesky — full solver: factorize then solve
  • mn_determinante_factorizacion_cholesky — determinant from the diagonal of L

Numerical Integration (Integrales/Simpson/)

Adaptive Simpson's rule (mn_simpson):

  • Fixed-N version: integrates with a user-specified number of subintervals.
  • Adaptive version: doubles N until the relative change between successive estimates falls below a given tolerance.

Interpolation (Interpolacion/)

MethodFilesDescription
Newton's divided differencesmetodoNewton/metodoNewton.cBuilds and evaluates the Newton interpolating polynomial; supports optimal Chebyshev node placement
Quadratic splinessplines2grado/splines2grado.cConstructs piecewise quadratic splines given nodes, values, and an initial derivative condition

2D Convolution Masks (Mascaras/)

mn_mascara3x3 applies an arbitrary 3×3 convolution kernel to a 2D array, with explicit boundary handling for edges and corners (replicating border pixels). Useful for image filtering operations such as smoothing or edge detection.


Building

Projects use Code::Blocks (.cbp files) with GCC. Each subdirectory contains its own project file. To build a module, open the corresponding .cbp file and build the default target.

Dependencies:

  • GCC with C99/C++11 support
  • TNT array library (headers included under tnt_array/)
  • Standard C math library (-lm)

Notes

  • The real type is defined as double throughout. Changing it to float in mn_aritmeticas.h will affect precision across all modules.
  • File I/O for matrices and vectors uses a simple plain-text format: first line is dimensions, followed by values row by row. See datos/generacion_datos.c for examples of how test data is generated.