How to check whether a given matrix is in the image of a representation? Let $G$ be a compact simple Lie group, and let $\rho$ be a (faithful, unitary) irreducible representation thereof of $\mathbb K$-dimension $n$, where $\mathbb K=\mathbb C/\mathbb R/\mathbb H$ if $R$ is real/complex/pseudo-real, respectively. It then follows that there is a subgroup of $SU(n)/SO(n)/Sp(n)$, respectively, isomorphic to $G$. One can think of $\rho$ as a map from $G$ to this subgroup.
How can I check whether a given matrix $M\in SU(n)/SO(n)/Sp(n)$ is in the image of $\rho$? In other words, given one such matrix $M$, how can I decide whether there exists some $g\in G$ such that $\rho(g)=M$?
For the sake of concreteness, say $G=G_2$ is the first exceptional simple group, and let $\rho$ be the representation with highest weight $2\omega_2$ (which is real and $27$-dimensional). This means that for any $g\in G_2$, $\rho(g)$ is a $27$-dimensional orthogonal matrix. If I take some arbitrary $27$-dimensional orthogonal matrix $M$, how can I check whether it can be written as $M=\rho(g)$ for some $g\in G_2$?
Note: I am particularly interested in the case where $M$ is diagonal, but I'd be interested in hearing about the general case as well. In the diagonal case, where everything is abelian, and one can essentially focus on a Cartan subalgebra, I assume one can be quite explicit about the image of $\rho$. In the general case, I wouldn't be surprised if one has to work harder.
 A: Suppose that $G$ is a compact Lie group (or, more generally, an algebraic group over some field $F$), ${\mathfrak g}$ is its Lie algebra. You are given a linear representation $\rho: G\to  GL(n, F)$. From this, compute the representation of the Lie algebra $\rho': {\mathfrak g}\to End(F^n)$. In fact, the representation $\rho$ is frequently given in terms of the highest weight of $\rho'$. Now, you check section 4.5 "Computing defining polynomials of an algebraic group in terms of its Lie algebra" in 
W. de Graaf, "Computing with Linear Algebraic Groups", CRC, 2017. 
He describes an algorithm for computing defining polynomial equations $p_i, i=1,...,N$, for $\rho(G)$ in terms of $\rho'({\mathfrak g})$ (more precisely, in terms of a basis for this subalgebra in $End(F^n)$). He even has software (check his webpage https://www.science.unitn.it/~degraaf/) for practical computations of this type. 
Lastly, to verify if the given matrix $A\in GL(n, F)$ belongs to $\rho(G)$, evaluate the polynomials $p_i$ on $A$. 
A: I can answer the diagonal case, at least, where the answer is fairly easy. This assumes that you have everything set up 'nicely', which I'll define. I'm going to do the case of an algebraic group, as that's what I have used in the past, but a real Lie group should be similar.
Assume that you have a maximal torus $T$ of your simple algebraic group $G$ and a representation $\rho$. Assume that the elements of $T$ are sent to diagonal matrices under $\rho$. (This is what I mean by a nice setup.)
Notice that $\rho(g)$ is diagonal if and only if $g\in T$, so it suffices to check that. If $t_1(a_1)\ldots t_n(a_n)$ is an expression for a general element of $T$, then we have $g=t_1(a_1)\ldots t_n(a_n)$. Using $\rho(t_i(a_i))$ one obtains a system of equations, one for each diagonal entry of the equation $\rho(g)=\rho(t_1(a_1)\ldots t_n(a_n))$. Because there are more than $n$ equations (in general) one obtains an overspecified system of equations in the $a_i$, which one may then solve via elimination (or fail to find a solution for).
Of course, if you don't have things set up in this way, then I think in general it becomes incredibly complicated.
Edit: For the single concrete example you give of the $27$-dimensional representation for $G_2$, there is a better way, but this won't be available in general. That representation gives an embedding of $G_2$ into $E_6$ (for algebraic groups only! See comment below), and so stabilizes the $E_6$ trilinear form. Over positive characteristic, so I guess over characteristic $0$ as well, $G_2$ stabilizes exactly a $2$-space of symmetric trilinear forms.
An explicit basis for this 2-space can be constructed by solving a bunch of linear equations, if you can generate a few elements of your group. Note that $G_2$ must be the exact stabilizer of this $2$-space of symmetric trilinear forms, because it is a maximal subgroup of $E_6$, which stabilizes a unique $3$-form.
