本文整理匯總了Golang中github.com/hrautila/go/opt/matrix.FloatMatrix.Apply方法的典型用法代碼示例。如果您正苦於以下問題:Golang FloatMatrix.Apply方法的具體用法?Golang FloatMatrix.Apply怎麽用?Golang FloatMatrix.Apply使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/hrautila/go/opt/matrix.FloatMatrix
的用法示例。
在下文中一共展示了FloatMatrix.Apply方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: computeScaling
/*
Returns the Nesterov-Todd scaling W at points s and z, and stores the
scaled variable in lmbda.
W * z = W^{-T} * s = lmbda.
W is a MatrixSet with entries:
- W['dnl']: positive vector
- W['dnli']: componentwise inverse of W['dnl']
- W['d']: positive vector
- W['di']: componentwise inverse of W['d']
- W['v']: lists of 2nd order cone vectors with unit hyperbolic norms
- W['beta']: list of positive numbers
- W['r']: list of square matrices
- W['rti']: list of square matrices. rti[k] is the inverse transpose
of r[k].
*/
func computeScaling(s, z, lmbda *matrix.FloatMatrix, dims *DimensionSet, mnl int) (W *FloatMatrixSet, err error) {
/*DEBUGGED*/
err = nil
W = FloatSetNew("dnl", "dnli", "d", "di", "v", "beta", "r", "rti")
// For the nonlinear block:
//
// W['dnl'] = sqrt( s[:mnl] ./ z[:mnl] )
// W['dnli'] = sqrt( z[:mnl] ./ s[:mnl] )
// lambda[:mnl] = sqrt( s[:mnl] .* z[:mnl] )
var stmp, ztmp, lmd *matrix.FloatMatrix
if mnl > 0 {
stmp = matrix.FloatVector(s.FloatArray()[:mnl])
ztmp = matrix.FloatVector(z.FloatArray()[:mnl])
dnl := stmp.Div(ztmp)
dnl.Apply(dnl, math.Sqrt)
dnli := dnl.Copy()
dnli.Apply(dnli, func(a float64) float64 { return 1.0 / a })
W.Set("dnl", dnl)
W.Set("dnli", dnli)
lmd = stmp.Mul(ztmp)
lmd.Apply(lmd, math.Sqrt)
lmbda.SetIndexes(matrix.MakeIndexSet(0, mnl, 1), lmd.FloatArray())
} else {
mnl = 0
}
// For the 'l' block:
//
// W['d'] = sqrt( sk ./ zk )
// W['di'] = sqrt( zk ./ sk )
// lambdak = sqrt( sk .* zk )
//
// where sk and zk are the first dims['l'] entries of s and z.
// lambda_k is stored in the first dims['l'] positions of lmbda.
m := dims.At("l")[0]
td := s.FloatArray()
stmp = matrix.FloatVector(td[mnl : mnl+m])
zd := z.FloatArray()
//fmt.Printf("zdata=%v\n", zd[mnl:mnl+m])
ztmp = matrix.FloatVector(zd[mnl : mnl+m])
d := stmp.Div(ztmp)
d.Apply(d, math.Sqrt)
di := d.Copy()
di.Apply(di, func(a float64) float64 { return 1.0 / a })
//fmt.Printf("d:\n%v\n", d)
//fmt.Printf("di:\n%v\n", di)
W.Set("d", d)
W.Set("di", di)
lmd = stmp.Mul(ztmp)
lmd.Apply(lmd, math.Sqrt)
// lmd has indexes mnl:mnl+m and length of m
lmbda.SetIndexes(matrix.MakeIndexSet(mnl, mnl+m, 1), lmd.FloatArray())
//fmt.Printf("after l:\n%v\n", lmbda)
/*
For the 'q' blocks, compute lists 'v', 'beta'.
The vector v[k] has unit hyperbolic norm:
(sqrt( v[k]' * J * v[k] ) = 1 with J = [1, 0; 0, -I]).
beta[k] is a positive scalar.
The hyperbolic Householder matrix H = 2*v[k]*v[k]' - J
defined by v[k] satisfies
(beta[k] * H) * zk = (beta[k] * H) \ sk = lambda_k
where sk = s[indq[k]:indq[k+1]], zk = z[indq[k]:indq[k+1]].
lambda_k is stored in lmbda[indq[k]:indq[k+1]].
*/
ind := mnl + dims.At("l")[0]
var beta *matrix.FloatMatrix
for _, k := range dims.At("q") {
W.Append("v", matrix.FloatZeros(k, 1))
}
//.........這裏部分代碼省略.........
示例2: updateScaling
func updateScaling(W *FloatMatrixSet, lmbda, s, z *matrix.FloatMatrix) (err error) {
err = nil
var stmp, ztmp *matrix.FloatMatrix
/*
Nonlinear and 'l' blocks
d := d .* sqrt( s ./ z )
lmbda := lmbda .* sqrt(s) .* sqrt(z)
*/
mnl := 0
dnlset := W.At("dnl")
dnliset := W.At("dnli")
dset := W.At("d")
diset := W.At("di")
beta := W.At("beta")[0]
if dnlset != nil && dnlset[0].NumElements() > 0 {
mnl = dnlset[0].NumElements()
}
ml := dset[0].NumElements()
m := mnl + ml
//fmt.Printf("ml=%d, mnl=%d, m=%d'n", ml, mnl, m)
stmp = matrix.FloatVector(s.FloatArray()[:m])
stmp.Apply(stmp, math.Sqrt)
s.SetIndexes(matrix.MakeIndexSet(0, m, 1), stmp.FloatArray())
ztmp = matrix.FloatVector(z.FloatArray()[:m])
ztmp.Apply(ztmp, math.Sqrt)
z.SetIndexes(matrix.MakeIndexSet(0, m, 1), ztmp.FloatArray())
// d := d .* s .* z
if len(dnlset) > 0 {
blas.TbmvFloat(s, dnlset[0], &la_.IOpt{"n", mnl}, &la_.IOpt{"k", 0}, &la_.IOpt{"lda", 1})
blas.TbsvFloat(z, dnlset[0], &la_.IOpt{"n", mnl}, &la_.IOpt{"k", 0}, &la_.IOpt{"lda", 1})
dnliset[0].Apply(dnlset[0], func(a float64) float64 { return 1.0 / a })
}
blas.TbmvFloat(s, dset[0], &la_.IOpt{"n", ml},
&la_.IOpt{"k", 0}, &la_.IOpt{"lda", 1}, &la_.IOpt{"offseta", mnl})
blas.TbsvFloat(z, dset[0], &la_.IOpt{"n", ml},
&la_.IOpt{"k", 0}, &la_.IOpt{"lda", 1}, &la_.IOpt{"offseta", mnl})
diset[0].Apply(dset[0], func(a float64) float64 { return 1.0 / a })
// lmbda := s .* z
blas.CopyFloat(s, lmbda, &la_.IOpt{"n", m})
blas.TbmvFloat(z, lmbda, &la_.IOpt{"n", m}, &la_.IOpt{"k", 0}, &la_.IOpt{"lda", 1})
//fmt.Printf("-- end of l:\nz=\n%v\nlmbda=\n%v\n", z.ConvertToString(), lmbda.ConvertToString())
//fmt.Printf("W[d]=\n%v\n", dset[0].ConvertToString())
//fmt.Printf("W[di]=\n%v\n", diset[0].ConvertToString())
// 'q' blocks.
// Let st and zt be the new variables in the old scaling:
//
// st = s_k, zt = z_k
//
// and a = sqrt(st' * J * st), b = sqrt(zt' * J * zt).
//
// 1. Compute the hyperbolic Householder transformation 2*q*q' - J
// that maps st/a to zt/b.
//
// c = sqrt( (1 + st'*zt/(a*b)) / 2 )
// q = (st/a + J*zt/b) / (2*c).
//
// The new scaling point is
//
// wk := betak * sqrt(a/b) * (2*v[k]*v[k]' - J) * q
//
// with betak = W['beta'][k].
//
// 3. The scaled variable:
//
// lambda_k0 = sqrt(a*b) * c
// lambda_k1 = sqrt(a*b) * ( (2vk*vk' - J) * (-d*q + u/2) )_1
//
// where
//
// u = st/a - J*zt/b
// d = ( vk0 * (vk'*u) + u0/2 ) / (2*vk0 *(vk'*q) - q0 + 1).
//
// 4. Update scaling
//
// v[k] := wk^1/2
// = 1 / sqrt(2*(wk0 + 1)) * (wk + e).
// beta[k] *= sqrt(a/b)
ind := m
for k, v := range W.At("v") {
m = v.NumElements()
// ln = sqrt( lambda_k' * J * lambda_k ) !! NOT USED!!
jnrm2(lmbda, m, ind) // ?? NOT USED ??
// a = sqrt( sk' * J * sk ) = sqrt( st' * J * st )
// s := s / a = st / a
aa := jnrm2(s, m, ind)
blas.ScalFloat(s, 1.0/aa, &la_.IOpt{"n", m}, &la_.IOpt{"offset", ind})
// b = sqrt( zk' * J * zk ) = sqrt( zt' * J * zt )
// z := z / a = zt / b
bb := jnrm2(z, m, ind)
//.........這裏部分代碼省略.........