|
38 | 38 |
|
39 | 39 | --- |
40 | 40 |
|
41 | | -hyperspherical coordinates and spherical harmonics |
| 41 | +Hyperspherical coordinates in NumPy / PyTorch / JAX. |
42 | 42 |
|
43 | 43 | ## Installation |
44 | 44 |
|
45 | 45 | Install this via pip (or your favourite package manager): |
46 | 46 |
|
47 | | -`pip install ultrasphere` |
| 47 | +```shell |
| 48 | +pip install ultrasphere |
| 49 | +``` |
| 50 | + |
| 51 | +## Usage |
| 52 | + |
| 53 | +### Spherical Coordinates ↔ Cartesian Coordinates |
| 54 | + |
| 55 | +```python |
| 56 | +import ultrasphere as us |
| 57 | +import torch |
| 58 | + |
| 59 | +# 1. specify the structure of spherical coordinates |
| 60 | +c = us.c_spherical() |
| 61 | + |
| 62 | +# 2. get spherical coordinates from euclidean coordinates |
| 63 | +spherical = c.from_euclidean(torch.asarray([1.0, 2.0, 3.0])) |
| 64 | +print(spherical) |
| 65 | +# {'r': tensor(3.7417), 'phi': tensor(1.1071), 'theta': tensor(0.6405)} |
| 66 | + |
| 67 | +# 3. get euclidean coordinates from spherical coordinates |
| 68 | +euclidean = c.to_euclidean(spherical) |
| 69 | +print(euclidean) |
| 70 | +# {0: tensor(1.), 1: tensor(2.0000), 2: tensor(3.)} |
| 71 | +``` |
| 72 | + |
| 73 | +### Using various spherical coordinates |
| 74 | + |
| 75 | +```python |
| 76 | +c = us.polar() # polar coordinates |
| 77 | +c = us.c_spherical() # spherical coordinates |
| 78 | +c = us.standard(3) # bba coordinates |
| 79 | +c = us.standard_prime(4) # b'b'b'a coordinates |
| 80 | +c = us.hopf(3) # ccaacaa coordinates |
| 81 | +c = us.from_branching_types("cbab'a") |
| 82 | +c = us.random(10) |
| 83 | + |
| 84 | +# get the branching types expression |
| 85 | +print(c.branching_types_expression_str) |
| 86 | +# ccabbab'b'ba |
| 87 | +``` |
| 88 | + |
| 89 | +### Drawing spherical coordinates using rooted trees (Vilenkin's method of trees) |
| 90 | + |
| 91 | +#### Python |
| 92 | + |
| 93 | +```python |
| 94 | +import ultrasphere as us |
| 95 | + |
| 96 | +# 1. specify the structure of spherical coordinates |
| 97 | +c = us.random(10) |
| 98 | + |
| 99 | +# 2. draw the rooted tree |
| 100 | +us.draw(c) |
| 101 | +``` |
| 102 | + |
| 103 | +#### CLI |
| 104 | + |
| 105 | +```shell |
| 106 | +ultrasphere "ccabbab'b'ba" |
| 107 | +``` |
| 108 | + |
| 109 | +Output: |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +### Integration over sphere using spherical coordinates |
| 114 | + |
| 115 | +```python |
| 116 | +import ultrasphere as us |
| 117 | +import numpy as np |
| 118 | + |
| 119 | +# 1. specify the structure of spherical coordinates |
| 120 | +c = us.c_spherical() |
| 121 | + |
| 122 | +# 2. integrate a function over the sphere |
| 123 | +integral = us.integrate( |
| 124 | + c, lambda spherical: spherical["theta"] ** 2 * spherical["phi"], False, 10, xp=np |
| 125 | +) |
| 126 | +print(integral) |
| 127 | +# 110.02620812972036 |
| 128 | +``` |
| 129 | + |
| 130 | +### Random sampling |
| 131 | + |
| 132 | +```python |
| 133 | +import ultrasphere as us |
| 134 | +import numpy as np |
| 135 | + |
| 136 | +# 1. specify the structure of spherical coordinates |
| 137 | +c = us.c_spherical() |
| 138 | + |
| 139 | +# 2. sample random points uniformly from the ball |
| 140 | +points_ball = us.random_ball(c, shape=(), xp=np) |
| 141 | +print(points_ball, np.linalg.vector_norm(points_ball)) |
| 142 | +# [ 0.83999061 0.02552206 -0.29185517] 0.8896151114371893 |
| 143 | + |
| 144 | +# 3. sample random points uniformly from the sphere |
| 145 | +points_sphere = us.random_ball(c, shape=(), xp=np, surface=True) |
| 146 | +print(points_sphere, np.linalg.vector_norm(points_sphere)) |
| 147 | +# [-0.68194186 0.71310149 -0.16260864] 1.0 |
| 148 | +``` |
| 149 | + |
| 150 | +#### References |
| 151 | + |
| 152 | +- Barthe, F., Guedon, O., Mendelson, S., & Naor, A. (2005). A probabilistic approach to the geometry of the \ell_p^n-ball. arXiv, math/0503650. Retrieved from https://arxiv.org/abs/math/0503650v1 |
48 | 153 |
|
49 | 154 | ## Contributors ✨ |
50 | 155 |
|
|
0 commit comments