aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/gonum.org/v1/gonum/mat/shadow.go
blob: 4fc24c3466a6c73d15d251b09a647cec023098b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright ©2015 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package mat

import "gonum.org/v1/gonum/blas/blas64"

// checkOverlap returns false if the receiver does not overlap data elements
// referenced by the parameter and panics otherwise.
//
// checkOverlap methods return a boolean to allow the check call to be added to a
// boolean expression, making use of short-circuit operators.
func checkOverlap(a, b blas64.General) bool {
	if cap(a.Data) == 0 || cap(b.Data) == 0 {
		return false
	}

	off := offset(a.Data[:1], b.Data[:1])

	if off == 0 {
		// At least one element overlaps.
		if a.Cols == b.Cols && a.Rows == b.Rows && a.Stride == b.Stride {
			panic(regionIdentity)
		}
		panic(regionOverlap)
	}

	if off > 0 && len(a.Data) <= off {
		// We know a is completely before b.
		return false
	}
	if off < 0 && len(b.Data) <= -off {
		// We know a is completely after b.
		return false
	}

	if a.Stride != b.Stride && a.Stride != 1 && b.Stride != 1 {
		// Too hard, so assume the worst; if either stride
		// is one it will be caught in rectanglesOverlap.
		panic(mismatchedStrides)
	}

	if off < 0 {
		off = -off
		a.Cols, b.Cols = b.Cols, a.Cols
	}
	if rectanglesOverlap(off, a.Cols, b.Cols, min(a.Stride, b.Stride)) {
		panic(regionOverlap)
	}
	return false
}

func (m *Dense) checkOverlap(a blas64.General) bool {
	return checkOverlap(m.RawMatrix(), a)
}

func (m *Dense) checkOverlapMatrix(a Matrix) bool {
	if m == a {
		return false
	}
	var amat blas64.General
	switch ar := a.(type) {
	default:
		return false
	case RawMatrixer:
		amat = ar.RawMatrix()
	case RawSymmetricer:
		amat = generalFromSymmetric(ar.RawSymmetric())
	case RawSymBander:
		amat = generalFromSymmetricBand(ar.RawSymBand())
	case RawTriangular:
		amat = generalFromTriangular(ar.RawTriangular())
	case RawVectorer:
		r, c := a.Dims()
		amat = generalFromVector(ar.RawVector(), r, c)
	}
	return m.checkOverlap(amat)
}

func (s *SymDense) checkOverlap(a blas64.General) bool {
	return checkOverlap(generalFromSymmetric(s.RawSymmetric()), a)
}

func (s *SymDense) checkOverlapMatrix(a Matrix) bool {
	if s == a {
		return false
	}
	var amat blas64.General
	switch ar := a.(type) {
	default:
		return false
	case RawMatrixer:
		amat = ar.RawMatrix()
	case RawSymmetricer:
		amat = generalFromSymmetric(ar.RawSymmetric())
	case RawSymBander:
		amat = generalFromSymmetricBand(ar.RawSymBand())
	case RawTriangular:
		amat = generalFromTriangular(ar.RawTriangular())
	case RawVectorer:
		r, c := a.Dims()
		amat = generalFromVector(ar.RawVector(), r, c)
	}
	return s.checkOverlap(amat)
}

// generalFromSymmetric returns a blas64.General with the backing
// data and dimensions of a.
func generalFromSymmetric(a blas64.Symmetric) blas64.General {
	return blas64.General{
		Rows:   a.N,
		Cols:   a.N,
		Stride: a.Stride,
		Data:   a.Data,
	}
}

func (t *TriDense) checkOverlap(a blas64.General) bool {
	return checkOverlap(generalFromTriangular(t.RawTriangular()), a)
}

func (t *TriDense) checkOverlapMatrix(a Matrix) bool {
	if t == a {
		return false
	}
	var amat blas64.General
	switch ar := a.(type) {
	default:
		return false
	case RawMatrixer:
		amat = ar.RawMatrix()
	case RawSymmetricer:
		amat = generalFromSymmetric(ar.RawSymmetric())
	case RawSymBander:
		amat = generalFromSymmetricBand(ar.RawSymBand())
	case RawTriangular:
		amat = generalFromTriangular(ar.RawTriangular())
	case RawVectorer:
		r, c := a.Dims()
		amat = generalFromVector(ar.RawVector(), r, c)
	}
	return t.checkOverlap(amat)
}

// generalFromTriangular returns a blas64.General with the backing
// data and dimensions of a.
func generalFromTriangular(a blas64.Triangular) blas64.General {
	return blas64.General{
		Rows:   a.N,
		Cols:   a.N,
		Stride: a.Stride,
		Data:   a.Data,
	}
}

func (v *VecDense) checkOverlap(a blas64.Vector) bool {
	mat := v.mat
	if cap(mat.Data) == 0 || cap(a.Data) == 0 {
		return false
	}

	off := offset(mat.Data[:1], a.Data[:1])

	if off == 0 {
		// At least one element overlaps.
		if mat.Inc == a.Inc && len(mat.Data) == len(a.Data) {
			panic(regionIdentity)
		}
		panic(regionOverlap)
	}

	if off > 0 && len(mat.Data) <= off {
		// We know v is completely before a.
		return false
	}
	if off < 0 && len(a.Data) <= -off {
		// We know v is completely after a.
		return false
	}

	if mat.Inc != a.Inc && mat.Inc != 1 && a.Inc != 1 {
		// Too hard, so assume the worst; if either
		// increment is one it will be caught below.
		panic(mismatchedStrides)
	}
	inc := min(mat.Inc, a.Inc)

	if inc == 1 || off&inc == 0 {
		panic(regionOverlap)
	}
	return false
}

// generalFromVector returns a blas64.General with the backing
// data and dimensions of a.
func generalFromVector(a blas64.Vector, r, c int) blas64.General {
	return blas64.General{
		Rows:   r,
		Cols:   c,
		Stride: a.Inc,
		Data:   a.Data,
	}
}

func (s *SymBandDense) checkOverlap(a blas64.General) bool {
	return checkOverlap(generalFromSymmetricBand(s.RawSymBand()), a)
}

//lint:ignore U1000 This will be used when we do shadow checks for banded matrices.
func (s *SymBandDense) checkOverlapMatrix(a Matrix) bool {
	if s == a {
		return false
	}
	var amat blas64.General
	switch ar := a.(type) {
	default:
		return false
	case RawMatrixer:
		amat = ar.RawMatrix()
	case RawSymmetricer:
		amat = generalFromSymmetric(ar.RawSymmetric())
	case RawSymBander:
		amat = generalFromSymmetricBand(ar.RawSymBand())
	case RawTriangular:
		amat = generalFromTriangular(ar.RawTriangular())
	case RawVectorer:
		r, c := a.Dims()
		amat = generalFromVector(ar.RawVector(), r, c)
	}
	return s.checkOverlap(amat)
}

// generalFromSymmetricBand returns a blas64.General with the backing
// data and dimensions of a.
func generalFromSymmetricBand(a blas64.SymmetricBand) blas64.General {
	return blas64.General{
		Rows:   a.N,
		Cols:   a.K + 1,
		Data:   a.Data,
		Stride: a.Stride,
	}
}