示例#1
0
func syrkTest(t *testing.T, C, A *matrix.FloatMatrix, flags Flags, vlen, nb int) bool {
	//var B0 *matrix.FloatMatrix
	P := A.Cols()
	S := 0
	E := C.Rows()
	C0 := C.Copy()

	trans := linalg.OptNoTrans
	if flags&TRANSA != 0 {
		trans = linalg.OptTrans
		P = A.Rows()
	}
	uplo := linalg.OptUpper
	if flags&LOWER != 0 {
		uplo = linalg.OptLower
	}

	blas.SyrkFloat(A, C0, 1.0, 1.0, uplo, trans)
	if A.Rows() < 8 {
		//t.Logf("..A\n%v\n", A)
		t.Logf("  BLAS C0:\n%v\n", C0)
	}

	Ar := A.FloatArray()
	Cr := C.FloatArray()
	DSymmRankBlk(Cr, Ar, 1.0, 1.0, flags, C.LeadingIndex(), A.LeadingIndex(),
		P, S, E, vlen, nb)
	result := C0.AllClose(C)
	t.Logf("   C0 == C: %v\n", result)
	if A.Rows() < 8 {
		t.Logf("  DMRank C:\n%v\n", C)
	}
	return result
}
示例#2
0
文件: gp.go 项目: hrautila/cvx
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
}
示例#3
0
文件: acent.go 项目: hrautila/go.opt
// 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 := matrix.Minus(b, matrix.Times(A, x)).Inv()
		g := matrix.Times(A.Transpose(), 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.SetColumn(i, d)
		}

		// Function mul creates element wise product of matrices.
		Asc := matrix.Mul(Dmn, A)
		blas.SyrkFloat(Asc, H, 1.0, 0.0, linalg.OptTrans)

		// Newton step is v = H^-1 * g.
		v := g.Copy().Scale(-1.0)
		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
}