# Molecular integrals in spherical harmonics?

I have been trying to write a basic HF code that can compute the MOs and total energy of a molecule. I followed the following reference for overlap and kinetic integrals. During a conversation with a developer of Libint, I was told that I should try implementing spherical harmonics into the code. I found this paper for the conversion from cartesian to spherical harmonics. Any beginner or intermediate level pointers on how the integrals are computed in spherical harmonics would be much appreciated. I am not looking for specific implementation details, but for the general algorithm.

• +1 but it's not clear to me where exactly your problem lies? You know what spherical harmonics are, but you don't know how to write a code to do integrals that involve them? It also might help for us to understand better the underlying motivation for this code. Is it meant to be faster than libcint (the GitHub repository talks a lot about parallelization)? I'll also mention this question: mattermodeling.stackexchange.com/q/9595/5 which was asked by a student that was writing an HF code for their high school project. That code is now complete: github.com/lela2011/Maturaarbeit May 9, 2023 at 17:26
• The motivation behind writing the code is twofold: (i) I would eventually need to teach students how these codes work (if I land a faculty position), and (ii) Even otherwise, it is good to know how the codes that we routinely use work. To answer the question about "is it meant to be faster than libcint?" No, I just want to know how these integrals are implemented in spherical harmonics. May 9, 2023 at 17:51
• Thanks, this answers my question about the motivation behind writing the code, but not the part about what your problem is. Right now I don't know how to answer the question because I don't know where exactly the problem lies! I do agree that there's value in knowing how a code works, but writing a full fledged software to do the same thing as an existing code, isn't the only way to learn how the existing code works. Writing a code for electron repulsion integrals is more time-consuming than it is valuable in my opinion. It's re-inventing a wheel rather than a car. But tell us how we can help! May 9, 2023 at 18:10
• Prof. Dattani, I agree that I would be reinventing the wheel if I try implementing it. Leaving the part of me implementing the routines, can you guide me to some pointers on how the integrals are computed in spherical harmonics? May 9, 2023 at 18:25
• Yes, I think the question needs to be more specific though! May 9, 2023 at 18:26

Molecular integrals aren't typically computed in spherical harmonics. What you do instead is to compute the integrals in the Cartesian representation, where they are separable, and then transform to the spherical representation as quickly as possible your algorithm allows, since there are $$n_c=\lambda(\lambda+1)/2$$ cartesians but only $$n_s=2\lambda+1$$ spherical harmonics for angular momentum $$\lambda$$.
What this means is that when computing the $$(pq|rs)$$ integrals, you convert an index to the spherical basis as soon as possible, so that the recursions for the remaining indices only deal with $$n_s$$ components for each transformed index instead of $$n_c$$ components for untransformed indices.