本文整理汇总了Golang中github.com/gonum/matrix/mat64.Vector类的典型用法代码示例。如果您正苦于以下问题:Golang Vector类的具体用法?Golang Vector怎么用?Golang Vector使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Vector类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: GradientDescent
func GradientDescent(X *mat64.Dense, y *mat64.Vector, alpha, tolerance float64, maxIters int) *mat64.Vector {
// m = Number of Training Examples
// n = Number of Features
m, n := X.Dims()
h := mat64.NewVector(m, nil)
partials := mat64.NewVector(n, nil)
new_theta := mat64.NewVector(n, nil)
Regression:
for i := 0; i < maxIters; i++ {
// Calculate partial derivatives
h.MulVec(X, new_theta)
for el := 0; el < m; el++ {
val := (h.At(el, 0) - y.At(el, 0)) / float64(m)
h.SetVec(el, val)
}
partials.MulVec(X.T(), h)
// Update theta values
for el := 0; el < n; el++ {
new_val := new_theta.At(el, 0) - (alpha * partials.At(el, 0))
new_theta.SetVec(el, new_val)
}
// Check the "distance" to the local minumum
dist := math.Sqrt(mat64.Dot(partials, partials))
if dist <= tolerance {
break Regression
}
}
return new_theta
}
开发者ID:erubboli,项目名称:mlt,代码行数:33,代码来源:gradient_descent.go
示例2: onesDotUnitary
// onesDotUnitary performs the equivalent of a Ddot of v with
// a ones vector of equal length. v must have have a unitary
// vector increment.
func onesDotUnitary(alpha float64, v *mat64.Vector) float64 {
var sum float64
for _, f := range v.RawVector().Data {
sum += alpha * f
}
return sum
}
开发者ID:sbinet,项目名称:gonum-graph,代码行数:10,代码来源:page.go
示例3: dotUnitary
// dotUnitary performs a simplified scatter-based Ddot operations on
// v and the receiver. v must have have a unitary vector increment.
func (r compressedRow) dotUnitary(v *mat64.Vector) float64 {
var sum float64
vec := v.RawVector().Data
for _, e := range r {
sum += vec[e.index] * e.value
}
return sum
}
开发者ID:sbinet,项目名称:gonum-graph,代码行数:10,代码来源:page.go
示例4: ExampleCholesky
func ExampleCholesky() {
// Construct a symmetric positive definite matrix.
tmp := mat64.NewDense(4, 4, []float64{
2, 6, 8, -4,
1, 8, 7, -2,
2, 2, 1, 7,
8, -2, -2, 1,
})
var a mat64.SymDense
a.SymOuterK(1, tmp)
fmt.Printf("a = %0.4v\n", mat64.Formatted(&a, mat64.Prefix(" ")))
// Compute the cholesky factorization.
var chol mat64.Cholesky
if ok := chol.Factorize(&a); !ok {
fmt.Println("a matrix is not positive semi-definite.")
}
// Find the determinant.
fmt.Printf("\nThe determinant of a is %0.4g\n\n", chol.Det())
// Use the factorization to solve the system of equations a * x = b.
b := mat64.NewVector(4, []float64{1, 2, 3, 4})
var x mat64.Vector
if err := x.SolveCholeskyVec(&chol, b); err != nil {
fmt.Println("Matrix is near singular: ", err)
}
fmt.Println("Solve a * x = b")
fmt.Printf("x = %0.4v\n", mat64.Formatted(&x, mat64.Prefix(" ")))
// Extract the factorization and check that it equals the original matrix.
var t mat64.TriDense
t.LFromCholesky(&chol)
var test mat64.Dense
test.Mul(&t, t.T())
fmt.Println()
fmt.Printf("L * L^T = %0.4v\n", mat64.Formatted(&a, mat64.Prefix(" ")))
// Output:
// a = ⎡120 114 -4 -16⎤
// ⎢114 118 11 -24⎥
// ⎢ -4 11 58 17⎥
// ⎣-16 -24 17 73⎦
//
// The determinant of a is 1.543e+06
//
// Solve a * x = b
// x = ⎡ -0.239⎤
// ⎢ 0.2732⎥
// ⎢-0.04681⎥
// ⎣ 0.1031⎦
//
// L * L^T = ⎡120 114 -4 -16⎤
// ⎢114 118 11 -24⎥
// ⎢ -4 11 58 17⎥
// ⎣-16 -24 17 73⎦
}
开发者ID:rawlingsj,项目名称:gofabric8,代码行数:58,代码来源:cholesky_example_test.go
示例5: vectorDistance
func vectorDistance(vec1, vec2 *mat.Vector) (v float64) {
result := mat.NewVector(vec1.Len(), nil)
result.SubVec(vec1, vec2)
result.MulElemVec(result, result)
v = mat.Sum(result)
return
}
开发者ID:kingzbauer,项目名称:kmeans,代码行数:9,代码来源:utils.go
示例6: Scatter
// Scatter copies the values of x into the corresponding locations in the dense
// vector y. Both vectors must have the same dimension.
func Scatter(y *mat64.Vector, x *Vector) {
if x.N != y.Len() {
panic("sparse: vector dimension mismatch")
}
raw := y.RawVector()
for i, index := range x.Indices {
raw.Data[index*raw.Inc] = x.Data[i]
}
}
开发者ID:vladimir-ch,项目名称:sparse,代码行数:12,代码来源:level1.go
示例7: findIn
// findIn returns the indexes of the values in vec that match scalar
func findIn(scalar float64, vec *mat.Vector) *mat.Vector {
var result []float64
for i := 0; i < vec.Len(); i++ {
if scalar == vec.At(i, 0) {
result = append(result, float64(i))
}
}
return mat.NewVector(len(result), result)
}
开发者ID:kingzbauer,项目名称:kmeans,代码行数:12,代码来源:utils.go
示例8: Dot
// Dot computes the dot product of the sparse vector x with the dense vector y.
// The vectors must have the same dimension.
func Dot(x *Vector, y *mat64.Vector) (dot float64) {
if x.N != y.Len() {
panic("sparse: vector dimension mismatch")
}
raw := y.RawVector()
for i, index := range x.Indices {
dot += x.Data[i] * raw.Data[index*raw.Inc]
}
return
}
开发者ID:vladimir-ch,项目名称:sparse,代码行数:13,代码来源:level1.go
示例9: Gather
// Gather gathers entries given by indices of the dense vector y into the sparse
// vector x. Indices must not be nil.
func Gather(x *Vector, y *mat64.Vector, indices []int) {
if indices == nil {
panic("sparse: slice is nil")
}
x.reuseAs(y.Len(), len(indices))
copy(x.Indices, indices)
raw := y.RawVector()
for i, index := range x.Indices {
x.Data[i] = raw.Data[index*raw.Inc]
}
}
开发者ID:vladimir-ch,项目名称:sparse,代码行数:14,代码来源:level1.go
示例10: Axpy
// Axpy scales the sparse vector x by alpha and adds the result to the dense
// vector y. If alpha is zero, y is not modified.
func Axpy(y *mat64.Vector, alpha float64, x *Vector) {
if x.N != y.Len() {
panic("sparse: vector dimension mismatch")
}
if alpha == 0 {
return
}
raw := y.RawVector()
for i, index := range x.Indices {
raw.Data[index*raw.Inc] += alpha * x.Data[i]
}
}
开发者ID:vladimir-ch,项目名称:sparse,代码行数:15,代码来源:level1.go
示例11: rowIndexIn
// 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
}
开发者ID:kingzbauer,项目名称:kmeans,代码行数:15,代码来源:utils.go
示例12: Cost
func Cost(x *mat64.Dense, y, theta *mat64.Vector) float64 {
//initialize receivers
m, _ := x.Dims()
h := mat64.NewDense(m, 1, make([]float64, m))
squaredErrors := mat64.NewDense(m, 1, make([]float64, m))
//actual calculus
h.Mul(x, theta)
squaredErrors.Apply(func(r, c int, v float64) float64 {
return math.Pow(h.At(r, c)-y.At(r, c), 2)
}, h)
j := mat64.Sum(squaredErrors) * 1.0 / (2.0 * float64(m))
return j
}
开发者ID:erubboli,项目名称:mlt,代码行数:15,代码来源:cost.go
示例13: Solve
func Solve(a sparse.Matrix, b, xInit *mat64.Vector, settings *Settings, method Method) (result Result, err error) {
stats := Stats{
StartTime: time.Now(),
}
dim, c := a.Dims()
if dim != c {
panic("iterative: matrix is not square")
}
if xInit != nil && dim != xInit.Len() {
panic("iterative: mismatched size of the initial guess")
}
if b.Len() != dim {
panic("iterative: mismatched size of the right-hand side vector")
}
if xInit == nil {
xInit = mat64.NewVector(dim, nil)
}
if settings == nil {
settings = DefaultSettings(dim)
}
ctx := Context{
X: mat64.NewVector(dim, nil),
Residual: mat64.NewVector(dim, nil),
}
// X = xInit
ctx.X.CopyVec(xInit)
if mat64.Norm(ctx.X, math.Inf(1)) > 0 {
// Residual = Ax
sparse.MulMatVec(ctx.Residual, 1, false, a, ctx.X)
stats.MatVecMultiplies++
}
// Residual = Ax - b
ctx.Residual.SubVec(ctx.Residual, b)
if mat64.Norm(ctx.Residual, 2) >= settings.Tolerance {
err = iterate(method, a, b, settings, &ctx, &stats)
}
result = Result{
X: ctx.X,
Stats: stats,
Runtime: time.Since(stats.StartTime),
}
return result, err
}
开发者ID:vladimir-ch,项目名称:sparse,代码行数:48,代码来源:iterative.go
示例14: Map
// Map produces a vector that is within the bounds of the
// rectangular manifold of toroidal space, given a vector
// that is on the torus but may be outside these bounds.
func (t Torus) Map(v *mat64.Vector) {
x := v.At(0, 0)
y := v.At(1, 0)
remx := x
right := t.W / 2
if math.Abs(x) > right {
remx = math.Mod(t.W, -x)
}
remy := y
top := t.H / 2
if math.Abs(y) > top {
remy = math.Mod(t.H, -y)
}
v.SetVec(0, remx)
v.SetVec(1, remy)
}
开发者ID:johnny-morrice,项目名称:amoebethics,代码行数:21,代码来源:vec.go
示例15: StdDev
// StdDev predicts the standard deviation of the function at x.
func (g *GP) StdDev(x []float64) float64 {
if len(x) != g.inputDim {
panic(badInputLength)
}
// nu_* = k(x_*, k_*) - k_*^T * K^-1 * k_*
n := len(g.outputs)
kstar := mat64.NewVector(n, nil)
for i := 0; i < n; i++ {
v := g.kernel.Distance(g.inputs.RawRowView(i), x)
kstar.SetVec(i, v)
}
self := g.kernel.Distance(x, x)
var tmp mat64.Vector
tmp.SolveCholeskyVec(g.cholK, kstar)
var tmp2 mat64.Vector
tmp2.MulVec(kstar.T(), &tmp)
rt, ct := tmp2.Dims()
if rt != 1 || ct != 1 {
panic("bad size")
}
return math.Sqrt(self-tmp2.At(0, 0)) * g.std
}
开发者ID:btracey,项目名称:gaussproc,代码行数:23,代码来源:gp.go
示例16: StdDevBatch
// StdDevBatch predicts the standard deviation at a set of locations of x.
func (g *GP) StdDevBatch(std []float64, x mat64.Matrix) []float64 {
r, c := x.Dims()
if c != g.inputDim {
panic(badInputLength)
}
if std == nil {
std = make([]float64, r)
}
if len(std) != r {
panic(badStorage)
}
// For a single point, the stddev is
// sigma = k(x,x) - k_*^T * K^-1 * k_*
// where k is the vector of kernels between the input points and the output points
// For many points, the formula is:
// nu_* = k(x_*, k_*) - k_*^T * K^-1 * k_*
// This creates the full covariance matrix which is an rxr matrix. However,
// the standard deviations are just the diagonal of this matrix. Instead, be
// smart about it and compute the diagonal terms one at a time.
kStar := g.formKStar(x)
var tmp mat64.Dense
tmp.SolveCholesky(g.cholK, kStar)
// set k(x_*, x_*) into std then subtract k_*^T K^-1 k_* , computed one row at a time
var tmp2 mat64.Vector
row := make([]float64, c)
for i := range std {
for k := 0; k < c; k++ {
row[k] = x.At(i, k)
}
std[i] = g.kernel.Distance(row, row)
tmp2.MulVec(kStar.ColView(i).T(), tmp.ColView(i))
rt, ct := tmp2.Dims()
if rt != 1 && ct != 1 {
panic("bad size")
}
std[i] -= tmp2.At(0, 0)
std[i] = math.Sqrt(std[i])
}
// Need to scale the standard deviation to be in the same units as y.
floats.Scale(g.std, std)
return std
}
开发者ID:btracey,项目名称:gaussproc,代码行数:44,代码来源:gp.go
示例17: BlasVec2UserVec
func BlasVec2UserVec(v *mat64.Vector) UserVec {
u := UserVec{}
u.X = v.At(0, 0)
u.Y = v.At(1, 0)
return u
}
开发者ID:johnny-morrice,项目名称:amoebethics,代码行数:6,代码来源:vec.go
示例18: mulVecUnitary
// mulVecUnitary multiplies the receiver by the src vector, storing
// the result in dst. It assumes src and dst are the same length as m
// and that both have unitary vector increments.
func (m rowCompressedMatrix) mulVecUnitary(dst, src *mat64.Vector) {
dMat := dst.RawVector().Data
for i, r := range m {
dMat[i] = r.dotUnitary(src)
}
}
开发者ID:sbinet,项目名称:gonum-graph,代码行数:9,代码来源:page.go
示例19: VectorToMatrix
func VectorToMatrix(vector *mat64.Vector) *mat64.Dense {
vec := vector.RawVector()
return mat64.NewDense(1, len(vec.Data), vec.Data)
}
开发者ID:CTLife,项目名称:golearn,代码行数:4,代码来源:utilities.go
示例20: ConditionNormal
// ConditionNormal returns the Normal distribution that is the receiver conditioned
// on the input evidence. The returned multivariate normal has dimension
// n - len(observed), where n is the dimension of the original receiver. The updated
// mean and covariance are
// mu = mu_un + sigma_{ob,un}^T * sigma_{ob,ob}^-1 (v - mu_ob)
// sigma = sigma_{un,un} - sigma_{ob,un}^T * sigma_{ob,ob}^-1 * sigma_{ob,un}
// where mu_un and mu_ob are the original means of the unobserved and observed
// variables respectively, sigma_{un,un} is the unobserved subset of the covariance
// matrix, sigma_{ob,ob} is the observed subset of the covariance matrix, and
// sigma_{un,ob} are the cross terms. The elements of x_2 have been observed with
// values v. The dimension order is preserved during conditioning, so if the value
// of dimension 1 is observed, the returned normal represents dimensions {0, 2, ...}
// of the original Normal distribution.
//
// ConditionNormal returns {nil, false} if there is a failure during the update.
// Mathematically this is impossible, but can occur with finite precision arithmetic.
func (n *Normal) ConditionNormal(observed []int, values []float64, src *rand.Rand) (*Normal, bool) {
if len(observed) == 0 {
panic("normal: no observed value")
}
if len(observed) != len(values) {
panic("normal: input slice length mismatch")
}
for _, v := range observed {
if v < 0 || v >= n.Dim() {
panic("normal: observed value out of bounds")
}
}
ob := len(observed)
unob := n.Dim() - ob
obMap := make(map[int]struct{})
for _, v := range observed {
if _, ok := obMap[v]; ok {
panic("normal: observed dimension occurs twice")
}
obMap[v] = struct{}{}
}
if len(observed) == n.Dim() {
panic("normal: all dimensions observed")
}
unobserved := make([]int, 0, unob)
for i := 0; i < n.Dim(); i++ {
if _, ok := obMap[i]; !ok {
unobserved = append(unobserved, i)
}
}
mu1 := make([]float64, unob)
for i, v := range unobserved {
mu1[i] = n.mu[v]
}
mu2 := make([]float64, ob) // really v - mu2
for i, v := range observed {
mu2[i] = values[i] - n.mu[v]
}
n.setSigma()
var sigma11, sigma22 mat64.SymDense
sigma11.SubsetSym(n.sigma, unobserved)
sigma22.SubsetSym(n.sigma, observed)
sigma21 := mat64.NewDense(ob, unob, nil)
for i, r := range observed {
for j, c := range unobserved {
v := n.sigma.At(r, c)
sigma21.Set(i, j, v)
}
}
var chol mat64.Cholesky
ok := chol.Factorize(&sigma22)
if !ok {
return nil, ok
}
// Compute sigma_{2,1}^T * sigma_{2,2}^-1 (v - mu_2).
v := mat64.NewVector(ob, mu2)
var tmp, tmp2 mat64.Vector
err := tmp.SolveCholeskyVec(&chol, v)
if err != nil {
return nil, false
}
tmp2.MulVec(sigma21.T(), &tmp)
// Compute sigma_{2,1}^T * sigma_{2,2}^-1 * sigma_{2,1}.
// TODO(btracey): Should this be a method of SymDense?
var tmp3, tmp4 mat64.Dense
err = tmp3.SolveCholesky(&chol, sigma21)
if err != nil {
return nil, false
}
tmp4.Mul(sigma21.T(), &tmp3)
for i := range mu1 {
mu1[i] += tmp2.At(i, 0)
}
// TODO(btracey): If tmp2 can constructed with a method, then this can be
// replaced with SubSym.
//.........这里部分代码省略.........
开发者ID:darrenmcc,项目名称:stat,代码行数:101,代码来源:normal.go
注:本文中的github.com/gonum/matrix/mat64.Vector类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论