本文整理汇总了Golang中github.com/gonum/matrix/mat64.Col函数的典型用法代码示例。如果您正苦于以下问题:Golang Col函数的具体用法?Golang Col怎么用?Golang Col使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Col函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GcvInitCameraMatrix2D
// GcvInitCameraMatrix2D takes one 3-by-N matrix and one 2-by-N Matrix as input.
// Each column in the input matrix represents a point in real world (objPts) or
// in image (imgPts).
// Return: the camera matrix.
func GcvInitCameraMatrix2D(objPts, imgPts *mat64.Dense, dims [2]int,
aspectRatio float64) (camMat *mat64.Dense) {
objDim, nObjPts := objPts.Dims()
imgDim, nImgPts := imgPts.Dims()
if objDim != 3 || imgDim != 2 || nObjPts != nImgPts {
panic("Invalid dimensions for objPts and imgPts")
}
objPtsVec := NewGcvPoint3f32Vector(int64(nObjPts))
imgPtsVec := NewGcvPoint2f32Vector(int64(nObjPts))
for j := 0; j < nObjPts; j++ {
objPtsVec.Set(j, NewGcvPoint3f32(mat64.Col(nil, j, objPts)...))
}
for j := 0; j < nObjPts; j++ {
imgPtsVec.Set(j, NewGcvPoint2f32(mat64.Col(nil, j, imgPts)...))
}
_imgSize := NewGcvSize2i(dims[0], dims[1])
camMat = GcvMatToMat64(GcvInitCameraMatrix2D_(
objPtsVec, imgPtsVec, _imgSize, aspectRatio))
return camMat
}
示例2: TestMarginal
func TestMarginal(t *testing.T) {
for _, test := range []struct {
mu []float64
sigma *mat64.SymDense
marginal []int
}{
{
mu: []float64{2, 3, 4},
sigma: mat64.NewSymDense(3, []float64{2, 0.5, 3, 0.5, 1, 0.6, 3, 0.6, 10}),
marginal: []int{0},
},
{
mu: []float64{2, 3, 4},
sigma: mat64.NewSymDense(3, []float64{2, 0.5, 3, 0.5, 1, 0.6, 3, 0.6, 10}),
marginal: []int{0, 2},
},
{
mu: []float64{2, 3, 4, 5},
sigma: mat64.NewSymDense(4, []float64{2, 0.5, 3, 0.1, 0.5, 1, 0.6, 0.2, 3, 0.6, 10, 0.3, 0.1, 0.2, 0.3, 3}),
marginal: []int{0, 3},
},
} {
normal, ok := NewNormal(test.mu, test.sigma, nil)
if !ok {
t.Fatalf("Bad test, covariance matrix not positive definite")
}
marginal, ok := normal.MarginalNormal(test.marginal, nil)
if !ok {
t.Fatalf("Bad test, marginal matrix not positive definite")
}
dim := normal.Dim()
nSamples := 1000000
samps := mat64.NewDense(nSamples, dim, nil)
for i := 0; i < nSamples; i++ {
normal.Rand(samps.RawRowView(i))
}
estMean := make([]float64, dim)
for i := range estMean {
estMean[i] = stat.Mean(mat64.Col(nil, i, samps), nil)
}
for i, v := range test.marginal {
if math.Abs(marginal.mu[i]-estMean[v]) > 1e-2 {
t.Errorf("Mean mismatch: want: %v, got %v", estMean[v], marginal.mu[i])
}
}
marginalCov := marginal.CovarianceMatrix(nil)
estCov := stat.CovarianceMatrix(nil, samps, nil)
for i, v1 := range test.marginal {
for j, v2 := range test.marginal {
c := marginalCov.At(i, j)
ec := estCov.At(v1, v2)
if math.Abs(c-ec) > 5e-2 {
t.Errorf("Cov mismatch element i = %d, j = %d: want: %v, got %v", i, j, c, ec)
}
}
}
}
}
示例3: findLinearlyIndependent
// findLinearlyIndependnt finds a set of linearly independent columns of A, and
// returns the column indexes of the linearly independent columns.
func findLinearlyIndependent(A mat64.Matrix) []int {
m, n := A.Dims()
idxs := make([]int, 0, m)
columns := mat64.NewDense(m, m, nil)
newCol := make([]float64, m)
// Walk in reverse order because slack variables are typically the last columns
// of A.
for i := n - 1; i >= 0; i-- {
if len(idxs) == m {
break
}
mat64.Col(newCol, i, A)
if len(idxs) == 0 {
// A column is linearly independent from the null set.
// This is what needs to be changed if zero columns are allowed, as
// a column of all zeros is not linearly independent from itself.
columns.SetCol(len(idxs), newCol)
idxs = append(idxs, i)
continue
}
if linearlyDependent(mat64.NewVector(m, newCol), columns.View(0, 0, m, len(idxs))) {
continue
}
columns.SetCol(len(idxs), newCol)
idxs = append(idxs, i)
}
return idxs
}
示例4: linearlyDependent
// linearlyDependent returns whether the vector is linearly dependent
// with the columns of A. It assumes that A is a full-rank matrix.
func linearlyDependent(vec *mat64.Vector, A mat64.Matrix) bool {
// Add vec to the columns of A, and see if the condition number is reasonable.
m, n := A.Dims()
aNew := mat64.NewDense(m, n+1, nil)
aNew.Copy(A)
col := mat64.Col(nil, 0, vec)
aNew.SetCol(n, col)
cond := mat64.Cond(aNew, 1)
return cond > 1e12
}
示例5: extractColumns
// extractColumns creates a new matrix out of the columns of A specified by cols.
// TODO(btracey): Allow this to take a receiver.
func extractColumns(A mat64.Matrix, cols []int) *mat64.Dense {
r, _ := A.Dims()
sub := mat64.NewDense(r, len(cols), nil)
col := make([]float64, r)
for j, idx := range cols {
mat64.Col(col, idx, A)
sub.SetCol(j, col)
}
return sub
}
示例6: TestGcvCalibrateCamera
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)
}
示例7: getColumnVector
func getColumnVector(index int, M mat.Matrix) *mat.Vector {
rows, _ := M.Dims()
var colData []float64
if rows == 0 {
colData = []float64{}
} else {
colData = mat.Col(nil, index, M)
}
return mat.NewVector(rows, colData)
}
示例8: TestNormRand
func TestNormRand(t *testing.T) {
for _, test := range []struct {
mean []float64
cov []float64
}{
{
mean: []float64{0, 0},
cov: []float64{
1, 0,
0, 1,
},
},
{
mean: []float64{0, 0},
cov: []float64{
1, 0.9,
0.9, 1,
},
},
{
mean: []float64{6, 7},
cov: []float64{
5, 0.9,
0.9, 2,
},
},
} {
dim := len(test.mean)
cov := mat64.NewSymDense(dim, test.cov)
n, ok := NewNormal(test.mean, cov, nil)
if !ok {
t.Errorf("bad covariance matrix")
}
nSamples := 1000000
samps := mat64.NewDense(nSamples, dim, nil)
for i := 0; i < nSamples; i++ {
n.Rand(samps.RawRowView(i))
}
estMean := make([]float64, dim)
for i := range estMean {
estMean[i] = stat.Mean(mat64.Col(nil, i, samps), nil)
}
if !floats.EqualApprox(estMean, test.mean, 1e-2) {
t.Errorf("Mean mismatch: want: %v, got %v", test.mean, estMean)
}
estCov := stat.CovarianceMatrix(nil, samps, nil)
if !mat64.EqualApprox(estCov, cov, 1e-2) {
t.Errorf("Cov mismatch: want: %v, got %v", cov, estCov)
}
}
}
示例9: GcvCalibrateCamera
func GcvCalibrateCamera(objPts, imgPts, camMat, distCoeffs *mat64.Dense,
dims [2]int, flags int) (calCamMat, rvec, tvec *mat64.Dense) {
objDim, nObjPts := objPts.Dims()
imgDim, nImgPts := imgPts.Dims()
if objDim != 3 || imgDim != 2 || nObjPts != nImgPts {
panic("Invalid dimensions for objPts and imgPts")
}
objPtsVec := NewGcvPoint3f32Vector(int64(nObjPts))
imgPtsVec := NewGcvPoint2f32Vector(int64(nObjPts))
for j := 0; j < nObjPts; j++ {
objPtsVec.Set(j, NewGcvPoint3f32(mat64.Col(nil, j, objPts)...))
}
for j := 0; j < nObjPts; j++ {
imgPtsVec.Set(j, NewGcvPoint2f32(mat64.Col(nil, j, imgPts)...))
}
_camMat := Mat64ToGcvMat(camMat)
_distCoeffs := Mat64ToGcvMat(distCoeffs)
_rvec := NewGcvMat()
_tvec := NewGcvMat()
_imgSize := NewGcvSize2i(dims[0], dims[1])
GcvCalibrateCamera_(
objPtsVec, imgPtsVec,
_imgSize, _camMat, _distCoeffs,
_rvec, _tvec, flags)
calCamMat = GcvMatToMat64(_camMat)
rvec = GcvMatToMat64(_rvec)
tvec = GcvMatToMat64(_tvec)
return calCamMat, rvec, tvec
}
示例10: TestMat64
func TestMat64(t *testing.T) {
fm := readFm()
dense := fm.Mat64(false, false)
compareCol := func(i int, exp []float64) {
col := mat64.Col(nil, i, dense)
assert.Equal(t, len(col), len(exp))
for i := range exp {
assert.Equal(t, col[i], exp[i])
}
}
compareCol(1, []float64{0, 0, 0, 0, 0, 1, 1, 1})
compareCol(2, []float64{0, 0, 0, 0, 0, 0, 0, 1})
}
示例11: PrincipalComponents
// PrincipalComponents returns the principal component direction vectors and
// the column variances of the principal component scores, vecs * a, computed
// using the singular value decomposition of the input. The input a is an n×d
// matrix where each row is an observation and each column represents a variable.
//
// PrincipalComponents centers the variables but does not scale the variance.
//
// The slice weights is used to weight the observations. If weights is nil,
// each weight is considered to have a value of one, otherwise the length of
// weights must match the number of observations or PrincipalComponents will
// panic.
//
// On successful completion, the principal component direction vectors are
// returned in vecs as a d×min(n, d) matrix, and the variances are returned in
// vars as a min(n, d)-long slice in descending sort order.
//
// If no singular value decomposition is possible, vecs and vars are returned
// nil and ok is returned false.
func PrincipalComponents(a mat64.Matrix, weights []float64) (vecs *mat64.Dense, vars []float64, ok bool) {
n, d := a.Dims()
if weights != nil && len(weights) != n {
panic("stat: len(weights) != observations")
}
centered := mat64.NewDense(n, d, nil)
col := make([]float64, n)
for j := 0; j < d; j++ {
mat64.Col(col, j, a)
floats.AddConst(-Mean(col, weights), col)
centered.SetCol(j, col)
}
for i, w := range weights {
floats.Scale(math.Sqrt(w), centered.RawRowView(i))
}
kind := matrix.SVDFull
if n > d {
kind = matrix.SVDThin
}
var svd mat64.SVD
ok = svd.Factorize(centered, kind)
if !ok {
return nil, nil, false
}
vecs = &mat64.Dense{}
vecs.VFromSVD(&svd)
if n < d {
// Don't retain columns that are not valid direction vectors.
vecs.Clone(vecs.View(0, 0, d, n))
}
vars = svd.Values(nil)
var f float64
if weights == nil {
f = 1 / float64(n-1)
} else {
f = 1 / (floats.Sum(weights) - 1)
}
for i, v := range vars {
vars[i] = f * v * v
}
return vecs, vars, true
}
示例12: TestRejection
func TestRejection(t *testing.T) {
// Test by finding the expected value of a uniform.
dim := 3
bounds := make([]distmv.Bound, dim)
for i := 0; i < dim; i++ {
min := rand.NormFloat64()
max := rand.NormFloat64()
if min > max {
min, max = max, min
}
bounds[i].Min = min
bounds[i].Max = max
}
target := distmv.NewUniform(bounds, nil)
mu := target.Mean(nil)
muImp := make([]float64, dim)
sigmaImp := mat64.NewSymDense(dim, nil)
for i := 0; i < dim; i++ {
sigmaImp.SetSym(i, i, 6)
}
proposal, ok := distmv.NewNormal(muImp, sigmaImp, nil)
if !ok {
t.Fatal("bad test, sigma not pos def")
}
nSamples := 1000
batch := mat64.NewDense(nSamples, dim, nil)
weights := make([]float64, nSamples)
_, ok = Rejection(batch, target, proposal, 1000, nil)
if !ok {
t.Error("Bad test, nan samples")
}
for i := 0; i < dim; i++ {
col := mat64.Col(nil, i, batch)
ev := stat.Mean(col, weights)
if math.Abs(ev-mu[i]) > 1e-2 {
t.Errorf("Mean mismatch: Want %v, got %v", mu[i], ev)
}
}
}
示例13: computeMove
// computeMove computes how far can be moved replacing each index. The results
// are stored into move.
func computeMove(move []float64, minIdx int, A mat64.Matrix, ab *mat64.Dense, xb []float64, nonBasicIdx []int) error {
// Find ae.
col := mat64.Col(nil, nonBasicIdx[minIdx], A)
aCol := mat64.NewVector(len(col), col)
// d = - Ab^-1 Ae
nb, _ := ab.Dims()
d := make([]float64, nb)
dVec := mat64.NewVector(nb, d)
err := dVec.SolveVec(ab, aCol)
if err != nil {
return ErrLinSolve
}
floats.Scale(-1, d)
for i, v := range d {
if math.Abs(v) < dRoundTol {
d[i] = 0
}
}
// If no di < 0, then problem is unbounded.
if floats.Min(d) >= 0 {
return ErrUnbounded
}
// move = bhat_i / - d_i, assuming d is negative.
bHat := xb // ab^-1 b
for i, v := range d {
if v >= 0 {
move[i] = math.Inf(1)
} else {
move[i] = bHat[i] / math.Abs(v)
}
}
return nil
}
示例14: compareNormal
func compareNormal(t *testing.T, want *distmv.Normal, batch *mat64.Dense, weights []float64) {
dim := want.Dim()
mu := want.Mean(nil)
sigma := want.CovarianceMatrix(nil)
n, _ := batch.Dims()
if weights == nil {
weights = make([]float64, n)
for i := range weights {
weights[i] = 1
}
}
for i := 0; i < dim; i++ {
col := mat64.Col(nil, i, batch)
ev := stat.Mean(col, weights)
if math.Abs(ev-mu[i]) > 1e-2 {
t.Errorf("Mean mismatch: Want %v, got %v", mu[i], ev)
}
}
cov := stat.CovarianceMatrix(nil, batch, weights)
if !mat64.EqualApprox(cov, sigma, 1.5e-1) {
t.Errorf("Covariance matrix mismatch")
}
}
示例15: TestCorrelationMatrix
func TestCorrelationMatrix(t *testing.T) {
for i, test := range []struct {
data *mat64.Dense
weights []float64
ans *mat64.Dense
}{
{
data: mat64.NewDense(3, 3, []float64{
1, 2, 3,
3, 4, 5,
5, 6, 7,
}),
weights: nil,
ans: mat64.NewDense(3, 3, []float64{
1, 1, 1,
1, 1, 1,
1, 1, 1,
}),
},
{
data: mat64.NewDense(5, 2, []float64{
-2, -4,
-1, 2,
0, 0,
1, -2,
2, 4,
}),
weights: nil,
ans: mat64.NewDense(2, 2, []float64{
1, 0.6,
0.6, 1,
}),
}, {
data: mat64.NewDense(3, 2, []float64{
1, 1,
2, 4,
3, 9,
}),
weights: []float64{
1,
1.5,
1,
},
ans: mat64.NewDense(2, 2, []float64{
1, 0.9868703275903379,
0.9868703275903379, 1,
}),
},
} {
// Make a copy of the data to check that it isn't changing.
r := test.data.RawMatrix()
d := make([]float64, len(r.Data))
copy(d, r.Data)
w := make([]float64, len(test.weights))
if test.weights != nil {
copy(w, test.weights)
}
c := CorrelationMatrix(nil, test.data, test.weights)
if !mat64.Equal(c, test.ans) {
t.Errorf("%d: expected corr %v, found %v", i, test.ans, c)
}
if !floats.Equal(d, r.Data) {
t.Errorf("%d: data was modified during execution", i)
}
if !floats.Equal(w, test.weights) {
t.Errorf("%d: weights was modified during execution", i)
}
// compare with call to Covariance
_, cols := c.Dims()
for ci := 0; ci < cols; ci++ {
for cj := 0; cj < cols; cj++ {
x := mat64.Col(nil, ci, test.data)
y := mat64.Col(nil, cj, test.data)
corr := Correlation(x, y, test.weights)
if math.Abs(corr-c.At(ci, cj)) > 1e-14 {
t.Errorf("CorrMat does not match at (%v, %v). Want %v, got %v.", ci, cj, corr, c.At(ci, cj))
}
}
}
}
if !Panics(func() { CorrelationMatrix(nil, mat64.NewDense(5, 2, nil), []float64{}) }) {
t.Errorf("CorrelationMatrix did not panic with weight size mismatch")
}
if !Panics(func() { CorrelationMatrix(mat64.NewDense(1, 1, nil), mat64.NewDense(5, 2, nil), nil) }) {
t.Errorf("CorrelationMatrix did not panic with preallocation size mismatch")
}
if !Panics(func() { CorrelationMatrix(nil, mat64.NewDense(2, 2, []float64{1, 2, 3, 4}), []float64{1, -1}) }) {
t.Errorf("CorrelationMatrix did not panic with negative weights")
}
}