本文整理汇总了Golang中github.com/gonum/matrix/mat64.Vector.Len方法的典型用法代码示例。如果您正苦于以下问题:Golang Vector.Len方法的具体用法?Golang Vector.Len怎么用?Golang Vector.Len使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/gonum/matrix/mat64.Vector
的用法示例。
在下文中一共展示了Vector.Len方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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]
}
}
示例3: 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
}
示例4: 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)
}
示例5: 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]
}
}
示例6: 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]
}
}
示例7: dokMulMatVec
func dokMulMatVec(y *mat64.Vector, alpha float64, transA bool, a *DOK, x *mat64.Vector) {
r, c := a.Dims()
if transA {
if r != x.Len() || c != y.Len() {
panic("sparse: dimension mismatch")
}
} else {
if r != y.Len() || c != x.Len() {
panic("sparse: dimension mismatch")
}
}
if alpha == 0 {
return
}
xRaw := x.RawVector()
yRaw := y.RawVector()
if transA {
for ij, aij := range a.data {
yRaw.Data[ij[1]*yRaw.Inc] += alpha * aij * xRaw.Data[ij[0]*xRaw.Inc]
}
} else {
for ij, aij := range a.data {
yRaw.Data[ij[0]*yRaw.Inc] += alpha * aij * xRaw.Data[ij[1]*xRaw.Inc]
}
}
}
示例8: 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
}
示例9: 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
}
示例10: csrMulMatVec
func csrMulMatVec(y *mat64.Vector, alpha float64, transA bool, a *CSR, x *mat64.Vector) {
r, c := a.Dims()
if transA {
if r != x.Len() || c != y.Len() {
panic("sparse: dimension mismatch")
}
} else {
if r != y.Len() || c != x.Len() {
panic("sparse: dimension mismatch")
}
}
if alpha == 0 {
return
}
yRaw := y.RawVector()
if transA {
row := Vector{N: y.Len()}
for i := 0; i < r; i++ {
start := a.rowIndex[i]
end := a.rowIndex[i+1]
row.Data = a.values[start:end]
row.Indices = a.columns[start:end]
Axpy(y, alpha*x.At(i, 0), &row)
}
} else {
row := Vector{N: x.Len()}
for i := 0; i < r; i++ {
start := a.rowIndex[i]
end := a.rowIndex[i+1]
row.Data = a.values[start:end]
row.Indices = a.columns[start:end]
yRaw.Data[i*yRaw.Inc] += alpha * Dot(&row, x)
}
}
}