Skip to content
This repository was archived by the owner on Dec 10, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions mat64/dense.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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++ {
Expand All @@ -360,14 +356,22 @@ 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more question: the default case reuses the slice because there is no way (unlike with *Vector or RawMatrixer) that such matrix could shadow a Dense, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually could if someone constructed it that way. A matrix that stores its elements in a blas64.General but does not have a RawMatrix method would allow this. Probably I should allocate a new slice there since I am now saying that Clone will not cause shadowing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable and consistent, and reduces my doubt.

blas64.Copy(aU.n,
blas64.Vector{Inc: amat.Inc, Data: amat.Data},
blas64.Vector{Inc: 1, Data: mat.Data})
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
Expand Down Expand Up @@ -404,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++ {
Expand Down
3 changes: 2 additions & 1 deletion mat64/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down