本文整理匯總了Golang中github.com/henrylee2cn/algorithm/matrix.FloatMatrix.GetIndex方法的典型用法代碼示例。如果您正苦於以下問題:Golang FloatMatrix.GetIndex方法的具體用法?Golang FloatMatrix.GetIndex怎麽用?Golang FloatMatrix.GetIndex使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/henrylee2cn/algorithm/matrix.FloatMatrix
的用法示例。
在下文中一共展示了FloatMatrix.GetIndex方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: pack
/*
Copy x to y using packed storage.
The vector x is an element of S, with the 's' components stored in
unpacked storage. On return, x is copied to y with the 's' components
stored in packed storage and the off-diagonal entries scaled by
sqrt(2).
*/
func pack(x, y *matrix.FloatMatrix, dims *sets.DimensionSet, opts ...la_.Option) (err error) {
/*DEBUGGED*/
err = nil
mnl := la_.GetIntOpt("mnl", 0, opts...)
offsetx := la_.GetIntOpt("offsetx", 0, opts...)
offsety := la_.GetIntOpt("offsety", 0, opts...)
nlq := mnl + dims.At("l")[0] + dims.Sum("q")
blas.Copy(x, y, &la_.IOpt{"n", nlq}, &la_.IOpt{"offsetx", offsetx},
&la_.IOpt{"offsety", offsety})
iu, ip := offsetx+nlq, offsety+nlq
for _, n := range dims.At("s") {
for k := 0; k < n; k++ {
blas.Copy(x, y, &la_.IOpt{"n", n - k}, &la_.IOpt{"offsetx", iu + k*(n+1)},
&la_.IOpt{"offsety", ip})
y.SetIndex(ip, (y.GetIndex(ip) / math.Sqrt(2.0)))
ip += n - k
}
iu += n * n
}
np := dims.SumPacked("s")
blas.ScalFloat(y, math.Sqrt(2.0), &la_.IOpt{"n", np}, &la_.IOpt{"offset", offsety + nlq})
return
}
示例2: AMax
// max |x|
func AMax(X *matrix.FloatMatrix) float64 {
ix := IAMax(X)
if ix == -1 {
return math.NaN()
}
return X.GetIndex(ix)
}
示例3: jdot
/*
Returns x' * J * y, where J = [1, 0; 0, -I].
*/
func jdot(x, y *matrix.FloatMatrix, n, offsetx, offsety int) float64 {
if n <= 0 {
n = x.NumElements()
}
a := blas.DotFloat(x, y, &la_.IOpt{"n", n - 1}, &la_.IOpt{"offsetx", offsetx + 1},
&la_.IOpt{"offsety", offsety + 1})
return x.GetIndex(offsetx)*y.GetIndex(offsety) - a
}
示例4: F2
func (p *acenterProg) F2(x, z *matrix.FloatMatrix) (f, Df, H *matrix.FloatMatrix, err error) {
f, Df, err = p.F1(x)
u := matrix.Pow(x, 2.0).Scale(-1.0).Add(1.0)
z0 := z.GetIndex(0)
u2 := matrix.Pow(u, 2.0)
hd := matrix.Div(matrix.Add(u2, 1.0), u2).Scale(2 * z0)
H = matrix.FloatDiagonal(hd.NumElements(), hd.FloatArray()...)
return
}
示例5: jnrm2
/*
Returns sqrt(x' * J * x) where J = [1, 0; 0, -I], for a vector
x in a second order cone.
*/
func jnrm2(x *matrix.FloatMatrix, n, offset int) float64 {
/*DEBUGGED*/
if n <= 0 {
n = x.NumElements()
}
if offset < 0 {
offset = 0
}
a := blas.Nrm2Float(x, &la_.IOpt{"n", n - 1}, &la_.IOpt{"offset", offset + 1})
fst := x.GetIndex(offset)
return math.Sqrt(fst-a) * math.Sqrt(fst+a)
}
示例6: maxStep
// Returns min {t | x + t*e >= 0}, where e is defined as follows
//
// - For the nonlinear and 'l' blocks: e is the vector of ones.
// - For the 'q' blocks: e is the first unit vector.
// - For the 's' blocks: e is the identity matrix.
//
// When called with the argument sigma, also returns the eigenvalues
// (in sigma) and the eigenvectors (in x) of the 's' components of x.
func maxStep(x *matrix.FloatMatrix, dims *sets.DimensionSet, mnl int, sigma *matrix.FloatMatrix) (rval float64, err error) {
/*DEBUGGED*/
rval = 0.0
err = nil
t := make([]float64, 0, 10)
ind := mnl + dims.Sum("l")
if ind > 0 {
t = append(t, -minvec(x.FloatArray()[:ind]))
}
for _, m := range dims.At("q") {
if m > 0 {
v := blas.Nrm2Float(x, &la_.IOpt{"offset", ind + 1}, &la_.IOpt{"n", m - 1})
v -= x.GetIndex(ind)
t = append(t, v)
}
ind += m
}
//var Q *matrix.FloatMatrix
//var w *matrix.FloatMatrix
ind2 := 0
//if sigma == nil && len(dims.At("s")) > 0 {
// mx := dims.Max("s")
// Q = matrix.FloatZeros(mx, mx)
// w = matrix.FloatZeros(mx, 1)
//}
for _, m := range dims.At("s") {
if sigma == nil {
Q := matrix.FloatZeros(m, m)
w := matrix.FloatZeros(m, 1)
blas.Copy(x, Q, &la_.IOpt{"offsetx", ind}, &la_.IOpt{"n", m * m})
err = lapack.SyevrFloat(Q, w, nil, 0.0, nil, []int{1, 1}, la_.OptRangeInt,
&la_.IOpt{"n", m}, &la_.IOpt{"lda", m})
if m > 0 && err == nil {
t = append(t, -w.GetIndex(0))
}
} else {
err = lapack.SyevdFloat(x, sigma, la_.OptJobZValue, &la_.IOpt{"n", m},
&la_.IOpt{"lda", m}, &la_.IOpt{"offseta", ind}, &la_.IOpt{"offsetw", ind2})
if m > 0 {
t = append(t, -sigma.GetIndex(ind2))
}
}
ind += m * m
ind2 += m
}
if len(t) > 0 {
rval = maxvec(t)
}
return
}
示例7: F2
func (gp *gpConvexProg) F2(x, z *matrix.FloatMatrix) (f, Df, H *matrix.FloatMatrix, err error) {
err = nil
f = matrix.FloatZeros(gp.mnl+1, 1)
Df = matrix.FloatZeros(gp.mnl+1, gp.n)
H = matrix.FloatZeros(gp.n, gp.n)
y := gp.g.Copy()
Fsc := matrix.FloatZeros(gp.maxK, gp.n)
blas.GemvFloat(gp.F, x, y, 1.0, 1.0)
//fmt.Printf("y=\n%v\n", y.ToString("%.3f"))
for i, s := range gp.ind {
start := s[0]
stop := s[1]
// yi := exp(yi) = exp(Fi*x+gi)
ymax := maxvec(y.FloatArray()[start:stop])
ynew := matrix.Exp(matrix.FloatVector(y.FloatArray()[start:stop]).Add(-ymax))
y.SetIndexesFromArray(ynew.FloatArray(), matrix.Indexes(start, stop)...)
// fi = log sum yi = log sum exp(Fi*x+gi)
ysum := blas.AsumFloat(y, &la.IOpt{"n", stop - start}, &la.IOpt{"offset", start})
f.SetIndex(i, ymax+math.Log(ysum))
blas.ScalFloat(y, 1.0/ysum, &la.IOpt{"n", stop - start}, &la.IOpt{"offset", start})
blas.GemvFloat(gp.F, y, Df, 1.0, 0.0, la.OptTrans, &la.IOpt{"m", stop - start},
&la.IOpt{"incy", gp.mnl + 1}, &la.IOpt{"offseta", start},
&la.IOpt{"offsetx", start}, &la.IOpt{"offsety", i})
Fsc.SetSubMatrix(0, 0, gp.F.GetSubMatrix(start, 0, stop-start))
for k := start; k < stop; k++ {
blas.AxpyFloat(Df, Fsc, -1.0, &la.IOpt{"n", gp.n},
&la.IOpt{"incx", gp.mnl + 1}, &la.IOpt{"incy", Fsc.Rows()},
&la.IOpt{"offsetx", i}, &la.IOpt{"offsety", k - start})
blas.ScalFloat(Fsc, math.Sqrt(y.GetIndex(k)),
&la.IOpt{"inc", Fsc.Rows()}, &la.IOpt{"offset", k - start})
}
// H += z[i]*Hi = z[i] *Fisc' * Fisc
blas.SyrkFloat(Fsc, H, z.GetIndex(i), 1.0, la.OptTrans,
&la.IOpt{"k", stop - start})
}
return
}
示例8: ssqr
// The product x := y o y. The 's' components of y are diagonal and
// only the diagonals of x and y are stored.
func ssqr(x, y *matrix.FloatMatrix, dims *sets.DimensionSet, mnl int) (err error) {
/*DEBUGGED*/
blas.Copy(y, x)
ind := mnl + dims.At("l")[0]
err = blas.Tbmv(y, x, &la_.IOpt{"n", ind}, &la_.IOpt{"k", 0}, &la_.IOpt{"lda", 1})
if err != nil {
return
}
for _, m := range dims.At("q") {
v := blas.Nrm2Float(y, &la_.IOpt{"n", m}, &la_.IOpt{"offset", ind})
x.SetIndex(ind, v*v)
blas.ScalFloat(x, 2.0*y.GetIndex(ind), &la_.IOpt{"n", m - 1}, &la_.IOpt{"offset", ind + 1})
ind += m
}
err = blas.Tbmv(y, x, &la_.IOpt{"n", dims.Sum("s")}, &la_.IOpt{"k", 0},
&la_.IOpt{"lda", 1}, &la_.IOpt{"offseta", ind}, &la_.IOpt{"offsetx", ind})
return
}
示例9: sinv
func sinv(x, y *matrix.FloatMatrix, dims *sets.DimensionSet, mnl int) (err error) {
/*DEBUGGED*/
err = nil
// For the nonlinear and 'l' blocks:
//
// yk o\ xk = yk .\ xk.
ind := mnl + dims.At("l")[0]
blas.Tbsv(y, x, &la_.IOpt{"n", ind}, &la_.IOpt{"k", 0}, &la_.IOpt{"ldA", 1})
// For the 'q' blocks:
//
// [ l0 -l1' ]
// yk o\ xk = 1/a^2 * [ ] * xk
// [ -l1 (a*I + l1*l1')/l0 ]
//
// where yk = (l0, l1) and a = l0^2 - l1'*l1.
for _, m := range dims.At("q") {
aa := blas.Nrm2Float(y, &la_.IOpt{"n", m - 1}, &la_.IOpt{"offset", ind + 1})
ee := y.GetIndex(ind)
aa = (ee + aa) * (ee - aa)
cc := x.GetIndex(ind)
dd := blas.DotFloat(x, y, &la_.IOpt{"n", m - 1}, &la_.IOpt{"offsetx", ind + 1},
&la_.IOpt{"offsety", ind + 1})
x.SetIndex(ind, cc*ee-dd)
blas.ScalFloat(x, aa/ee, &la_.IOpt{"n", m - 1}, &la_.IOpt{"offset", ind + 1})
blas.AxpyFloat(y, x, dd/ee-cc, &la_.IOpt{"n", m - 1},
&la_.IOpt{"offsetx", ind + 1}, &la_.IOpt{"offsety", ind + 1})
blas.ScalFloat(x, 1.0/aa, &la_.IOpt{"n", m}, &la_.IOpt{"offset", ind})
ind += m
}
// For the 's' blocks:
//
// yk o\ xk = xk ./ gamma
//
// where gammaij = .5 * (yk_i + yk_j).
ind2 := ind
for _, m := range dims.At("s") {
for j := 0; j < m; j++ {
u := matrix.FloatVector(y.FloatArray()[ind2+j : ind2+m])
u.Add(y.GetIndex(ind2 + j))
u.Scale(0.5)
blas.Tbsv(u, x, &la_.IOpt{"n", m - j}, &la_.IOpt{"k", 0}, &la_.IOpt{"lda", 1},
&la_.IOpt{"offsetx", ind + j*(m+1)})
}
ind += m * m
ind2 += m
}
return
}
示例10: MVUpdateDiag
/*
* Generic rank update of diagonal matrix.
* diag(D) = diag(D) + alpha * x * y.T
*
* Arguments:
* D N element column or row vector or N-by-N matrix
*
* x, y N element vectors
*
* alpha scalar
*/
func MVUpdateDiag(D, x, y *matrix.FloatMatrix, alpha float64) error {
var d *matrix.FloatMatrix
var dvec matrix.FloatMatrix
if !isVector(x) || !isVector(y) {
return errors.New("x, y not vectors")
}
if D.Rows() > 0 && D.Cols() == D.Rows() {
D.Diag(&dvec)
d = &dvec
} else if isVector(D) {
d = D
} else {
return errors.New("D not a diagonal")
}
N := d.NumElements()
for k := 0; k < N; k++ {
val := d.GetIndex(k)
val += x.GetIndex(k) * y.GetIndex(k) * alpha
d.SetIndex(k, val)
}
return nil
}
示例11: computeScaling
//.........這裏部分代碼省略.........
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))
}
beta = matrix.FloatZeros(len(dims.At("q")), 1)
W.Set("beta", beta)
vset := W.At("v")
for k, m := range dims.At("q") {
v := vset[k]
// a = sqrt( sk' * J * sk ) where J = [1, 0; 0, -I]
aa := jnrm2(s, m, ind)
// b = sqrt( zk' * J * zk )
bb := jnrm2(z, m, ind)
// beta[k] = ( a / b )**1/2
beta.SetIndex(k, math.Sqrt(aa/bb))
// c = sqrt( (sk/a)' * (zk/b) + 1 ) / sqrt(2)
c0 := blas.DotFloat(s, z, &la_.IOpt{"n", m},
&la_.IOpt{"offsetx", ind}, &la_.IOpt{"offsety", ind})
cc := math.Sqrt((c0/aa/bb + 1.0) / 2.0)
// vk = 1/(2*c) * ( (sk/a) + J * (zk/b) )
blas.CopyFloat(z, v, &la_.IOpt{"offsetx", ind}, &la_.IOpt{"n", m})
blas.ScalFloat(v, -1.0/bb)
v.SetIndex(0, -1.0*v.GetIndex(0))
blas.AxpyFloat(s, v, 1.0/aa, &la_.IOpt{"offsetx", ind}, &la_.IOpt{"n", m})
blas.ScalFloat(v, 1.0/2.0/cc)
// v[k] = 1/sqrt(2*(vk0 + 1)) * ( vk + e ), e = [1; 0]
v.SetIndex(0, v.GetIndex(0)+1.0)
blas.ScalFloat(v, (1.0 / math.Sqrt(2.0*v.GetIndex(0))))
/*
To get the scaled variable lambda_k
d = sk0/a + zk0/b + 2*c
lambda_k = [ c;
(c + zk0/b)/d * sk1/a + (c + sk0/a)/d * zk1/b ]
lambda_k *= sqrt(a * b)
*/
lmbda.SetIndex(ind, cc)
dd := 2*cc + s.GetIndex(ind)/aa + z.GetIndex(ind)/bb
blas.CopyFloat(s, lmbda, &la_.IOpt{"offsetx", ind + 1}, &la_.IOpt{"offsety", ind + 1},
&la_.IOpt{"n", m - 1})
zz := (cc + z.GetIndex(ind)/bb) / dd / aa
ss := (cc + s.GetIndex(ind)/aa) / dd / bb
blas.ScalFloat(lmbda, zz, &la_.IOpt{"offset", ind + 1}, &la_.IOpt{"n", m - 1})
blas.AxpyFloat(z, lmbda, ss, &la_.IOpt{"offsetx", ind + 1},
&la_.IOpt{"offsety", ind + 1}, &la_.IOpt{"n", m - 1})
blas.ScalFloat(lmbda, math.Sqrt(aa*bb), &la_.IOpt{"offset", ind}, &la_.IOpt{"n", m})
ind += m
//fmt.Printf("after q[%d]:\n%v\n", k, lmbda)
}
/*
For the 's' blocks: compute two lists 'r' and 'rti'.
r[k]' * sk^{-1} * r[k] = diag(lambda_k)^{-1}
示例12: updateScaling
//.........這裏部分代碼省略.........
// 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)
blas.ScalFloat(z, 1.0/bb, &la_.IOpt{"n", m}, &la_.IOpt{"offset", ind})
// c = sqrt( ( 1 + (st'*zt) / (a*b) ) / 2 )
cc := blas.DotFloat(s, z, &la_.IOpt{"offsetx", ind}, &la_.IOpt{"offsety", ind},
&la_.IOpt{"n", m})
cc = math.Sqrt((1.0 + cc) / 2.0)
// vs = v' * st / a
vs := blas.DotFloat(v, s, &la_.IOpt{"offsety", ind}, &la_.IOpt{"n", m})
// vz = v' * J *zt / b
vz := jdot(v, z, m, 0, ind)
// vq = v' * q where q = (st/a + J * zt/b) / (2 * c)
vq := (vs + vz) / 2.0 / cc
// vq = v' * q where q = (st/a + J * zt/b) / (2 * c)
vu := vs - vz
// lambda_k0 = c
lmbda.SetIndex(ind, cc)
// wk0 = 2 * vk0 * (vk' * q) - q0
wk0 := 2.0*v.GetIndex(0)*vq - (s.GetIndex(ind)+z.GetIndex(ind))/2.0/cc
// d = (v[0] * (vk' * u) - u0/2) / (wk0 + 1)
dd := (v.GetIndex(0)*vu - s.GetIndex(ind)/2.0 + z.GetIndex(ind)/2.0) / (wk0 + 1.0)
// lambda_k1 = 2 * v_k1 * vk' * (-d*q + u/2) - d*q1 + u1/2
blas.CopyFloat(v, lmbda, &la_.IOpt{"offsetx", 1}, &la_.IOpt{"offsety", ind + 1},
&la_.IOpt{"n", m - 1})
blas.ScalFloat(lmbda, (2.0 * (-dd*vq + 0.5*vu)),
&la_.IOpt{"offsetx", ind + 1}, &la_.IOpt{"offsety", ind + 1}, &la_.IOpt{"n", m - 1})
blas.AxpyFloat(s, lmbda, 0.5*(1.0-dd/cc),
&la_.IOpt{"offsetx", ind + 1}, &la_.IOpt{"offsety", ind + 1}, &la_.IOpt{"n", m - 1})
blas.AxpyFloat(z, lmbda, 0.5*(1.0+dd/cc),
&la_.IOpt{"offsetx", ind + 1}, &la_.IOpt{"offsety", ind + 1}, &la_.IOpt{"n", m - 1})
// Scale so that sqrt(lambda_k' * J * lambda_k) = sqrt(aa*bb).
blas.ScalFloat(lmbda, math.Sqrt(aa*bb), &la_.IOpt{"offset", ind}, &la_.IOpt{"n", m})
// v := (2*v*v' - J) * q
// = 2 * (v'*q) * v' - (J* st/a + zt/b) / (2*c)
blas.ScalFloat(v, 2.0*vq)
v.SetIndex(0, v.GetIndex(0)-(s.GetIndex(ind)/2.0/cc))
blas.AxpyFloat(s, v, 0.5/cc, &la_.IOpt{"offsetx", ind + 1}, &la_.IOpt{"offsety", 1},
&la_.IOpt{"n", m - 1})
blas.AxpyFloat(z, v, -0.5/cc, &la_.IOpt{"offsetx", ind}, &la_.IOpt{"n", m})
// v := v^{1/2} = 1/sqrt(2 * (v0 + 1)) * (v + e)
v0 := v.GetIndex(0) + 1.0
v.SetIndex(0, v0)
blas.ScalFloat(v, 1.0/math.Sqrt(2.0*v0))
// beta[k] *= ( aa / bb )**1/2
bk := beta.GetIndex(k)
示例13: scale2
/*
Evaluates
x := H(lambda^{1/2}) * x (inverse is 'N')
x := H(lambda^{-1/2}) * x (inverse is 'I').
H is the Hessian of the logarithmic barrier.
*/
func scale2(lmbda, x *matrix.FloatMatrix, dims *sets.DimensionSet, mnl int, inverse bool) (err error) {
err = nil
//var minor int = 0
//if ! checkpnt.MinorEmpty() {
// minor = checkpnt.MinorTop()
//}
//fmt.Printf("\n%d.%04d scale2 x=\n%v\nlmbda=\n%v\n", checkpnt.Major(), minor,
// x.ToString("%.17f"), lmbda.ToString("%.17f"))
//if ! checkpnt.MinorEmpty() {
// checkpnt.Check("000scale2", minor)
//}
// For the nonlinear and 'l' blocks,
//
// xk := xk ./ l (inverse is 'N')
// xk := xk .* l (inverse is 'I')
//
// where l is lmbda[:mnl+dims['l']].
ind := mnl + dims.Sum("l")
if !inverse {
blas.TbsvFloat(lmbda, x, &la_.IOpt{"n", ind}, &la_.IOpt{"k", 0}, &la_.IOpt{"lda", 1})
} else {
blas.TbmvFloat(lmbda, x, &la_.IOpt{"n", ind}, &la_.IOpt{"k", 0}, &la_.IOpt{"lda", 1})
}
//if ! checkpnt.MinorEmpty() {
// checkpnt.Check("010scale2", minor)
//}
// For 'q' blocks, if inverse is 'N',
//
// xk := 1/a * [ l'*J*xk;
// xk[1:] - (xk[0] + l'*J*xk) / (l[0] + 1) * l[1:] ].
//
// If inverse is 'I',
//
// xk := a * [ l'*xk;
// xk[1:] + (xk[0] + l'*xk) / (l[0] + 1) * l[1:] ].
//
// a = sqrt(lambda_k' * J * lambda_k), l = lambda_k / a.
for _, m := range dims.At("q") {
var lx, a, c, x0 float64
a = jnrm2(lmbda, m, ind) //&la_.IOpt{"n", m}, &la_.IOpt{"offset", ind})
if !inverse {
lx = jdot(lmbda, x, m, ind, ind) //&la_.IOpt{"n", m}, &la_.IOpt{"offsetx", ind},
//&la_.IOpt{"offsety", ind})
lx /= a
} else {
lx = blas.DotFloat(lmbda, x, &la_.IOpt{"n", m}, &la_.IOpt{"offsetx", ind},
&la_.IOpt{"offsety", ind})
lx /= a
}
x0 = x.GetIndex(ind)
x.SetIndex(ind, lx)
c = (lx + x0) / (lmbda.GetIndex(ind)/a + 1.0) / a
if !inverse {
c *= -1.0
}
blas.AxpyFloat(lmbda, x, c, &la_.IOpt{"n", m - 1}, &la_.IOpt{"offsetx", ind + 1},
&la_.IOpt{"offsety", ind + 1})
if !inverse {
a = 1.0 / a
}
blas.ScalFloat(x, a, &la_.IOpt{"offset", ind}, &la_.IOpt{"n", m})
ind += m
}
//if ! checkpnt.MinorEmpty() {
// checkpnt.Check("020scale2", minor)
//}
// For the 's' blocks, if inverse is 'N',
//
// xk := vec( diag(l)^{-1/2} * mat(xk) * diag(k)^{-1/2}).
//
// If inverse is true,
//
// xk := vec( diag(l)^{1/2} * mat(xk) * diag(k)^{1/2}).
//
// where l is kth block of lambda.
//
// We scale upper and lower triangular part of mat(xk) because the
// inverse operation will be applied to nonsymmetric matrices.
ind2 := ind
sdims := dims.At("s")
for k := 0; k < len(sdims); k++ {
m := sdims[k]
scaleF := func(v, x float64) float64 {
//.........這裏部分代碼省略.........
示例14: sprod
// The product x := (y o x). If diag is 'D', the 's' part of y is
// diagonal and only the diagonal is stored.
func sprod(x, y *matrix.FloatMatrix, dims *sets.DimensionSet, mnl int, opts ...la_.Option) (err error) {
err = nil
diag := la_.GetStringOpt("diag", "N", opts...)
// For the nonlinear and 'l' blocks:
//
// yk o xk = yk .* xk.
ind := mnl + dims.At("l")[0]
err = blas.Tbmv(y, x, &la_.IOpt{"n", ind}, &la_.IOpt{"k", 0}, &la_.IOpt{"lda", 1})
if err != nil {
return
}
//fmt.Printf("Sprod l:x=\n%v\n", x)
// For 'q' blocks:
//
// [ l0 l1' ]
// yk o xk = [ ] * xk
// [ l1 l0*I ]
//
// where yk = (l0, l1).
for _, m := range dims.At("q") {
dd := blas.DotFloat(x, y, &la_.IOpt{"offsetx", ind}, &la_.IOpt{"offsety", ind},
&la_.IOpt{"n", m})
//fmt.Printf("dd=%v\n", dd)
alpha := y.GetIndex(ind)
//fmt.Printf("scal=%v\n", alpha)
blas.ScalFloat(x, alpha, &la_.IOpt{"offset", ind + 1}, &la_.IOpt{"n", m - 1})
alpha = x.GetIndex(ind)
//fmt.Printf("axpy=%v\n", alpha)
blas.AxpyFloat(y, x, alpha, &la_.IOpt{"offsetx", ind + 1}, &la_.IOpt{"offsety", ind + 1},
&la_.IOpt{"n", m - 1})
x.SetIndex(ind, dd)
ind += m
}
//fmt.Printf("Sprod q :x=\n%v\n", x)
// For the 's' blocks:
//
// yk o sk = .5 * ( Yk * mat(xk) + mat(xk) * Yk )
//
// where Yk = mat(yk) if diag is 'N' and Yk = diag(yk) if diag is 'D'.
if diag[0] == 'N' {
// DEBUGGED
maxm := maxdim(dims.At("s"))
A := matrix.FloatZeros(maxm, maxm)
for _, m := range dims.At("s") {
blas.Copy(x, A, &la_.IOpt{"offsetx", ind}, &la_.IOpt{"n", m * m})
for i := 0; i < m-1; i++ { // i < m-1 --> i < m
symm(A, m, 0)
symm(y, m, ind)
}
err = blas.Syr2kFloat(A, y, x, 0.5, 0.0, &la_.IOpt{"n", m}, &la_.IOpt{"k", m},
&la_.IOpt{"lda", m}, &la_.IOpt{"ldb", m}, &la_.IOpt{"ldc", m},
&la_.IOpt{"offsetb", ind}, &la_.IOpt{"offsetc", ind})
if err != nil {
return
}
ind += m * m
}
//fmt.Printf("Sprod diag=N s:x=\n%v\n", x)
} else {
ind2 := ind
for _, m := range dims.At("s") {
for i := 0; i < m; i++ {
// original: u = 0.5 * ( y[ind2+i:ind2+m] + y[ind2+i] )
// creates matrix of elements: [ind2+i ... ind2+m] then
// element wisely adds y[ind2+i] and scales by 0.5
iset := matrix.MakeIndexSet(ind2+i, ind2+m, 1)
u := matrix.FloatVector(y.GetIndexes(iset...))
u.Add(y.GetIndex(ind2 + i))
u.Scale(0.5)
err = blas.Tbmv(u, x, &la_.IOpt{"n", m - i}, &la_.IOpt{"k", 0}, &la_.IOpt{"lda", 1},
&la_.IOpt{"offsetx", ind + i*(m+1)})
if err != nil {
return
}
}
ind += m * m
ind2 += m
}
//fmt.Printf("Sprod diag=T s:x=\n%v\n", x)
}
return
}
示例15: coneqp_solver
//.........這裏部分代碼省略.........
mCopy(q, x)
x.Scal(-1.0)
mCopy(b, y)
blas.Copy(h, z)
checkpnt.Check("00init", 1)
err = f(x, y, z)
if err != nil {
s := fmt.Sprintf("kkt error: %s", err)
err = errors.New("4: Rank(A) < p or Rank([P; G; A]) < n : " + s)
return
}
blas.Copy(z, s)
blas.ScalFloat(s, -1.0)
checkpnt.Check("05init", 1)
nrms = snrm2(s, dims, 0)
ts, _ = maxStep(s, dims, 0, nil)
//fmt.Printf("nrms = %.7f, ts = %.7f\n", nrms, ts)
if ts >= -1e-8*math.Max(nrms, 1.0) {
// a = 1.0 + ts
a := 1.0 + ts
is := make([]int, 0)
// indexes s[:dims['l']]
is = append(is, matrix.MakeIndexSet(0, dims.At("l")[0], 1)...)
// indexes s[indq[:-1]]
is = append(is, indq[:len(indq)-1]...)
ind := dims.Sum("l", "q")
// indexes s[ind:ind+m*m:m+1] (diagonal)
for _, m := range dims.At("s") {
is = append(is, matrix.MakeIndexSet(ind, ind+m*m, m+1)...)
ind += m * m
}
for _, k := range is {
s.SetIndex(k, a+s.GetIndex(k))
}
}
nrmz = snrm2(z, dims, 0)
tz, _ = maxStep(z, dims, 0, nil)
//fmt.Printf("nrmz = %.7f, tz = %.7f\n", nrmz, tz)
if tz >= -1e-8*math.Max(nrmz, 1.0) {
a := 1.0 + tz
is := make([]int, 0)
is = append(is, matrix.MakeIndexSet(0, dims.At("l")[0], 1)...)
is = append(is, indq[:len(indq)-1]...)
ind := dims.Sum("l", "q")
for _, m := range dims.At("s") {
is = append(is, matrix.MakeIndexSet(ind, ind+m*m, m+1)...)
ind += m * m
}
for _, k := range is {
z.SetIndex(k, a+z.GetIndex(k))
}
}
} else {
ix := initvals.At("x")[0]
if ix != nil {
mCopy(&matrixVar{ix}, x)
} else {
x.Scal(0.0)
}
is := initvals.At("s")[0]
if is != nil {
blas.Copy(is, s)