1 // Copyright ©2017 The Gonum Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 "gonum.org/v1/gonum/blas"
12 "gonum.org/v1/gonum/blas/blas64"
15 func TestNewSymBand(t *testing.T) {
16 for i, test := range []struct {
35 mat: blas64.SymmetricBand{
50 dense: NewDense(6, 6, []float64{
60 band := NewSymBandDense(test.n, test.k, test.data)
61 rows, cols := band.Dims()
64 t.Errorf("unexpected number of rows for test %d: got: %d want: %d", i, rows, test.n)
67 t.Errorf("unexpected number of cols for test %d: got: %d want: %d", i, cols, test.n)
69 if !reflect.DeepEqual(band, test.mat) {
70 t.Errorf("unexpected value via reflect for test %d: got: %v want: %v", i, band, test.mat)
72 if !Equal(band, test.mat) {
73 t.Errorf("unexpected value via mat.Equal for test %d: got: %v want: %v", i, band, test.mat)
75 if !Equal(band, test.dense) {
76 t.Errorf("unexpected value via mat.Equal(band, dense) for test %d:\ngot:\n% v\nwant:\n% v", i, Formatted(band), Formatted(test.dense))
81 func TestNewDiagonal(t *testing.T) {
82 for i, test := range []struct {
89 data: []float64{1, 2, 3, 4, 5, 6},
92 mat: blas64.SymmetricBand{
96 Data: []float64{1, 2, 3, 4, 5, 6},
99 dense: NewDense(6, 6, []float64{
109 band := NewDiagonal(test.n, test.data)
110 rows, cols := band.Dims()
113 t.Errorf("unexpected number of rows for test %d: got: %d want: %d", i, rows, test.n)
116 t.Errorf("unexpected number of cols for test %d: got: %d want: %d", i, cols, test.n)
118 if !reflect.DeepEqual(band, test.mat) {
119 t.Errorf("unexpected value via reflect for test %d: got: %v want: %v", i, band, test.mat)
121 if !Equal(band, test.mat) {
122 t.Errorf("unexpected value via mat.Equal for test %d: got: %v want: %v", i, band, test.mat)
124 if !Equal(band, test.dense) {
125 t.Errorf("unexpected value via mat.Equal(band, dense) for test %d:\ngot:\n% v\nwant:\n% v", i, Formatted(band), Formatted(test.dense))
130 func TestSymBandAtSet(t *testing.T) {
137 band := NewSymBandDense(6, 2, []float64{
146 rows, cols := band.Dims()
147 kl, ku := band.Bandwidth()
149 // Explicitly test all indexes.
150 want := bandImplicit{rows, cols, kl, ku, func(i, j int) float64 {
154 return float64(i*ku + j + 1)
156 for i := 0; i < 6; i++ {
157 for j := 0; j < 6; j++ {
158 if band.At(i, j) != want.At(i, j) {
159 t.Errorf("unexpected value for band.At(%d, %d): got:%v want:%v", i, j, band.At(i, j), want.At(i, j))
163 // Do that same thing via a call to Equal.
164 if !Equal(band, want) {
165 t.Errorf("unexpected value via mat.Equal:\ngot:\n% v\nwant:\n% v", Formatted(band), Formatted(want))
168 // Check At out of bounds
169 for _, row := range []int{-1, rows, rows + 1} {
170 panicked, message := panics(func() { band.At(row, 0) })
171 if !panicked || message != ErrRowAccess.Error() {
172 t.Errorf("expected panic for invalid row access N=%d r=%d", rows, row)
175 for _, col := range []int{-1, cols, cols + 1} {
176 panicked, message := panics(func() { band.At(0, col) })
177 if !panicked || message != ErrColAccess.Error() {
178 t.Errorf("expected panic for invalid column access N=%d c=%d", cols, col)
182 // Check Set out of bounds
183 for _, row := range []int{-1, rows, rows + 1} {
184 panicked, message := panics(func() { band.SetSymBand(row, 0, 1.2) })
185 if !panicked || message != ErrRowAccess.Error() {
186 t.Errorf("expected panic for invalid row access N=%d r=%d", rows, row)
189 for _, col := range []int{-1, cols, cols + 1} {
190 panicked, message := panics(func() { band.SetSymBand(0, col, 1.2) })
191 if !panicked || message != ErrColAccess.Error() {
192 t.Errorf("expected panic for invalid column access N=%d c=%d", cols, col)
196 for _, st := range []struct {
209 panicked, message := panics(func() { band.SetSymBand(st.row, st.col, 1.2) })
210 if !panicked || message != ErrBandSet.Error() {
211 t.Errorf("expected panic for %+v %s", st, message)
215 for _, st := range []struct {
219 {row: 1, col: 2, orig: 5, new: 15},
220 {row: 2, col: 3, orig: 8, new: 15},
222 if e := band.At(st.row, st.col); e != st.orig {
223 t.Errorf("unexpected value for At(%d, %d): got: %v want: %v", st.row, st.col, e, st.orig)
225 band.SetSymBand(st.row, st.col, st.new)
226 if e := band.At(st.row, st.col); e != st.new {
227 t.Errorf("unexpected value for At(%d, %d) after SetSymBand(%[1]d, %d, %v): got: %v want: %[3]v", st.row, st.col, st.new, e)