Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues related to factorial2 function #303

Closed
yingxingcheng opened this issue Dec 19, 2023 · 2 comments
Closed

Issues related to factorial2 function #303

yingxingcheng opened this issue Dec 19, 2023 · 2 comments
Assignees

Comments

@yingxingcheng
Copy link

yingxingcheng commented Dec 19, 2023

The issue with the factorial2 function persists, as the GitHub test workflow fails.

Additionally, the second argument of factorial2 is of bool type. The following usage of factorial2 is quite confusing:

iodata/iodata/overlap.py

Lines 225 to 243 in 6b9c6e0

class GaussianOverlap:
"""Gaussian Overlap Class."""
def __init__(self, n_max):
"""Initialize class.
Parameters
----------
n_max : int
Maximum angular momentum.
"""
self.binomials = [
[scipy.special.binom(n, i) for i in range(n + 1)] for n in range(n_max + 1)
]
facts = [factorial2(m, 2) for m in range(2 * n_max)]
facts.insert(0, 1)
self.facts = np.array(facts)

@PaulWAyers
Copy link
Member

PaulWAyers commented Jan 11, 2024

Yes, the documentation is embarrassingly bad.

It took me a bit of time to remember how the integral works. It would have helped if we would have documented it. The integral is

$$ \int_{-\infty}^{\infty} (x-x_1)^{n_1} (x-x_2)^{n_2} e^{-\alpha_1 (x-x_1)^2} e^{-\alpha_2 (x-x_2)^2} dx $$

but before this funtion is invoked the Gaussian product rule has been invoked, and the utility variable

$$ \mathtt{two}\textunderscore\mathtt{at} = 2\frac{\alpha_1 x_1 + \alpha_2 x_2}{\alpha_1 + \alpha_2} $$

was defined. Then the constant prefactor

$$ \sqrt{\frac{2\pi}{\mathtt{two}\textunderscore\mathtt{at}}} \exp\left(-\frac{\alpha_1 \alpha_2}{\alpha_1 + \alpha_2}(x_1-x_2)^2 \right) $$

is factored out. (At least these utility variables are relatively clearly). So this function actually computes

$$ \sqrt{\frac{\mathtt{two}\textunderscore\mathtt{at}}{2\pi}}\int_{-\infty}^{\infty} (x-x_1)^{n_1} (x-x_2)^{n_2} e^{-\tfrac{1}{2}\mathtt{two}\textunderscore\mathtt{at} \cdot x^2} dx $$

Then, using the standard Gaussian integral formula,

$$ \int_{-\infty}^{\infty} x^{i+j} e^{-\tfrac{1}{2}\mathtt{two}\textunderscore\mathtt{at} \cdot x^2} dx = \begin{cases} \frac{(i+j-1)!!}{\mathtt{two}\textunderscore\mathtt{at}^{i+j}}\sqrt{\frac{2\pi}{\mathtt{two}\textunderscore\mathtt{at}}}, & (i+j)\mod{2} = 0\\ 0 & (i+j)\mod{2} = 1 \end{cases} $$

We have:

$$ \sqrt{\frac{\mathtt{two}\textunderscore\mathtt{at}}{2\pi}}\int_{-\infty}^{\infty} (x-x_1)^{n_1} (x-x_2)^{n_2} e^{-\tfrac{1}{2}\mathtt{two}\textunderscore\mathtt{at} \cdot x^2} dx = \sum_{i=0}^{n_1} \sum_{{j=0} \atop {(i+j) \mod 2 = 0}}^{n_2} \binom{n_1}{i} \binom{n_2}{j} x_1^{n_1-i} x_2^{n_2 - j} \frac{(i+j-1)!!}{\mathtt{two}\textunderscore\mathtt{at}^{i+j}} $$

Realistically, this is overcomplicated: IMO some of the things we did to "save evaluations in the inner loop" were probably not worth the loss in readability, and at least demanded more documentation. It also seems to me that the double-factorial we are using is off by one, but I can't imagine how we managed to pass the old tests if that is the case, so I must be missing something.

@tovrstra
Copy link
Member

Sorry, forgot to mention that this is fixed in #319

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants