I am currently taking a course in Electrodynamics - really beautiful physics, utterly mathematical. Because of that, I am trying to have a few mathematical barriers as possible. A big focus on the course is Tensor Calculus, and one of the tasks I gave myself is to calculate the divergence of a smooth vector field in cylindrical coordinates.
The divergence of a vector field $\mathbf{A}:\mathbb{R}^n\to\mathbb{R}^n$ as a tensor is defined as (at least as my professor showed): $$\vec{\nabla}\cdot\mathbf{A}=\frac{1}{\sqrt{\det{g}}}\frac{\partial}{\partial x^i}\left(\sqrt{\det{g}}\ A^i\right)$$
Where $i$ is summed from $1$ to $n$, and $g$ is the metric tensor. For example, in cartesian coordinates, since $g$ is the identity tensor we get $\vec{\nabla}\cdot\mathbf{A}=\partial_iA^i$ as expected.
Now, choosing $n=3$ and the following coordinates (cylindrical):
$$\begin{align} x &= r\cos\theta\quad \hat{x}=\cos\theta\hat{r}-\sin\theta\hat\theta \\ y &= r\sin\theta\quad \hat{y}=\sin\theta\hat{r}+\cos\theta\hat\theta \\ z &= z\quad\quad\quad \hat{z}=\hat{z}\end{align}$$
The metric is:
$$g=\left(\begin{matrix} 1 & 0 & 0 \\ 0 & r^2 & 0 \\ 0 & 0 & 1 \end{matrix}\right)$$
Thus $\sqrt{\det{g}}=r$ and then:
$$\vec{\nabla}\cdot\mathbf{A}=\frac{1}{r}\frac{\partial}{\partial r}(rA^r)+\frac{1}{r}\frac{\partial}{\partial \theta}(\color{red}{r}A^\theta)+\frac{\partial}{\partial z}(A^z)$$
Which is almost true. I spent a lot of time trying to understand where is the problem, but to no avail. Something tells me it has to do with the basis covariant vectors transformation, since I didn't use it.
Notice: I know there are other ways (multivariable calculus ways, for instance) to derive the formula (maybe chain rule?) but I am not interested in them. I want to practice on the tensor-calculus-ways to do this.
Edit: Looking at the metric tensor, I realized that $\hat\theta\cdot\hat\theta=r^2$, therefore $\lVert\hat\theta\rVert=r$. In other words, the $\hat\theta$ basis vector is not normalized! Does it mean that I need divide $A^\theta$ by $r$ in order to normalize everything? I am not sure if this is the correct thought, but it if it is - then the red $r$ that shouldn't be in the divergence would cancel out and then everything is okay.
Thank you!