Utilities for working with matrices and vectors in Go. This module provides core linear algebra routines (row-reduced echelon form, dot products, identity matrices, scalar operations, basic validation helpers) and a small demo app under cmd/graph that renders simple 2D vectors to an image.
go.mod— Go modulegithub.com/igomez10/linearalgebramain.go— Library packagelinearalgebrawith matrix/vector helpersmain_test.go— Unit tests for the librarycmd/graph/— Demo app that draws vectors and saves3dplot.pngmain.go— Render a simple grid and a few vectorsmain_test.go— Tests for rendering helpers
python/main_unitest.py— A small Python unittest example (unrelated to the Go module; useful for teaching/tests)
- Go 1.23+
Add the module to your project:
go get github.com/igomez10/linearalgebra@latestThen import it in your code:
import "github.com/igomez10/linearalgebra"package main
import (
"fmt"
la "github.com/igomez10/linearalgebra"
)
func main() {
A := [][]float64{{1, 2, 3}, {2, 5, 3}, {1, 0, 8}}
rref := la.ToRowReducedEchelonForm(A)
fmt.Println("RREF:", rref)
}A := [][]float64{{1, 2}, {3, 4}} // 2x2
B := [][]float64{{5, 6}, {7, 8}} // 2x2
C := linearalgebra.DotProduct(A, B)
// C = [[19, 22], [43, 50]]I := linearalgebra.GenerateIdentityMatrix(3) // 3x3 identity
J := linearalgebra.MultiplyMatrixByScalar(I, 2) // every entry *2u := []float64{1, 2, 3}
v := []float64{4, 5, 6}
dp := linearalgebra.DotProductVectors(u, v) // 1*4 + 2*5 + 3*6 = 32Common helpers you’ll find in the package include:
ToRowReducedEchelonForm(matrix [][]float64) [][]float64DotProduct(A, B [][]float64) [][]float64DotProductVectors(a, b []float64) float64GenerateIdentityMatrix(n int) [][]float64AddMatrices(A, B [][]float64) [][]float64MultiplyMatrixByScalar(M [][]float64, s float64) [][]float64MultiplyRowByScalar(M [][]float64, row int, s float64) [][]float64AddRowToRow(M [][]float64, row []float64, idx int) [][]float64CanMultiplyMatrices(A, B [][]float64) boolIsZeroMatrix(M [][]float64) bool
Note: Some functions may panic on illegal operations (e.g., dimension mismatch); validate inputs with helpers like CanMultiplyMatrices first.
There’s a small program under cmd/graph that renders a grid and a few 2D vectors, saving the result as 3dplot.png.
Run it:
go run ./cmd/graphYou should see a 3dplot.png file created in cmd/graph/ with axis lines and example vectors.
Run all Go tests:
go test ./...The demo also has its own tests:
go test ./cmd/graphOptional Python example (requires NumPy):
python3 python/main_unitest.py- The library focuses on clarity and teaching; some routines are not optimized for large matrices.
- Future ideas: determinants, inverses, LU/QR decompositions, eigenvalues/eigenvectors, and improved numerical stability.