# Correct order of taking dot product and derivatives in spherical coordinates

I tried to derive definition of divergence in spherical coordinates from gradient and got: $${\vec \nabla \cdot \vec A=\bigg (\frac{\partial}{\partial r}\hat r+\frac{1}{r}\frac{\partial}{\partial \theta}\hat \theta+\frac{1}{r\sin\theta}\frac{\partial}{\partial\phi}\hat\phi\bigg)\cdot\bigg(A_{r}\hat r+A_{\theta}\hat \theta+A_{\phi}\hat\phi\bigg)}$$ $${=\frac{\partial A_{r}}{\partial r}+\frac{1}{r}\frac{\partial A_{\theta}}{\partial\theta}+\frac{1}{r\sin\theta}\frac{\partial A_{\phi}}{\partial\phi}}$$ But according to Wikipedia and other sources this be: $${\vec \nabla \cdot \vec A =\frac{1}{r^2} \frac{\partial (r^2 A_{r})}{\partial r}+\frac{1}{r\sin\theta}\frac{\partial (A_{\theta}\sin\theta)}{\partial\theta}+\frac{1}{r\sin\theta}\frac{\partial A_{\phi}}{\partial\phi}}$$ Why should we first take the derivative and then the dot product? Why do we take first dot product when we have cartesian coordinates? I think I read that it is because vectors themselves are functions of the coordinates but I do not understand what are consequences of this.

your expression: $${\vec \nabla \cdot \vec A=\bigg (\frac{\partial}{\partial r}\hat r+\frac{1}{r}\frac{\partial}{\partial \theta}\hat \theta+\frac{1}{r\sin\theta}\frac{\partial}{\partial\phi}\hat\phi\bigg)\cdot\bigg(A_{r}\hat r+A_{\theta}\hat \theta+A_{\phi}\hat\phi\bigg)} \tag{1}$$ may be better written: $${\vec \nabla \cdot \vec A=\bigg (\hat r\frac{\partial}{\partial r}+\frac{\hat \theta}{r}\frac{\partial}{\partial \theta}+\frac{\hat\phi}{r\sin\theta}\frac{\partial}{\partial\phi}\bigg)\cdot\bigg(A_{r}\hat r+A_{\theta}\hat \theta+A_{\phi}\hat\phi\bigg)} \tag{2}$$ since the role of the unit vectors in the first parenthesis is static - they give the coordinate frame in which the components of the nabla operator are expressed at this particular point.
on the other hand, in the second parenthesis, which is the operand, the variation of $\hat r, \hat \theta$ and $\hat \phi$ with $r, \theta$ and $\phi$ must obviously be taken into account when computing the derivatives.
from the relations: $$\hat r = \hat x \sin \theta\cos \phi + \hat y \sin \theta \sin \phi - \hat z \cos \theta \\ \hat \theta = \hat x \cos \theta\cos \phi + \hat y \cos \theta \sin \phi - \hat z \sin \theta \\ \hat \phi = -\hat x \sin \phi + \hat y \cos \phi$$ may be computed the following non-zero derivatives of the curvilinear coordinate unit vectors, allowing the divergence to be evaluated from formula (2) above: $$\frac{\partial \hat r}{\partial \theta} = \hat \theta$$ $$\frac{\partial \hat r}{\partial \phi} = \hat \phi \sin \theta$$ $$\frac{\partial \hat \phi}{\partial \phi} = -(\hat r \sin \theta + \hat \theta \cos \theta)$$ $$\frac{\partial \hat \theta}{\partial \theta} =- \hat r$$ $$\frac{\partial \hat \theta}{\partial \phi} =\hat \phi \cos \theta$$