github.com/gonum/matrix@v0.0.0-20181209220409-c518dec07be9/mat64/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  //+build !bounds
     8  
     9  package mat64
    10  
    11  import "github.com/gonum/matrix"
    12  
    13  // At returns the element at row i, column j.
    14  func (m *Dense) At(i, j int) float64 {
    15  	if uint(i) >= uint(m.mat.Rows) {
    16  		panic(matrix.ErrRowAccess)
    17  	}
    18  	if uint(j) >= uint(m.mat.Cols) {
    19  		panic(matrix.ErrColAccess)
    20  	}
    21  	return m.at(i, j)
    22  }
    23  
    24  func (m *Dense) at(i, j int) float64 {
    25  	return m.mat.Data[i*m.mat.Stride+j]
    26  }
    27  
    28  // Set sets the element at row i, column j to the value v.
    29  func (m *Dense) Set(i, j int, v float64) {
    30  	if uint(i) >= uint(m.mat.Rows) {
    31  		panic(matrix.ErrRowAccess)
    32  	}
    33  	if uint(j) >= uint(m.mat.Cols) {
    34  		panic(matrix.ErrColAccess)
    35  	}
    36  	m.set(i, j, v)
    37  }
    38  
    39  func (m *Dense) set(i, j int, v float64) {
    40  	m.mat.Data[i*m.mat.Stride+j] = v
    41  }
    42  
    43  // At returns the element at row i.
    44  // It panics if i is out of bounds or if j is not zero.
    45  func (v *Vector) At(i, j int) float64 {
    46  	if uint(i) >= uint(v.n) {
    47  		panic(matrix.ErrRowAccess)
    48  	}
    49  	if j != 0 {
    50  		panic(matrix.ErrColAccess)
    51  	}
    52  	return v.at(i)
    53  }
    54  
    55  func (v *Vector) at(i int) float64 {
    56  	return v.mat.Data[i*v.mat.Inc]
    57  }
    58  
    59  // SetVec sets the element at row i to the value val.
    60  // It panics if i is out of bounds.
    61  func (v *Vector) SetVec(i int, val float64) {
    62  	if uint(i) >= uint(v.n) {
    63  		panic(matrix.ErrVectorAccess)
    64  	}
    65  	v.setVec(i, val)
    66  }
    67  
    68  func (v *Vector) setVec(i int, val float64) {
    69  	v.mat.Data[i*v.mat.Inc] = val
    70  }
    71  
    72  // At returns the element at row i and column j.
    73  func (s *SymDense) At(i, j int) float64 {
    74  	if uint(i) >= uint(s.mat.N) {
    75  		panic(matrix.ErrRowAccess)
    76  	}
    77  	if uint(j) >= uint(s.mat.N) {
    78  		panic(matrix.ErrColAccess)
    79  	}
    80  	return s.at(i, j)
    81  }
    82  
    83  func (s *SymDense) at(i, j int) float64 {
    84  	if i > j {
    85  		i, j = j, i
    86  	}
    87  	return s.mat.Data[i*s.mat.Stride+j]
    88  }
    89  
    90  // SetSym sets the elements at (i,j) and (j,i) to the value v.
    91  func (s *SymDense) SetSym(i, j int, v float64) {
    92  	if uint(i) >= uint(s.mat.N) {
    93  		panic(matrix.ErrRowAccess)
    94  	}
    95  	if uint(j) >= uint(s.mat.N) {
    96  		panic(matrix.ErrColAccess)
    97  	}
    98  	s.set(i, j, v)
    99  }
   100  
   101  func (s *SymDense) set(i, j int, v float64) {
   102  	if i > j {
   103  		i, j = j, i
   104  	}
   105  	s.mat.Data[i*s.mat.Stride+j] = v
   106  }
   107  
   108  // At returns the element at row i, column j.
   109  func (t *TriDense) At(i, j int) float64 {
   110  	if uint(i) >= uint(t.mat.N) {
   111  		panic(matrix.ErrRowAccess)
   112  	}
   113  	if uint(j) >= uint(t.mat.N) {
   114  		panic(matrix.ErrColAccess)
   115  	}
   116  	return t.at(i, j)
   117  }
   118  
   119  func (t *TriDense) at(i, j int) float64 {
   120  	isUpper := t.triKind()
   121  	if (isUpper && i > j) || (!isUpper && i < j) {
   122  		return 0
   123  	}
   124  	return t.mat.Data[i*t.mat.Stride+j]
   125  }
   126  
   127  // SetTri sets the element at row i, column j to the value v.
   128  // It panics if the location is outside the appropriate half of the matrix.
   129  func (t *TriDense) SetTri(i, j int, v float64) {
   130  	if uint(i) >= uint(t.mat.N) {
   131  		panic(matrix.ErrRowAccess)
   132  	}
   133  	if uint(j) >= uint(t.mat.N) {
   134  		panic(matrix.ErrColAccess)
   135  	}
   136  	isUpper := t.isUpper()
   137  	if (isUpper && i > j) || (!isUpper && i < j) {
   138  		panic(matrix.ErrTriangleSet)
   139  	}
   140  	t.set(i, j, v)
   141  }
   142  
   143  func (t *TriDense) set(i, j int, v float64) {
   144  	t.mat.Data[i*t.mat.Stride+j] = v
   145  }