Skip to content

Commit

Permalink
Fix data overwrite bug in Clone
Browse files Browse the repository at this point in the history
Previously we dropped the mat.Data prior to copying when the src and
dst were the same (this is used in SubMatrix).
  • Loading branch information
kortschak committed Jan 21, 2014
1 parent 85652f6 commit 322057a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
17 changes: 8 additions & 9 deletions mat64/dense.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,34 +194,33 @@ func (m *Dense) Reset() {

func (m *Dense) Clone(a Matrix) {
r, c := a.Dims()
m.mat = RawMatrix{
mat := RawMatrix{
Order: BlasOrder,
Rows: r,
Cols: c,
Data: make([]float64, r*c),
}
data := make([]float64, r*c)
switch a := a.(type) {
case RawMatrixer:
amat := a.RawMatrix()
for i := 0; i < r; i++ {
copy(data[i*c:(i+1)*c], amat.Data[i*amat.Stride:i*amat.Stride+c])
copy(mat.Data[i*c:(i+1)*c], amat.Data[i*amat.Stride:i*amat.Stride+c])
}
m.mat.Stride = c
m.mat.Data = data
mat.Stride = c
case Vectorer:
for i := 0; i < r; i++ {
a.Row(data[i*c:(i+1)*c], i)
a.Row(mat.Data[i*c:(i+1)*c], i)
}
m.mat.Stride = c
m.mat.Data = data
mat.Stride = c
default:
m.mat.Data = data
m.mat.Data = mat.Data
for i := 0; i < r; i++ {
for j := 0; j < c; j++ {
m.Set(i, j, a.At(i, j))
}
}
}
m.mat = mat
}

func (m *Dense) Copy(a Matrix) (r, c int) {
Expand Down
26 changes: 26 additions & 0 deletions mat64/dense_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,32 @@ func (s *S) TestApply(c *check.C) {
}
}

func (s *S) TestClone(c *check.C) {
for i, test := range []struct {
a [][]float64
i, j int
v float64
}{
{
[][]float64{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}},
1, 1,
1,
},
{
[][]float64{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}},
0, 0,
0,
},
} {
a := NewDense(flatten(test.a))
b := *a
a.Clone(a)
a.Set(test.i, test.j, test.v)

c.Check(b.Equals(a), check.Equals, false, check.Commentf("Test %d: %v cloned and altered = %v", i, a, &b))
}
}

func (s *S) TestStack(c *check.C) {
for i, test := range []struct {
a, b, e [][]float64
Expand Down

0 comments on commit 322057a

Please sign in to comment.