gonum.org/v1/gonum@v0.14.0/mat/index_no_bound_checks.go (about)

     1  // Copyright ©2014 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.
     4  
     5  // This file must be kept in sync with index_bound_checks.go.
     6  
     7  //go:build !bounds
     8  // +build !bounds
     9  
    10  package mat
    11  
    12  // At returns the element at row i, column j.
    13  func (m *Dense) At(i, j int) float64 {
    14  	if uint(i) >= uint(m.mat.Rows) {
    15  		panic(ErrRowAccess)
    16  	}
    17  	if uint(j) >= uint(m.mat.Cols) {
    18  		panic(ErrColAccess)
    19  	}
    20  	return m.at(i, j)
    21  }
    22  
    23  func (m *Dense) at(i, j int) float64 {
    24  	return m.mat.Data[i*m.mat.Stride+j]
    25  }
    26  
    27  // Set sets the element at row i, column j to the value v.
    28  func (m *Dense) Set(i, j int, v float64) {
    29  	if uint(i) >= uint(m.mat.Rows) {
    30  		panic(ErrRowAccess)
    31  	}
    32  	if uint(j) >= uint(m.mat.Cols) {
    33  		panic(ErrColAccess)
    34  	}
    35  	m.set(i, j, v)
    36  }
    37  
    38  func (m *Dense) set(i, j int, v float64) {
    39  	m.mat.Data[i*m.mat.Stride+j] = v
    40  }
    41  
    42  // At returns the element at row i, column j.
    43  func (m *CDense) At(i, j int) complex128 {
    44  	if uint(i) >= uint(m.mat.Rows) {
    45  		panic(ErrRowAccess)
    46  	}
    47  	if uint(j) >= uint(m.mat.Cols) {
    48  		panic(ErrColAccess)
    49  	}
    50  	return m.at(i, j)
    51  }
    52  
    53  func (m *CDense) at(i, j int) complex128 {
    54  	return m.mat.Data[i*m.mat.Stride+j]
    55  }
    56  
    57  // Set sets the element at row i, column j to the value v.
    58  func (m *CDense) Set(i, j int, v complex128) {
    59  	if uint(i) >= uint(m.mat.Rows) {
    60  		panic(ErrRowAccess)
    61  	}
    62  	if uint(j) >= uint(m.mat.Cols) {
    63  		panic(ErrColAccess)
    64  	}
    65  	m.set(i, j, v)
    66  }
    67  
    68  func (m *CDense) set(i, j int, v complex128) {
    69  	m.mat.Data[i*m.mat.Stride+j] = v
    70  }
    71  
    72  // At returns the element at row i.
    73  // It panics if i is out of bounds or if j is not zero.
    74  func (v *VecDense) At(i, j int) float64 {
    75  	if uint(i) >= uint(v.mat.N) {
    76  		panic(ErrRowAccess)
    77  	}
    78  	if j != 0 {
    79  		panic(ErrColAccess)
    80  	}
    81  	return v.at(i)
    82  }
    83  
    84  // AtVec returns the element at row i.
    85  // It panics if i is out of bounds.
    86  func (v *VecDense) AtVec(i int) float64 {
    87  	if uint(i) >= uint(v.mat.N) {
    88  		panic(ErrRowAccess)
    89  	}
    90  	return v.at(i)
    91  }
    92  
    93  func (v *VecDense) at(i int) float64 {
    94  	return v.mat.Data[i*v.mat.Inc]
    95  }
    96  
    97  // SetVec sets the element at row i to the value val.
    98  // It panics if i is out of bounds.
    99  func (v *VecDense) SetVec(i int, val float64) {
   100  	if uint(i) >= uint(v.mat.N) {
   101  		panic(ErrVectorAccess)
   102  	}
   103  	v.setVec(i, val)
   104  }
   105  
   106  func (v *VecDense) setVec(i int, val float64) {
   107  	v.mat.Data[i*v.mat.Inc] = val
   108  }
   109  
   110  // At returns the element at row i and column j.
   111  func (s *SymDense) At(i, j int) float64 {
   112  	if uint(i) >= uint(s.mat.N) {
   113  		panic(ErrRowAccess)
   114  	}
   115  	if uint(j) >= uint(s.mat.N) {
   116  		panic(ErrColAccess)
   117  	}
   118  	return s.at(i, j)
   119  }
   120  
   121  func (s *SymDense) at(i, j int) float64 {
   122  	if i > j {
   123  		i, j = j, i
   124  	}
   125  	return s.mat.Data[i*s.mat.Stride+j]
   126  }
   127  
   128  // SetSym sets the elements at (i,j) and (j,i) to the value v.
   129  func (s *SymDense) SetSym(i, j int, v float64) {
   130  	if uint(i) >= uint(s.mat.N) {
   131  		panic(ErrRowAccess)
   132  	}
   133  	if uint(j) >= uint(s.mat.N) {
   134  		panic(ErrColAccess)
   135  	}
   136  	s.set(i, j, v)
   137  }
   138  
   139  func (s *SymDense) set(i, j int, v float64) {
   140  	if i > j {
   141  		i, j = j, i
   142  	}
   143  	s.mat.Data[i*s.mat.Stride+j] = v
   144  }
   145  
   146  // At returns the element at row i, column j.
   147  func (t *TriDense) At(i, j int) float64 {
   148  	if uint(i) >= uint(t.mat.N) {
   149  		panic(ErrRowAccess)
   150  	}
   151  	if uint(j) >= uint(t.mat.N) {
   152  		panic(ErrColAccess)
   153  	}
   154  	return t.at(i, j)
   155  }
   156  
   157  func (t *TriDense) at(i, j int) float64 {
   158  	isUpper := t.triKind()
   159  	if (isUpper && i > j) || (!isUpper && i < j) {
   160  		return 0
   161  	}
   162  	return t.mat.Data[i*t.mat.Stride+j]
   163  }
   164  
   165  // SetTri sets the element at row i, column j to the value v.
   166  // It panics if the location is outside the appropriate half of the matrix.
   167  func (t *TriDense) SetTri(i, j int, v float64) {
   168  	if uint(i) >= uint(t.mat.N) {
   169  		panic(ErrRowAccess)
   170  	}
   171  	if uint(j) >= uint(t.mat.N) {
   172  		panic(ErrColAccess)
   173  	}
   174  	isUpper := t.isUpper()
   175  	if (isUpper && i > j) || (!isUpper && i < j) {
   176  		panic(ErrTriangleSet)
   177  	}
   178  	t.set(i, j, v)
   179  }
   180  
   181  func (t *TriDense) set(i, j int, v float64) {
   182  	t.mat.Data[i*t.mat.Stride+j] = v
   183  }
   184  
   185  // At returns the element at row i, column j.
   186  func (b *BandDense) At(i, j int) float64 {
   187  	if uint(i) >= uint(b.mat.Rows) {
   188  		panic(ErrRowAccess)
   189  	}
   190  	if uint(j) >= uint(b.mat.Cols) {
   191  		panic(ErrColAccess)
   192  	}
   193  	return b.at(i, j)
   194  }
   195  
   196  func (b *BandDense) at(i, j int) float64 {
   197  	pj := j + b.mat.KL - i
   198  	if pj < 0 || b.mat.KL+b.mat.KU+1 <= pj {
   199  		return 0
   200  	}
   201  	return b.mat.Data[i*b.mat.Stride+pj]
   202  }
   203  
   204  // SetBand sets the element at row i, column j to the value v.
   205  // It panics if the location is outside the appropriate region of the matrix.
   206  func (b *BandDense) SetBand(i, j int, v float64) {
   207  	if uint(i) >= uint(b.mat.Rows) {
   208  		panic(ErrRowAccess)
   209  	}
   210  	if uint(j) >= uint(b.mat.Cols) {
   211  		panic(ErrColAccess)
   212  	}
   213  	pj := j + b.mat.KL - i
   214  	if pj < 0 || b.mat.KL+b.mat.KU+1 <= pj {
   215  		panic(ErrBandSet)
   216  	}
   217  	b.set(i, j, v)
   218  }
   219  
   220  func (b *BandDense) set(i, j int, v float64) {
   221  	pj := j + b.mat.KL - i
   222  	b.mat.Data[i*b.mat.Stride+pj] = v
   223  }
   224  
   225  // At returns the element at row i, column j.
   226  func (s *SymBandDense) At(i, j int) float64 {
   227  	if uint(i) >= uint(s.mat.N) {
   228  		panic(ErrRowAccess)
   229  	}
   230  	if uint(j) >= uint(s.mat.N) {
   231  		panic(ErrColAccess)
   232  	}
   233  	return s.at(i, j)
   234  }
   235  
   236  func (s *SymBandDense) at(i, j int) float64 {
   237  	if i > j {
   238  		i, j = j, i
   239  	}
   240  	pj := j - i
   241  	if s.mat.K+1 <= pj {
   242  		return 0
   243  	}
   244  	return s.mat.Data[i*s.mat.Stride+pj]
   245  }
   246  
   247  // SetSymBand sets the element at row i, column j to the value v.
   248  // It panics if the location is outside the appropriate region of the matrix.
   249  func (s *SymBandDense) SetSymBand(i, j int, v float64) {
   250  	if uint(i) >= uint(s.mat.N) {
   251  		panic(ErrRowAccess)
   252  	}
   253  	if uint(j) >= uint(s.mat.N) {
   254  		panic(ErrColAccess)
   255  	}
   256  	s.set(i, j, v)
   257  }
   258  
   259  func (s *SymBandDense) set(i, j int, v float64) {
   260  	if i > j {
   261  		i, j = j, i
   262  	}
   263  	pj := j - i
   264  	if s.mat.K+1 <= pj {
   265  		panic(ErrBandSet)
   266  	}
   267  	s.mat.Data[i*s.mat.Stride+pj] = v
   268  }
   269  
   270  func (t *TriBandDense) At(i, j int) float64 {
   271  	if uint(i) >= uint(t.mat.N) {
   272  		panic(ErrRowAccess)
   273  	}
   274  	if uint(j) >= uint(t.mat.N) {
   275  		panic(ErrColAccess)
   276  	}
   277  	return t.at(i, j)
   278  }
   279  
   280  func (t *TriBandDense) at(i, j int) float64 {
   281  	// TODO(btracey): Support Diag field, see #692.
   282  	isUpper := t.isUpper()
   283  	if (isUpper && i > j) || (!isUpper && i < j) {
   284  		return 0
   285  	}
   286  	kl := t.mat.K
   287  	ku := 0
   288  	if isUpper {
   289  		ku = t.mat.K
   290  		kl = 0
   291  	}
   292  	pj := j + kl - i
   293  	if pj < 0 || kl+ku+1 <= pj {
   294  		return 0
   295  	}
   296  	return t.mat.Data[i*t.mat.Stride+pj]
   297  }
   298  
   299  func (t *TriBandDense) SetTriBand(i, j int, v float64) {
   300  	if uint(i) >= uint(t.mat.N) {
   301  		panic(ErrRowAccess)
   302  	}
   303  	if uint(j) >= uint(t.mat.N) {
   304  		panic(ErrColAccess)
   305  	}
   306  	isUpper := t.isUpper()
   307  	if (isUpper && i > j) || (!isUpper && i < j) {
   308  		panic(ErrTriangleSet)
   309  	}
   310  	kl, ku := t.mat.K, 0
   311  	if isUpper {
   312  		kl, ku = 0, t.mat.K
   313  	}
   314  	pj := j + kl - i
   315  	if pj < 0 || kl+ku+1 <= pj {
   316  		panic(ErrBandSet)
   317  	}
   318  	// TODO(btracey): Support Diag field, see #692.
   319  	t.mat.Data[i*t.mat.Stride+pj] = v
   320  }
   321  
   322  // At returns the element at row i, column j.
   323  func (d *DiagDense) At(i, j int) float64 {
   324  	if uint(i) >= uint(d.mat.N) {
   325  		panic(ErrRowAccess)
   326  	}
   327  	if uint(j) >= uint(d.mat.N) {
   328  		panic(ErrColAccess)
   329  	}
   330  	return d.at(i, j)
   331  }
   332  
   333  func (d *DiagDense) at(i, j int) float64 {
   334  	if i != j {
   335  		return 0
   336  	}
   337  	return d.mat.Data[i*d.mat.Inc]
   338  }
   339  
   340  // SetDiag sets the element at row i, column i to the value v.
   341  // It panics if the location is outside the appropriate region of the matrix.
   342  func (d *DiagDense) SetDiag(i int, v float64) {
   343  	if uint(i) >= uint(d.mat.N) {
   344  		panic(ErrRowAccess)
   345  	}
   346  	d.setDiag(i, v)
   347  }
   348  
   349  func (d *DiagDense) setDiag(i int, v float64) {
   350  	d.mat.Data[i*d.mat.Inc] = v
   351  }
   352  
   353  // At returns the element at row i, column j.
   354  func (a *Tridiag) At(i, j int) float64 {
   355  	if uint(i) >= uint(a.mat.N) {
   356  		panic(ErrRowAccess)
   357  	}
   358  	if uint(j) >= uint(a.mat.N) {
   359  		panic(ErrColAccess)
   360  	}
   361  	return a.at(i, j)
   362  }
   363  
   364  func (a *Tridiag) at(i, j int) float64 {
   365  	switch i - j {
   366  	case -1:
   367  		return a.mat.DU[i]
   368  	case 0:
   369  		return a.mat.D[i]
   370  	case 1:
   371  		return a.mat.DL[j]
   372  	default:
   373  		return 0
   374  	}
   375  }
   376  
   377  // SetBand sets the element at row i, column j to the value v.
   378  // It panics if the location is outside the appropriate region of the matrix.
   379  func (a *Tridiag) SetBand(i, j int, v float64) {
   380  	if uint(i) >= uint(a.mat.N) {
   381  		panic(ErrRowAccess)
   382  	}
   383  	if uint(j) >= uint(a.mat.N) {
   384  		panic(ErrColAccess)
   385  	}
   386  	a.set(i, j, v)
   387  }
   388  
   389  func (a *Tridiag) set(i, j int, v float64) {
   390  	switch i - j {
   391  	case -1:
   392  		a.mat.DU[i] = v
   393  	case 0:
   394  		a.mat.D[i] = v
   395  	case 1:
   396  		a.mat.DL[j] = v
   397  	default:
   398  		panic(ErrBandSet)
   399  	}
   400  }