aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/gonum.org/v1/gonum/mat/shadow_complex.go
blob: 1a3f3fc2313fb4234e1b46e83ff39449d9d268a1 (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
// 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.

// TODO(kortschak): Generate this file from shadow.go when all complex type are available.

package mat

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

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

	off := offsetComplex(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 *CDense) checkOverlap(a cblas128.General) bool {
	return checkOverlapComplex(m.RawCMatrix(), a)
}

func (m *CDense) checkOverlapMatrix(a CMatrix) bool {
	if m == a {
		return false
	}
	var amat cblas128.General
	switch ar := a.(type) {
	default:
		return false
	case RawCMatrixer:
		amat = ar.RawCMatrix()
	}
	return m.checkOverlap(amat)
}