# Solving a gradient equation involving Jacobian of orthonormal vectors

Revised to more specific question after realizing some steps

Let $$N(x)$$ be a $$K\times D$$ matrix function of $$x\in \mathbb{R}^D$$ with orthonormal rows, so that $$N(x)N(x)^T=I_{K\times K}$$. Let $$\vec{n}_k$$ be the $$k$$-th row of $$N$$, written as a column vector, and let $$D[\vec{n}_k]$$ be the Jacobian matrix of $$\vec{n}_k$$. Consider the matrix equation $$P(x) \nabla \log p(x) = N(x)^T \vec{q}(x)+\sum_{k=1}^K D[\vec{n}_k(x)]\vec{n}_k(x),$$ where the vector $$\vec{q}(x)$$ has $$r$$-th component $$q^r(x) = \operatorname{Tr}(N(x) D[n_r(x)] N^T(x)),$$ for $$r=1,2,\dotsc, K$$ and where $$P(x)=I_{D\times D} - N(x)^T N(x)$$ is the orthogonal projection of $$\mathbb{R}^D$$ onto the tangent space $$T_x M$$ at $$x$$ of some manifold $$M$$, and the unknown $$p(x)$$ is a nice enough function of $$x\in \mathbb{R}^D$$.

Question: Can we solve this equation explicitly for the unknown function $$p(x)$$?

Some simplifications and easy cases: When $$K=1$$, and $$\operatorname{Tr}(n(x)^T D[n(x)] n(x)) n=0$$ we have $$P(x)\nabla \log p(x) = D[n]n$$, I know how to proceed, see this answer. This more general case is giving me some trouble however.

In general, notice that applying $$P(x)$$ to both sides of the original equation and using the fact that $$P(x)^2=P(x)$$, that $$D[\vec{n}_k(x)]\vec{n}_k(x)$$ already lies on $$T_x M$$, and that (suppresing notation on $$x$$) \begin{align*} PN^T q &= (I-N^TN)N^T q\\ & = N^Tq -N^T N N^Tq\\ & = N^Tq - N^T I_{p\times p} q\\ & = N^Tq-N^Tq=\vec{0} \end{align*} where we have used the orthogonality of $$N$$, i.e. $$NN^T=I_{K\times K}$$, we obtain the equation $$P \nabla \log p = \sum_{k=1}^K D[\vec{n}_k]\vec{n}_k.$$ It follows that $$\nabla \log p = \sum_{k=1}^K \left(D[\vec{n}_k] +c_kI_{D\times D} \right)\vec{n}_k$$ for some scalar functions $$c_k$$ (since $$\{n_1,\dotsc, n_K\}$$ is an orthonormal basis for the kernel of $$P$$.

It remains to show the RHS is a gradient, analogous to the case $$K=1$$. I think it is possible to generalize the argument for $$K=1$$ but the exact details are escaping me at the moment, so I would appreciate any tips or ideas.

Update 2/26/2023: When $$N$$ comes from orthonormalizing a Jacobian matrix $$D[f]$$ with full rank on $$f^{-1}(\{0\})$$ of some smooth function, and we also assume that the rows of $$D[f]$$ are already orthogonal then we can directly use the previous method linked above with the appropriate changes. We obtain in this case that $$\sum_{k=1}^K D[\vec{n}_k]\vec{n}_k = \frac12 \nabla \left[\sum_{k=1}^K H_k\right] - \sum_{k=1}^K \mu_k \vec{n}_k$$ and then we get that $$\nabla \log p(x) = \nabla \left[\sum_{k=1}^K \frac12 H_k\right]$$ $$= \nabla \left[\log \prod_{k=1}^K \|\nabla f_k\|\right],$$ hence $$p(x) = \prod_{k=1}^K \|\nabla f_k(x)\|$$. Here $$H_k = 2\log \|\nabla f_k\|$$ and $$\mu_k = \frac{1}{\|\nabla f_k\|} n_k^T (\nabla^2 f_k)n_k,$$ and $$n_k = \nabla f_k / \|\nabla f_k\|$$. I have omitted a bit of the details but if anyone wants to see them I can add them in later when I have more time.

In general, my conjecture is the solution is $$p(x) = \sqrt{\det J_f(x) J_f(x)^T}$$, where $$J_f := D[f]$$.