本文整理汇总了Golang中github.com/gonum/matrix/mat64.Vector类的典型用法代码示例。如果您正苦于以下问题:Golang Vector类的具体用法?Golang Vector怎么用?Golang Vector使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Vector类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
}
示例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
}
示例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
}
示例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⎦
}
示例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
}
示例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]
}
}
示例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)
}
示例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
}
示例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]
}
}
示例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]
}
}
示例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
}
示例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
}
示例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
}
示例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)
}
示例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
}