def trace2(self,W=None):
"""
Compute the trace of A*A (Note this is the square of Frob norm, since A is symmetic).
If the weight W is provided, it will compute the trace of (AW)^2.
This is equivalent to
tr_W(A) = \sum_i lambda_i^2,
where lambda_i are the generalized eigenvalues of
A x = lambda W^-1 x.
Note if U is a W-orthogonal matrix then
tr_W(A) = \sum_i D(i,i)^2.
"""
if W is None:
UtU = np.dot(self.U.T, self.U)
dUtU = self.d[:,None] * UtU #diag(d)*UtU.
tr2 = np.sum(dUtU*dUtU)
else:
WU = np.zeros(self.U.shape, dtype=self.U.dtype)
u, wu = Vector(), Vector()
W.init_vector(u,1)
W.init_vector(wu,0)
for i in range(self.U.shape[1]):
u.set_local(self.U[:,i])
W.mult(u,wu)
WU[:,i] = wu.get_local()
UtWU = np.dot(self.U.T, WU)
dUtWU = self.d[:,None] * UtWU #diag(d)*UtU.
tr2 = np.power(np.linalg.norm(dUtWU),2)
return tr2
评论列表
文章目录