Skip to content

About

Haskell solver for n×n linear systems — Gaussian elimination with partial pivoting and explicit classification of unique, inconsistent, and rank-deficient systems.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

Linear System Solver

A command-line linear system solver written in Haskell. Given a square system of n equations in n unknowns, it solves for the variables using Gaussian elimination with partial pivoting, and correctly reports when a system has no solution or infinitely many.

How it works

The input is an augmented matrix: each row is a list of coefficients followed by the equation's right-hand side. For example, [[1,1,5],[3,-2,10]] represents:

 x +  y =  5
3x - 2y = 10

The solver:

  1. Reduces the matrix to row echelon form (gaussElim), using partial pivoting — if a column's entry in the current row is zero (or too close to zero to trust), it searches the remaining rows for the largest-magnitude usable pivot and swaps it into place, rather than dividing by zero.
  2. Classifies the reduced matrix:
    • If any row reduces to all-zero coefficients with a non-zero right-hand side, the system is inconsistent → no solution.
    • Otherwise, if fewer pivots were found than there are unknowns, the system is rank-deficient → infinitely many solutions.
    • Otherwise it has a unique solution, found via back-substitution (backSubstitute) from the last equation upward.

This replaces an earlier version of this project that only handled these cases by accident — a zero pivot caused a runtime 0/0, and the resulting NaN/Infinity values were used to guess whether the system had infinite or no solutions. That version also crashed outright on any input that needed row pivoting (e.g. a leading coefficient of zero). See CHANGELOG.md for details.

Requirements

  • GHC (developed and tested on 9.2.8)

Usage

Compile and run:

ghc -O2 -o solver linear-system-solver.hs
./solver

You'll be prompted for a path to an input file containing an augmented matrix literal, e.g.:

Enter the path to the input file:
equation.txt
Solution:
Unique solution: [4.0,1.0]

Or run it directly without compiling:

runghc linear-system-solver.hs

Example inputs

File System Result
equation.txt x + y = 5, 3x - 2y = 10 Unique solution
equation2.txt x + 2y = -4, 2x + 3y = 5 Unique solution
equation3.txt 3x3 system Unique solution
infsolequation.txt x + y = 1, 2x + 2y = 2 Infinitely many solutions
infsolequation2.txt 2x + y = 4, 2x + y = 4 (scaled) Infinitely many solutions
nosolequation.txt Contradictory pair No solution

Limitations

  • Only square systems (n equations, n unknowns) are supported — this matches the assignment this project was originally built for.
  • Numbers close to zero (within 1e-9) are treated as zero to absorb floating-point rounding error from repeated elimination steps; this is a fixed tolerance rather than one scaled to the input's magnitude.

About

Haskell solver for n×n linear systems — Gaussian elimination with partial pivoting and explicit classification of unique, inconsistent, and rank-deficient systems.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages