This repository was archived by the owner on Dec 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Added Triangular type, At and Set methods, and tests #77
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c97817f
Added Triangular type, At and Set methods, and tests
btracey bcf721a
Minor fixes from comments
btracey 92c22c9
Added documentation to NewTriangular
btracey f3c4b08
Removed TriType and made it a boolean
btracey 189e177
Merged from master
btracey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package mat64 | ||
|
|
||
| import ( | ||
| "github.com/gonum/blas" | ||
| "github.com/gonum/blas/blas64" | ||
| ) | ||
|
|
||
| var ( | ||
| triangular *Triangular | ||
|
|
||
| _ Matrix = triangular | ||
| _ Mutable = triangular | ||
| ) | ||
|
|
||
| // Triangular represents an upper or lower triangular matrix. | ||
| type Triangular struct { | ||
| mat blas64.Triangular | ||
| } | ||
|
|
||
| // NewTriangular constructs an n x n triangular matrix. The constructed matrix | ||
| // is upper triangular if upper == true, and lower triangular otherwise. | ||
| // 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 that of a Dense matrix, | ||
| // except the values of the entries in the opposite half are completely ignored. | ||
| func NewTriangular(n int, upper bool, mat []float64) *Triangular { | ||
| if n < 0 { | ||
| panic("mat64: negative dimension") | ||
| } | ||
| if mat != nil && n*n != len(mat) { | ||
| panic(ErrShape) | ||
| } | ||
| if mat == nil { | ||
| mat = make([]float64, n*n) | ||
| } | ||
| uplo := blas.Lower | ||
| if upper { | ||
| uplo = blas.Upper | ||
| } | ||
| return &Triangular{blas64.Triangular{ | ||
| N: n, | ||
| Stride: n, | ||
| Data: mat, | ||
| Uplo: uplo, | ||
| Diag: blas.NonUnit, | ||
| }} | ||
| } | ||
|
|
||
| func (t *Triangular) Dims() (r, c int) { | ||
| return t.mat.N, t.mat.N | ||
| } | ||
|
|
||
| func (t *Triangular) RawTriangular() blas64.Triangular { | ||
| return t.mat | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package mat64 | ||
|
|
||
| import ( | ||
| "github.com/gonum/blas" | ||
| "github.com/gonum/blas/blas64" | ||
| "gopkg.in/check.v1" | ||
| ) | ||
|
|
||
| func (s *S) TestNewTriangular(c *check.C) { | ||
| for i, test := range []struct { | ||
| data []float64 | ||
| N int | ||
| upper bool | ||
| mat *Triangular | ||
| }{ | ||
| { | ||
| data: []float64{ | ||
| 1, 2, 3, | ||
| 4, 5, 6, | ||
| 7, 8, 9, | ||
| }, | ||
| N: 3, | ||
| upper: true, | ||
| mat: &Triangular{blas64.Triangular{ | ||
| N: 3, | ||
| Stride: 3, | ||
| Uplo: blas.Upper, | ||
| Data: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9}, | ||
| Diag: blas.NonUnit, | ||
| }}, | ||
| }, | ||
| } { | ||
| t := NewTriangular(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 := &Triangular{blas64.Triangular{ | ||
| N: 3, | ||
| Stride: 3, | ||
| Uplo: blas.Upper, | ||
| Data: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9}, | ||
| Diag: blas.NonUnit, | ||
| }} | ||
| 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: triangular 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: triangular 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) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
naive comment: isn't "cleaner" to just spell it out
_ Matrix = (*Triangular)(nil)admittedly more verbose, but no "pollution" of the global scope.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm following the pattern that exists there now. We can change it if there's a better way, but consistency is better.