Skip to content
Snippets Groups Projects
Commit afad393d authored by Daniel Jankowski's avatar Daniel Jankowski
Browse files

Added: CheckGaussian for partial gaussian checks

parent 2ec25e5c
No related branches found
No related tags found
No related merge requests found
Pipeline #4340 passed
package gomatrix
import (
"math/big"
)
// GaussianElimination converts the matrix to an echelon form
//
// This function applies the gaussian elemination to the matrix in order to
// This function applies the gaussian elimination to the matrix in order to
// create an echelon form.
func (f *F2) GaussianElimination() {
// iterate through all possible pivot bits
......@@ -125,3 +129,53 @@ func (f *F2) partialDiagonalize(startRow, startCol, stopRow, stopCol int) {
}
}
}
// CheckGaussian checks if the given range in the matrix is the identity matrix
//
// @param int startRow The row where the check starts
// @param int startCol The column where the check starts
// @param int n The size of the submatrix to check
//
// @return bool
func (f *F2) CheckGaussian(startRow, startCol, n int) bool {
counter := 0
// iterate through the rows
for _, row := range f.Rows[startRow:] {
// if the counter is reached...
if counter == n {
// ...break
break
}
// create the bitmask for the row
bitmask := big.NewInt(0).SetBit(
big.NewInt(0),
startCol+counter,
1,
)
// get the bits to check
bitsToCheck := big.NewInt(0).And(
row,
bitmask,
)
// xor the bits to check with the bitmask
shouldBeZero := big.NewInt(0).Xor(
bitsToCheck,
bitmask,
)
// if the xor'ed result is not zero...
if shouldBeZero.Cmp(big.NewInt(0)) != 0 {
// ...the check failed
return false
}
// increase the counter
counter++
}
return true
}
......@@ -90,3 +90,73 @@ func TestPartialGaussianElimination(t *testing.T) {
assert.True(t, test.matrixA.IsEqual(test.expectedMatrix))
}
}
func TestCheckGaussian(t *testing.T) {
tests := []struct {
description string
matrix *F2
startRow int
startCol int
n int
expectedResult bool
}{
{
description: "3x3 identity matrix",
matrix: NewF2(3, 3).Set([]*big.Int{
big.NewInt(1),
big.NewInt(2),
big.NewInt(4),
}),
startRow: 0,
startCol: 0,
n: 3,
expectedResult: true,
},
{
description: "3x3 matrix",
matrix: NewF2(3, 3).Set([]*big.Int{
big.NewInt(2),
big.NewInt(1),
big.NewInt(4),
}),
startRow: 0,
startCol: 0,
n: 3,
expectedResult: false,
},
{
description: "3x3 matrix with lower right identity matrix",
matrix: NewF2(3, 3).Set([]*big.Int{
big.NewInt(2),
big.NewInt(2),
big.NewInt(4),
}),
startRow: 1,
startCol: 1,
n: 3,
expectedResult: true,
},
{
description: "3x3 matrix with upper left identity matrix",
matrix: NewF2(3, 3).Set([]*big.Int{
big.NewInt(1),
big.NewInt(2),
big.NewInt(7),
}),
startRow: 0,
startCol: 0,
n: 2,
expectedResult: true,
},
}
for _, test := range tests {
result := test.matrix.CheckGaussian(
test.startRow,
test.startCol,
test.n,
)
assert.Equalf(t, test.expectedResult, result, test.description)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment