From d63975562b2f4a18648ebd9c39d63165cf65d8d9 Mon Sep 17 00:00:00 2001 From: kortschak Date: Fri, 11 Mar 2016 11:04:36 +1030 Subject: [PATCH 1/3] mat64: remove stale TODO This is now covered in the package level documentation and addressed in code. --- mat64/dense.go | 16 +++++++--------- mat64/matrix.go | 3 ++- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/mat64/dense.go b/mat64/dense.go index 33d46cf..88f4a0a 100644 --- a/mat64/dense.go +++ b/mat64/dense.go @@ -327,7 +327,8 @@ func (m *Dense) Reset() { } // Clone makes a copy of a into the receiver, overwriting the previous value of -// the receiver. The clone operation does not make any restriction on shape. +// the receiver. The clone operation does not make any restriction on shape and +// will not cause shadowing. // // See the Cloner interface for more information. func (m *Dense) Clone(a Matrix) { @@ -343,11 +344,6 @@ func (m *Dense) Clone(a Matrix) { switch aU := aU.(type) { case RawMatrixer: amat := aU.RawMatrix() - // TODO(kortschak): Consider being more precise with determining whether a and m are aliases. - // The current approach is that all RawMatrixers are considered potential aliases. - // Note that below we assume that non-RawMatrixers are not aliases; this is not necessarily - // true, but cases where it is not are not sensible. We should probably fix or document - // this though. mat.Data = make([]float64, r*c) if trans { for i := 0; i < r; i++ { @@ -361,13 +357,15 @@ func (m *Dense) Clone(a Matrix) { } } default: - mat.Data = use(m.mat.Data, r*c) - m.mat = mat + mat.Data = make([]float64, r*c) + w := *m + w.mat = mat for i := 0; i < r; i++ { for j := 0; j < c; j++ { - m.set(i, j, a.At(i, j)) + w.set(i, j, a.At(i, j)) } } + *m = w return } m.mat = mat diff --git a/mat64/matrix.go b/mat64/matrix.go index 12da4e1..af3654c 100644 --- a/mat64/matrix.go +++ b/mat64/matrix.go @@ -112,7 +112,8 @@ type RawColViewer interface { } // A Cloner can make a copy of a into the receiver, overwriting the previous value of the -// receiver. The clone operation does not make any restriction on shape. +// receiver. The clone operation does not make any restriction on shape and will not cause +// shadowing. type Cloner interface { Clone(a Matrix) } From 6be0ffe893e0d6cb883eb42a24832c0d78752d54 Mon Sep 17 00:00:00 2001 From: kortschak Date: Fri, 11 Mar 2016 11:57:25 +1030 Subject: [PATCH 2/3] mat64: add Vector fast path to Clone --- mat64/dense.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mat64/dense.go b/mat64/dense.go index 88f4a0a..ded3f0f 100644 --- a/mat64/dense.go +++ b/mat64/dense.go @@ -356,6 +356,12 @@ func (m *Dense) Clone(a Matrix) { copy(mat.Data[i*c:(i+1)*c], amat.Data[i*amat.Stride:i*amat.Stride+c]) } } + case *Vector: + amat := aU.mat + mat.Data = make([]float64, aU.n) + blas64.Copy(aU.n, + blas64.Vector{Inc: amat.Inc, Data: amat.Data}, + blas64.Vector{Inc: 1, Data: mat.Data}) default: mat.Data = make([]float64, r*c) w := *m From 05edb4948f0b174984516cd4edd9cd04466f24d6 Mon Sep 17 00:00:00 2001 From: kortschak Date: Fri, 11 Mar 2016 12:10:48 +1030 Subject: [PATCH 3/3] mat64: add Vector fast path to Copy --- mat64/dense.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/mat64/dense.go b/mat64/dense.go index ded3f0f..6e6c141 100644 --- a/mat64/dense.go +++ b/mat64/dense.go @@ -408,6 +408,19 @@ func (m *Dense) Copy(a Matrix) (r, c int) { copy(m.mat.Data[i*m.mat.Stride:i*m.mat.Stride+c], amat.Data[i*amat.Stride:i*amat.Stride+c]) } } + case *Vector: + var n, stride int + if trans { + n = c + stride = 1 + } else { + n = r + stride = m.mat.Stride + } + amat := aU.mat + blas64.Copy(n, + blas64.Vector{Inc: amat.Inc, Data: amat.Data}, + blas64.Vector{Inc: stride, Data: m.mat.Data}) default: for i := 0; i < r; i++ { for j := 0; j < c; j++ {