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/blas64"
14 func TestNewBand(t *testing.T) {
15 for i, test := range []struct {
50 dense: NewDense(6, 6, []float64{
89 dense: NewDense(10, 6, []float64{
130 dense: NewDense(6, 10, []float64{
131 1, 2, 3, 0, 0, 0, 0, 0, 0, 0,
132 4, 5, 6, 7, 0, 0, 0, 0, 0, 0,
133 0, 8, 9, 10, 11, 0, 0, 0, 0, 0,
134 0, 0, 12, 13, 14, 15, 0, 0, 0, 0,
135 0, 0, 0, 16, 17, 18, 19, 0, 0, 0,
136 0, 0, 0, 0, 20, 21, 22, 23, 0, 0,
140 band := NewBandDense(test.r, test.c, test.kl, test.ku, test.data)
141 rows, cols := band.Dims()
144 t.Errorf("unexpected number of rows for test %d: got: %d want: %d", i, rows, test.r)
147 t.Errorf("unexpected number of cols for test %d: got: %d want: %d", i, cols, test.c)
149 if !reflect.DeepEqual(band, test.mat) {
150 t.Errorf("unexpected value via reflect for test %d: got: %v want: %v", i, band, test.mat)
152 if !Equal(band, test.mat) {
153 t.Errorf("unexpected value via mat.Equal for test %d: got: %v want: %v", i, band, test.mat)
155 if !Equal(band, test.dense) {
156 t.Errorf("unexpected value via mat.Equal(band, dense) for test %d:\ngot:\n% v\nwant:\n% v", i, Formatted(band), Formatted(test.dense))
161 func TestNewDiagonalRect(t *testing.T) {
162 for i, test := range []struct {
169 data: []float64{1, 2, 3, 4, 5, 6},
176 Data: []float64{1, 2, 3, 4, 5, 6},
179 dense: NewDense(6, 6, []float64{
189 data: []float64{1, 2, 3, 4, 5, 6},
196 Data: []float64{1, 2, 3, 4, 5, 6},
199 dense: NewDense(7, 6, []float64{
210 data: []float64{1, 2, 3, 4, 5, 6},
217 Data: []float64{1, 2, 3, 4, 5, 6},
220 dense: NewDense(6, 7, []float64{
230 band := NewDiagonalRect(test.r, test.c, test.data)
231 rows, cols := band.Dims()
234 t.Errorf("unexpected number of rows for test %d: got: %d want: %d", i, rows, test.r)
237 t.Errorf("unexpected number of cols for test %d: got: %d want: %d", i, cols, test.c)
239 if !reflect.DeepEqual(band, test.mat) {
240 t.Errorf("unexpected value via reflect for test %d: got: %v want: %v", i, band, test.mat)
242 if !Equal(band, test.mat) {
243 t.Errorf("unexpected value via mat.Equal for test %d: got: %v want: %v", i, band, test.mat)
245 if !Equal(band, test.dense) {
246 t.Errorf("unexpected value via mat.Equal(band, dense) for test %d:\ngot:\n% v\nwant:\n% v", i, Formatted(band), Formatted(test.dense))
251 func TestBandAtSet(t *testing.T) {
258 band := NewBandDense(6, 6, 1, 2, []float64{
267 rows, cols := band.Dims()
268 kl, ku := band.Bandwidth()
270 // Explicitly test all indexes.
271 want := bandImplicit{rows, cols, kl, ku, func(i, j int) float64 {
272 return float64(i*(kl+ku) + j + kl + 1)
274 for i := 0; i < 6; i++ {
275 for j := 0; j < 6; j++ {
276 if band.At(i, j) != want.At(i, j) {
277 t.Errorf("unexpected value for band.At(%d, %d): got:%v want:%v", i, j, band.At(i, j), want.At(i, j))
281 // Do that same thing via a call to Equal.
282 if !Equal(band, want) {
283 t.Errorf("unexpected value via mat.Equal:\ngot:\n% v\nwant:\n% v", Formatted(band), Formatted(want))
286 // Check At out of bounds
287 for _, row := range []int{-1, rows, rows + 1} {
288 panicked, message := panics(func() { band.At(row, 0) })
289 if !panicked || message != ErrRowAccess.Error() {
290 t.Errorf("expected panic for invalid row access N=%d r=%d", rows, row)
293 for _, col := range []int{-1, cols, cols + 1} {
294 panicked, message := panics(func() { band.At(0, col) })
295 if !panicked || message != ErrColAccess.Error() {
296 t.Errorf("expected panic for invalid column access N=%d c=%d", cols, col)
300 // Check Set out of bounds
301 for _, row := range []int{-1, rows, rows + 1} {
302 panicked, message := panics(func() { band.SetBand(row, 0, 1.2) })
303 if !panicked || message != ErrRowAccess.Error() {
304 t.Errorf("expected panic for invalid row access N=%d r=%d", rows, row)
307 for _, col := range []int{-1, cols, cols + 1} {
308 panicked, message := panics(func() { band.SetBand(0, col, 1.2) })
309 if !panicked || message != ErrColAccess.Error() {
310 t.Errorf("expected panic for invalid column access N=%d c=%d", cols, col)
314 for _, st := range []struct {
328 panicked, message := panics(func() { band.SetBand(st.row, st.col, 1.2) })
329 if !panicked || message != ErrBandSet.Error() {
330 t.Errorf("expected panic for %+v %s", st, message)
334 for _, st := range []struct {
338 {row: 1, col: 2, orig: 7, new: 15},
339 {row: 2, col: 3, orig: 11, new: 15},
341 if e := band.At(st.row, st.col); e != st.orig {
342 t.Errorf("unexpected value for At(%d, %d): got: %v want: %v", st.row, st.col, e, st.orig)
344 band.SetBand(st.row, st.col, st.new)
345 if e := band.At(st.row, st.col); e != st.new {
346 t.Errorf("unexpected value for At(%d, %d) after SetBand(%[1]d, %d, %v): got: %v want: %[3]v", st.row, st.col, st.new, e)
351 // bandImplicit is an implicit band matrix returning val(i, j)
352 // for the value at (i, j).
353 type bandImplicit struct {
355 val func(i, j int) float64
358 func (b bandImplicit) Dims() (r, c int) {
362 func (b bandImplicit) T() Matrix {
366 func (b bandImplicit) At(i, j int) float64 {
367 if i < 0 || b.r <= i {
370 if j < 0 || b.c <= j {
373 if j < i-b.kl || i+b.ku < j {