---
title: "Computing the Wigner 9j Symbols Efficiently"
author: "Thomas (Tom) C. Gorordo"
published: false

references:
- type: book
  id: GrahamKnuthPatashnik1994ConcreteMathematics2e
  author:
  - family: Graham
    given: Ronald L.
  - family: Knuth
    given: Donald E.
  - family: Patashnik
    given: Oren
  title: 'Concrete Mathematics: A Foundation for Computer Science'
  title-short: 'Concrete Mathematics'

---

As a test post, here's a transcription of some notes I have laying around from writing an [`add9j` PR](https://github.com/tgorordo/WignerSymbols.jl)
for [`Jutho/WignerSymbols.jl`](https://github.com/Jutho/WignerSymbols).

## Wigner Symbols Formulae

The Wigner \(3x\text{j}\) symbols are all exactly representable in the form

\[W_{3x} = \frac{n\sqrt{s}}{q}\hspace{1cm} n,s,q\in\mathbb{Z}\]

So, if one can use exact integer arithmetic in computing the three components,
a computer implementation of the symbols should be able to incur at-worst
typical floating point root and ratio errors.

This requires the usage of formulae that are amenible to such integer manipulations.

### \(3j\) Formula

Let \(\delta(j_1, j_2, j_3) = (j_1 \leq j_2 + j_3)\bigwedge (j_2 \leq j_1 + j_3)\bigwedge (j_3 \leq j_1 + j_2)\bigwedge(j_1 + j_2 + j_3\in\mathbb{Z})\)
for \(j_i\in\frac{1}{2}\mathbb{Z}\) a half-integer be called the "triangle condition". If not satisfied, a symbol is zero.

The \(3j\) symbol can be computed according to:


### \(6j\) Formula
The \(6j\) symbol, similarly, can be computed as:

Define the Wei square brackets purely in terms of binomial coefficients by

### \(9j\) Formula
The \(9j\) formula has a few additional pieces.


One might have some fun manipulating some of these forms using the tricks in
[@GrahamKnuthPatashnik1994ConcreteMathematics2e].

## Calculation and Tabulation

For fast, exact evaluation, the main integer-arithmetic idea is to simplify the evaluation 
of the various sums of ratios of (potentially large) factorials by using prime factorizations
of those factorials to reduce ratios by an LCD ad extract common factors across the sums.
Remaining arithmetic can be done via a big integer implementation.

The core of this approach uses that prime factorizations of factorials are conveniently
obtained recursively (and thus, to tabulate). The crux is:

### Euclid's Lemma
> *If \( p | ab\) for prime \(p\), then \(p|a\) or \(p|b\).*

> *\(\implies\) If prime \(p|n!\) then \(p|k\) for \(k\leq n\) one of the factors.*

More specifically, *Legendre's Theorem* says:

\[\sharp_p(n!) = \sum_{k=1} \sharp_p(k) = \sum_{k=1}^{\lfloor\log_p(n)\rfloor} \lfloor\frac{n}{p^k}\rfloor\]

where \(\sharp_p(k)\) gives the number of times $p$ divides \(k\).

