github.com/gopherd/gonum@v0.0.4/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 func (t *TriBandDense) setTriBand(i, j int, v float64) { 323 var kl int 324 if !t.isUpper() { 325 kl = t.mat.K 326 } 327 pj := j + kl - i 328 t.mat.Data[i*t.mat.Stride+pj] = v 329 } 330 331 // At returns the element at row i, column j. 332 func (d *DiagDense) At(i, j int) float64 { 333 if uint(i) >= uint(d.mat.N) { 334 panic(ErrRowAccess) 335 } 336 if uint(j) >= uint(d.mat.N) { 337 panic(ErrColAccess) 338 } 339 return d.at(i, j) 340 } 341 342 func (d *DiagDense) at(i, j int) float64 { 343 if i != j { 344 return 0 345 } 346 return d.mat.Data[i*d.mat.Inc] 347 } 348 349 // SetDiag sets the element at row i, column i to the value v. 350 // It panics if the location is outside the appropriate region of the matrix. 351 func (d *DiagDense) SetDiag(i int, v float64) { 352 if uint(i) >= uint(d.mat.N) { 353 panic(ErrRowAccess) 354 } 355 d.setDiag(i, v) 356 } 357 358 func (d *DiagDense) setDiag(i int, v float64) { 359 d.mat.Data[i*d.mat.Inc] = v 360 } 361 362 // At returns the element at row i, column j. 363 func (a *Tridiag) At(i, j int) float64 { 364 if uint(i) >= uint(a.mat.N) { 365 panic(ErrRowAccess) 366 } 367 if uint(j) >= uint(a.mat.N) { 368 panic(ErrColAccess) 369 } 370 return a.at(i, j) 371 } 372 373 func (a *Tridiag) at(i, j int) float64 { 374 switch i - j { 375 case -1: 376 return a.mat.DU[i] 377 case 0: 378 return a.mat.D[i] 379 case 1: 380 return a.mat.DL[j] 381 default: 382 return 0 383 } 384 } 385 386 // SetBand sets the element at row i, column j to the value v. 387 // It panics if the location is outside the appropriate region of the matrix. 388 func (a *Tridiag) SetBand(i, j int, v float64) { 389 if uint(i) >= uint(a.mat.N) { 390 panic(ErrRowAccess) 391 } 392 if uint(j) >= uint(a.mat.N) { 393 panic(ErrColAccess) 394 } 395 a.set(i, j, v) 396 } 397 398 func (a *Tridiag) set(i, j int, v float64) { 399 switch i - j { 400 case -1: 401 a.mat.DU[i] = v 402 case 0: 403 a.mat.D[i] = v 404 case 1: 405 a.mat.DL[j] = v 406 default: 407 panic(ErrBandSet) 408 } 409 }