// See cairo_matrix_rotate().
//
// C API documentation: http://cairographics.org/manual/cairo-cairo-matrix-t.html#cairo-matrix-rotate
436605
func (matrix *Matrix) Rotate(radians float64) {
	C.cairo_matrix_rotate((*C.cairo_matrix_t)(unsafe.Pointer(matrix)), C.double(radians))
}
178051
func rotate(data Interface, a, m, b int) {
	i := m - a
	j := b - m

	for i != j {
		if i > j {
			swapRange(data, m-i, m, j)
			i -= j
		} else {
			swapRange(data, m-i, m+j-i, i)
			j -= i
		}
	}
	// i == j
	swapRange(data, m-i, m, i)
}
1258262
func Random(r, c uint) *Matrix {
	rand.Seed(time.Now().UnixNano())
	res := make([][]float32, r)
	for y := 0; y < int(r); y++ {
		res[y] = make([]float32, c)
		for x := 0; x < int(c); x++ {
			res[y][x] = rand.Float32()
		}
	}
	return NewMatrix(res)
}
972905
func rotate(points []float64, x, y float64, angle int) {
	if angle < 0 || angle > 3 {
		panic("rotate:参数angle必须0,1,2,3三值之一")
	}

	for i := 0; i < len(points); i += 2 {
		px := points[i] - x
		py := points[i+1] - y
		points[i] = px*cos[angle] - py*sin[angle] + x
		points[i+1] = px*sin[angle] + py*cos[angle] + y
	}
}
148978
func (m matrix) Multiply(right matrix) (matrix, error) {
	if len(m[0]) != len(right) {
		return nil, fmt.Errorf("columns on left (%d) is different than rows on right (%d)", len(m[0]), len(right))
	}
	result, _ := newMatrix(len(m), len(right[0]))
	for r, row := range result {
		for c := range row {
			var value byte
			for i := range m[0] {
				value ^= galMultiply(m[r][i], right[i][c])
			}
			result[r][c] = value
		}
	}
	return result, nil
}
1080769
func (mat *T) Invert() *T {
	initialDet := mat.Determinant()
	mat.Adjugate()
	mat.Mul(1 / initialDet)
	return mat
}
525824
func (u Vec) Rotated(angle float64) Vec {
	sin, cos := math.Sincos(angle)
	return Vec{
		u.X*cos - u.Y*sin,
		u.X*sin + u.Y*cos,
	}
}
1355021
func (gc *StackGraphicContext) Rotate(angle float64) {
	gc.current.Tr.Rotate(angle)
}
639232
func Rotate2D(angle float64) Mat2 {
	//angle = (angle * math.Pi) / 180.0
	sin, cos := float64(math.Sin(float64(angle))), float64(math.Cos(float64(angle)))
	return Mat2{cos, sin, -sin, cos}
}
69577
func (m *Matrix) Flatten() []float32 {
	res := make([]float32, m.Rows*m.Cols)
	var wg sync.WaitGroup
	for y := 0; y < int(m.Rows); y++ {
		wg.Add(1)
		go m.flatten(res, y, &wg)
	}
	wg.Wait()
	return res
}
1168202
func (result *Matrix) Multiply(a, b *Matrix) {
	C.cairo_matrix_multiply((*C.cairo_matrix_t)(unsafe.Pointer(result)), (*C.cairo_matrix_t)(unsafe.Pointer(a)), (*C.cairo_matrix_t)(unsafe.Pointer(b)))
}
1004827
func (f *Fpdf) TransformRotate(angle, x, y float64) {
	y = (f.h - y) * f.k
	x *= f.k
	angle = angle * math.Pi / 180
	var tm TransformMatrix
	tm.A = math.Cos(angle)
	tm.B = math.Sin(angle)
	tm.C = -tm.B
	tm.D = tm.A
	tm.E = x + tm.B*y - tm.A*x
	tm.F = y - tm.A*y - tm.B*x
	f.Transform(tm)
}
514702
func (a Affine) Rotate(angle float64) Affine {
	s, c := math.Sincos(angle)
	return a.Mul(Affine{
		+c, +s, +0,
		-s, +c, +0,
		+0, +0, +1,
	})
}
848781
func (dc *Context) Rotate(angle float64) {
	dc.matrix = dc.matrix.Rotate(angle)
}
928585
func MakeMatrix2(x [][]complex128) *Matrix {
	dims := []int{len(x), len(x[0])}
	r := make([]complex128, dims[0]*dims[1])
	for n, v := range x {
		if len(v) != dims[1] {
			panic("ragged array")
		}

		copy(r[n*dims[1]:(n+1)*dims[1]], v)
	}

	return MakeMatrix(r, dims)
}
740389
func (m *Matrix) QR() (q, r *Matrix) {
	defer func() {
		q = q.Round()
		r = r.Round()
	}()

	rows, cols := m.Size()
	qr := m.Copy()
	q = New(rows, cols)
	r = New(rows, cols)

	var i, j, k int
	var norm, s float64

	for k = 0; k < cols; k++ {
		norm = 0
		for i = k; i < rows; i++ {
			norm = math.Hypot(norm, qr.Get(i, k))
		}

		if norm != 0 {
			if qr.Get(k, k) < 0 {
				norm = -norm
			}

			for i = k; i < rows; i++ {
				qr.Set(i, k, qr.Get(i, k)/norm)
			}
			qr.Set(k, k, qr.Get(k, k)+1.0)

			for j = k + 1; j < cols; j++ {
				s = 0
				for i = k; i < rows; i++ {
					s += qr.Get(i, k) * qr.Get(i, j)
				}
				s = -s / qr.Get(k, k)
				for i = k; i < rows; i++ {
					qr.Set(i, j, qr.Get(i, j)+s*qr.Get(i, k))

					if i < j {
						r.Set(i, j, qr.Get(i, j))
					}
				}

			}
		}

		r.Set(k, k, -norm)

	}

	//Q Matrix:
	i, j, k = 0, 0, 0

	for k = cols - 1; k >= 0; k-- {
		q.Set(k, k, 1.0)
		for j = k; j < cols; j++ {
			if qr.Get(k, k) != 0 {
				s = 0
				for i = k; i < rows; i++ {
					s += qr.Get(i, k) * q.Get(i, j)
				}
				s = -s / qr.Get(k, k)
				for i = k; i < rows; i++ {
					q.Set(i, j, q.Get(i, j)+s*qr.Get(i, k))
				}
			}
		}
	}

	return
}
1183368
func (c *Canvas) SetMatrix(m pixel.Matrix) {
	// pixel.Matrix is 3x2 with an implicit 0, 0, 1 row after it. So
	// [0] [2] [4]    [0] [3] [6]
	// [1] [3] [5] => [1] [4] [7]
	//  0   0   1      0   0   1
	// since all matrix ops are affine, the last row never changes, and we don't need to copy it
	for i, j := range [...]int{0, 1, 3, 4, 6, 7} {
		c.mat[j] = float32(m[i])
	}
}