From a3a7aeddc93dec1a2c73e07f7226c07a415c11f3 Mon Sep 17 00:00:00 2001 From: danieljankowski <daniel.jankowski@rub.de> Date: Mon, 18 Feb 2019 17:03:21 +0100 Subject: [PATCH] Added: gaussian elimination --- algebra.go | 1 + gaussian.go | 32 ++++++++++++++++++++++++++++++++ gaussian_test.go | 28 ++++++++++++++++++++++++++++ matrix.go | 1 + 4 files changed, 62 insertions(+) create mode 100644 gaussian.go create mode 100644 gaussian_test.go diff --git a/algebra.go b/algebra.go index 830fccf..cf7df57 100644 --- a/algebra.go +++ b/algebra.go @@ -1,3 +1,4 @@ +// Package gomatrix Is a go package for scientific operations with matrices in F2. package gomatrix import ( diff --git a/gaussian.go b/gaussian.go new file mode 100644 index 0000000..b50df76 --- /dev/null +++ b/gaussian.go @@ -0,0 +1,32 @@ +// Package gomatrix Is a go package for scientific operations with matrices in F2. +package gomatrix + +// GaussianElimination converts the matrix to an echelon form +// +// This function applies the gaussian elemination to the matrix in order to +// create an echelon form. +func (f *F2) GaussianElimination() { + // iterate through all possible pivot bits + for pivotBit := 0; pivotBit < f.M; pivotBit++ { + // iterate through the rows + for rowCounter := 0; rowCounter < f.N; rowCounter++ { + // if the pivotbit of this row is 0... + if f.Rows[rowCounter].Bit(pivotBit) == uint(0) { + // ...check the next row + continue + } + + // if the row with a valid pivot bit is not the first row... + if pivotBit != rowCounter { + // ...swap it with first one + f.SwapRows(pivotBit, rowCounter) + } + + // iterate through all other rows except the first one + for rr := pivotBit + 1; rr < f.N; rr++ { + // substract the 1 from all other rows with the pivotBit + f.Rows[rr].Xor(f.Rows[rr], f.Rows[pivotBit]) + } + } + } +} diff --git a/gaussian_test.go b/gaussian_test.go new file mode 100644 index 0000000..bb2a4f4 --- /dev/null +++ b/gaussian_test.go @@ -0,0 +1,28 @@ +// Package gomatrix Is a go package for scientific operations with matrices in F2. +package gomatrix + +import ( + "math/big" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGaussianElimination(t *testing.T) { + tests := []struct { + description string + matrixA *F2 + expectedMatrix *F2 + }{ + { + matrixA: NewF2(2, 2).Set([]*big.Int{big.NewInt(2), big.NewInt(1)}), + expectedMatrix: NewF2(2, 2).Set([]*big.Int{big.NewInt(1), big.NewInt(3)}), + }, + } + + for _, test := range tests { + test.matrixA.GaussianElimination() + + assert.True(t, test.matrixA.IsEqual(test.expectedMatrix)) + } +} diff --git a/matrix.go b/matrix.go index eb32b70..2ccc137 100644 --- a/matrix.go +++ b/matrix.go @@ -1,3 +1,4 @@ +// Package gomatrix Is a go package for scientific operations with matrices in F2. package gomatrix import ( -- GitLab