本文整理匯總了Golang中github.com/hrautila/go/opt/matrix.FloatMatrix.Minus方法的典型用法代碼示例。如果您正苦於以下問題:Golang FloatMatrix.Minus方法的具體用法?Golang FloatMatrix.Minus怎麽用?Golang FloatMatrix.Minus使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/hrautila/go/opt/matrix.FloatMatrix
的用法示例。
在下文中一共展示了FloatMatrix.Minus方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: error
func error(ref, val *matrix.FloatMatrix) (nrm float64, diff *matrix.FloatMatrix) {
diff = ref.Minus(val)
nrm = blas.Nrm2(diff).Float()
return
}
示例2: Acent
// Computes analytic center of A*x <= b with A m by n of rank n.
// We assume that b > 0 and the feasible set is bounded.
func Acent(A, b *matrix.FloatMatrix, niters int) (*matrix.FloatMatrix, []float64) {
if niters <= 0 {
niters = MAXITERS
}
ntdecrs := make([]float64, 0, niters)
if A.Rows() != b.Rows() {
return nil, nil
}
m, n := A.Size()
x := matrix.FloatZeros(n, 1)
H := matrix.FloatZeros(n, n)
// Helper m*n matrix
Dmn := matrix.FloatZeros(m, n)
for i := 0; i < niters; i++ {
// Gradient is g = A^T * (1.0/(b - A*x)). d = 1.0/(b - A*x)
// d is m*1 matrix, g is n*1 matrix
d := b.Minus(A.Times(x))
d.Apply(d, func(a float64) float64 { return 1.0 / a })
g := A.Transpose().Times(d)
// Hessian is H = A^T * diag(1./(b-A*x))^2 * A.
// in the original python code expression d[:,n*[0]] creates
// a m*n matrix where each column is copy of column 0.
// We do it here manually.
for i := 0; i < n; i++ {
Dmn.SetColumnMatrix(i, d)
}
// Function mul creates element wise product of matrices.
Asc := Dmn.Mul(A)
blas.SyrkFloat(Asc, H, 1.0, 0.0, linalg.OptTrans)
// Newton step is v = H^-1 * g.
v := g.Copy().Neg()
lapack.PosvFloat(H, v)
// Directional derivative and Newton decrement.
lam := blas.DotFloat(g, v)
ntdecrs = append(ntdecrs, math.Sqrt(-lam))
if ntdecrs[len(ntdecrs)-1] < TOL {
fmt.Printf("last Newton decrement < TOL(%v)\n", TOL)
return x, ntdecrs
}
// Backtracking line search.
// y = d .* A*v
y := d.Mul(A.Times(v))
step := 1.0
for 1-step*y.Max() < 0 {
step *= BETA
}
search:
for {
// t = -step*y
t := y.Copy().Scale(-step)
// t = (1 + t) [e.g. t = 1 - step*y]
t.Add(1.0)
// ts = sum(log(1-step*y))
ts := t.Log().Sum()
if -ts < ALPHA*step*lam {
break search
}
step *= BETA
}
v.Scale(step)
x = x.Plus(v)
}
// no solution !!
fmt.Printf("Iteration %d exhausted\n", niters)
return x, ntdecrs
}