Skip to content

Commit

Permalink
Simplify Triplet.ReadSmat functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cpmech committed Sep 2, 2021
1 parent 936bd13 commit 1a05a67
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions la/sparse_matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ func (o *Triplet) ToDense() (a *Matrix) {
func (o *Triplet) ReadSmat(filename string, mirrorIfSym bool) (symmetric bool) {
deltaIndex := 0
initialized := false
id, sz := 0, 1
start, endp1 := 0, 0
indexNnz := 0
m, n, nnz, pos := 0, 0, 0, 0
io.ReadLines(filename, func(idx int, line string) (stop bool) {
if strings.HasPrefix(line, "%%MatrixMarket") {
info := strings.Fields(line)
Expand Down Expand Up @@ -241,26 +239,25 @@ func (o *Triplet) ReadSmat(filename string, mirrorIfSym bool) (symmetric bool) {
if len(r) != 3 {
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])
start, endp1 = (id*nnz)/sz, ((id+1)*nnz)/sz
m, n, nnz = io.Atoi(r[0]), io.Atoi(r[1]), io.Atoi(r[2])
if symmetric && mirrorIfSym {
o.Init(m, n, (endp1-start)*2) // assuming that the diagonal is all-zeros (for safety)
o.Init(m, n, nnz*2) // assuming that the diagonal is all-zeros (for safety)
} else {
o.Init(m, n, endp1-start)
o.Init(m, n, nnz)
}
initialized = true
} else {
if len(r) != 3 {
chk.Panic("the number of columns in the data lines must be 3 (i,j,x)\n")
}
i, j, x := io.Atoi(r[0]), io.Atoi(r[1]), io.Atof(r[2])
if indexNnz >= start && indexNnz < endp1 {
if pos < nnz {
o.Put(i-deltaIndex, j-deltaIndex, x)
if symmetric && mirrorIfSym && i != j {
o.Put(j-deltaIndex, i-deltaIndex, x)
}
}
indexNnz++
pos++
}
return
})
Expand Down Expand Up @@ -521,9 +518,7 @@ func (o *TripletC) ToDense() (a *MatrixC) {
func (o *TripletC) ReadSmat(filename string, mirrorIfSym bool) (symmetric bool) {
deltaIndex := 0
initialized := false
id, sz := 0, 1
start, endp1 := 0, 0
indexNnz := 0
m, n, nnz, pos := 0, 0, 0, 0
io.ReadLines(filename, func(idx int, line string) (stop bool) {
if strings.HasPrefix(line, "%%MatrixMarket") {
info := strings.Fields(line)
Expand Down Expand Up @@ -556,26 +551,25 @@ func (o *TripletC) ReadSmat(filename string, mirrorIfSym bool) (symmetric bool)
if len(r) != 3 {
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])
start, endp1 = (id*nnz)/sz, ((id+1)*nnz)/sz
m, n, nnz = io.Atoi(r[0]), io.Atoi(r[1]), io.Atoi(r[2])
if symmetric && mirrorIfSym {
o.Init(m, n, (endp1-start)*2) // assuming that the diagonal is all-zeros (for safety)
o.Init(m, n, nnz*2) // assuming that the diagonal is all-zeros (for safety)
} else {
o.Init(m, n, endp1-start)
o.Init(m, n, nnz)
}
initialized = true
} else {
if len(r) != 4 {
chk.Panic("number of columns in data lines must be 4 (i,j,xReal,xImag)\n")
}
i, j, x := io.Atoi(r[0]), io.Atoi(r[1]), complex(io.Atof(r[2]), io.Atof(r[3]))
if indexNnz >= start && indexNnz < endp1 {
if pos < nnz {
o.Put(i-deltaIndex, j-deltaIndex, x)
if symmetric && mirrorIfSym && i != j {
o.Put(j-deltaIndex, i-deltaIndex, x)
}
}
indexNnz++
pos++
}
return
})
Expand Down

0 comments on commit 1a05a67

Please sign in to comment.