{"id":17222790,"url":"https://github.com/cheind/torch-spherical-harmonics","last_synced_at":"2025-08-03T21:38:52.559Z","repository":{"id":66174999,"uuid":"561454140","full_name":"cheind/torch-spherical-harmonics","owner":"cheind","description":"Real Spherical Harmonics for PyTorch","archived":false,"fork":false,"pushed_at":"2022-12-05T12:39:50.000Z","size":2953,"stargazers_count":22,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-14T00:26:49.215Z","etag":null,"topics":["pytorch","spherical-harmonics","symbolic"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cheind.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-11-03T18:15:41.000Z","updated_at":"2025-04-03T14:49:15.000Z","dependencies_parsed_at":"2023-02-21T06:01:15.542Z","dependency_job_id":null,"html_url":"https://github.com/cheind/torch-spherical-harmonics","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/cheind/torch-spherical-harmonics","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheind%2Ftorch-spherical-harmonics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheind%2Ftorch-spherical-harmonics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheind%2Ftorch-spherical-harmonics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheind%2Ftorch-spherical-harmonics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cheind","download_url":"https://codeload.github.com/cheind/torch-spherical-harmonics/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheind%2Ftorch-spherical-harmonics/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268618172,"owners_count":24279241,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-03T02:00:12.545Z","response_time":2577,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["pytorch","spherical-harmonics","symbolic"],"created_at":"2024-10-15T04:06:22.120Z","updated_at":"2025-08-03T21:38:52.547Z","avatar_url":"https://github.com/cheind.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## **torch-spherical-harmonics**\n\nReal spherical harmonics (RSH) in Cartesian form for PyTorch. The resulting source code is auto-generated by converting optimized symbolic RSH expressions to PyTorch.\n\nThe following plot shows the first real spherical harmonics $Y_{nm}$ of degree $n \u003c 6$ and order $-n \\le m \\le n$ as a function of polar coordinates $\\theta \\in [0,\\pi]$ and $\\phi \\in [0,2\\pi]$.\n\n![](etc/rsph_theta_phi.png?raw=true)\n\n## Properties\n\nThe RSH functions provided in this package are ortho-normalized over the surface of the unit sphere. That is\n\n$$\n\\int\\limits_0^\\pi\\int\\limits_0^{2\\pi}Y_{nm}(\\mathbf r(\\theta,\\phi))Y_{n'm'}(\\mathbf r(\\theta,\\phi)) \\left\\lVert\t \\frac{\\partial\\mathbf{r}}{\\partial\\theta}\\times\\frac{\\partial\\mathbf{r}}{\\partial\\phi} \\right\\rVert \\thinspace d \\phi \\thinspace d \\theta = \\delta_{nn'}\\delta_{mm'},\n$$\n\nwith\n\n$$\n\\mathbf r(\\theta,\\phi) = \\sin\\theta\\cos\\phi\\mathbf{\\hat i} + \\sin\\theta\\sin\\phi\\mathbf{\\hat j} + \\cos\\theta\\mathbf{\\hat k},\n$$\n\nand\n\n$$\n\\left\\lVert \\frac{\\partial\\mathbf{r}}{\\partial\\theta} \\times \\frac{\\partial\\mathbf{r}}{\\partial\\phi} \\right\\lVert = \\sin\\theta.\n$$\n\n_Note that due to some rendering issue, you might not see the double vertical bars around the cross product._\n\n## Usage\n\nTo compute all $Y_{nm}$ values up to degree 3 use:\n\n```python\n\nimport torch\nfrom torchsh import rsh_cart_3\n\nxyz = ...             # tensor (N,...,3) of unit-sphere points\nynm = rsh_cart_3(xyz) # tensor (N,...,16) of Ynm values\n                      # with Ynm at index `n*(n+1) + m`\n```\n\nThis relies on pre-generated RSH functions, which `torchsh` contains up to degree 8. They all follow the same naming convention `rsh_cart_{degree}`.\n\nIf you do not want to add a new library dependency, you may just as well just include [`torchsh/rsh.py`](./torchsh/rsh.py) in your project, which requires only `torch` to be installed.\n\n## Code Generation\n\nWe use `sympy` to generate RSH expressions in Cartesian form reyling on the [Herglotzian](https://en.wikipedia.org/wiki/Spherical_harmonics#Separated_Cartesian_form) definition. These expressions are simplified and transformed into Python/PyTorch functions using a code template and the string engine `mako`. In the tests we use `sympy.Znm` to verify our numerical results.\n\nWe initially intended to use `sympy.Znm` directly for code generation, by substituting polar coordinate definitions with respective Cartesian ones, but found that substitution did not work all of the times. This is the reason we switched to a different generating definition.\n\nCode can be generated via\n\n```\n$ python -m torchsh.symbolic.codegen --help\n```\n\nwhich requires all `dev-requirements.txt` to be installed. To run the unit tests call\n\n```\n$ pytest\n```\n\n## References\n\nThe basic idea for using `sympy` to generate code for RSH functions is taken from https://nvlabs.github.io/instant-ngp, where it is used to generate Cuda code for forward and backward passes.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcheind%2Ftorch-spherical-harmonics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcheind%2Ftorch-spherical-harmonics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcheind%2Ftorch-spherical-harmonics/lists"}