Skip to content

Commit

Permalink
Return symmetric flag in ReadSmat
Browse files Browse the repository at this point in the history
  • Loading branch information
cpmech committed Oct 7, 2020
1 parent 5d97d8a commit b5d60ad
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
12 changes: 9 additions & 3 deletions la/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ func (o *Triplet) PutMatAndMatT(a *Triplet)
[a10 a11 a12 ... ... ...] 4 [. . .]
[... ... ... ... ... ...] 5
func (o *Triplet) ReadSmat(filename string)
func (o *Triplet) ReadSmat(filename string) (symmetric bool)
ReadSmat reads a SMAT file or a MatrixMarket file
About the .smat file:
Expand Down Expand Up @@ -1317,6 +1317,9 @@ func (o *Triplet) ReadSmat(filename string)
NOTE: this function can only read a "coordinate" type MatrixMarket at the moment
Output:
symmetric -- [MatrixMarket only] return true if the MatrixMarket header has "symmetric"
func (o *Triplet) Size() (m, n int)
Size returns the row/column size of the matrix represented by the Triplet
Expand Down Expand Up @@ -1380,7 +1383,7 @@ func (o *TripletC) Put(i, j int, x complex128)
Put inserts an element to a pre-allocated (with Init) triplet (complex)
matrix
func (o *TripletC) ReadSmat(filename string)
func (o *TripletC) ReadSmat(filename string) (symmetric bool)
ReadSmat reads a SMAT file or a MatrixMarket file
About the .smat file:
Expand Down Expand Up @@ -1445,6 +1448,9 @@ func (o *TripletC) ReadSmat(filename string)
NOTE: this function can only read a "coordinate" type MatrixMarket at the moment
Output:
symmetric -- [MatrixMarket only] return true if the MatrixMarket header has "symmetric"
func (o *TripletC) Start()
Start (re)starts index for inserting items using the Put command
Expand Down Expand Up @@ -1671,4 +1677,4 @@ func (o VectorC) SplitRealImag(xR, xI Vector)
xI := imag(this)
NOTE: xR and xI must be pre-allocated with length = len(this)
```
```
26 changes: 16 additions & 10 deletions la/sparse_matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@ func (o *Triplet) ToDense() (a *Matrix) {
//
// NOTE: this function can only read a "coordinate" type MatrixMarket at the moment
//
func (o *Triplet) ReadSmat(filename string) {
mirrorBand := false
// Output:
// symmetric -- [MatrixMarket only] return true if the MatrixMarket header has "symmetric"
//
func (o *Triplet) ReadSmat(filename string) (symmetric bool) {
deltaIndex := 0
initialized := false
io.ReadLines(filename, func(idx int, line string) (stop bool) {
Expand All @@ -206,7 +208,7 @@ func (o *Triplet) ReadSmat(filename string) {
chk.Panic("this function only works with \"general\", \"symmetric\" and \"unsymmetric\" MatrixMarket files")
}
if info[4] == "symmetric" {
mirrorBand = true
symmetric = true
}
deltaIndex = 1
return
Expand All @@ -220,7 +222,7 @@ func (o *Triplet) ReadSmat(filename string) {
chk.Panic("the number of columns in the line with dimensions must be 3 (m,n,nnz)\n")
}
m, n, nnz := io.Atoi(r[0]), io.Atoi(r[1]), io.Atoi(r[2])
if mirrorBand {
if symmetric {
nnz = 2 * nnz // assuming that the diagonal is all-zeros (for safety)
}
o.Init(m, n, nnz)
Expand All @@ -231,12 +233,13 @@ func (o *Triplet) ReadSmat(filename string) {
}
i, j, x := io.Atoi(r[0]), io.Atoi(r[1]), io.Atof(r[2])
o.Put(i-deltaIndex, j-deltaIndex, x)
if mirrorBand && i != j {
if symmetric && i != j {
o.Put(j-deltaIndex, i-deltaIndex, x)
}
}
return
})
return
}

// WriteSmat writes a SMAT file (that can be visualised with vismatrix) or a MatrixMarket file
Expand Down Expand Up @@ -473,8 +476,10 @@ func (o *TripletC) ToDense() (a *MatrixC) {
//
// NOTE: this function can only read a "coordinate" type MatrixMarket at the moment
//
func (o *TripletC) ReadSmat(filename string) {
mirrorBand := false
// Output:
// symmetric -- [MatrixMarket only] return true if the MatrixMarket header has "symmetric"
//
func (o *TripletC) ReadSmat(filename string) (symmetric bool) {
deltaIndex := 0
initialized := false
io.ReadLines(filename, func(idx int, line string) (stop bool) {
Expand All @@ -493,7 +498,7 @@ func (o *TripletC) ReadSmat(filename string) {
chk.Panic("this function only works with \"general\", \"symmetric\" and \"unsymmetric\" MatrixMarket files")
}
if info[4] == "symmetric" {
mirrorBand = true
symmetric = true
}
deltaIndex = 1
return
Expand All @@ -507,7 +512,7 @@ func (o *TripletC) ReadSmat(filename string) {
chk.Panic("number of columns in header must be 3 (m,n,nnz)\n")
}
m, n, nnz := io.Atoi(r[0]), io.Atoi(r[1]), io.Atoi(r[2])
if mirrorBand {
if symmetric {
nnz = 2 * nnz // assuming that the diagonal is all-zeros (for safety)
}
o.Init(m, n, nnz)
Expand All @@ -518,12 +523,13 @@ func (o *TripletC) ReadSmat(filename string) {
}
i, j, x := io.Atoi(r[0]), io.Atoi(r[1]), complex(io.Atof(r[2]), io.Atof(r[3]))
o.Put(i-deltaIndex, j-deltaIndex, x)
if mirrorBand && i != j {
if symmetric && i != j {
o.Put(j-deltaIndex, i-deltaIndex, x)
}
}
return
})
return
}

// WriteSmat writes a SMAT file (that can be visualised with vismatrix) or a MatrixMarket file
Expand Down
36 changes: 24 additions & 12 deletions la/t_sp_matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,23 @@ func TestSmat01(tst *testing.T) {

// read MatrixMarket and check
var T Triplet
T.ReadSmat("data/small-sparse-matrix.mtx")
isSym1 := T.ReadSmat("data/small-sparse-matrix.mtx")
chk.Deep2(tst, "T", 1e-17, T.ToDense().GetDeep2(), correct)
chk.Bool(tst, "isSym1", isSym1, false)

// write SMAT and check
T.WriteSmat("/tmp/gosl/la", "small-test-matrix", 0, "", false, false)
var S Triplet
S.ReadSmat("/tmp/gosl/la/small-test-matrix.smat")
isSym2 := S.ReadSmat("/tmp/gosl/la/small-test-matrix.smat")
chk.Deep2(tst, "S", 1e-17, S.ToDense().GetDeep2(), correct)
chk.Bool(tst, "isSym2", isSym2, false)

// write MatrixMarket and check
T.WriteSmat("/tmp/gosl/la", "small-test-matrix", 0, "", true, false)
var M Triplet
M.ReadSmat("/tmp/gosl/la/small-test-matrix.mtx")
isSym3 := M.ReadSmat("/tmp/gosl/la/small-test-matrix.mtx")
chk.Deep2(tst, "M", 1e-17, M.ToDense().GetDeep2(), correct)
chk.Bool(tst, "isSym3", isSym3, false)
reference := `%%MatrixMarket matrix coordinate real general
5 5 12
1 1 2
Expand Down Expand Up @@ -217,20 +220,23 @@ func TestSmat02(tst *testing.T) {

// read MatrixMarket and check
var T Triplet
T.ReadSmat("data/small-sparse-matrix-sym.mtx")
isSym1 := T.ReadSmat("data/small-sparse-matrix-sym.mtx")
chk.Deep2(tst, "T", 1e-17, T.ToDense().GetDeep2(), correct)
chk.Bool(tst, "isSym1", isSym1, true)

// write SMAT and check
T.WriteSmat("/tmp/gosl/la", "small-test-matrix-sym", 1e-17, "", false, false)
var S Triplet
S.ReadSmat("/tmp/gosl/la/small-test-matrix-sym.smat")
isSym2 := S.ReadSmat("/tmp/gosl/la/small-test-matrix-sym.smat")
chk.Deep2(tst, "S", 1e-17, S.ToDense().GetDeep2(), correct)
chk.Bool(tst, "isSym2", isSym2, false) // smat doesn't know about symmetry

// write MatrixMarket and check
T.WriteSmat("/tmp/gosl/la", "small-test-matrix-sym", 0, "", true, true)
var M Triplet
M.ReadSmat("/tmp/gosl/la/small-test-matrix-sym.mtx")
isSym3 := M.ReadSmat("/tmp/gosl/la/small-test-matrix-sym.mtx")
chk.Deep2(tst, "M", 1e-17, M.ToDense().GetDeep2(), correct)
chk.Bool(tst, "isSym3", isSym3, true)
reference := `%%MatrixMarket matrix coordinate real symmetric
5 5 7
1 1 2
Expand Down Expand Up @@ -259,20 +265,23 @@ func TestSmat03(tst *testing.T) {

// read MatrixMarket and check
var T TripletC
T.ReadSmat("data/small-sparse-matrix-complex.mtx")
isSym1 := T.ReadSmat("data/small-sparse-matrix-complex.mtx")
chk.Deep2c(tst, "T", 1e-17, T.ToDense().GetDeep2(), correct)
chk.Bool(tst, "isSym1", isSym1, false)

// write SMAT and check
T.WriteSmat("/tmp/gosl/la", "small-test-matrix-complex", 1e-17, "", false, false, false)
var S TripletC
S.ReadSmat("/tmp/gosl/la/small-test-matrix-complex.smat")
isSym2 := S.ReadSmat("/tmp/gosl/la/small-test-matrix-complex.smat")
chk.Deep2c(tst, "S", 1e-17, S.ToDense().GetDeep2(), correct)
chk.Bool(tst, "isSym2", isSym2, false)

// write MatrixMarket and check
T.WriteSmat("/tmp/gosl/la", "small-test-matrix-complex", 0, "", true, false, false)
var M TripletC
M.ReadSmat("/tmp/gosl/la/small-test-matrix-complex.mtx")
isSym3 := M.ReadSmat("/tmp/gosl/la/small-test-matrix-complex.mtx")
chk.Deep2c(tst, "M", 1e-17, M.ToDense().GetDeep2(), correct)
chk.Bool(tst, "isSym3", isSym3, false)
reference := `%%MatrixMarket matrix coordinate complex general
5 5 13
1 1 2 0
Expand Down Expand Up @@ -307,20 +316,23 @@ func TestSmat04(tst *testing.T) {

// read MatrixMarket and check
var T TripletC
T.ReadSmat("data/small-sparse-matrix-complex-sym.mtx")
isSym1 := T.ReadSmat("data/small-sparse-matrix-complex-sym.mtx")
chk.Deep2c(tst, "T", 1e-17, T.ToDense().GetDeep2(), correct)
chk.Bool(tst, "isSym1", isSym1, true)

// write SMAT and check
T.WriteSmat("/tmp/gosl/la", "small-test-matrix-complex-sym", 1e-17, "", false, false, false)
var S TripletC
S.ReadSmat("/tmp/gosl/la/small-test-matrix-complex-sym.smat")
isSym2 := S.ReadSmat("/tmp/gosl/la/small-test-matrix-complex-sym.smat")
chk.Deep2c(tst, "S", 1e-17, S.ToDense().GetDeep2(), correct)
chk.Bool(tst, "isSym2", isSym2, false) // smat doesn't know about symmetry

// write MatrixMarket and check
T.WriteSmat("/tmp/gosl/la", "small-test-matrix-complex-sym", 0, "", true, true, false)
var M TripletC
M.ReadSmat("/tmp/gosl/la/small-test-matrix-complex-sym.mtx")
isSym3 := M.ReadSmat("/tmp/gosl/la/small-test-matrix-complex-sym.mtx")
chk.Deep2c(tst, "M", 1e-17, M.ToDense().GetDeep2(), correct)
chk.Bool(tst, "isSym3", isSym3, true)
reference := `%%MatrixMarket matrix coordinate complex symmetric
5 5 7
1 1 2 0
Expand Down

0 comments on commit b5d60ad

Please sign in to comment.