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