Skip to content

Commit

Permalink
mat: rename Vector->VecDense
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak committed Jul 31, 2017
1 parent c91540f commit 862a4c5
Show file tree
Hide file tree
Showing 45 changed files with 383 additions and 383 deletions.
4 changes: 2 additions & 2 deletions diff/fd/jacobian.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func jacobianConcurrent(dst *mat.Dense, f func([]float64, []float64), x, origin
defer wg.Done()
xcopy := make([]float64, n)
y := make([]float64, m)
yVec := mat.NewVector(m, y)
yVec := mat.NewVecDense(m, y)
for job := range jobs {
copy(xcopy, x)
xcopy[job.j] += job.pt.Loc * step
Expand Down Expand Up @@ -179,7 +179,7 @@ func jacobianConcurrent(dst *mat.Dense, f func([]float64, []float64), x, origin
// all columns of dst. Iterate again over all Formula points
// because we don't forbid repeated locations.

originVec := mat.NewVector(m, origin)
originVec := mat.NewVecDense(m, origin)
for _, pt := range formula.Stencil {
if pt.Loc != 0 {
continue
Expand Down
14 changes: 7 additions & 7 deletions graph/network/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func PageRank(g graph.Directed, damp, tol float64) map[int64]float64 {
for i := range last {
last[i] = 1
}
lastV := mat.NewVector(len(nodes), last)
lastV := mat.NewVecDense(len(nodes), last)

vec := make([]float64, len(nodes))
var sum float64
Expand All @@ -68,7 +68,7 @@ func PageRank(g graph.Directed, damp, tol float64) map[int64]float64 {
for i := range vec {
vec[i] *= f
}
v := mat.NewVector(len(nodes), vec)
v := mat.NewVecDense(len(nodes), vec)

for {
lastV, v = v, lastV
Expand Down Expand Up @@ -122,7 +122,7 @@ func PageRankSparse(g graph.Directed, damp, tol float64) map[int64]float64 {
for i := range last {
last[i] = 1
}
lastV := mat.NewVector(len(nodes), last)
lastV := mat.NewVecDense(len(nodes), last)

vec := make([]float64, len(nodes))
var sum float64
Expand All @@ -135,7 +135,7 @@ func PageRankSparse(g graph.Directed, damp, tol float64) map[int64]float64 {
for i := range vec {
vec[i] *= f
}
v := mat.NewVector(len(nodes), vec)
v := mat.NewVecDense(len(nodes), vec)

dt := (1 - damp) / float64(len(nodes))
for {
Expand Down Expand Up @@ -171,7 +171,7 @@ func (m rowCompressedMatrix) addTo(i, j int, v float64) { m[i].addTo(j, v) }
// mulVecUnitary multiplies the receiver by the src vector, storing
// the result in dst. It assumes src and dst are the same length as m
// and that both have unitary vector increments.
func (m rowCompressedMatrix) mulVecUnitary(dst, src *mat.Vector) {
func (m rowCompressedMatrix) mulVecUnitary(dst, src *mat.VecDense) {
dMat := dst.RawVector().Data
for i, r := range m {
dMat[i] = r.dotUnitary(src)
Expand All @@ -190,7 +190,7 @@ func (r *compressedRow) addTo(j int, v float64) {

// dotUnitary performs a simplified scatter-based Ddot operations on
// v and the receiver. v must have have a unitary vector increment.
func (r compressedRow) dotUnitary(v *mat.Vector) float64 {
func (r compressedRow) dotUnitary(v *mat.VecDense) float64 {
var sum float64
vec := v.RawVector().Data
for _, e := range r {
Expand All @@ -208,7 +208,7 @@ type sparseElement struct {
// onesDotUnitary performs the equivalent of a Ddot of v with
// a ones vector of equal length. v must have have a unitary
// vector increment.
func onesDotUnitary(alpha float64, v *mat.Vector) float64 {
func onesDotUnitary(alpha float64, v *mat.VecDense) float64 {
var sum float64
for _, f := range v.RawVector().Data {
sum += alpha * f
Expand Down
4 changes: 2 additions & 2 deletions mat/cholesky.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (m *Dense) solveTwoChol(a, b *Cholesky) error {

// SolveVec finds the vector v that solves A * v = b where A is represented
// by the Cholesky decomposition, placing the result in v.
func (c *Cholesky) SolveVec(v, b *Vector) error {
func (c *Cholesky) SolveVec(v, b *VecDense) error {
if !c.valid() {
panic(badCholesky)
}
Expand Down Expand Up @@ -322,7 +322,7 @@ func (c *Cholesky) InverseTo(s *SymDense) error {
//
// SymRankOne updates a Cholesky factorization in O(n²) time. The Cholesky
// factorization computation from scratch is O(n³).
func (c *Cholesky) SymRankOne(orig *Cholesky, alpha float64, x *Vector) (ok bool) {
func (c *Cholesky) SymRankOne(orig *Cholesky, alpha float64, x *VecDense) (ok bool) {
if !orig.valid() {
panic(badCholesky)
}
Expand Down
6 changes: 3 additions & 3 deletions mat/cholesky_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func ExampleCholesky() {
fmt.Printf("\nThe determinant of a is %0.4g\n\n", chol.Det())

// Use the factorization to solve the system of equations a * x = b.
b := mat.NewVector(4, []float64{1, 2, 3, 4})
var x mat.Vector
b := mat.NewVecDense(4, []float64{1, 2, 3, 4})
var x mat.VecDense
if err := chol.SolveVec(&x, b); err != nil {
fmt.Println("Matrix is near singular: ", err)
}
Expand Down Expand Up @@ -83,7 +83,7 @@ func ExampleCholesky_SymRankOne() {
fmt.Println("matrix a is not positive definite.")
}

x := mat.NewVector(4, []float64{0, 0, 0, 1})
x := mat.NewVecDense(4, []float64{0, 0, 0, 1})
fmt.Printf("\nx = %0.4v\n", mat.Formatted(x, mat.Prefix(" ")))

// Rank-1 update the factorization.
Expand Down
20 changes: 10 additions & 10 deletions mat/cholesky_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,25 +178,25 @@ func TestSolveTwoChol(t *testing.T) {
func TestCholeskySolveVec(t *testing.T) {
for _, test := range []struct {
a *SymDense
b *Vector
ans *Vector
b *VecDense
ans *VecDense
}{
{
a: NewSymDense(2, []float64{
1, 0,
0, 1,
}),
b: NewVector(2, []float64{5, 6}),
ans: NewVector(2, []float64{5, 6}),
b: NewVecDense(2, []float64{5, 6}),
ans: NewVecDense(2, []float64{5, 6}),
},
{
a: NewSymDense(3, []float64{
53, 59, 37,
0, 83, 71,
0, 0, 101,
}),
b: NewVector(3, []float64{5, 6, 7}),
ans: NewVector(3, []float64{0.20745069393718094, -0.17421475529583694, 0.11577794010226464}),
b: NewVecDense(3, []float64{5, 6, 7}),
ans: NewVecDense(3, []float64{0.20745069393718094, -0.17421475529583694, 0.11577794010226464}),
},
} {
var chol Cholesky
Expand All @@ -205,13 +205,13 @@ func TestCholeskySolveVec(t *testing.T) {
t.Fatal("unexpected Cholesky factorization failure: not positive definite")
}

var x Vector
var x VecDense
chol.SolveVec(&x, test.b)
if !EqualApprox(&x, test.ans, 1e-12) {
t.Error("incorrect Cholesky solve solution")
}

var ans Vector
var ans VecDense
ans.MulVec(test.a, &x)
if !EqualApprox(&ans, test.b, 1e-12) {
t.Error("incorrect Cholesky solve solution product")
Expand Down Expand Up @@ -320,7 +320,7 @@ func TestCholeskySymRankOne(t *testing.T) {
for i := range xdata {
xdata[i] = rand.NormFloat64()
}
x := NewVector(n, xdata)
x := NewVecDense(n, xdata)

var chol Cholesky
ok := chol.Factorize(&a)
Expand Down Expand Up @@ -397,7 +397,7 @@ func TestCholeskySymRankOne(t *testing.T) {
continue
}

x := NewVector(len(test.x), test.x)
x := NewVecDense(len(test.x), test.x)
ok = chol.SymRankOne(&chol, test.alpha, x)
if !ok {
if test.wantOk {
Expand Down
16 changes: 8 additions & 8 deletions mat/dense.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,14 @@ func (m *Dense) T() Matrix {
return Transpose{m}
}

// ColView returns a Vector reflecting the column j, backed by the matrix data.
// ColView returns a VecDense reflecting the column j, backed by the matrix data.
//
// See ColViewer for more information.
func (m *Dense) ColView(j int) *Vector {
func (m *Dense) ColView(j int) *VecDense {
if j >= m.mat.Cols || j < 0 {
panic(ErrColAccess)
}
return &Vector{
return &VecDense{
mat: blas64.Vector{
Inc: m.mat.Stride,
Data: m.mat.Data[j : (m.mat.Rows-1)*m.mat.Stride+j+1],
Expand Down Expand Up @@ -250,11 +250,11 @@ func (m *Dense) SetRow(i int, src []float64) {
// backed by the matrix data.
//
// See RowViewer for more information.
func (m *Dense) RowView(i int) *Vector {
func (m *Dense) RowView(i int) *VecDense {
if i >= m.mat.Rows || i < 0 {
panic(ErrRowAccess)
}
return &Vector{
return &VecDense{
mat: blas64.Vector{
Inc: 1,
Data: m.rawRowView(i),
Expand Down Expand Up @@ -391,7 +391,7 @@ 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:
case *VecDense:
amat := aU.mat
mat.Data = make([]float64, aU.n)
blas64.Copy(aU.n,
Expand All @@ -415,7 +415,7 @@ func (m *Dense) Clone(a Matrix) {
// Copy makes a copy of elements of a into the receiver. It is similar to the
// built-in copy; it copies as much as the overlap between the two matrices and
// returns the number of rows and columns it copied. If a aliases the receiver
// and is a transposed Dense or Vector, with a non-unitary increment, Copy will
// and is a transposed Dense or VecDense, with a non-unitary increment, Copy will
// panic.
//
// See the Copier interface for more information.
Expand Down Expand Up @@ -457,7 +457,7 @@ func (m *Dense) Copy(a Matrix) (r, c int) {
// Nothing to do.
}
}
case *Vector:
case *VecDense:
var n, stride int
amat := aU.mat
if trans {
Expand Down
8 changes: 4 additions & 4 deletions mat/dense_arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (m *Dense) Mul(a, b Matrix) {
blas64.Trmm(blas.Right, bT, 1, bmat, m.mat)
return
}
if bU, ok := bU.(*Vector); ok {
if bU, ok := bU.(*VecDense); ok {
m.checkOverlap(bU.asGeneral())
bvec := bU.RawVector()
if bTrans {
Expand Down Expand Up @@ -399,7 +399,7 @@ func (m *Dense) Mul(a, b Matrix) {
blas64.Trmm(blas.Left, aT, 1, amat, m.mat)
return
}
if aU, ok := aU.(*Vector); ok {
if aU, ok := aU.(*VecDense); ok {
m.checkOverlap(aU.asGeneral())
avec := aU.RawVector()
if aTrans {
Expand Down Expand Up @@ -653,7 +653,7 @@ func (m *Dense) Apply(fn func(i, j int, v float64) float64, a Matrix) {
// RankOne performs a rank-one update to the matrix a and stores the result
// in the receiver. If a is zero, see Outer.
// m = a + alpha * x * y'
func (m *Dense) RankOne(a Matrix, alpha float64, x, y *Vector) {
func (m *Dense) RankOne(a Matrix, alpha float64, x, y *VecDense) {
ar, ac := a.Dims()
if x.Len() != ar {
panic(ErrShape)
Expand Down Expand Up @@ -683,7 +683,7 @@ func (m *Dense) RankOne(a Matrix, alpha float64, x, y *Vector) {
// in the receiver.
// m = alpha * x * y'
// In order to update an existing matrix, see RankOne.
func (m *Dense) Outer(alpha float64, x, y *Vector) {
func (m *Dense) Outer(alpha float64, x, y *VecDense) {
r := x.Len()
c := y.Len()

Expand Down
10 changes: 5 additions & 5 deletions mat/dense_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ func TestCopyDenseAlias(t *testing.T) {
}
}

func TestCopyVectorAlias(t *testing.T) {
func TestCopyVecDenseAlias(t *testing.T) {
for _, horiz := range []bool{false, true} {
for do := 0; do < 2; do++ {
for di := 0; di < 3; di++ {
Expand All @@ -1212,7 +1212,7 @@ func TestCopyVectorAlias(t *testing.T) {
4, 5, 6,
7, 8, 9,
})
var src *Vector
var src *VecDense
var want *Dense
if horiz {
src = a.RowView(si)
Expand Down Expand Up @@ -1549,12 +1549,12 @@ func TestRankOne(t *testing.T) {
a := NewDense(flatten(test.m))
m := &Dense{}
// Check with a new matrix
m.RankOne(a, test.alpha, NewVector(len(test.x), test.x), NewVector(len(test.y), test.y))
m.RankOne(a, test.alpha, NewVecDense(len(test.x), test.x), NewVecDense(len(test.y), test.y))
if !Equal(m, want) {
t.Errorf("unexpected result for RankOne test %d iteration 0: got: %+v want: %+v", i, m, want)
}
// Check with the same matrix
a.RankOne(a, test.alpha, NewVector(len(test.x), test.x), NewVector(len(test.y), test.y))
a.RankOne(a, test.alpha, NewVecDense(len(test.x), test.x), NewVecDense(len(test.y), test.y))
if !Equal(a, want) {
t.Errorf("unexpected result for Outer test %d iteration 1: got: %+v want: %+v", i, m, want)
}
Expand Down Expand Up @@ -1603,7 +1603,7 @@ func TestOuter(t *testing.T) {
var m Dense
for j := 0; j < 2; j++ {
// Check with a new matrix - and then again.
m.Outer(f, NewVector(len(test.x), test.x), NewVector(len(test.y), test.y))
m.Outer(f, NewVecDense(len(test.x), test.x), NewVecDense(len(test.y), test.y))
if !Equal(&m, want) {
t.Errorf("unexpected result for Outer test %d iteration %d scale %v: got: %+v want: %+v", i, j, f, m, want)
}
Expand Down
2 changes: 1 addition & 1 deletion mat/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
// if a and b both implement RawMatrixer, that is, they can be represented as a
// blas64.General, blas64.Gemm (general matrix multiplication) is called, while
// instead if b is a RawSymmetricer blas64.Symm is used (general-symmetric
// multiplication), and if b is a *Vector blas64.Gemv is used.
// multiplication), and if b is a *VecDense blas64.Gemv is used.
//
// There are many possible type combinations and special cases. No specific guarantees
// are made about the performance of any method, and in particular, note that an
Expand Down
2 changes: 1 addition & 1 deletion mat/eigen.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (e *Eigen) succFact() bool {
// Factorize returns whether the decomposition succeeded. If the decomposition
// failed, methods that require a successful factorization will panic.
func (e *Eigen) Factorize(a Matrix, left, right bool) (ok bool) {
// TODO(btracey): Change implementation to store Vectors as a *CMat when
// TODO(btracey): Change implementation to store VecDenses as a *CMat when
// #308 is resolved.

// Copy a because it is modified during the Lapack call.
Expand Down
6 changes: 3 additions & 3 deletions mat/eigen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ func TestSymEigen(t *testing.T) {

// Check that the eigenvalues are actually eigenvalues.
for i := 0; i < n; i++ {
v := NewVector(n, Col(nil, i, es.vectors))
var m Vector
v := NewVecDense(n, Col(nil, i, es.vectors))
var m VecDense
m.MulVec(s, v)

var scal Vector
var scal VecDense
scal.ScaleVec(es.values[i], v)

if !EqualApprox(&m, &scal, 1e-8) {
Expand Down
2 changes: 1 addition & 1 deletion mat/format_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func ExampleExcerpt() {
mat.Formatted(big, mat.Prefix(" "), mat.Excerpt(3)))

// The long vector is also too large, ...
long := mat.NewVector(100, nil)
long := mat.NewVecDense(100, nil)
for i := 0; i < 100; i++ {
long.SetVec(i, float64(i))
}
Expand Down
8 changes: 4 additions & 4 deletions mat/index_bound_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ func (m *Dense) set(i, j int, v float64) {

// At returns the element at row i.
// It panics if i is out of bounds or if j is not zero.
func (v *Vector) At(i, j int) float64 {
func (v *VecDense) At(i, j int) float64 {
if j != 0 {
panic(ErrColAccess)
}
return v.at(i)
}

func (v *Vector) at(i int) float64 {
func (v *VecDense) at(i int) float64 {
if uint(i) >= uint(v.n) {
panic(ErrRowAccess)
}
Expand All @@ -56,11 +56,11 @@ func (v *Vector) at(i int) float64 {

// SetVec sets the element at row i to the value val.
// It panics if i is out of bounds.
func (v *Vector) SetVec(i int, val float64) {
func (v *VecDense) SetVec(i int, val float64) {
v.setVec(i, val)
}

func (v *Vector) setVec(i int, val float64) {
func (v *VecDense) setVec(i int, val float64) {
if uint(i) >= uint(v.n) {
panic(ErrVectorAccess)
}
Expand Down
Loading

0 comments on commit 862a4c5

Please sign in to comment.