GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - momierr/wigxj: Fast Wigner 3j, 6j, 9j symbols and Clebsch-Gordan coefficient for Python (C extension around wigxjpf, pure-Python fallback)

BranchesTags
Open more actions menu

Latest commit

 

History

24 Commits
24 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
examples
examples
 
 
src/wigxj
src/wigxj
 
 
tests
tests
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
.pre-commit-config.yaml
.pre-commit-config.yaml
 
 
CITATION.cff
CITATION.cff
 
 
LICENSE
LICENSE
 
 
MANIFEST.in
MANIFEST.in
 
 
README.md
README.md
 
 
pixi.lock
pixi.lock
 
 
pyproject.toml
pyproject.toml
 
 
setup.py
setup.py
 
 

Repository files navigation

wigxj

CI License: MIT PyPI

Fast Wigner 3j, 6j, 9j symbols and Clebsch-Gordan coefficients for Python.

  • Fast: a CPython extension around wigxjpf (exact prime-factorisation arithmetic), about 0.3 µs per symbol for small j.
  • Simple: pass spins as plain numbers (0.5, 1, 1.5); no doubled integers, no manual setup.
  • Always works: if the C extension cannot be built, a pure-Python backend with exact integer arithmetic takes over, with the same API.
  • Typed: ships type stubs (py.typed).

Installation

pip install wigxj

Building from source needs a C compiler (GCC, Clang or MSVC). Without one, installation still succeeds and wigxj uses the pure-Python backend. To check which backend is active:

import wigxj

print(wigxj._backend)  # "C" or "pure"

Quick start

from wigxj import clebsch_gordan, wigner3j, wigner6j, wigner9j

wigner3j(1, 1, 2, 0, 0, 0)  # 0.3651... = sqrt(2/15)
wigner6j(1, 1, 0, 1, 1, 0)  # 0.3333... = 1/3
clebsch_gordan(0.5, 0.5, 0.5, -0.5, 0, 0)  # 0.7071... = 1/sqrt(2)  (spin singlet)
wigner9j(1, 1, 1, 1, 1, 1, 1, 1, 0)  # 0.0555... = 1/18

API

All functions take integer or half-integer arguments and return a float. Arguments that violate the selection rules (triangle conditions, m1 + m2 + m3 != 0, ...) return 0.0.

Function Signature Symbol
wigner3j (j1, j2, j3, m1, m2, m3) (j1 j2 j3 / m1 m2 m3)
wigner6j (j1, j2, j3, j4, j5, j6) {j1 j2 j3 / j4 j5 j6}
wigner9j (j1, j2, j3, j4, j5, j6, j7, j8, j9) {j1 j2 j3 / j4 j5 j6 / j7 j8 j9}
clebsch_gordan (j1, m1, j2, m2, j3, m3) ⟨j1 m1 j2 m2 | j3 m3⟩
init (max_j) resize the C backend's tables (see below)

clebsch_gordan uses the Condon-Shortley convention:

$$\langle j_1 m_1 j_2 m_2 | j_3 m_3 \rangle = (-1)^{-j_1+j_2-m_3} \sqrt{2j_3+1} \begin{pmatrix} j_1 & j_2 & j_3 \ m_1 & m_2 & -m_3 \end{pmatrix}$$

Large j

The C backend precomputes tables for j up to 200. Larger values raise a ValueError until you extend the tables:

from wigxj import init, wigner3j

init(500)  # tables for j up to 500
wigner3j(400, 400, 0, 0, 0, 0)

The pure-Python backend has no tables and no limit on j.

Performance

Time per call (best of 5, single machine, Python 3.15):

Symbol wigxj (C) wigxj (pure) sympy.physics.wigner
3j, small (2 2 2 / 1 -1 0) 0.27 µs 17 µs 22 µs
3j, j = 50 (50 50 50 / 0 0 0) 6.2 µs 218 µs 180 µs
6j, small {2 2 2 / 2 2 2} 0.34 µs 8.6 µs 122 µs
6j, j = 30 {30 30 30 / 30 30 30} 5.2 µs 103 µs 427 µs

Why wigxj?

Package When to use it
wigxj Fast floating-point 3j/6j/9j/Clebsch-Gordan with a plain-number API.
pywigxjpf wigxjpf's own binding: same core, but arguments are doubled integers and tables are set up manually.
sympy.physics.wigner You need exact symbolic results (rationals and square roots), and speed does not matter.
wigner You need Wigner d-functions.

Examples

See examples/, e.g. a dipole matrix element calculation.

Development

The development environment is managed with pixi:

pixi run test          # build the C extension, run the tests on both backends
pixi run lint          # ruff
pixi run format-check  # ruff format
pixi run typecheck     # mypy

The tests run every check against both the C and the pure-Python backend, and use Hypothesis to verify orthogonality relations, symmetries and agreement with sympy's exact results on random inputs.

License

wigxj is MIT-licensed (see LICENSE). It bundles wigxjpf 1.15 by H. T. Johansson and C. Forssén, licensed under LGPL-3.0-or-later (see src/wigxj/_ext/wigxjpf/COPYING.LESSER).

If you use wigxj in your research, please cite wigxjpf: H. T. Johansson and C. Forssén, Fast and Accurate Evaluation of Wigner 3j, 6j, and 9j Symbols Using Prime Factorization and Multiword Integer Arithmetic, SIAM J. Sci. Comput. 38, A376 (2016), doi:10.1137/15M1021908.

About

Fast Wigner 3j, 6j, 9j symbols and Clebsch-Gordan coefficient for Python (C extension around wigxjpf, pure-Python fallback)

pypi.org/project/wigxj

Topics

angular-momentumc-extensionclebsch-gordanpythonquantum-physicswigner-symbols

Resources

Readme
MIT license
Activity

Stars

0 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.