Esempio n. 1
0
// Cov returns the covariance between a set of data points based on the current
// GP fit.
func (g *GP) Cov(m *mat64.SymDense, x mat64.Matrix) *mat64.SymDense {
	if m != nil {
		// TODO(btracey): Make this k**
		panic("resuing m not coded")
	}
	// The joint covariance matrix is
	// K(x_*, k_*) - k(x_*, x) k(x,x)^-1 k(x, x*)
	nSamp, nDim := x.Dims()
	if nDim != g.inputDim {
		panic(badInputLength)
	}

	// Compute K(x_*, x) K(x, x)^-1 K(x, x_*)
	kstar := g.formKStar(x)
	var tmp mat64.Dense
	tmp.SolveCholesky(g.cholK, kstar)
	var tmp2 mat64.Dense
	tmp2.Mul(kstar.T(), &tmp)

	// Compute k(x_*, x_*) and perform the subtraction.
	kstarstar := mat64.NewSymDense(nSamp, nil)
	for i := 0; i < nSamp; i++ {
		for j := i; j < nSamp; j++ {
			v := g.kernel.Distance(mat64.Row(nil, i, x), mat64.Row(nil, j, x))
			if i == j {
				v += g.noise
			}
			kstarstar.SetSym(i, j, v-tmp2.At(i, j))
		}
	}
	return kstarstar
}
Esempio n. 2
0
func TestGcvRodrigues(t *testing.T) {
	rvec := mat64.NewDense(3, 1, []float64{
		-0.98405029,
		-0.93443411,
		-0.26304667,
	})
	rmat := GcvRodrigues(rvec)

	assert.InDeltaSlice(t, []float64{0.59922526, 0.57799222, -0.55394411}, mat64.Row(nil, 0, rmat), DELTA)
	assert.InDeltaSlice(t, []float64{0.20413818, 0.558743, 0.80382452}, mat64.Row(nil, 1, rmat), DELTA)
	assert.InDeltaSlice(t, []float64{0.77411672, -0.5947531, 0.21682264}, mat64.Row(nil, 2, rmat), DELTA)
}
Esempio n. 3
0
func TestGcvCalibrateCamera(t *testing.T) {
	objPts := mat64.NewDense(10, 3, []float64{
		-1.482676, -1.419348, 1.166475,
		-0.043819, -0.729445, 1.212821,
		0.960825, 1.147328, 0.485541,
		1.738245, 0.597865, 1.026016,
		-0.430206, -1.281281, 0.870726,
		-1.627323, -2.203264, -0.381758,
		0.166347, -0.571246, 0.428893,
		0.376266, 0.213996, -0.299131,
		-0.226950, 0.942377, -0.899869,
		-1.148912, 0.093725, 0.634745,
	})
	objPts.Clone(objPts.T())

	imgPts := mat64.NewDense(10, 2, []float64{
		-0.384281, -0.299055,
		0.361833, 0.087737,
		1.370253, 1.753933,
		1.421390, 0.853312,
		0.107177, -0.443076,
		3.773328, 5.437829,
		0.624914, -0.280949,
		-0.825577, -0.245594,
		0.631444, -0.340257,
		-0.647580, 0.502113,
	})
	imgPts.Clone(imgPts.T())

	camMat := GcvInitCameraMatrix2D(objPts, imgPts, [2]int{1920, 1080}, 1)

	distCoeffs := mat64.NewDense(5, 1, []float64{0, 0, 0, 0, 0})

	camMat, rvec, tvec := GcvCalibrateCamera(
		objPts, imgPts, camMat, distCoeffs, [2]int{1920, 1080}, 14575)

	assert.InDeltaSlice(t, []float64{-46.15296606, 0., 959.5}, mat64.Row(nil, 0, camMat), DELTA)
	assert.InDeltaSlice(t, []float64{0., -46.15296606, 539.5}, mat64.Row(nil, 1, camMat), DELTA)
	assert.InDeltaSlice(t, []float64{0., 0., 1.}, mat64.Row(nil, 2, camMat), DELTA)

	assert.InDeltaSlice(t, []float64{-0.98405029, -0.93443411, -0.26304667}, mat64.Col(nil, 0, rvec), DELTA)
	assert.InDeltaSlice(t, []float64{0.6804739, 0.47530207, -0.04833094}, mat64.Col(nil, 0, tvec), DELTA)
}
Esempio n. 4
0
func getRowVector(index int, M mat.Matrix) *mat.Vector {
	_, cols := M.Dims()
	var rowData []float64

	if cols == 0 {
		rowData = []float64{}
	} else {
		rowData = mat.Row(nil, index, M)
	}
	return mat.NewVector(cols, rowData)
}
Esempio n. 5
0
// MoveCentroid computes the averages for all the points inside each of the cluster
// centroid groups, then move the cluster centroid points to those averages.
// It then returns the new Centroids
func MoveCentroids(idx *mat.Vector, X, Mu mat.Matrix) mat.Matrix {
	muRows, muCols := Mu.Dims()
	NewMu := mat.NewDense(muRows, muCols, nil)

	for k := 0; k < muRows; k++ {
		CentroidKMean := columnMean(rowIndexIn(findIn(float64(k), idx), X))
		NewMu.SetRow(k,
			mat.Row(nil, 0, CentroidKMean))
	}

	return NewMu
}
Esempio n. 6
0
// rowIndexIn returns a matrix contains the rows in indexes vector
func rowIndexIn(indexes *mat.Vector, M mat.Matrix) mat.Matrix {
	m := indexes.Len()
	_, n := M.Dims()
	Res := mat.NewDense(m, n, nil)

	for i := 0; i < m; i++ {
		Res.SetRow(i, mat.Row(
			nil,
			int(indexes.At(i, 0)),
			M))
	}

	return Res
}
Esempio n. 7
0
func TestGcvInitCameraMatrix2D(t *testing.T) {
	objPts := mat64.NewDense(10, 3, []float64{
		-1.482676, -1.419348, 1.166475,
		-0.043819, -0.729445, 1.212821,
		0.960825, 1.147328, 0.485541,
		1.738245, 0.597865, 1.026016,
		-0.430206, -1.281281, 0.870726,
		-1.627323, -2.203264, -0.381758,
		0.166347, -0.571246, 0.428893,
		0.376266, 0.213996, -0.299131,
		-0.226950, 0.942377, -0.899869,
		-1.148912, 0.093725, 0.634745,
	})
	objPts.Clone(objPts.T())

	imgPts := mat64.NewDense(10, 2, []float64{
		-0.384281, -0.299055,
		0.361833, 0.087737,
		1.370253, 1.753933,
		1.421390, 0.853312,
		0.107177, -0.443076,
		3.773328, 5.437829,
		0.624914, -0.280949,
		-0.825577, -0.245594,
		0.631444, -0.340257,
		-0.647580, 0.502113,
	})
	imgPts.Clone(imgPts.T())

	camMat := GcvInitCameraMatrix2D(objPts, imgPts, [2]int{1920, 1080}, 1)
	assert.InDeltaSlice(t, []float64{1.47219772e+03, 0.00000000e+00, 9.59500000e+02},
		mat64.Row(nil, 0, camMat), DELTA)
	assert.InDeltaSlice(t, []float64{0.00000000e+00, 1.47219772e+03, 5.39500000e+02},
		mat64.Row(nil, 1, camMat), DELTA)
	assert.InDeltaSlice(t, []float64{0.00000000e+00, 0.00000000e+00, 1.00000000e+00},
		mat64.Row(nil, 2, camMat), DELTA)
}
Esempio n. 8
0
// Given number of clusters K and the training set X of dimension (m*n)
// InitializeCentroids returns matrix Mu of dimension (K*n)
// The steps to initialize the centroids are as follows
//     1. Randomly pick K training examples (Make sure the selected examples are unique)
//     2. Set Mu to the K examples
func InitializeCentroids(K int, X mat.Matrix) (Mu mat.Matrix) {
	m, n := X.Dims()

	// panic if K >= m
	if K >= m {
		panic("K should be less than the size of the training set")
	}

	randomIndexes := randArray(0, m, K, true) // 1. pick K training examples

	// 2. set Mu
	Mu = mat.NewDense(K, n, nil)
	for i := 0; i < K; i++ {
		Mu.(*mat.Dense).SetRow(i, mat.Row(nil, randomIndexes[i], X))
	}

	return
}