gaussian

package module
v0.0.0-...-7aa099f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2025 License: MIT Imports: 2 Imported by: 0

README

Gaussian Elimination in Go

Build Status Go Reference Go Report Card Maintainability Code Coverage

Solve linear equations using Gaussian Elimination.

See Wikipedia for more information about Gaussian elimination.

Usage

For three linear equations:

3a  + 2b   - 1c = 1
2a  - 2b   + 4c = -2
-1a + 0.5b - 1c = 0

This can be converted into a coefficient matrix A:

| 3   2    -1 |
| 2  -2     4 |
| -1  0.5  -1 |

and a right-hand side vector B:

| 1 -2 0 |

Solving the linear equations would be done with:

import (
    "fmt"

    "github.com/albertyw/gaussian"
)

func main() {
    a := [][]float64{
        {3, 2, -1},
        {2, -2, 4},
        {-1, 0.5, -1},
    }
    b := []float64{1, -2, 0}
    solution, err := gaussian.Solve(a, b)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(solution) // [1, -2, -2]
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSingularMatrix         = errors.New("singular matrix")
	ErrEmptyInput             = errors.New("input cannot be empty")
	ErrRectangularMatrix      = errors.New("matrix must be square")
	ErrInconsistentDimensions = errors.New("matrix and vector sizes do not match")
)

Functions

func Solve

func Solve(A [][]float64, b []float64) (x []float64, err error)

Solve solves the linear system Ax = b using Gaussian elimination. A is a square n x n matrix, b is a vector of length n. It returns the solution vector x or an error if the matrix is singular or if the input dimensions are inconsistent.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL