Skip to content

FerrarioChristian/iterative-linear-solver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Linear Solver

A Python library for solving linear systems using iterative methods, analyzing matrix properties, benchmarking solvers, and visualizing performance. The library is designed to solve large sparse linear systems efficiently, provided in sparse .mtx format.


Features

  • Solve linear systems using:
    • Jacobi method
    • Gauss-Seidel method
    • Gradient Descent
    • Conjugate Gradient
  • Analyze structural matrix properties:
    • Symmetry
    • Positive-definiteness
    • Diagonal dominance
    • Condition number
  • Benchmark solvers (execution time, number of iterations, convergence behavior)
  • Generate comparison plots (execution time, relative error, iterations)

Installation

This project uses Python 3 and open-source libraries only.

1. Clone the repository

git clone https://github.com/FerrarioChristian/iterative-linear-solver.git
cd iterative-linear-solver

2. Create a virtual environment (optional but recommended)

python3 -m venv .venv
source .venv/bin/activate # On Windows use `venv\Scripts\activate`

3. Install dependencies

# Install the dependencies
pip install -e .

Usage

You can run the full benchmark from the command line:

linear-solver-demo [-h] [-it MAX_ITER] [-sc] [--spy] [-t TOLERANCES ...]

Options

-h, --help: Show this help message and exit
-it, --max-iter: Set maximum iterations (default 20000)
-sc, --skip-check: Skip matrix property check
-o, --output-dir: Specify output directory for the generated plots (Optional *) --spy: Generate a spy plot of the matrices
-t, --tolerances: Set a list of tolerances (default [1e-4, 1e-6, 1e-8, 1e-10])

  • If -o is omitted the plots are only shown, if -o is specified without value the results will be saved in restults.csv and plots will be generated in the results/plots directory, if a path is specified the results will be stored in the given directory

Library functions

Solving a system

You can use any solver directly:

from linear_solver.solvers import JacobiSolver

solver = JacobiSolver(A, b, tolerance=1e-6, max_iterations=10000)
solution = solver.solve()

Benchmarking solvers

You can benchmark any solver using the benchmark_solver function:

from linear_solver.benchmark import benchmark_solver
from linear_solver.solvers import JacobiSolver

result = benchmark_solver(JacobiSolver, A, b, tol=1e-8, max_iter=5000)
print(result.execution_time, result.iterations)

Analyzing matrix properties

You can analyze matrix properties using the MatrixAnalyzer class:

from linear_solver.matrix_analysis.structure import analyze_matrix

properties = analyze_matrix(A)
print(properties.is_symmetric, properties.is_positive_definite, properties.is_diagonally_dominant)

Project Structure

linear-iterative-solver
├── demo
│   ├── cli.py                       # Parameters options
│   ├── constants.py                 # Solver settings (matrices, tolerances, etc.)
│   ├── logger.py                    # Funtions to print the status of the comutation
│   └── main.py                      # CLI interface
├── linear_solver
│   ├── analysis
│   │   ├── benchmark.py             # Benchmarking utilities  
│   │   ├── compare_plot.py          # Plotting utilities
│   │   └── plot.py
│   ├── convergence
│   │   └── criteria.py              # Stopping conditions  
│   ├── matrix_analysis
│   │   └── structure.py             # Matrix property analysis
│   └── solvers
│       ├── base_solver.py
│       ├── conjugate_gradient.py
│       ├── gauss_seidel.py
│       ├── gradient.py
│       ├── jacobi.py
│       └── lower_triangular.py
├── matrices
│   ├── spa1.mtx
│   ├── spa2.mtx
│   ├── vem1.mtx
│   └── vem2.mtx
└── pyproject.toml

About

Iterative linear solver library for Python

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages