Skip to content

Commit

Permalink
mat: extend Vector slicing sematics to allow slicing up to capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak committed Jun 21, 2017
1 parent 66c06cc commit 82a7dd2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
24 changes: 21 additions & 3 deletions mat/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func NewVector(n int, data []float64) *Vector {

// SliceVec returns a new Vector that shares backing data with the receiver.
// The returned matrix starts at i of the receiver and extends k-i elements.
// SliceVec panics with ErrIndexOutOfRange if the slice is outside the bounds
// SliceVec panics with ErrIndexOutOfRange if the slice is outside the capacity
// of the receiver.
func (v *Vector) SliceVec(i, k int) *Vector {
if i < 0 || k <= i || v.n < k {
if i < 0 || k <= i || v.Cap() < k {
panic(ErrIndexOutOfRange)
}
return &Vector{
Expand All @@ -64,19 +64,37 @@ func (v *Vector) SliceVec(i, k int) *Vector {
}
}

// Dims returns the number of rows and columns in the matrix. Columns is always 1.
// Dims returns the number of rows and columns in the matrix. Columns is always 1
// for a non-Reset vector.
func (v *Vector) Dims() (r, c int) {
if v.isZero() {
return 0, 0
}
return v.n, 1
}

// Caps returns the number of rows and columns in the backing matrix. Columns is always 1
// for a non-Reset vector.
func (v *Vector) Caps() (r, c int) {
if v.isZero() {
return 0, 0
}
return v.Cap(), 1
}

// Len returns the length of the vector.
func (v *Vector) Len() int {
return v.n
}

// Cap returns the capacity of the vector.
func (v *Vector) Cap() int {
if v.isZero() {
return 0
}
return (cap(v.mat.Data)-1)/v.mat.Inc + 1
}

// T performs an implicit transpose by returning the receiver inside a Transpose.
func (v *Vector) T() Matrix {
return Transpose{v}
Expand Down
64 changes: 64 additions & 0 deletions mat/vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,70 @@ func TestNewVector(t *testing.T) {
}
}

func TestCap(t *testing.T) {
for i, test := range []struct {
vector *Vector
want int
}{
{vector: NewVector(3, nil), want: 3},
{
vector: &Vector{
mat: blas64.Vector{
Data: make([]float64, 7, 10),
Inc: 3,
},
n: 3,
},
want: 4,
},
{
vector: &Vector{
mat: blas64.Vector{
Data: make([]float64, 10),
Inc: 3,
},
n: 4,
},
want: 4,
},
{
vector: &Vector{
mat: blas64.Vector{
Data: make([]float64, 11),
Inc: 3,
},
n: 4,
},
want: 4,
},
{
vector: &Vector{
mat: blas64.Vector{
Data: make([]float64, 12),
Inc: 3,
},
n: 4,
},
want: 4,
},
{
vector: &Vector{
mat: blas64.Vector{
Data: make([]float64, 13),
Inc: 3,
},
n: 4,
},
want: 5,
},
} {
got := test.vector.Cap()
if got != test.want {
t.Errorf("unexpected capacty for test %d: got: %d want: %d", i, got, test.want)
}
}
}

func TestVectorAtSet(t *testing.T) {
for i, test := range []struct {
vector *Vector
Expand Down

0 comments on commit 82a7dd2

Please sign in to comment.