mat: rename Symmetric method to SymmetricDim

This commit is contained in:
Dan Kortschak
2021-08-21 19:22:39 +09:30
parent af39aebcaa
commit 3d32334f97
27 changed files with 81 additions and 81 deletions

View File

@@ -25,7 +25,7 @@ func Hessian(dst *mat.SymDense, f func(x []float64) float64, x []float64, settin
n := len(x) n := len(x)
if dst.IsEmpty() { if dst.IsEmpty() {
*dst = *(dst.GrowSym(n).(*mat.SymDense)) *dst = *(dst.GrowSym(n).(*mat.SymDense))
} else if dst.Symmetric() != n { } else if dst.SymmetricDim() != n {
panic("hessian: dst size mismatch") panic("hessian: dst size mismatch")
} }
dst.Zero() dst.Zero()
@@ -111,7 +111,7 @@ func hessianSerial(dst *mat.SymDense, f func(x []float64) float64, x []float64,
} }
func hessianConcurrent(dst *mat.SymDense, nWorkers, evals int, f func(x []float64) float64, x []float64, stencil []Point, step float64, originKnown bool, originValue float64) { func hessianConcurrent(dst *mat.SymDense, nWorkers, evals int, f func(x []float64) float64, x []float64, stencil []Point, step float64, originKnown bool, originValue float64) {
n := dst.Symmetric() n := dst.SymmetricDim()
type run struct { type run struct {
i, j int i, j int
iIdx, jIdx int iIdx, jIdx int

View File

@@ -86,7 +86,7 @@ func (Watson) Grad(grad, x []float64) {
func (Watson) Hess(hess mat.MutableSymmetric, x []float64) { func (Watson) Hess(hess mat.MutableSymmetric, x []float64) {
dim := len(x) dim := len(x)
if dim != hess.Symmetric() { if dim != hess.SymmetricDim() {
panic("incorrect size of the Hessian") panic("incorrect size of the Hessian")
} }

View File

@@ -107,7 +107,7 @@ func (g *UndirectedMatrix) From(id int64) graph.Nodes {
return graph.Empty return graph.Empty
} }
var nodes []graph.Node var nodes []graph.Node
r := g.mat.Symmetric() r := g.mat.SymmetricDim()
for i := 0; i < r; i++ { for i := 0; i < r; i++ {
if int64(i) == id { if int64(i) == id {
continue continue
@@ -161,7 +161,7 @@ func (g *UndirectedMatrix) Nodes() graph.Nodes {
copy(nodes, g.nodes) copy(nodes, g.nodes)
return iterator.NewOrderedNodes(nodes) return iterator.NewOrderedNodes(nodes)
} }
r := g.mat.Symmetric() r := g.mat.SymmetricDim()
// Matrix graphs must have at least one node. // Matrix graphs must have at least one node.
return iterator.NewImplicitNodes(0, r, newSimpleNode) return iterator.NewImplicitNodes(0, r, newSimpleNode)
} }
@@ -263,6 +263,6 @@ func (g *UndirectedMatrix) WeightedEdges() graph.WeightedEdges {
} }
func (g *UndirectedMatrix) has(id int64) bool { func (g *UndirectedMatrix) has(id int64) bool {
r := g.mat.Symmetric() r := g.mat.SymmetricDim()
return 0 <= id && id < int64(r) return 0 <= id && id < int64(r)
} }

View File

@@ -56,7 +56,7 @@ var _ Symmetric = &basicSymmetric{}
func (m *basicSymmetric) At(r, c int) float64 { return (*SymDense)(m).At(r, c) } func (m *basicSymmetric) At(r, c int) float64 { return (*SymDense)(m).At(r, c) }
func (m *basicSymmetric) Dims() (r, c int) { return (*SymDense)(m).Dims() } func (m *basicSymmetric) Dims() (r, c int) { return (*SymDense)(m).Dims() }
func (m *basicSymmetric) T() Matrix { return m } func (m *basicSymmetric) T() Matrix { return m }
func (m *basicSymmetric) Symmetric() int { return (*SymDense)(m).Symmetric() } func (m *basicSymmetric) SymmetricDim() int { return (*SymDense)(m).SymmetricDim() }
type basicTriangular TriDense type basicTriangular TriDense
@@ -87,7 +87,7 @@ func (m *basicSymBanded) Dims() (r, c int) { return (*SymBandDense)(m).Di
func (m *basicSymBanded) T() Matrix { return m } func (m *basicSymBanded) T() Matrix { return m }
func (m *basicSymBanded) Bandwidth() (kl, ku int) { return (*SymBandDense)(m).Bandwidth() } func (m *basicSymBanded) Bandwidth() (kl, ku int) { return (*SymBandDense)(m).Bandwidth() }
func (m *basicSymBanded) TBand() Banded { return m } func (m *basicSymBanded) TBand() Banded { return m }
func (m *basicSymBanded) Symmetric() int { return (*SymBandDense)(m).Symmetric() } func (m *basicSymBanded) SymmetricDim() int { return (*SymBandDense)(m).SymmetricDim() }
func (m *basicSymBanded) SymBand() (n, k int) { return (*SymBandDense)(m).SymBand() } func (m *basicSymBanded) SymBand() (n, k int) { return (*SymBandDense)(m).SymBand() }
type basicTriBanded TriBandDense type basicTriBanded TriBandDense
@@ -112,7 +112,7 @@ func (m *basicDiagonal) At(r, c int) float64 { return (*DiagDense)
func (m *basicDiagonal) Dims() (r, c int) { return (*DiagDense)(m).Dims() } func (m *basicDiagonal) Dims() (r, c int) { return (*DiagDense)(m).Dims() }
func (m *basicDiagonal) T() Matrix { return Transpose{m} } func (m *basicDiagonal) T() Matrix { return Transpose{m} }
func (m *basicDiagonal) Diag() int { return (*DiagDense)(m).Diag() } func (m *basicDiagonal) Diag() int { return (*DiagDense)(m).Diag() }
func (m *basicDiagonal) Symmetric() int { return (*DiagDense)(m).Symmetric() } func (m *basicDiagonal) SymmetricDim() int { return (*DiagDense)(m).SymmetricDim() }
func (m *basicDiagonal) SymBand() (n, k int) { return (*DiagDense)(m).SymBand() } func (m *basicDiagonal) SymBand() (n, k int) { return (*DiagDense)(m).SymBand() }
func (m *basicDiagonal) Bandwidth() (kl, ku int) { return (*DiagDense)(m).Bandwidth() } func (m *basicDiagonal) Bandwidth() (kl, ku int) { return (*DiagDense)(m).Bandwidth() }
func (m *basicDiagonal) TBand() Banded { return TransposeBand{m} } func (m *basicDiagonal) TBand() Banded { return TransposeBand{m} }

View File

@@ -90,7 +90,7 @@ func (c *Cholesky) At(i, j int) float64 {
if !c.valid() { if !c.valid() {
panic(badCholesky) panic(badCholesky)
} }
n := c.Symmetric() n := c.SymmetricDim()
if uint(i) >= uint(n) { if uint(i) >= uint(n) {
panic(ErrRowAccess) panic(ErrRowAccess)
} }
@@ -110,9 +110,9 @@ func (c *Cholesky) T() Matrix {
return c return c
} }
// Symmetric implements the Symmetric interface and returns the number of rows // SymmetricDim implements the Symmetric interface and returns the number of rows
// in the matrix (this is also the number of columns). // in the matrix (this is also the number of columns).
func (c *Cholesky) Symmetric() int { func (c *Cholesky) SymmetricDim() int {
r, _ := c.chol.Dims() r, _ := c.chol.Dims()
return r return r
} }
@@ -129,7 +129,7 @@ func (c *Cholesky) Cond() float64 {
// whether the matrix is positive definite. If Factorize returns false, the // whether the matrix is positive definite. If Factorize returns false, the
// factorization must not be used. // factorization must not be used.
func (c *Cholesky) Factorize(a Symmetric) (ok bool) { func (c *Cholesky) Factorize(a Symmetric) (ok bool) {
n := a.Symmetric() n := a.SymmetricDim()
if c.chol == nil { if c.chol == nil {
c.chol = NewTriDense(n, Upper, nil) c.chol = NewTriDense(n, Upper, nil)
} else { } else {
@@ -193,7 +193,7 @@ func (c *Cholesky) Clone(chol *Cholesky) {
if !chol.valid() { if !chol.valid() {
panic(badCholesky) panic(badCholesky)
} }
n := chol.Symmetric() n := chol.SymmetricDim()
if c.chol == nil { if c.chol == nil {
c.chol = NewTriDense(n, Upper, nil) c.chol = NewTriDense(n, Upper, nil)
} else { } else {
@@ -377,7 +377,7 @@ func (c *Cholesky) ToSym(dst *SymDense) {
if dst.IsEmpty() { if dst.IsEmpty() {
dst.ReuseAsSym(n) dst.ReuseAsSym(n)
} else { } else {
n2 := dst.Symmetric() n2 := dst.SymmetricDim()
if n != n2 { if n != n2 {
panic(ErrShape) panic(ErrShape)
} }
@@ -458,7 +458,7 @@ func (c *Cholesky) Scale(f float64, orig *Cholesky) {
if f <= 0 { if f <= 0 {
panic("cholesky: scaling by a non-positive constant") panic("cholesky: scaling by a non-positive constant")
} }
n := orig.Symmetric() n := orig.SymmetricDim()
if c.chol == nil { if c.chol == nil {
c.chol = NewTriDense(n, Upper, nil) c.chol = NewTriDense(n, Upper, nil)
} else if c.chol.mat.N != n { } else if c.chol.mat.N != n {
@@ -480,7 +480,7 @@ func (c *Cholesky) Scale(f float64, orig *Cholesky) {
// ExtendVecSym will panic if v.Len() != a.Symmetric()+1 or if a does not contain // ExtendVecSym will panic if v.Len() != a.Symmetric()+1 or if a does not contain
// a valid decomposition. // a valid decomposition.
func (c *Cholesky) ExtendVecSym(a *Cholesky, v Vector) (ok bool) { func (c *Cholesky) ExtendVecSym(a *Cholesky, v Vector) (ok bool) {
n := a.Symmetric() n := a.SymmetricDim()
if v.Len() != n+1 { if v.Len() != n+1 {
panic(badSliceLength) panic(badSliceLength)
@@ -549,7 +549,7 @@ func (c *Cholesky) SymRankOne(orig *Cholesky, alpha float64, x Vector) (ok bool)
if !orig.valid() { if !orig.valid() {
panic(badCholesky) panic(badCholesky)
} }
n := orig.Symmetric() n := orig.SymmetricDim()
if r, c := x.Dims(); r != n || c != 1 { if r, c := x.Dims(); r != n || c != 1 {
panic(ErrShape) panic(ErrShape)
} }
@@ -872,9 +872,9 @@ func (ch *BandCholesky) TBand() Banded {
return ch return ch
} }
// Symmetric implements the Symmetric interface and returns the number of rows // SymmetricDim implements the Symmetric interface and returns the number of rows
// in the matrix (this is also the number of columns). // in the matrix (this is also the number of columns).
func (ch *BandCholesky) Symmetric() int { func (ch *BandCholesky) SymmetricDim() int {
n, _ := ch.chol.Triangle() n, _ := ch.chol.Triangle()
return n return n
} }

View File

@@ -89,8 +89,8 @@ func TestCholeskyAt(t *testing.T) {
if !ok { if !ok {
t.Fatalf("Matrix not positive definite") t.Fatalf("Matrix not positive definite")
} }
n := test.Symmetric() n := test.SymmetricDim()
cn := chol.Symmetric() cn := chol.SymmetricDim()
if cn != n { if cn != n {
t.Errorf("Cholesky size does not match. Got %d, want %d", cn, n) t.Errorf("Cholesky size does not match. Got %d, want %d", cn, n)
} }
@@ -516,7 +516,7 @@ func TestCholeskyExtendVecSym(t *testing.T) {
}), }),
}, },
} { } {
n := test.a.Symmetric() n := test.a.SymmetricDim()
as := test.a.sliceSym(0, n-1) as := test.a.sliceSym(0, n-1)
// Compute the full factorization to use later (do the full factorization // Compute the full factorization to use later (do the full factorization

View File

@@ -54,10 +54,10 @@ type Diagonal interface {
Triangle() (int, TriKind) Triangle() (int, TriKind)
TTri() Triangular TTri() Triangular
// Symmetric and SymBand are included in the Diagonal interface // SymmetricDim and SymBand are included in the Diagonal interface
// to allow the use of Diagonal types in symmetric and banded symmetric // to allow the use of Diagonal types in symmetric and banded symmetric
// functions respectively. // functions respectively.
Symmetric() int SymmetricDim() int
SymBand() (n, k int) SymBand() (n, k int)
// TriBand and TTriBand are included in the Diagonal interface // TriBand and TTriBand are included in the Diagonal interface
@@ -137,8 +137,8 @@ func (d *DiagDense) Bandwidth() (kl, ku int) {
return 0, 0 return 0, 0
} }
// Symmetric implements the Symmetric interface. // SymmetricDim implements the Symmetric interface.
func (d *DiagDense) Symmetric() int { func (d *DiagDense) SymmetricDim() int {
return d.mat.N return d.mat.N
} }

View File

@@ -38,7 +38,7 @@ func (e *EigenSym) Factorize(a Symmetric, vectors bool) (ok bool) {
e.vectorsComputed = false e.vectorsComputed = false
e.values = e.values[:] e.values = e.values[:]
n := a.Symmetric() n := a.SymmetricDim()
sd := NewSymDense(n, nil) sd := NewSymDense(n, nil)
sd.CopySym(a) sd.CopySym(a)

View File

@@ -765,7 +765,7 @@ func makeCopyOf(a Matrix) Matrix {
m.CloneFrom(a) m.CloneFrom(a)
return returnAs(&m, t) return returnAs(&m, t)
case *SymDense, *basicSymmetric: case *SymDense, *basicSymmetric:
n := t.(Symmetric).Symmetric() n := t.(Symmetric).SymmetricDim()
m := NewSymDense(n, nil) m := NewSymDense(n, nil)
m.CopySym(t.(Symmetric)) m.CopySym(t.(Symmetric))
return returnAs(m, t) return returnAs(m, t)

View File

@@ -36,8 +36,8 @@ type SymBandDense struct {
type SymBanded interface { type SymBanded interface {
Banded Banded
// Symmetric returns the number of rows/columns in the matrix. // SymmetricDim returns the number of rows/columns in the matrix.
Symmetric() int SymmetricDim() int
// SymBand returns the number of rows/columns in the matrix, and the size of // SymBand returns the number of rows/columns in the matrix, and the size of
// the bandwidth. // the bandwidth.
@@ -115,8 +115,8 @@ func (s *SymBandDense) Dims() (r, c int) {
return s.mat.N, s.mat.N return s.mat.N, s.mat.N
} }
// Symmetric returns the size of the receiver. // SymmetricDim returns the size of the receiver.
func (s *SymBandDense) Symmetric() int { func (s *SymBandDense) SymmetricDim() int {
return s.mat.N return s.mat.N
} }

View File

@@ -37,8 +37,8 @@ type SymDense struct {
// the element at {j, i}). Symmetric matrices are always square. // the element at {j, i}). Symmetric matrices are always square.
type Symmetric interface { type Symmetric interface {
Matrix Matrix
// Symmetric returns the number of rows/columns in the matrix. // SymmetricDim returns the number of rows/columns in the matrix.
Symmetric() int SymmetricDim() int
} }
// A RawSymmetricer can return a view of itself as a BLAS Symmetric matrix. // A RawSymmetricer can return a view of itself as a BLAS Symmetric matrix.
@@ -100,9 +100,9 @@ func (s *SymDense) T() Matrix {
return s return s
} }
// Symmetric implements the Symmetric interface and returns the number of rows // SymmetricDim implements the Symmetric interface and returns the number of rows
// and columns in the matrix. // and columns in the matrix.
func (s *SymDense) Symmetric() int { func (s *SymDense) SymmetricDim() int {
return s.mat.N return s.mat.N
} }
@@ -234,7 +234,7 @@ func (s *SymDense) reuseAsZeroed(n int) {
} }
func (s *SymDense) isolatedWorkspace(a Symmetric) (w *SymDense, restore func()) { func (s *SymDense) isolatedWorkspace(a Symmetric) (w *SymDense, restore func()) {
n := a.Symmetric() n := a.SymmetricDim()
if n == 0 { if n == 0 {
panic(ErrZeroLength) panic(ErrZeroLength)
} }
@@ -258,8 +258,8 @@ func (s *SymDense) DiagView() Diagonal {
} }
func (s *SymDense) AddSym(a, b Symmetric) { func (s *SymDense) AddSym(a, b Symmetric) {
n := a.Symmetric() n := a.SymmetricDim()
if n != b.Symmetric() { if n != b.SymmetricDim() {
panic(ErrShape) panic(ErrShape)
} }
s.reuseAsNonZeroed(n) s.reuseAsNonZeroed(n)
@@ -295,7 +295,7 @@ func (s *SymDense) AddSym(a, b Symmetric) {
} }
func (s *SymDense) CopySym(a Symmetric) int { func (s *SymDense) CopySym(a Symmetric) int {
n := a.Symmetric() n := a.SymmetricDim()
n = min(n, s.mat.N) n = min(n, s.mat.N)
if n == 0 { if n == 0 {
return 0 return 0
@@ -325,7 +325,7 @@ func (s *SymDense) CopySym(a Symmetric) int {
// s = a + alpha * x * xᵀ // s = a + alpha * x * xᵀ
func (s *SymDense) SymRankOne(a Symmetric, alpha float64, x Vector) { func (s *SymDense) SymRankOne(a Symmetric, alpha float64, x Vector) {
n := x.Len() n := x.Len()
if a.Symmetric() != n { if a.SymmetricDim() != n {
panic(ErrShape) panic(ErrShape)
} }
s.reuseAsNonZeroed(n) s.reuseAsNonZeroed(n)
@@ -357,7 +357,7 @@ func (s *SymDense) SymRankOne(a Symmetric, alpha float64, x Vector) {
// result into the receiver. If a is zero, see SymOuterK. // result into the receiver. If a is zero, see SymOuterK.
// s = a + alpha * x * x' // s = a + alpha * x * x'
func (s *SymDense) SymRankK(a Symmetric, alpha float64, x Matrix) { func (s *SymDense) SymRankK(a Symmetric, alpha float64, x Matrix) {
n := a.Symmetric() n := a.SymmetricDim()
r, _ := x.Dims() r, _ := x.Dims()
if r != n { if r != n {
panic(ErrShape) panic(ErrShape)
@@ -495,7 +495,7 @@ func (s *SymDense) RankTwo(a Symmetric, alpha float64, x, y Vector) {
// ScaleSym multiplies the elements of a by f, placing the result in the receiver. // ScaleSym multiplies the elements of a by f, placing the result in the receiver.
func (s *SymDense) ScaleSym(f float64, a Symmetric) { func (s *SymDense) ScaleSym(f float64, a Symmetric) {
n := a.Symmetric() n := a.SymmetricDim()
s.reuseAsNonZeroed(n) s.reuseAsNonZeroed(n)
if a, ok := a.(RawSymmetricer); ok { if a, ok := a.(RawSymmetricer); ok {
amat := a.RawSymmetric() amat := a.RawSymmetric()
@@ -523,7 +523,7 @@ func (s *SymDense) ScaleSym(f float64, a Symmetric) {
// have to be a strict subset, dimension repeats are allowed. // have to be a strict subset, dimension repeats are allowed.
func (s *SymDense) SubsetSym(a Symmetric, set []int) { func (s *SymDense) SubsetSym(a Symmetric, set []int) {
n := len(set) n := len(set)
na := a.Symmetric() na := a.SymmetricDim()
s.reuseAsNonZeroed(n) s.reuseAsNonZeroed(n)
var restore func() var restore func()
if a == s { if a == s {
@@ -663,7 +663,7 @@ func (s *SymDense) GrowSym(n int) Symmetric {
// PowPSD returns an error if the matrix is not positive symmetric definite // PowPSD returns an error if the matrix is not positive symmetric definite
// or the Eigen decomposition is not successful. // or the Eigen decomposition is not successful.
func (s *SymDense) PowPSD(a Symmetric, pow float64) error { func (s *SymDense) PowPSD(a Symmetric, pow float64) error {
dim := a.Symmetric() dim := a.SymmetricDim()
s.reuseAsNonZeroed(dim) s.reuseAsNonZeroed(dim)
var eigen EigenSym var eigen EigenSym

View File

@@ -659,7 +659,7 @@ func TestViewGrowSquare(t *testing.T) {
// Grow the matrix back to the original view // Grow the matrix back to the original view
gn := n - start1 - start2 gn := n - start1 - start2
g := v2.GrowSym(gn - v2.Symmetric()).(*SymDense) g := v2.GrowSym(gn - v2.SymmetricDim()).(*SymDense)
g.SetSym(1, 1, 2.2) g.SetSym(1, 1, 2.2)
for i := 0; i < gn; i++ { for i := 0; i < gn; i++ {

View File

@@ -659,7 +659,7 @@ func (t *TriDense) Trace() float64 {
// copySymIntoTriangle copies a symmetric matrix into a TriDense // copySymIntoTriangle copies a symmetric matrix into a TriDense
func copySymIntoTriangle(t *TriDense, s Symmetric) { func copySymIntoTriangle(t *TriDense, s Symmetric) {
n, upper := t.Triangle() n, upper := t.Triangle()
ns := s.Symmetric() ns := s.SymmetricDim()
if n != ns { if n != ns {
panic("mat: triangle size mismatch") panic("mat: triangle size mismatch")
} }

View File

@@ -206,7 +206,7 @@ func (cma *CmaEsChol) Init(dim, tasks int) int {
cma.mean = resize(cma.mean, dim) // mean location initialized at the start of Run cma.mean = resize(cma.mean, dim) // mean location initialized at the start of Run
if cma.InitCholesky != nil { if cma.InitCholesky != nil {
if cma.InitCholesky.Symmetric() != dim { if cma.InitCholesky.SymmetricDim() != dim {
panic("cma-es-chol: incorrect InitCholesky size") panic("cma-es-chol: incorrect InitCholesky size")
} }
cma.chol.Clone(cma.InitCholesky) cma.chol.Clone(cma.InitCholesky)

View File

@@ -60,7 +60,7 @@ func (Beale) Hess(dst *mat.SymDense, x []float64) {
if len(x) != 2 { if len(x) != 2 {
panic("dimension of the problem must be 2") panic("dimension of the problem must be 2")
} }
if len(x) != dst.Symmetric() { if len(x) != dst.SymmetricDim() {
panic("incorrect size of the Hessian") panic("incorrect size of the Hessian")
} }
@@ -562,7 +562,7 @@ func (BrownBadlyScaled) Hess(dst *mat.SymDense, x []float64) {
if len(x) != 2 { if len(x) != 2 {
panic("dimension of the problem must be 2") panic("dimension of the problem must be 2")
} }
if len(x) != dst.Symmetric() { if len(x) != dst.SymmetricDim() {
panic("incorrect size of the Hessian") panic("incorrect size of the Hessian")
} }
@@ -639,7 +639,7 @@ func (BrownAndDennis) Hess(dst *mat.SymDense, x []float64) {
if len(x) != 4 { if len(x) != 4 {
panic("dimension of the problem must be 4") panic("dimension of the problem must be 4")
} }
if len(x) != dst.Symmetric() { if len(x) != dst.SymmetricDim() {
panic("incorrect size of the Hessian") panic("incorrect size of the Hessian")
} }
@@ -1274,7 +1274,7 @@ func (PowellBadlyScaled) Hess(dst *mat.SymDense, x []float64) {
if len(x) != 2 { if len(x) != 2 {
panic("dimension of the problem must be 2") panic("dimension of the problem must be 2")
} }
if len(x) != dst.Symmetric() { if len(x) != dst.SymmetricDim() {
panic("incorrect size of the Hessian") panic("incorrect size of the Hessian")
} }
@@ -1522,7 +1522,7 @@ func (Watson) Grad(grad, x []float64) {
func (Watson) Hess(dst *mat.SymDense, x []float64) { func (Watson) Hess(dst *mat.SymDense, x []float64) {
dim := len(x) dim := len(x)
if len(x) != dst.Symmetric() { if len(x) != dst.SymmetricDim() {
panic("incorrect size of the Hessian") panic("incorrect size of the Hessian")
} }
@@ -1644,7 +1644,7 @@ func (Wood) Hess(dst *mat.SymDense, x []float64) {
if len(x) != 4 { if len(x) != 4 {
panic("dimension of the problem must be 4") panic("dimension of the problem must be 4")
} }
if len(x) != dst.Symmetric() { if len(x) != dst.SymmetricDim() {
panic("incorrect size of the Hessian") panic("incorrect size of the Hessian")
} }

View File

@@ -447,7 +447,7 @@ func getInitLocation(dim int, initX []float64, initValues *Location) (Operation,
op |= GradEvaluation op |= GradEvaluation
} }
if initValues.Hessian != nil { if initValues.Hessian != nil {
if initValues.Hessian.Symmetric() != dim { if initValues.Hessian.SymmetricDim() != dim {
panic("optimize: initial Hessian does not match problem dimension") panic("optimize: initial Hessian does not match problem dimension")
} }
loc.Hessian = initValues.Hessian loc.Hessian = initValues.Hessian

View File

@@ -44,7 +44,7 @@ type Wishart struct {
// //
// NewWishart panics if nu <= d - 1 where d is the order of v. // NewWishart panics if nu <= d - 1 where d is the order of v.
func NewWishart(v mat.Symmetric, nu float64, src rand.Source) (*Wishart, bool) { func NewWishart(v mat.Symmetric, nu float64, src rand.Source) (*Wishart, bool) {
dim := v.Symmetric() dim := v.SymmetricDim()
if nu <= float64(dim-1) { if nu <= float64(dim-1) {
panic("wishart: nu must be greater than dim-1") panic("wishart: nu must be greater than dim-1")
} }
@@ -75,7 +75,7 @@ func NewWishart(v mat.Symmetric, nu float64, src rand.Source) (*Wishart, bool) {
func (w *Wishart) MeanSymTo(dst *mat.SymDense) { func (w *Wishart) MeanSymTo(dst *mat.SymDense) {
if dst.IsEmpty() { if dst.IsEmpty() {
dst.ReuseAsSym(w.dim) dst.ReuseAsSym(w.dim)
} else if dst.Symmetric() != w.dim { } else if dst.SymmetricDim() != w.dim {
panic(badDim) panic(badDim)
} }
w.setV() w.setV()
@@ -94,7 +94,7 @@ func (w *Wishart) ProbSym(x mat.Symmetric) float64 {
// LogProbSym returns -∞ if the input matrix is not positive definite (the Cholesky // LogProbSym returns -∞ if the input matrix is not positive definite (the Cholesky
// decomposition fails). // decomposition fails).
func (w *Wishart) LogProbSym(x mat.Symmetric) float64 { func (w *Wishart) LogProbSym(x mat.Symmetric) float64 {
dim := x.Symmetric() dim := x.SymmetricDim()
if dim != w.dim { if dim != w.dim {
panic(badDim) panic(badDim)
} }
@@ -109,7 +109,7 @@ func (w *Wishart) LogProbSym(x mat.Symmetric) float64 {
// LogProbSymChol returns the log of the probability of the input symmetric matrix // LogProbSymChol returns the log of the probability of the input symmetric matrix
// given its Cholesky decomposition. // given its Cholesky decomposition.
func (w *Wishart) LogProbSymChol(cholX *mat.Cholesky) float64 { func (w *Wishart) LogProbSymChol(cholX *mat.Cholesky) float64 {
dim := cholX.Symmetric() dim := cholX.SymmetricDim()
if dim != w.dim { if dim != w.dim {
panic(badDim) panic(badDim)
} }

View File

@@ -111,7 +111,7 @@ func TestWishartRand(t *testing.T) {
}, },
} { } {
rnd := rand.New(rand.NewSource(1)) rnd := rand.New(rand.NewSource(1))
dim := test.v.Symmetric() dim := test.v.SymmetricDim()
w, ok := NewWishart(test.v, test.nu, rnd) w, ok := NewWishart(test.v, test.nu, rnd)
if !ok { if !ok {
panic("bad test") panic("bad test")

View File

@@ -66,7 +66,7 @@ func NewDirichlet(alpha []float64, src rand.Source) *Dirichlet {
func (d *Dirichlet) CovarianceMatrix(dst *mat.SymDense) { func (d *Dirichlet) CovarianceMatrix(dst *mat.SymDense) {
if dst.IsEmpty() { if dst.IsEmpty() {
*dst = *(dst.GrowSym(d.dim).(*mat.SymDense)) *dst = *(dst.GrowSym(d.dim).(*mat.SymDense))
} else if dst.Symmetric() != d.dim { } else if dst.SymmetricDim() != d.dim {
panic("dirichelet: input matrix size mismatch") panic("dirichelet: input matrix size mismatch")
} }
scale := 1 / (d.sumAlpha * d.sumAlpha * (d.sumAlpha + 1)) scale := 1 / (d.sumAlpha * d.sumAlpha * (d.sumAlpha + 1))

View File

@@ -77,7 +77,7 @@ type Cover interface {
func checkCov(t *testing.T, cas int, x *mat.Dense, c Cover, tol float64) { func checkCov(t *testing.T, cas int, x *mat.Dense, c Cover, tol float64) {
var cov mat.SymDense var cov mat.SymDense
c.CovarianceMatrix(&cov) c.CovarianceMatrix(&cov)
n := cov.Symmetric() n := cov.SymmetricDim()
cov2 := mat.NewSymDense(n, nil) cov2 := mat.NewSymDense(n, nil)
c.CovarianceMatrix(cov2) c.CovarianceMatrix(cov2)
if !mat.Equal(&cov, cov2) { if !mat.Equal(&cov, cov2) {

View File

@@ -43,7 +43,7 @@ func NewNormal(mu []float64, sigma mat.Symmetric, src rand.Source) (*Normal, boo
if len(mu) == 0 { if len(mu) == 0 {
panic(badZeroDimension) panic(badZeroDimension)
} }
dim := sigma.Symmetric() dim := sigma.SymmetricDim()
if dim != len(mu) { if dim != len(mu) {
panic(badSizeMismatch) panic(badSizeMismatch)
} }
@@ -69,7 +69,7 @@ func NewNormal(mu []float64, sigma mat.Symmetric, src rand.Source) (*Normal, boo
// panics if len(mu) is not equal to chol.Size(). // panics if len(mu) is not equal to chol.Size().
func NewNormalChol(mu []float64, chol *mat.Cholesky, src rand.Source) *Normal { func NewNormalChol(mu []float64, chol *mat.Cholesky, src rand.Source) *Normal {
dim := len(mu) dim := len(mu)
if dim != chol.Symmetric() { if dim != chol.SymmetricDim() {
panic(badSizeMismatch) panic(badSizeMismatch)
} }
n := &Normal{ n := &Normal{
@@ -93,7 +93,7 @@ func NewNormalPrecision(mu []float64, prec *mat.SymDense, src rand.Source) (norm
if len(mu) == 0 { if len(mu) == 0 {
panic(badZeroDimension) panic(badZeroDimension)
} }
dim := prec.Symmetric() dim := prec.SymmetricDim()
if dim != len(mu) { if dim != len(mu) {
panic(badSizeMismatch) panic(badSizeMismatch)
} }
@@ -161,7 +161,7 @@ func (n *Normal) ConditionNormal(observed []int, values []float64, src rand.Sour
func (n *Normal) CovarianceMatrix(dst *mat.SymDense) { func (n *Normal) CovarianceMatrix(dst *mat.SymDense) {
if dst.IsEmpty() { if dst.IsEmpty() {
*dst = *(dst.GrowSym(n.dim).(*mat.SymDense)) *dst = *(dst.GrowSym(n.dim).(*mat.SymDense))
} else if dst.Symmetric() != n.dim { } else if dst.SymmetricDim() != n.dim {
panic("normal: input matrix size mismatch") panic("normal: input matrix size mismatch")
} }
dst.CopySym(&n.sigma) dst.CopySym(&n.sigma)
@@ -197,7 +197,7 @@ func NormalLogProb(x, mu []float64, chol *mat.Cholesky) float64 {
if len(x) != dim { if len(x) != dim {
panic(badSizeMismatch) panic(badSizeMismatch)
} }
if chol.Symmetric() != dim { if chol.SymmetricDim() != dim {
panic(badSizeMismatch) panic(badSizeMismatch)
} }
logSqrtDet := 0.5 * chol.LogDet() logSqrtDet := 0.5 * chol.LogDet()
@@ -301,7 +301,7 @@ func (n *Normal) Rand(x []float64) []float64 {
// available. Otherwise, the NewNormal function should be used. // available. Otherwise, the NewNormal function should be used.
func NormalRand(x, mean []float64, chol *mat.Cholesky, src rand.Source) []float64 { func NormalRand(x, mean []float64, chol *mat.Cholesky, src rand.Source) []float64 {
x = reuseAs(x, len(mean)) x = reuseAs(x, len(mean))
if len(mean) != chol.Symmetric() { if len(mean) != chol.SymmetricDim() {
panic(badInputLength) panic(badInputLength)
} }
if src == nil { if src == nil {

View File

@@ -399,7 +399,7 @@ func TestCovarianceMatrix(t *testing.T) {
if !mat.EqualApprox(&cov, test.sigma, 1e-14) { if !mat.EqualApprox(&cov, test.sigma, 1e-14) {
t.Errorf("Covariance mismatch with nil input") t.Errorf("Covariance mismatch with nil input")
} }
dim := test.sigma.Symmetric() dim := test.sigma.SymmetricDim()
cov = *mat.NewSymDense(dim, nil) cov = *mat.NewSymDense(dim, nil)
normal.CovarianceMatrix(&cov) normal.CovarianceMatrix(&cov)
if !mat.EqualApprox(&cov, test.sigma, 1e-14) { if !mat.EqualApprox(&cov, test.sigma, 1e-14) {

View File

@@ -55,7 +55,7 @@ func NewStudentsT(mu []float64, sigma mat.Symmetric, nu float64, src rand.Source
if len(mu) == 0 { if len(mu) == 0 {
panic(badZeroDimension) panic(badZeroDimension)
} }
dim := sigma.Symmetric() dim := sigma.SymmetricDim()
if dim != len(mu) { if dim != len(mu) {
panic(badSizeMismatch) panic(badSizeMismatch)
} }
@@ -231,7 +231,7 @@ func findUnob(observed []int, dim int) (unobserved []int) {
func (st *StudentsT) CovarianceMatrix(dst *mat.SymDense) { func (st *StudentsT) CovarianceMatrix(dst *mat.SymDense) {
if dst.IsEmpty() { if dst.IsEmpty() {
*dst = *(dst.GrowSym(st.dim).(*mat.SymDense)) *dst = *(dst.GrowSym(st.dim).(*mat.SymDense))
} else if dst.Symmetric() != st.dim { } else if dst.SymmetricDim() != st.dim {
panic("studentst: input matrix size mismatch") panic("studentst: input matrix size mismatch")
} }
dst.CopySym(&st.sigma) dst.CopySym(&st.sigma)

View File

@@ -24,7 +24,7 @@ import (
func TorgersonScaling(dst *mat.Dense, eigdst []float64, dis mat.Symmetric) (k int, eig []float64) { func TorgersonScaling(dst *mat.Dense, eigdst []float64, dis mat.Symmetric) (k int, eig []float64) {
// https://doi.org/10.1007/0-387-28981-X_12 // https://doi.org/10.1007/0-387-28981-X_12
n := dis.Symmetric() n := dis.SymmetricDim()
if dst.IsEmpty() { if dst.IsEmpty() {
dst.ReuseAs(n, n) dst.ReuseAs(n, n)
} else { } else {

View File

@@ -173,7 +173,7 @@ type ProposalNormal struct {
// //
// NewProposalNormal returns {nil, false} if the covariance matrix is not positive-definite. // NewProposalNormal returns {nil, false} if the covariance matrix is not positive-definite.
func NewProposalNormal(sigma *mat.SymDense, src rand.Source) (*ProposalNormal, bool) { func NewProposalNormal(sigma *mat.SymDense, src rand.Source) (*ProposalNormal, bool) {
mu := make([]float64, sigma.Symmetric()) mu := make([]float64, sigma.SymmetricDim())
normal, ok := distmv.NewNormal(mu, sigma, src) normal, ok := distmv.NewNormal(mu, sigma, src)
if !ok { if !ok {
return nil, false return nil, false

View File

@@ -29,7 +29,7 @@ func CovarianceMatrix(dst *mat.SymDense, x mat.Matrix, weights []float64) {
if dst.IsEmpty() { if dst.IsEmpty() {
*dst = *(dst.GrowSym(c).(*mat.SymDense)) *dst = *(dst.GrowSym(c).(*mat.SymDense))
} else if n := dst.Symmetric(); n != c { } else if n := dst.SymmetricDim(); n != c {
panic(mat.ErrShape) panic(mat.ErrShape)
} }
@@ -86,7 +86,7 @@ func CorrelationMatrix(dst *mat.SymDense, x mat.Matrix, weights []float64) {
// covToCorr converts a covariance matrix to a correlation matrix. // covToCorr converts a covariance matrix to a correlation matrix.
func covToCorr(c *mat.SymDense) { func covToCorr(c *mat.SymDense) {
r := c.Symmetric() r := c.SymmetricDim()
s := make([]float64, r) s := make([]float64, r)
for i := 0; i < r; i++ { for i := 0; i < r; i++ {

View File

@@ -244,7 +244,7 @@ func TestCorrCov(t *testing.T) {
CorrelationMatrix(&corr, test.data, test.weights) CorrelationMatrix(&corr, test.data, test.weights)
CovarianceMatrix(&cov, test.data, test.weights) CovarianceMatrix(&cov, test.data, test.weights)
r := cov.Symmetric() r := cov.SymmetricDim()
// Get the diagonal elements from cov to determine the sigmas. // Get the diagonal elements from cov to determine the sigmas.
sigmas := make([]float64, r) sigmas := make([]float64, r)
@@ -252,11 +252,11 @@ func TestCorrCov(t *testing.T) {
sigmas[i] = math.Sqrt(cov.At(i, i)) sigmas[i] = math.Sqrt(cov.At(i, i))
} }
covFromCorr := mat.NewSymDense(corr.Symmetric(), nil) covFromCorr := mat.NewSymDense(corr.SymmetricDim(), nil)
covFromCorr.CopySym(&corr) covFromCorr.CopySym(&corr)
corrToCov(covFromCorr, sigmas) corrToCov(covFromCorr, sigmas)
corrFromCov := mat.NewSymDense(cov.Symmetric(), nil) corrFromCov := mat.NewSymDense(cov.SymmetricDim(), nil)
corrFromCov.CopySym(&cov) corrFromCov.CopySym(&cov)
covToCorr(corrFromCov) covToCorr(corrFromCov)
@@ -450,7 +450,7 @@ func BenchmarkCovToCorr(b *testing.B) {
m := randMat(small, small) m := randMat(small, small)
var cov mat.SymDense var cov mat.SymDense
CovarianceMatrix(&cov, m, nil) CovarianceMatrix(&cov, m, nil)
cc := mat.NewSymDense(cov.Symmetric(), nil) cc := mat.NewSymDense(cov.SymmetricDim(), nil)
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
b.StopTimer() b.StopTimer()
@@ -465,7 +465,7 @@ func BenchmarkCorrToCov(b *testing.B) {
m := randMat(small, small) m := randMat(small, small)
var corr mat.SymDense var corr mat.SymDense
CorrelationMatrix(&corr, m, nil) CorrelationMatrix(&corr, m, nil)
cc := mat.NewSymDense(corr.Symmetric(), nil) cc := mat.NewSymDense(corr.SymmetricDim(), nil)
sigma := make([]float64, small) sigma := make([]float64, small)
for i := range sigma { for i := range sigma {
sigma[i] = 2 sigma[i] = 2