def calc_stiefel(): # Y is a matrix point eta = matrices('eta') Y = stiefels('Y') a = sm.sym_symb('a') al0, al1 = scalars('al0 al1') # scalars are symmetric sm.g_symms.update((al0, al1)) def J(Y, eta): return mat_spfy(t(Y) * eta + t(eta) * Y).doit() def J_adj(Y, a): dY = symbols('dY', commutative=False) return xtrace(trace(mat_spfy(J(Y, dY) * a)), dY) def g(Y, eta): return al0*eta+(al1-al0)*Y*t(Y)*eta def g_inv(Y, eta): return mat_spfy(1/al0*eta + (1/al1-1/al0)*Y*t(Y)*eta) J_giv_J_adj = J(Y, g_inv(Y, J_adj(Y, a))) print(J_giv_J_adj) def proj(Y, omg): jo = mat_spfy(J(Y, omg)) ifactor = al1/Integer(4) return omg - mat_spfy( g_inv(Y, mat_spfy(J_adj(Y, ifactor*jo)))) def r_gradient(Y, omg): return mat_spfy( proj(Y, mat_spfy(g_inv(Y, omg)))) print(r_gradient(Y, eta)) xi, phi = matrices('xi phi') trilinear = mat_spfy(trace(DDR(g(Y, eta), Y, phi) * t(xi))) xcross = xtrace(trilinear, phi) K = (Integer(1)/Integer(2))*(DDR(g(Y, eta), Y, xi) + DDR(g(Y, xi), Y, eta) - xcross) def d_proj(Y, xi, omg): e = matrices('e') r = mat_spfy(proj(Y, e)) expr = DDR(r, Y, xi) return expr.xreplace({e: omg}) dp_xi_eta = d_proj(Y, xi, eta) prK = simplify_stiefel_tangent(proj(Y, g_inv(Y, K)), Y, (xi, eta)) Gamma = mat_spfy( simplify_stiefel_tangent(prK - dp_xi_eta, Y, (xi, eta))) print("This is the Christoffel function:") pprint(Gamma) fY, fYY = matrices('fY fYY') rhess02 = trace(mat_spfy(t(eta)*fYY*xi-Gamma * t(fY))) rhess11_bf_gr = xtrace(rhess02, eta) print("This is the Riemannian Hessian Vector Product:") pprint(rhess11_bf_gr)
def EN_inner(Y, P, Ba, Da, Bb, Db): return trace(mat_spfy(Da * Db) + mat_spfy(Ba * t(Bb)))
def ambient_inner(Y, P, omg_Y, omg_P, xi_Y, xi_P): return mat_spfy( trace( mat_spfy((al0 * omg_Y + (al1 - al0) * Y * t(Y) * omg_Y) * t(xi_Y))) + trace(mat_spfy(bt * inv(P) * omg_P * inv(P) * t(xi_P))))
def base_ambient_inner(omg_Y, omg_P, xi_Y, xi_P): return mat_spfy( trace(mat_spfy(omg_Y * t(xi_Y))) + trace(mat_spfy(omg_P * t(xi_P))))
def EJ_inner(Y, P, a_P, a_YP, b_P, b_YP): return trace(mat_spfy(-a_P * b_P) + mat_spfy(a_YP * t(b_YP)))
def J_adj(Y, a): dY = symbols('dY', commutative=False) return xtrace(trace(mat_spfy(J(Y, dY) * a)), dY)
def calc_pd(): """ For positive definite matrices Y is a matrix point, a positive definite matrix eta is an ambient point, same size with Y not necessarily symmetric or invertible b is a point in E_J. b is antisymmetric """ # eta is an ambient Y = sm.sym_symb('Y') eta = matrices('eta') b = sm.asym_symb('b') def J(Y, eta): return eta - t(eta) def J_adj(Y, a): dY = symbols('dY', commutative=False) return xtrace(trace(mat_spfy(J(Y, dY) * a)), dY) def g(Y, eta): return inv(Y)*eta*inv(Y) def g_inv(Y, eta): return Y*eta*Y J_g_inv_J_adj = J(Y, g_inv(Y, J_adj(Y, b))) pprint(J_g_inv_J_adj) def solve_JginvJadj(Y, a): return Integer(-1)/Integer(4)*inv(Y)*a*inv(Y) def proj(Y, omg): jo = mat_spfy(J(Y, omg)) cJinvjo = solve_JginvJadj(Y, jo) return mat_spfy(omg - mat_spfy( g_inv(Y, mat_spfy(J_adj(Y, cJinvjo))))) def r_gradient(Y, omg): return mat_spfy( proj(Y, mat_spfy(g_inv(Y, omg)))) print(proj(Y, eta)) print(r_gradient(Y, eta)) xi, phi = matrices('xi phi') xcross = xtrace(mat_spfy(trace(DDR(g(Y, eta), Y, phi) * t(xi))), phi) K = (Integer(1)/Integer(2))*( DDR(g(Y, eta), Y, xi) + DDR(g(Y, xi), Y, eta) - xcross) def d_proj(Y, xi, omg): e = matrices('e') r = mat_spfy(proj(Y, e)) expr = DDR(r, Y, xi) return expr.xreplace({e: omg}) dp_xi_eta = d_proj(Y, xi, eta) prK = simplify_pd_tangent(proj(Y, mat_spfy(g_inv(Y, K))), Y, (xi, eta)) Gamma = mat_spfy( simplify_pd_tangent(-dp_xi_eta+prK, Y, (xi, eta))) print("This is the Christoffel function:") pprint(Gamma) fY, fYY = matrices('fY fYY') rhess02 = trace(mat_spfy(t(eta)*fYY*xi-Gamma * t(fY))) rhess11_bf_gr = xtrace(rhess02, eta) print("This is the Riemannian Hessian Vector Product:") pprint(r_gradient(Y, rhess11_bf_gr))