OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / mat / index_bound_checks.go
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_no_bound_checks.go.
6
7 //+build bounds
8
9 package mat
10
11 // At returns the element at row i, column j.
12 func (m *Dense) At(i, j int) float64 {
13         return m.at(i, j)
14 }
15
16 func (m *Dense) at(i, j int) float64 {
17         if uint(i) >= uint(m.mat.Rows) {
18                 panic(ErrRowAccess)
19         }
20         if uint(j) >= uint(m.mat.Cols) {
21                 panic(ErrColAccess)
22         }
23         return m.mat.Data[i*m.mat.Stride+j]
24 }
25
26 // Set sets the element at row i, column j to the value v.
27 func (m *Dense) Set(i, j int, v float64) {
28         m.set(i, j, v)
29 }
30
31 func (m *Dense) set(i, j int, v float64) {
32         if uint(i) >= uint(m.mat.Rows) {
33                 panic(ErrRowAccess)
34         }
35         if uint(j) >= uint(m.mat.Cols) {
36                 panic(ErrColAccess)
37         }
38         m.mat.Data[i*m.mat.Stride+j] = v
39 }
40
41 // At returns the element at row i.
42 // It panics if i is out of bounds or if j is not zero.
43 func (v *VecDense) At(i, j int) float64 {
44         if j != 0 {
45                 panic(ErrColAccess)
46         }
47         return v.at(i)
48 }
49
50 // AtVec returns the element at row i.
51 // It panics if i is out of bounds.
52 func (v *VecDense) AtVec(i int) float64 {
53         return v.at(i)
54 }
55
56 func (v *VecDense) at(i int) float64 {
57         if uint(i) >= uint(v.n) {
58                 panic(ErrRowAccess)
59         }
60         return v.mat.Data[i*v.mat.Inc]
61 }
62
63 // SetVec sets the element at row i to the value val.
64 // It panics if i is out of bounds.
65 func (v *VecDense) SetVec(i int, val float64) {
66         v.setVec(i, val)
67 }
68
69 func (v *VecDense) setVec(i int, val float64) {
70         if uint(i) >= uint(v.n) {
71                 panic(ErrVectorAccess)
72         }
73         v.mat.Data[i*v.mat.Inc] = val
74 }
75
76 // At returns the element at row i and column j.
77 func (t *SymDense) At(i, j int) float64 {
78         return t.at(i, j)
79 }
80
81 func (t *SymDense) at(i, j int) float64 {
82         if uint(i) >= uint(t.mat.N) {
83                 panic(ErrRowAccess)
84         }
85         if uint(j) >= uint(t.mat.N) {
86                 panic(ErrColAccess)
87         }
88         if i > j {
89                 i, j = j, i
90         }
91         return t.mat.Data[i*t.mat.Stride+j]
92 }
93
94 // SetSym sets the elements at (i,j) and (j,i) to the value v.
95 func (t *SymDense) SetSym(i, j int, v float64) {
96         t.set(i, j, v)
97 }
98
99 func (t *SymDense) set(i, j int, v float64) {
100         if uint(i) >= uint(t.mat.N) {
101                 panic(ErrRowAccess)
102         }
103         if uint(j) >= uint(t.mat.N) {
104                 panic(ErrColAccess)
105         }
106         if i > j {
107                 i, j = j, i
108         }
109         t.mat.Data[i*t.mat.Stride+j] = v
110 }
111
112 // At returns the element at row i, column j.
113 func (t *TriDense) At(i, j int) float64 {
114         return t.at(i, j)
115 }
116
117 func (t *TriDense) at(i, j int) float64 {
118         if uint(i) >= uint(t.mat.N) {
119                 panic(ErrRowAccess)
120         }
121         if uint(j) >= uint(t.mat.N) {
122                 panic(ErrColAccess)
123         }
124         isUpper := t.isUpper()
125         if (isUpper && i > j) || (!isUpper && i < j) {
126                 return 0
127         }
128         return t.mat.Data[i*t.mat.Stride+j]
129 }
130
131 // SetTri sets the element of the triangular matrix at row i, column j to the value v.
132 // It panics if the location is outside the appropriate half of the matrix.
133 func (t *TriDense) SetTri(i, j int, v float64) {
134         t.set(i, j, v)
135 }
136
137 func (t *TriDense) set(i, j int, v float64) {
138         if uint(i) >= uint(t.mat.N) {
139                 panic(ErrRowAccess)
140         }
141         if uint(j) >= uint(t.mat.N) {
142                 panic(ErrColAccess)
143         }
144         isUpper := t.isUpper()
145         if (isUpper && i > j) || (!isUpper && i < j) {
146                 panic(ErrTriangleSet)
147         }
148         t.mat.Data[i*t.mat.Stride+j] = v
149 }
150
151 // At returns the element at row i, column j.
152 func (b *BandDense) At(i, j int) float64 {
153         return b.at(i, j)
154 }
155
156 func (b *BandDense) at(i, j int) float64 {
157         if uint(i) >= uint(b.mat.Rows) {
158                 panic(ErrRowAccess)
159         }
160         if uint(j) >= uint(b.mat.Cols) {
161                 panic(ErrColAccess)
162         }
163         pj := j + b.mat.KL - i
164         if pj < 0 || b.mat.KL+b.mat.KU+1 <= pj {
165                 return 0
166         }
167         return b.mat.Data[i*b.mat.Stride+pj]
168 }
169
170 // SetBand sets the element at row i, column j to the value v.
171 // It panics if the location is outside the appropriate region of the matrix.
172 func (b *BandDense) SetBand(i, j int, v float64) {
173         b.set(i, j, v)
174 }
175
176 func (b *BandDense) set(i, j int, v float64) {
177         if uint(i) >= uint(b.mat.Rows) {
178                 panic(ErrRowAccess)
179         }
180         if uint(j) >= uint(b.mat.Cols) {
181                 panic(ErrColAccess)
182         }
183         pj := j + b.mat.KL - i
184         if pj < 0 || b.mat.KL+b.mat.KU+1 <= pj {
185                 panic(ErrBandSet)
186         }
187         b.mat.Data[i*b.mat.Stride+pj] = v
188 }
189
190 // At returns the element at row i, column j.
191 func (s *SymBandDense) At(i, j int) float64 {
192         return s.at(i, j)
193 }
194
195 func (s *SymBandDense) at(i, j int) float64 {
196         if uint(i) >= uint(s.mat.N) {
197                 panic(ErrRowAccess)
198         }
199         if uint(j) >= uint(s.mat.N) {
200                 panic(ErrColAccess)
201         }
202         if i > j {
203                 i, j = j, i
204         }
205         pj := j - i
206         if s.mat.K+1 <= pj {
207                 return 0
208         }
209         return s.mat.Data[i*s.mat.Stride+pj]
210 }
211
212 // SetSymBand sets the element at row i, column j to the value v.
213 // It panics if the location is outside the appropriate region of the matrix.
214 func (s *SymBandDense) SetSymBand(i, j int, v float64) {
215         s.set(i, j, v)
216 }
217
218 func (s *SymBandDense) set(i, j int, v float64) {
219         if uint(i) >= uint(s.mat.N) {
220                 panic(ErrRowAccess)
221         }
222         if uint(j) >= uint(s.mat.N) {
223                 panic(ErrColAccess)
224         }
225         if i > j {
226                 i, j = j, i
227         }
228         pj := j - i
229         if s.mat.K+1 <= pj {
230                 panic(ErrBandSet)
231         }
232         s.mat.Data[i*s.mat.Stride+pj] = v
233 }