From 191c887e5dc7d120e9a3cb4b45cb4504e081bf91 Mon Sep 17 00:00:00 2001 From: btracey Date: Tue, 13 Jan 2015 11:17:13 -0800 Subject: [PATCH 01/11] Added NewSymmetric and Dims functions --- mat64/symmetric.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 mat64/symmetric.go diff --git a/mat64/symmetric.go b/mat64/symmetric.go new file mode 100644 index 0000000..d2e5455 --- /dev/null +++ b/mat64/symmetric.go @@ -0,0 +1,46 @@ +package mat64 + +import ( + "github.com/gonum/blas" + "github.com/gonum/blas/blas64" +) + +// Symmetric represents a symmetric matrix. +type Symmetric struct { + mat blas64.Symmetric +} + +// NewTriangular constructs an n x n triangular matrix where the data is stored +// in the given orientation. If len(mat) == n * n, mat will be used to hold the +// underlying data, or if mat == nil, new data will be allocated. +// The underlying data representation is the same as a Dense matrix, except +// the values of the entries in the opposite half are completely ignored. +func NewSymmetric(n int, t triType, mat []float64) *Symmetric { + if n < 0 { + panic("mat64: negative dimension") + } + if mat != nil && n*n != len(mat) { + panic(ErrShape) + } + if mat == nil { + mat = make([]float64, n*n) + } + if t != Upper && t != Lower { + panic("mat64: bad TriSide") + } + return &Symmetric{blas64.Symmetric{ + N: n, + Stride: n, + Data: mat, + Uplo: blas.Uplo(t), + Diag: blas.NonUnit, + }} +} + +func (s *Symmetric) Dims() (r, c int) { + return s.mat.N, s.mat.N +} + +func (s *Symmetric) RawSymmetric() blas64.Symmetric { + return s.mat +} From ba4381705ce7b2bda57e6e652f8647d66b3c9884 Mon Sep 17 00:00:00 2001 From: btracey Date: Thu, 15 Jan 2015 14:23:08 -0800 Subject: [PATCH 02/11] Added symmetric At, Set, and added tests --- mat64/index_bound_checks.go | 46 +++++++++++++++++++++ mat64/index_no_bound_checks.go | 46 +++++++++++++++++++++ mat64/matrix.go | 2 + mat64/symmetric.go | 10 ++--- mat64/symmetric_test.go | 74 ++++++++++++++++++++++++++++++++++ 5 files changed, 173 insertions(+), 5 deletions(-) create mode 100644 mat64/symmetric_test.go diff --git a/mat64/index_bound_checks.go b/mat64/index_bound_checks.go index f518c74..2e564b5 100644 --- a/mat64/index_bound_checks.go +++ b/mat64/index_bound_checks.go @@ -8,6 +8,8 @@ package mat64 +import "github.com/gonum/blas" + func (m *Dense) At(r, c int) float64 { return m.at(r, c) } @@ -35,3 +37,47 @@ func (m *Dense) set(r, c int, v float64) { } m.mat.Data[r*m.mat.Stride+c] = v } + +// At returns the element at row r and column c. +func (t *Symmetric) At(r, c int) float64 { + return t.at(r, c) +} +func (t *Symmetric) at(r, c int) float64 { + if r >= t.mat.N || r < 0 { + panic(ErrRowAccess) + } + if c >= t.mat.N || c < 0 { + panic(ErrColAccess) + } + if t.mat.Uplo == blas.Upper { + if r > c { + r, c = c, r + } + return t.mat.Data[r*t.mat.Stride+c] + } + if r < c { + r, c = c, r + } + return t.mat.Data[r*t.mat.Stride+c] +} + +// Set sets the element at row r and column c. Set panics if the location is outside +// the appropriate half of the matrix. +func (t *Symmetric) Set(r, c int, v float64) { + t.set(r, c, v) +} +func (t *Symmetric) set(r, c int, v float64) { + if r >= t.mat.N || r < 0 { + panic(ErrRowAccess) + } + if c >= t.mat.N || c < 0 { + panic(ErrColAccess) + } + if t.mat.Uplo == blas.Upper && r > c { + panic("mat64: symmetric set out of bounds") + } + if t.mat.Uplo == blas.Lower && r < c { + panic("mat64: symmetric set out of bounds") + } + t.mat.Data[r*t.mat.Stride+c] = v +} diff --git a/mat64/index_no_bound_checks.go b/mat64/index_no_bound_checks.go index 1a6e76e..1d4ca4d 100644 --- a/mat64/index_no_bound_checks.go +++ b/mat64/index_no_bound_checks.go @@ -8,6 +8,8 @@ package mat64 +import "github.com/gonum/blas" + func (m *Dense) At(r, c int) float64 { if r >= m.mat.Rows || r < 0 { panic("index error: row access out of bounds") @@ -35,3 +37,47 @@ func (m *Dense) Set(r, c int, v float64) { func (m *Dense) set(r, c int, v float64) { m.mat.Data[r*m.mat.Stride+c] = v } + +// At returns the element at row r and column c. +func (t *Symmetric) At(r, c int) float64 { + if r >= t.mat.N || r < 0 { + panic(ErrRowAccess) + } + if c >= t.mat.N || c < 0 { + panic(ErrColAccess) + } + return t.at(r, c) +} +func (t *Symmetric) at(r, c int) float64 { + if t.mat.Uplo == blas.Upper { + if r > c { + r, c = c, r + } + return t.mat.Data[r*t.mat.Stride+c] + } + if r < c { + r, c = c, r + } + return t.mat.Data[r*t.mat.Stride+c] +} + +// Set sets the element at row r and column c. Set panics if the location is outside +// the appropriate half of the matrix. +func (t *Symmetric) Set(r, c int, v float64) { + if r >= t.mat.N || r < 0 { + panic(ErrRowAccess) + } + if c >= t.mat.N || c < 0 { + panic(ErrColAccess) + } + if t.mat.Uplo == blas.Upper && r > c { + panic("mat64: symmetric set out of bounds") + } + if t.mat.Uplo == blas.Lower && r < c { + panic("mat64: symmetric set out of bounds") + } + t.set(r, c, v) +} +func (t *Symmetric) set(r, c int, v float64) { + t.mat.Data[r*t.mat.Stride+c] = v +} diff --git a/mat64/matrix.go b/mat64/matrix.go index 178bf19..12086f2 100644 --- a/mat64/matrix.go +++ b/mat64/matrix.go @@ -385,6 +385,8 @@ func (err Error) Error() string { return string(err) } const ( ErrIndexOutOfRange = Error("mat64: index out of range") + ErrRowAccess = Error("mat64: row index out of range") + ErrColAccess = Error("mat64: column index out of range") ErrZeroLength = Error("mat64: zero length in matrix definition") ErrRowLength = Error("mat64: row length mismatch") ErrColLength = Error("mat64: col length mismatch") diff --git a/mat64/symmetric.go b/mat64/symmetric.go index d2e5455..36293f2 100644 --- a/mat64/symmetric.go +++ b/mat64/symmetric.go @@ -15,7 +15,7 @@ type Symmetric struct { // underlying data, or if mat == nil, new data will be allocated. // The underlying data representation is the same as a Dense matrix, except // the values of the entries in the opposite half are completely ignored. -func NewSymmetric(n int, t triType, mat []float64) *Symmetric { +func NewSymmetric(n int, upper bool, mat []float64) *Symmetric { if n < 0 { panic("mat64: negative dimension") } @@ -25,15 +25,15 @@ func NewSymmetric(n int, t triType, mat []float64) *Symmetric { if mat == nil { mat = make([]float64, n*n) } - if t != Upper && t != Lower { - panic("mat64: bad TriSide") + uplo := blas.Lower + if upper { + uplo = blas.Upper } return &Symmetric{blas64.Symmetric{ N: n, Stride: n, Data: mat, - Uplo: blas.Uplo(t), - Diag: blas.NonUnit, + Uplo: uplo, }} } diff --git a/mat64/symmetric_test.go b/mat64/symmetric_test.go new file mode 100644 index 0000000..e5cd782 --- /dev/null +++ b/mat64/symmetric_test.go @@ -0,0 +1,74 @@ +package mat64 + +import ( + "github.com/gonum/blas" + "github.com/gonum/blas/blas64" + "gopkg.in/check.v1" +) + +func (s *S) TestNewSymmetric(c *check.C) { + for i, test := range []struct { + data []float64 + N int + upper bool + mat *Symmetric + }{ + { + data: []float64{ + 1, 2, 3, + 4, 5, 6, + 7, 8, 9, + }, + N: 3, + upper: true, + mat: &Symmetric{blas64.Symmetric{ + N: 3, + Stride: 3, + Uplo: blas.Upper, + Data: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9}, + }}, + }, + } { + t := NewSymmetric(test.N, test.upper, test.data) + rows, cols := t.Dims() + c.Check(rows, check.Equals, test.N, check.Commentf("Test %d", i)) + c.Check(cols, check.Equals, test.N, check.Commentf("Test %d", i)) + c.Check(t, check.DeepEquals, test.mat, check.Commentf("Test %d", i)) + } +} + +func (s *S) TestTriAtSet(c *check.C) { + t := &Symmetric{blas64.Symmetric{ + N: 3, + Stride: 3, + Uplo: blas.Upper, + Data: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9}, + }} + rows, cols := t.Dims() + // Check At out of bounds + c.Check(func() { t.At(rows, 0) }, check.PanicMatches, ErrRowAccess.Error(), check.Commentf("Test row out of bounds")) + c.Check(func() { t.At(0, cols) }, check.PanicMatches, ErrColAccess.Error(), check.Commentf("Test col out of bounds")) + c.Check(func() { t.At(rows+1, 0) }, check.PanicMatches, ErrRowAccess.Error(), check.Commentf("Test row out of bounds")) + c.Check(func() { t.At(0, cols+1) }, check.PanicMatches, ErrColAccess.Error(), check.Commentf("Test col out of bounds")) + c.Check(func() { t.At(-1, 0) }, check.PanicMatches, ErrRowAccess.Error(), check.Commentf("Test row out of bounds")) + c.Check(func() { t.At(0, -1) }, check.PanicMatches, ErrColAccess.Error(), check.Commentf("Test col out of bounds")) + + // Check Set out of bounds + c.Check(func() { t.Set(rows, 0, 1.2) }, check.PanicMatches, ErrRowAccess.Error(), check.Commentf("Test row out of bounds")) + c.Check(func() { t.Set(0, cols, 1.2) }, check.PanicMatches, ErrColAccess.Error(), check.Commentf("Test col out of bounds")) + c.Check(func() { t.Set(rows+1, 0, 1.2) }, check.PanicMatches, ErrRowAccess.Error(), check.Commentf("Test row out of bounds")) + c.Check(func() { t.Set(0, cols+1, 1.2) }, check.PanicMatches, ErrColAccess.Error(), check.Commentf("Test col out of bounds")) + c.Check(func() { t.Set(-1, 0, 1.2) }, check.PanicMatches, ErrRowAccess.Error(), check.Commentf("Test row out of bounds")) + c.Check(func() { t.Set(0, -1, 1.2) }, check.PanicMatches, ErrColAccess.Error(), check.Commentf("Test col out of bounds")) + c.Check(func() { t.Set(2, 1, 1.2) }, check.PanicMatches, "mat64: symmetric set out of bounds", check.Commentf("Test lower access")) + t.mat.Uplo = blas.Lower + c.Check(func() { t.Set(1, 2, 1.2) }, check.PanicMatches, "mat64: symmetric set out of bounds", check.Commentf("Test upper access")) + + c.Check(t.At(2, 1), check.Equals, 8.0) + t.Set(2, 1, 15) + c.Check(t.At(2, 1), check.Equals, 15.0) + t.mat.Uplo = blas.Upper + c.Check(t.At(1, 2), check.Equals, 6.0) + t.Set(1, 2, 15) + c.Check(t.At(1, 2), check.Equals, 15.0) +} From 3be6cd06917eee3ba8f951981f9c93a3f38b486e Mon Sep 17 00:00:00 2001 From: btracey Date: Thu, 15 Jan 2015 17:03:16 -0800 Subject: [PATCH 03/11] Added At test on opposite side --- mat64/symmetric_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mat64/symmetric_test.go b/mat64/symmetric_test.go index e5cd782..3f5fdc9 100644 --- a/mat64/symmetric_test.go +++ b/mat64/symmetric_test.go @@ -65,10 +65,14 @@ func (s *S) TestTriAtSet(c *check.C) { c.Check(func() { t.Set(1, 2, 1.2) }, check.PanicMatches, "mat64: symmetric set out of bounds", check.Commentf("Test upper access")) c.Check(t.At(2, 1), check.Equals, 8.0) + c.Check(t.At(1, 2), check.Equals, 8.0) t.Set(2, 1, 15) c.Check(t.At(2, 1), check.Equals, 15.0) + c.Check(t.At(1, 2), check.Equals, 15.0) t.mat.Uplo = blas.Upper c.Check(t.At(1, 2), check.Equals, 6.0) + c.Check(t.At(2, 1), check.Equals, 6.0) t.Set(1, 2, 15) c.Check(t.At(1, 2), check.Equals, 15.0) + c.Check(t.At(2, 1), check.Equals, 15.0) } From ca98a6fbb4a8dbd76075f1f01f223c2fb999dcad Mon Sep 17 00:00:00 2001 From: btracey Date: Thu, 15 Jan 2015 17:11:46 -0800 Subject: [PATCH 04/11] Fixed incorrect comment --- mat64/symmetric.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mat64/symmetric.go b/mat64/symmetric.go index 36293f2..8513317 100644 --- a/mat64/symmetric.go +++ b/mat64/symmetric.go @@ -10,7 +10,7 @@ type Symmetric struct { mat blas64.Symmetric } -// NewTriangular constructs an n x n triangular matrix where the data is stored +// NewSymmetric constructs an n x n symmetric matrix where the data is stored // in the given orientation. If len(mat) == n * n, mat will be used to hold the // underlying data, or if mat == nil, new data will be allocated. // The underlying data representation is the same as a Dense matrix, except From e2bf67523651c3267af5991ffa1632c4d1d2b49b Mon Sep 17 00:00:00 2001 From: btracey Date: Thu, 15 Jan 2015 17:31:20 -0800 Subject: [PATCH 05/11] Added panic test for symmetric when input data is too small --- mat64/symmetric_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mat64/symmetric_test.go b/mat64/symmetric_test.go index 3f5fdc9..4286937 100644 --- a/mat64/symmetric_test.go +++ b/mat64/symmetric_test.go @@ -34,6 +34,8 @@ func (s *S) TestNewSymmetric(c *check.C) { c.Check(rows, check.Equals, test.N, check.Commentf("Test %d", i)) c.Check(cols, check.Equals, test.N, check.Commentf("Test %d", i)) c.Check(t, check.DeepEquals, test.mat, check.Commentf("Test %d", i)) + + c.Check(func() { NewSymmetric(3, test.upper, []float64{1, 2}) }, check.PanicMatches, ErrShape.Error()) } } From 77f844aa9ee4cc797f24be16e6ba04ad998c1004 Mon Sep 17 00:00:00 2001 From: btracey Date: Fri, 16 Jan 2015 12:19:47 -0800 Subject: [PATCH 06/11] Changed Set to SetSym and made all Symmetric matrices Upper. Updated tests --- mat64/index_bound_checks.go | 22 +++++----------------- mat64/index_no_bound_checks.go | 10 +++------- mat64/symmetric.go | 10 +++------- mat64/symmetric_test.go | 33 ++++++++++++++------------------- 4 files changed, 25 insertions(+), 50 deletions(-) diff --git a/mat64/index_bound_checks.go b/mat64/index_bound_checks.go index 3a480cd..4365aa0 100644 --- a/mat64/index_bound_checks.go +++ b/mat64/index_bound_checks.go @@ -8,8 +8,6 @@ package mat64 -import "github.com/gonum/blas" - func (m *Dense) At(r, c int) float64 { return m.at(r, c) } @@ -77,21 +75,14 @@ func (t *Symmetric) at(r, c int) float64 { if c >= t.mat.N || c < 0 { panic(ErrColAccess) } - if t.mat.Uplo == blas.Upper { - if r > c { - r, c = c, r - } - return t.mat.Data[r*t.mat.Stride+c] - } - if r < c { + if r > c { r, c = c, r } return t.mat.Data[r*t.mat.Stride+c] } -// Set sets the element at row r and column c. Set panics if the location is outside -// the appropriate half of the matrix. -func (t *Symmetric) Set(r, c int, v float64) { +// SetSym sets the elements at (r,c) and (c,r) to the value v. +func (t *Symmetric) SetSym(r, c int, v float64) { t.set(r, c, v) } func (t *Symmetric) set(r, c int, v float64) { @@ -101,11 +92,8 @@ func (t *Symmetric) set(r, c int, v float64) { if c >= t.mat.N || c < 0 { panic(ErrColAccess) } - if t.mat.Uplo == blas.Upper && r > c { - panic("mat64: symmetric set out of bounds") - } - if t.mat.Uplo == blas.Lower && r < c { - panic("mat64: symmetric set out of bounds") + if r > c { + r, c = c, r } t.mat.Data[r*t.mat.Stride+c] = v } diff --git a/mat64/index_no_bound_checks.go b/mat64/index_no_bound_checks.go index f66e42b..529e1ac 100644 --- a/mat64/index_no_bound_checks.go +++ b/mat64/index_no_bound_checks.go @@ -89,8 +89,7 @@ func (t *Symmetric) at(r, c int) float64 { return t.mat.Data[r*t.mat.Stride+c] } -// Set sets the element at row r and column c. Set panics if the location is outside -// the appropriate half of the matrix. +// SetSym sets the elements at (r,c) and (c,r) to the value v. func (t *Symmetric) Set(r, c int, v float64) { if r >= t.mat.N || r < 0 { panic(ErrRowAccess) @@ -98,11 +97,8 @@ func (t *Symmetric) Set(r, c int, v float64) { if c >= t.mat.N || c < 0 { panic(ErrColAccess) } - if t.mat.Uplo == blas.Upper && r > c { - panic("mat64: symmetric set out of bounds") - } - if t.mat.Uplo == blas.Lower && r < c { - panic("mat64: symmetric set out of bounds") + if r > c { + r, c = c, r } t.set(r, c, v) } diff --git a/mat64/symmetric.go b/mat64/symmetric.go index 8513317..14af810 100644 --- a/mat64/symmetric.go +++ b/mat64/symmetric.go @@ -14,8 +14,8 @@ type Symmetric struct { // in the given orientation. If len(mat) == n * n, mat will be used to hold the // underlying data, or if mat == nil, new data will be allocated. // The underlying data representation is the same as a Dense matrix, except -// the values of the entries in the opposite half are completely ignored. -func NewSymmetric(n int, upper bool, mat []float64) *Symmetric { +// the values of the entries in the lower triangular portion are completely ignored. +func NewSymmetric(n int, mat []float64) *Symmetric { if n < 0 { panic("mat64: negative dimension") } @@ -25,15 +25,11 @@ func NewSymmetric(n int, upper bool, mat []float64) *Symmetric { if mat == nil { mat = make([]float64, n*n) } - uplo := blas.Lower - if upper { - uplo = blas.Upper - } return &Symmetric{blas64.Symmetric{ N: n, Stride: n, Data: mat, - Uplo: uplo, + Uplo: blas.Upper, }} } diff --git a/mat64/symmetric_test.go b/mat64/symmetric_test.go index 4286937..46c83b2 100644 --- a/mat64/symmetric_test.go +++ b/mat64/symmetric_test.go @@ -8,10 +8,9 @@ import ( func (s *S) TestNewSymmetric(c *check.C) { for i, test := range []struct { - data []float64 - N int - upper bool - mat *Symmetric + data []float64 + N int + mat *Symmetric }{ { data: []float64{ @@ -19,8 +18,7 @@ func (s *S) TestNewSymmetric(c *check.C) { 4, 5, 6, 7, 8, 9, }, - N: 3, - upper: true, + N: 3, mat: &Symmetric{blas64.Symmetric{ N: 3, Stride: 3, @@ -29,13 +27,16 @@ func (s *S) TestNewSymmetric(c *check.C) { }}, }, } { - t := NewSymmetric(test.N, test.upper, test.data) + t := NewSymmetric(test.N, test.data) rows, cols := t.Dims() c.Check(rows, check.Equals, test.N, check.Commentf("Test %d", i)) c.Check(cols, check.Equals, test.N, check.Commentf("Test %d", i)) c.Check(t, check.DeepEquals, test.mat, check.Commentf("Test %d", i)) - c.Check(func() { NewSymmetric(3, test.upper, []float64{1, 2}) }, check.PanicMatches, ErrShape.Error()) + m := NewDense(test.N, test.N, test.data) + c.Check(t.mat.Data, check.DeepEquals, m.mat.Data, check.Commentf("Test %d", i)) + + c.Check(func() { NewSymmetric(3, []float64{1, 2}) }, check.PanicMatches, ErrShape.Error()) } } @@ -62,19 +63,13 @@ func (s *S) TestTriAtSet(c *check.C) { c.Check(func() { t.Set(0, cols+1, 1.2) }, check.PanicMatches, ErrColAccess.Error(), check.Commentf("Test col out of bounds")) c.Check(func() { t.Set(-1, 0, 1.2) }, check.PanicMatches, ErrRowAccess.Error(), check.Commentf("Test row out of bounds")) c.Check(func() { t.Set(0, -1, 1.2) }, check.PanicMatches, ErrColAccess.Error(), check.Commentf("Test col out of bounds")) - c.Check(func() { t.Set(2, 1, 1.2) }, check.PanicMatches, "mat64: symmetric set out of bounds", check.Commentf("Test lower access")) - t.mat.Uplo = blas.Lower - c.Check(func() { t.Set(1, 2, 1.2) }, check.PanicMatches, "mat64: symmetric set out of bounds", check.Commentf("Test upper access")) - c.Check(t.At(2, 1), check.Equals, 8.0) - c.Check(t.At(1, 2), check.Equals, 8.0) - t.Set(2, 1, 15) - c.Check(t.At(2, 1), check.Equals, 15.0) - c.Check(t.At(1, 2), check.Equals, 15.0) - t.mat.Uplo = blas.Upper - c.Check(t.At(1, 2), check.Equals, 6.0) c.Check(t.At(2, 1), check.Equals, 6.0) + c.Check(t.At(1, 2), check.Equals, 6.0) t.Set(1, 2, 15) - c.Check(t.At(1, 2), check.Equals, 15.0) c.Check(t.At(2, 1), check.Equals, 15.0) + c.Check(t.At(1, 2), check.Equals, 15.0) + t.Set(2, 1, 12) + c.Check(t.At(2, 1), check.Equals, 12.0) + c.Check(t.At(1, 2), check.Equals, 12.0) } From 625430ccf55fae36a6c40e9fa847ed43890278fa Mon Sep 17 00:00:00 2001 From: btracey Date: Fri, 16 Jan 2015 12:27:00 -0800 Subject: [PATCH 07/11] Changed to SetSym in both bounds files and updated tests --- mat64/index_no_bound_checks.go | 4 +++- mat64/symmetric_test.go | 16 ++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/mat64/index_no_bound_checks.go b/mat64/index_no_bound_checks.go index 529e1ac..a2f57a6 100644 --- a/mat64/index_no_bound_checks.go +++ b/mat64/index_no_bound_checks.go @@ -76,6 +76,7 @@ func (t *Symmetric) At(r, c int) float64 { } return t.at(r, c) } + func (t *Symmetric) at(r, c int) float64 { if t.mat.Uplo == blas.Upper { if r > c { @@ -90,7 +91,7 @@ func (t *Symmetric) at(r, c int) float64 { } // SetSym sets the elements at (r,c) and (c,r) to the value v. -func (t *Symmetric) Set(r, c int, v float64) { +func (t *Symmetric) SetSym(r, c int, v float64) { if r >= t.mat.N || r < 0 { panic(ErrRowAccess) } @@ -102,6 +103,7 @@ func (t *Symmetric) Set(r, c int, v float64) { } t.set(r, c, v) } + func (t *Symmetric) set(r, c int, v float64) { t.mat.Data[r*t.mat.Stride+c] = v } diff --git a/mat64/symmetric_test.go b/mat64/symmetric_test.go index 46c83b2..fec1a00 100644 --- a/mat64/symmetric_test.go +++ b/mat64/symmetric_test.go @@ -57,19 +57,19 @@ func (s *S) TestTriAtSet(c *check.C) { c.Check(func() { t.At(0, -1) }, check.PanicMatches, ErrColAccess.Error(), check.Commentf("Test col out of bounds")) // Check Set out of bounds - c.Check(func() { t.Set(rows, 0, 1.2) }, check.PanicMatches, ErrRowAccess.Error(), check.Commentf("Test row out of bounds")) - c.Check(func() { t.Set(0, cols, 1.2) }, check.PanicMatches, ErrColAccess.Error(), check.Commentf("Test col out of bounds")) - c.Check(func() { t.Set(rows+1, 0, 1.2) }, check.PanicMatches, ErrRowAccess.Error(), check.Commentf("Test row out of bounds")) - c.Check(func() { t.Set(0, cols+1, 1.2) }, check.PanicMatches, ErrColAccess.Error(), check.Commentf("Test col out of bounds")) - c.Check(func() { t.Set(-1, 0, 1.2) }, check.PanicMatches, ErrRowAccess.Error(), check.Commentf("Test row out of bounds")) - c.Check(func() { t.Set(0, -1, 1.2) }, check.PanicMatches, ErrColAccess.Error(), check.Commentf("Test col out of bounds")) + c.Check(func() { t.SetSym(rows, 0, 1.2) }, check.PanicMatches, ErrRowAccess.Error(), check.Commentf("Test row out of bounds")) + c.Check(func() { t.SetSym(0, cols, 1.2) }, check.PanicMatches, ErrColAccess.Error(), check.Commentf("Test col out of bounds")) + c.Check(func() { t.SetSym(rows+1, 0, 1.2) }, check.PanicMatches, ErrRowAccess.Error(), check.Commentf("Test row out of bounds")) + c.Check(func() { t.SetSym(0, cols+1, 1.2) }, check.PanicMatches, ErrColAccess.Error(), check.Commentf("Test col out of bounds")) + c.Check(func() { t.SetSym(-1, 0, 1.2) }, check.PanicMatches, ErrRowAccess.Error(), check.Commentf("Test row out of bounds")) + c.Check(func() { t.SetSym(0, -1, 1.2) }, check.PanicMatches, ErrColAccess.Error(), check.Commentf("Test col out of bounds")) c.Check(t.At(2, 1), check.Equals, 6.0) c.Check(t.At(1, 2), check.Equals, 6.0) - t.Set(1, 2, 15) + t.SetSym(1, 2, 15) c.Check(t.At(2, 1), check.Equals, 15.0) c.Check(t.At(1, 2), check.Equals, 15.0) - t.Set(2, 1, 12) + t.SetSym(2, 1, 12) c.Check(t.At(2, 1), check.Equals, 12.0) c.Check(t.At(1, 2), check.Equals, 12.0) } From 8332e7a2fd60e0e93f8c31cad418bd0e765168a6 Mon Sep 17 00:00:00 2001 From: btracey Date: Mon, 19 Jan 2015 21:45:14 -0800 Subject: [PATCH 08/11] Removed Trans check in Symmetric At --- mat64/index_no_bound_checks.go | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/mat64/index_no_bound_checks.go b/mat64/index_no_bound_checks.go index a2f57a6..77013d8 100644 --- a/mat64/index_no_bound_checks.go +++ b/mat64/index_no_bound_checks.go @@ -8,8 +8,6 @@ package mat64 -import "github.com/gonum/blas" - func (m *Dense) At(r, c int) float64 { if r >= m.mat.Rows || r < 0 { panic(ErrRowAccess) @@ -78,13 +76,7 @@ func (t *Symmetric) At(r, c int) float64 { } func (t *Symmetric) at(r, c int) float64 { - if t.mat.Uplo == blas.Upper { - if r > c { - r, c = c, r - } - return t.mat.Data[r*t.mat.Stride+c] - } - if r < c { + if r > c { r, c = c, r } return t.mat.Data[r*t.mat.Stride+c] From 2c5ab0babb8be55788e862ff139ebe75c1f79299 Mon Sep 17 00:00:00 2001 From: btracey Date: Thu, 22 Jan 2015 20:11:15 -0800 Subject: [PATCH 09/11] Fixed outdated comment --- mat64/symmetric.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mat64/symmetric.go b/mat64/symmetric.go index 14af810..4ac2d22 100644 --- a/mat64/symmetric.go +++ b/mat64/symmetric.go @@ -10,9 +10,8 @@ type Symmetric struct { mat blas64.Symmetric } -// NewSymmetric constructs an n x n symmetric matrix where the data is stored -// in the given orientation. If len(mat) == n * n, mat will be used to hold the -// underlying data, or if mat == nil, new data will be allocated. +// NewSymmetric constructs an n x n symmetric matrix. If len(mat) == n * n, +// mat will be used to hold the underlying data, or if mat == nil, new data will be allocated. // The underlying data representation is the same as a Dense matrix, except // the values of the entries in the lower triangular portion are completely ignored. func NewSymmetric(n int, mat []float64) *Symmetric { From e1e53ad3feba2be14b4f386fcd1b9935af63b8b9 Mon Sep 17 00:00:00 2001 From: btracey Date: Thu, 22 Jan 2015 20:14:33 -0800 Subject: [PATCH 10/11] Fixed SetSym to allow internal set on either side of the matrix --- mat64/index_no_bound_checks.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mat64/index_no_bound_checks.go b/mat64/index_no_bound_checks.go index 77013d8..e1bf12a 100644 --- a/mat64/index_no_bound_checks.go +++ b/mat64/index_no_bound_checks.go @@ -90,12 +90,12 @@ func (t *Symmetric) SetSym(r, c int, v float64) { if c >= t.mat.N || c < 0 { panic(ErrColAccess) } - if r > c { - r, c = c, r - } t.set(r, c, v) } func (t *Symmetric) set(r, c int, v float64) { + if r > c { + r, c = c, r + } t.mat.Data[r*t.mat.Stride+c] = v } From 7e92d51e1bea97213eb2ca6e6a21905ac3724895 Mon Sep 17 00:00:00 2001 From: btracey Date: Thu, 22 Jan 2015 21:23:03 -0800 Subject: [PATCH 11/11] Added newlines between function lines --- mat64/index_bound_checks.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mat64/index_bound_checks.go b/mat64/index_bound_checks.go index 4365aa0..077517c 100644 --- a/mat64/index_bound_checks.go +++ b/mat64/index_bound_checks.go @@ -68,6 +68,7 @@ func (m *Vector) set(r int, v float64) { func (t *Symmetric) At(r, c int) float64 { return t.at(r, c) } + func (t *Symmetric) at(r, c int) float64 { if r >= t.mat.N || r < 0 { panic(ErrRowAccess) @@ -85,6 +86,7 @@ func (t *Symmetric) at(r, c int) float64 { func (t *Symmetric) SetSym(r, c int, v float64) { t.set(r, c, v) } + func (t *Symmetric) set(r, c int, v float64) { if r >= t.mat.N || r < 0 { panic(ErrRowAccess)