Esempio n. 1
0
File: gp.go Progetto: hrautila/cvx
func (gp *gpConvexProg) F1(x *matrix.FloatMatrix) (f, Df *matrix.FloatMatrix, err error) {
	f = nil
	Df = nil
	err = nil
	f = matrix.FloatZeros(gp.mnl+1, 1)
	Df = matrix.FloatZeros(gp.mnl+1, gp.n)
	y := gp.g.Copy()
	blas.GemvFloat(gp.F, x, y, 1.0, 1.0)

	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 = exp(y[start:stop] - ymax)
		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})
	}
	return
}
Esempio n. 2
0
File: gp.go Progetto: 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
}