mat: add VecDenseCopyOf function
MODIFY
mat/index_bound_checks.go
mat/index_bound_checks.go
@@ -47,6 +47,12 @@ func (v *VecDense) At(i, j int) float64 {
return v.at(i)
}
+// AtVec returns the element at row i.
+// It panics if i is out of bounds.
+func (v *VecDense) AtVec(i int) float64 {
+ return v.at(i)
+}
+
func (v *VecDense) at(i int) float64 {
if uint(i) >= uint(v.n) {
panic(ErrRowAccess)

MODIFY
mat/index_no_bound_checks.go
mat/index_no_bound_checks.go
@@ -50,6 +50,15 @@ func (v *VecDense) At(i, j int) float64 {
return v.at(i)
}
+// AtVec returns the element at row i.
+// It panics if i is out of bounds.
+func (v *VecDense) AtVec(i int) float64 {
+ if uint(i) >= uint(v.n) {
+ panic(ErrRowAccess)
+ }
+ return v.at(i)
+}
+
func (v *VecDense) at(i int) float64 {
return v.mat.Data[i*v.mat.Inc]
}

MODIFY
mat/vector.go
mat/vector.go
@@ -21,6 +21,7 @@ var (
// Vector is a column vector.
type Vector interface {
Matrix
+ AtVec(int) float64
Len() int
}
@@ -120,16 +121,29 @@ func (v *VecDense) Reset() {
// CloneVec makes a copy of a into the receiver, overwriting the previous value
// of the receiver.
-func (v *VecDense) CloneVec(a *VecDense) {
+func (v *VecDense) CloneVec(a Vector) {
if v == a {
return
}
- v.n = a.n
+ v.n = a.Len()
v.mat = blas64.Vector{
Inc: 1,
Data: use(v.mat.Data, v.n),
}
- blas64.Copy(v.n, a.mat, v.mat)
+ if r, ok := a.(RawVectorer); ok {
+ blas64.Copy(v.n, r.RawVector(), v.mat)
+ return
+ }
+ for i := 0; i < a.Len(); i++ {
+ v.SetVec(i, a.AtVec(i))
+ }
+}
+
+// VecDenseCopyOf returns a newly allocated copy of the elements of a.
+func VecDenseCopyOf(a Vector) *VecDense {
+ v := &VecDense{}
+ v.CloneVec(a)
+ return v
}
func (v *VecDense) RawVector() blas64.Vector {

mat: add VecDenseCopyOf function