differt 0.0.9__tar.gz → 0.0.11__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. differt-0.0.11/.gitignore +22 -0
  2. differt-0.0.11/LICENSE.md +1 -0
  3. {DiffeRT-0.0.9 → differt-0.0.11}/PKG-INFO +71 -61
  4. {DiffeRT-0.0.9/python → differt-0.0.11}/differt/__init__.py +1 -0
  5. differt-0.0.11/differt/em/__init__.py +1 -0
  6. differt-0.0.11/differt/em/constants.py +12 -0
  7. differt-0.0.11/differt/em/special.py +242 -0
  8. differt-0.0.11/differt/em/utd.py +88 -0
  9. differt-0.0.11/differt/em/utils.py +83 -0
  10. {DiffeRT-0.0.9/python → differt-0.0.11}/differt/geometry/triangle_mesh.py +41 -4
  11. {DiffeRT-0.0.9/python → differt-0.0.11}/differt/geometry/utils.py +44 -3
  12. {DiffeRT-0.0.9/python → differt-0.0.11}/differt/plotting/__init__.py +11 -5
  13. differt-0.0.11/differt/plotting/_core.py +444 -0
  14. {DiffeRT-0.0.9/python → differt-0.0.11}/differt/plotting/_utils.py +127 -25
  15. {DiffeRT-0.0.9/python → differt-0.0.11}/differt/rt/fermat.py +2 -1
  16. {DiffeRT-0.0.9/python → differt-0.0.11}/differt/rt/image_method.py +24 -5
  17. {DiffeRT-0.0.9/python → differt-0.0.11}/differt/rt/utils.py +123 -15
  18. {DiffeRT-0.0.9/python → differt-0.0.11}/differt/scene/__init__.py +1 -0
  19. differt-0.0.11/differt/scene/sionna.py +141 -0
  20. differt-0.0.11/differt/scene/triangle_scene.py +112 -0
  21. {DiffeRT-0.0.9/python → differt-0.0.11}/differt/utils.py +3 -2
  22. {DiffeRT-0.0.9/python → differt-0.0.11}/differt/version.py +2 -1
  23. differt-0.0.11/pyproject.toml +85 -0
  24. DiffeRT-0.0.9/Cargo.lock +0 -1185
  25. DiffeRT-0.0.9/Cargo.toml +0 -38
  26. DiffeRT-0.0.9/LICENSE.md +0 -21
  27. DiffeRT-0.0.9/README.md +0 -107
  28. DiffeRT-0.0.9/pdm.lock +0 -3516
  29. DiffeRT-0.0.9/pyproject.toml +0 -164
  30. DiffeRT-0.0.9/python/differt/_core/__init__.pyi +0 -5
  31. DiffeRT-0.0.9/python/differt/_core/geometry/__init__.pyi +0 -3
  32. DiffeRT-0.0.9/python/differt/_core/geometry/triangle_mesh.pyi +0 -9
  33. DiffeRT-0.0.9/python/differt/_core/rt/__init__.pyi +0 -3
  34. DiffeRT-0.0.9/python/differt/_core/rt/graph.pyi +0 -64
  35. DiffeRT-0.0.9/python/differt/_core/rt/utils.pyi +0 -14
  36. DiffeRT-0.0.9/python/differt/plotting/_core.py +0 -226
  37. DiffeRT-0.0.9/python/differt/py.typed +0 -0
  38. DiffeRT-0.0.9/python/differt/rt/graph.py +0 -27
  39. DiffeRT-0.0.9/python/differt/scene/scene.py +0 -1
  40. DiffeRT-0.0.9/src/geometry/mod.rs +0 -9
  41. DiffeRT-0.0.9/src/geometry/triangle_mesh.rs +0 -112
  42. DiffeRT-0.0.9/src/lib.rs +0 -15
  43. DiffeRT-0.0.9/src/rt/graph.rs +0 -968
  44. DiffeRT-0.0.9/src/rt/mod.rs +0 -13
  45. DiffeRT-0.0.9/src/rt/utils.rs +0 -62
  46. DiffeRT-0.0.9/static/favicon.png +0 -0
  47. DiffeRT-0.0.9/static/logo.svg +0 -113
  48. DiffeRT-0.0.9/static/logo_250px.png +0 -0
  49. {DiffeRT-0.0.9/python → differt-0.0.11}/differt/geometry/__init__.py +0 -0
  50. {DiffeRT-0.0.9/python → differt-0.0.11}/differt/rt/__init__.py +0 -0
@@ -0,0 +1,22 @@
1
+ # Python build files and cache
2
+
3
+ *.pyc
4
+ *.so
5
+ *.egg-info
6
+
7
+ dist/
8
+ target/
9
+
10
+ # Docs
11
+
12
+ docs/build/
13
+ docs/source/reference/*.rst
14
+
15
+ # Sionna data
16
+ differt/src/differt/scene/scenes/
17
+
18
+ # Notebooks
19
+
20
+ .ipynb_checkpoints/
21
+
22
+ # Tools
@@ -0,0 +1 @@
1
+ ../LICENSE.md
@@ -1,62 +1,56 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: DiffeRT
3
- Version: 0.0.9
3
+ Version: 0.0.11
4
+ Summary: Differentiable Ray Tracing Toolbox for Radio Propagation Simulations
5
+ Project-URL: Changelog, https://github.com/jeertmans/DiffeRT/releases
6
+ Project-URL: Documentation, https://eertmans.be/DiffeRT
7
+ Project-URL: Founding, https://github.com/sponsors/jeertmans
8
+ Project-URL: Homepage, https://github.com/jeertmans/DiffeRT
9
+ Project-URL: Repository, https://github.com/jeertmans/DiffeRT
10
+ Author-email: Jérome Eertmans <jeertmans@icloud.com>
11
+ License-Expression: MIT
12
+ License-File: LICENSE.md
13
+ Keywords: differentiable,jax,propagation,radio,ray tracing
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Operating System :: OS Independent
4
16
  Classifier: Programming Language :: Python :: 3
5
17
  Classifier: Programming Language :: Python :: 3.9
6
18
  Classifier: Programming Language :: Python :: 3.10
7
19
  Classifier: Programming Language :: Python :: 3.11
8
20
  Classifier: Programming Language :: Python :: 3.12
9
- Classifier: License :: OSI Approved :: MIT License
10
- Classifier: Operating System :: OS Independent
11
21
  Classifier: Topic :: Scientific/Engineering
12
- Classifier: Programming Language :: Rust
13
- Classifier: Programming Language :: Python :: Implementation :: CPython
14
- Classifier: Programming Language :: Python :: Implementation :: PyPy
15
- Requires-Dist: beartype >=0.17.2
16
- Requires-Dist: equinox >=0.11.2
17
- Requires-Dist: jax >=0.4.20
18
- Requires-Dist: jaxtyping >=0.2.24
19
- Requires-Dist: numpy >=1.26.1
20
- Requires-Dist: optax >=0.1.7
21
- Requires-Dist: typing-extensions >=4.9.0 ; python_version < '3.10'
22
- Requires-Dist: differt[jupyter,matplotlib,plotly,vispy,vispy-backend] ; extra == 'all'
23
- Requires-Dist: differt[all] ; extra == 'docs'
24
- Requires-Dist: myst-nb >=0.17.2 ; extra == 'docs'
25
- Requires-Dist: pillow >=10.1.0 ; extra == 'docs'
26
- Requires-Dist: sphinx >=6, <7 ; extra == 'docs'
27
- Requires-Dist: sphinx-autodoc-typehints >=1.23 ; extra == 'docs'
28
- Requires-Dist: sphinx-book-theme >=1.0.1 ; extra == 'docs'
29
- Requires-Dist: sphinx-copybutton >=0.5.2 ; extra == 'docs'
30
- Requires-Dist: sphinx-design >=0.5.0 ; extra == 'docs'
31
- Requires-Dist: sphinx-remove-toctrees >=0.0.3 ; extra == 'docs'
32
- Requires-Dist: sphinxext-opengraph >=0.9.0 ; extra == 'docs'
33
- Requires-Dist: sphinxcontrib-apidoc >=0.4.0 ; extra == 'docs'
34
- Requires-Dist: sphinxcontrib-bibtex >=2.5.0 ; extra == 'docs'
35
- Requires-Dist: notebook >=7 ; extra == 'jupyter'
36
- Requires-Dist: jupyter-rfb >=0.4.2 ; extra == 'jupyter'
37
- Requires-Dist: ipympl ; extra == 'jupyter'
38
- Requires-Dist: matplotlib >=3.8.1 ; extra == 'maplotlib'
39
- Requires-Dist: plotly >=5.18.0 ; extra == 'plotly'
40
- Requires-Dist: vispy >=0.14.1 ; extra == 'vispy'
41
- Requires-Dist: PyQt6 >=6.6.1 ; extra == 'vispy-backend'
22
+ Requires-Python: >=3.9
23
+ Requires-Dist: beartype>=0.17.2
24
+ Requires-Dist: differt-core==0.0.11
25
+ Requires-Dist: equinox>=0.11.2
26
+ Requires-Dist: jax>=0.4.20
27
+ Requires-Dist: jaxtyping>=0.2.24
28
+ Requires-Dist: numpy>=1.26.1
29
+ Requires-Dist: optax>=0.1.7
30
+ Requires-Dist: requests>=2.31.0
31
+ Requires-Dist: tqdm>=4.66.2
32
+ Requires-Dist: typing-extensions>=4.9.0; python_version < '3.11'
42
33
  Provides-Extra: all
43
- Provides-Extra: docs
34
+ Requires-Dist: ipympl; extra == 'all'
35
+ Requires-Dist: jupyter-rfb>=0.4.2; extra == 'all'
36
+ Requires-Dist: matplotlib>=3.8.1; extra == 'all'
37
+ Requires-Dist: notebook>=7; extra == 'all'
38
+ Requires-Dist: plotly>=5.18.0; extra == 'all'
39
+ Requires-Dist: pyqt6>=6.6.1; extra == 'all'
40
+ Requires-Dist: vispy>=0.14.1; extra == 'all'
44
41
  Provides-Extra: jupyter
45
- Provides-Extra: maplotlib
42
+ Requires-Dist: ipympl; extra == 'jupyter'
43
+ Requires-Dist: jupyter-rfb>=0.4.2; extra == 'jupyter'
44
+ Requires-Dist: notebook>=7; extra == 'jupyter'
45
+ Provides-Extra: matplotlib
46
+ Requires-Dist: matplotlib>=3.8.1; extra == 'matplotlib'
46
47
  Provides-Extra: plotly
48
+ Requires-Dist: plotly>=5.18.0; extra == 'plotly'
47
49
  Provides-Extra: vispy
50
+ Requires-Dist: vispy>=0.14.1; extra == 'vispy'
48
51
  Provides-Extra: vispy-backend
49
- License-File: LICENSE.md
50
- Summary: Differentiable Ray Tracing Toolbox for Radio Propagation Simulations
51
- Keywords: ray tracing,differentiable,propagation,radio,jax
52
- Author-email: Jérome Eertmans <jeertmans@icloud.com>
53
- Requires-Python: >=3.9
54
- Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
55
- Project-URL: Changelog, https://github.com/jeertmans/DiffeRT/releases
56
- Project-URL: Documentation, https://eertmans.be/DiffeRT
57
- Project-URL: Founding, https://github.com/sponsors/jeertmans
58
- Project-URL: Homepage, https://github.com/jeertmans/DiffeRT
59
- Project-URL: Repository, https://github.com/jeertmans/DiffeRT
52
+ Requires-Dist: pyqt6>=6.6.1; extra == 'vispy-backend'
53
+ Description-Content-Type: text/markdown
60
54
 
61
55
  <div align="center">
62
56
  <img src="https://raw.githubusercontent.com/jeertmans/DiffeRT/main/static/logo_250px.png" alt="DiffeRT logo"></img>
@@ -70,22 +64,32 @@ Project-URL: Repository, https://github.com/jeertmans/DiffeRT
70
64
  [![Python version][pypi-python-version-badge]][pypi-version-url]
71
65
  [![Documentation][documentation-badge]][documentation-url]
72
66
  [![Codecov][codecov-badge]][codecov-url]
73
- [![PDM][pdm-badge]][pdm-url]
74
67
 
75
68
  </div>
76
69
 
77
70
  ## Usage
78
71
 
79
- > [!WARNING]
72
+ > **WARNING:**
80
73
  > Until this package reaches version `0.1.x`, breaking changes
81
- > should be expected.
74
+ > should be expected. Checkout the [ROADMAP](./ROADMAP.md) for
75
+ > future features.
82
76
  >
83
77
  > If you have any suggestion regarding the development of this package,
84
78
  > please open an [issue](https://github.com/jeertmans/DiffeRT/issues).
85
79
 
80
+ The easiest way to install DiffeRT is through pip:
81
+
82
+ ```bash
83
+ pip install differt
84
+ ```
85
+
86
+ We provide pre-built binaries for most platforms. If you want (or need)
87
+ to build the package from the source distribution, check out the
88
+ requirements below.
89
+
86
90
  ## Contributing
87
91
 
88
- > [!IMPORTANT]
92
+ > **IMPORTANT:**
89
93
  > The current documentation is very light and a more complete guide for
90
94
  > new contributors will be written in the near future.
91
95
  >
@@ -102,15 +106,24 @@ To run build this package locally, you need:
102
106
 
103
107
  - [Python 3.9](https://www.python.org/) or above;
104
108
  - [Rust](https://www.rust-lang.org/) stable toolchain;
109
+ - `make` (e.g., GNU Make or Make for Windows);
105
110
  - [Maturin](https://www.maturin.rs/) for building Python bindings from Rust code;
106
- - and [PDM](https://pdm-project.org) to manage all Python dependencies.
111
+ - and [Rye](https://rye-up.com/) to manage this project.
112
+
113
+
114
+ ## Local development
107
115
 
108
- ### Building locally
116
+ The following commands assume that you installed
117
+ the project locally with:
109
118
 
110
- You can build the project locally using:
119
+ ```bash
120
+ rye sync
121
+ ```
122
+
123
+ and that you activate the corresponding Python virtual environment:
111
124
 
112
125
  ```bash
113
- pdm install
126
+ . .venv/bin/activate # or .venv\Scripts\activate on Windows
114
127
  ```
115
128
 
116
129
  ### Documentation
@@ -119,7 +132,7 @@ To generate the documentation, please run the following:
119
132
 
120
133
  ```bash
121
134
  cd docs
122
- pdm run make html
135
+ make html
123
136
  ```
124
137
 
125
138
  Finally, you can open `build/html/index.html` to see the generated docs.
@@ -147,13 +160,13 @@ cargo bench
147
160
  in the same way, you can very test you code with Pytest:
148
161
 
149
162
  ```bash
150
- pdm run pytest
163
+ pytest
151
164
  ```
152
165
 
153
166
  or benchmark it:
154
167
 
155
168
  ```bash
156
- pdm run pytest --benchmark-only
169
+ pytest --benchmark-only
157
170
  ```
158
171
 
159
172
  [pypi-version-badge]: https://img.shields.io/pypi/v/DiffeRT?label=DiffeRT&color=blueviolet
@@ -163,6 +176,3 @@ pdm run pytest --benchmark-only
163
176
  [documentation-url]: https://differt.readthedocs.io/latest/?badge=latest
164
177
  [codecov-badge]: https://codecov.io/gh/jeertmans/DiffeRT/branch/main/graph/badge.svg?token=8P4DY9JCE4
165
178
  [codecov-url]: https://codecov.io/gh/jeertmans/DiffeRT
166
- [pdm-badge]: https://img.shields.io/badge/pdm-managed-blueviolet
167
- [pdm-url]: https://pdm-project.org
168
-
@@ -1,4 +1,5 @@
1
1
  """Differentiable Ray Tracing Toolbox for Radio Propagation."""
2
+
2
3
  from .version import VERSION
3
4
 
4
5
  __version__ = VERSION
@@ -0,0 +1 @@
1
+ """Electromagnetic fields utilities."""
@@ -0,0 +1,12 @@
1
+ """Physical constants used for EM fields computation."""
2
+
3
+ __all__ = ("c", "mu_0", "epsilon_0")
4
+
5
+ c = 299792458.0
6
+ """The speed of light in vacuum."""
7
+
8
+ mu_0 = 1.25663706212e-06
9
+ r"""The vacuum permeability :math:`\mu_0`."""
10
+
11
+ epsilon_0 = 8.8541878128e-12
12
+ r"""The vacuum permittivity :math:`\epsilon_0`."""
@@ -0,0 +1,242 @@
1
+ """
2
+ Special functions.
3
+
4
+ This module extends the :py:mod:`jax.scipy.special` module
5
+ by adding missing function from :py:mod:`scipy.special`,
6
+ or by extending already implemented function to the
7
+ complex domain.
8
+
9
+ Those new implementation are needed to keep the ability
10
+ of differentating code, otherwise we could just
11
+ call the SciPy function and wrap their output
12
+ with :py:func:`jnp.asarray<jax.numpy.asarray>`.
13
+ """
14
+
15
+ import jax
16
+ import jax.numpy as jnp
17
+ from beartype import beartype as typechecker
18
+ from jax.scipy.special import erf as erfx
19
+ from jaxtyping import Array, Inexact, jaxtyped
20
+
21
+
22
+ @jax.jit
23
+ @jaxtyped(typechecker=typechecker)
24
+ def erf(z: Inexact[Array, " *batch"]) -> Inexact[Array, " *batch"]:
25
+ r"""
26
+ Evaluate the error function at the given points.
27
+
28
+ The current implementation is written using
29
+ the real-valued error function :py:func:`jax.scipy.special.erf`
30
+ and the approximation as detailed in :cite:`erf-complex`.
31
+
32
+ The output type (real or complex) is determined by the
33
+ input type.
34
+
35
+ Warning:
36
+ Currently, we observe that
37
+ this function and :py:data:`scipy.special.erf`
38
+ starts to diverge for :math:`|z| > 6`. If you know
39
+ how to avoid this problem, please contact us!
40
+
41
+ Args:
42
+ z: The array of real or complex points to evaluate.
43
+
44
+ Return:
45
+ The values of the error function at the given point.
46
+
47
+ Notes:
48
+ Regarding performances, there are two possible outputs:
49
+
50
+ 1. If ``z`` is real, then this function compiles to
51
+ :py:func:`jax.scipy.special.erf`, and will therefore
52
+ have the same performances (when JIT compilation
53
+ is done). Compared to the SciPy equivalent, we measured
54
+ that our implementation is **~ 10 times faster**.
55
+ 2. If ``z`` is complex, then our implementation is
56
+ **~ 3 times faster** than
57
+ :py:data:`scipy.special.erf`.
58
+
59
+ Those results were measured on centered random uniform arrays
60
+ with :math:`10^5` elements.
61
+
62
+ Examples:
63
+ The following plots the error function for real-valued inputs.
64
+
65
+ .. plot::
66
+
67
+ >>> from differt.em.special import erf
68
+ >>>
69
+ >>> x = jnp.linspace(-3.0, +3.0)
70
+ >>> y = erf(x)
71
+ >>> plt.plot(x, y.real) # doctest: +SKIP
72
+ >>> plt.xlabel("$x$") # doctest: +SKIP
73
+ >>> plt.ylabel(r"$\text{erf}(x)$") # doctest: +SKIP
74
+
75
+ The following plots the error function for complex-valued inputs.
76
+
77
+ .. plotly::
78
+
79
+ >>> from differt.em.special import erf
80
+ >>> from scipy.special import erf
81
+ >>>
82
+ >>> x = y = jnp.linspace(-2.0, +2.0, 200)
83
+ >>> a, b = jnp.meshgrid(x, y)
84
+ >>> z = erf(a + 1j * b)
85
+ >>> fig = go.Figure(
86
+ ... data=[
87
+ ... go.Surface(
88
+ ... x=x,
89
+ ... y=y,
90
+ ... z=jnp.abs(z),
91
+ ... colorscale="phase",
92
+ ... surfacecolor=jnp.angle(z),
93
+ ... colorbar=dict(title="Arg(erf(z))"),
94
+ ... )
95
+ ... ]
96
+ ... )
97
+ >>> fig.update_layout(
98
+ ... scene=dict(
99
+ ... xaxis=dict(title="Re(z)"),
100
+ ... yaxis=dict(title="Im(z)"),
101
+ ... zaxis=dict(title="Abs(erf(z))"),
102
+ ... )
103
+ ... ) # doctest: +SKIP
104
+ >>> fig # doctest: +SKIP
105
+ """
106
+ if jnp.issubdtype(z.dtype, jnp.floating):
107
+ return erfx(z)
108
+ # https://granite.phys.s.u-tokyo.ac.jp/svn/LCGT/trunk/sensitivity/Matlab/bKAGRA/@double/erfz.pdf
109
+
110
+ if jnp.issubdtype(z.dtype, jnp.complex128): # double precision
111
+ N = 13 # noqa: N806
112
+ M = 14 # noqa: N806
113
+ else: # single precision
114
+ N = 9 # noqa: N806
115
+ M = 10 # noqa: N806
116
+
117
+ r = z.real
118
+ i = jnp.abs(z.imag)
119
+ r_squared = r * r
120
+
121
+ exp_r_squared = jnp.exp(-r_squared)
122
+ exp_2j_r_i = jnp.exp(-2j * r * i)
123
+
124
+ f_sum = jnp.zeros_like(z)
125
+ g_sum = jnp.zeros_like(z)
126
+ h_sum = jnp.zeros_like(z)
127
+
128
+ for n in range(1, N + 1):
129
+ n_squared = n * n
130
+ n_squared_over_four = n_squared / 4
131
+ den = 1 / (n_squared_over_four + r_squared)
132
+ exp_f = jnp.exp(-n_squared_over_four)
133
+ exp_g = jnp.exp(+n * i - n_squared_over_four)
134
+ exp_h = jnp.exp(-n * i - n_squared_over_four)
135
+
136
+ f_sum += exp_f * den
137
+ g_sum += exp_g * (r - 1j * n / 2) * den
138
+ h_sum += exp_h * (r + 1j * n / 2) * den
139
+
140
+ for n in range(N + 1, N + M + 1):
141
+ n_squared = n * n
142
+ n_squared_over_four = n_squared / 4
143
+ exp_g = jnp.exp(+n * i - n_squared_over_four)
144
+
145
+ g_sum += exp_g * (r - 1j * n / 2) / (n_squared_over_four + r_squared)
146
+
147
+ r_non_zero = jnp.where(r == 0.0, 1.0, r)
148
+ e = jnp.where(
149
+ r == 0.0,
150
+ 1j * i / jnp.pi,
151
+ (exp_r_squared * (1 - exp_2j_r_i)) / (2 * jnp.pi * r_non_zero),
152
+ ) # Fixes limit r -> 0
153
+ f = r * exp_r_squared * f_sum / jnp.pi
154
+ g = exp_r_squared * g_sum / (2 * jnp.pi)
155
+ h = exp_r_squared * h_sum / (2 * jnp.pi)
156
+
157
+ res = erfx(r) + e + f - exp_2j_r_i * (g + h)
158
+ res = jnp.where(z.imag < 0, jnp.conj(res), res)
159
+ return res
160
+
161
+
162
+ @jax.jit
163
+ @jaxtyped(typechecker=typechecker)
164
+ def erfc(z: Inexact[Array, " *batch"]) -> Inexact[Array, " *batch"]:
165
+ r"""
166
+ Evaluate the complementary error function at the given points.
167
+
168
+ The output type (real or complex) is determined by the
169
+ input type.
170
+
171
+ See :py:func:`erf` for more details.
172
+
173
+ Args:
174
+ z: The array of real or complex points to evaluate.
175
+
176
+ Return:
177
+ The values of the complementary error function at the given point.
178
+
179
+ Examples:
180
+ The following plots the complementary error function for real-valued inputs.
181
+
182
+ .. plot::
183
+
184
+ >>> from differt.em.special import erfc
185
+ >>>
186
+ >>> x = jnp.linspace(-3.0, +3.0)
187
+ >>> y = erfc(x)
188
+ >>> plt.plot(x, y.real) # doctest: +SKIP
189
+ >>> plt.xlabel("$x$") # doctest: +SKIP
190
+ >>> plt.ylabel(r"$\text{erfc}(x)$") # doctest: +SKIP
191
+ """
192
+ return 1.0 - erf(z)
193
+
194
+
195
+ @jax.jit
196
+ @jaxtyped(typechecker=typechecker)
197
+ def fresnel(
198
+ z: Inexact[Array, " *batch"],
199
+ ) -> tuple[Inexact[Array, " *batch"], Inexact[Array, " *batch"]]:
200
+ """
201
+ Evaluate the two Fresnel integrals at the given points.
202
+
203
+ This current implementation is written using
204
+ the error function :py:func:`erf<differt.em.special.erf>`
205
+ see :cite:`fresnel-integrals`.
206
+
207
+ The output type (real or complex) is determined by the
208
+ input type.
209
+
210
+ Args:
211
+ z: The array of real or complex points to evaluate.
212
+
213
+ Return:
214
+ A tuple of two arrays, one for each of the Fresnel integrals.
215
+
216
+ Examples:
217
+ The following plots the Fresnel for real-valued inputs.
218
+
219
+ .. plot::
220
+
221
+ >>> from differt.em.special import fresnel
222
+ >>>
223
+ >>> t = jnp.linspace(0.0, 5.0, 200)
224
+ >>> s, c = fresnel(t)
225
+ >>> plt.plot(t, s.real, label=r"$y=S(x)$") # doctest: +SKIP
226
+ >>> plt.plot(t, c.real, "--", label=r"$y=C(x)$") # doctest: +SKIP
227
+ >>> plt.xlabel("$x$") # doctest: +SKIP
228
+ >>> plt.ylabel("$y$") # doctest: +SKIP
229
+ >>> plt.legend() # doctest: +SKIP
230
+ """
231
+ # Constant factors
232
+ sqrtpi_2_4 = 0.31332853432887503 # 0.25 * jnp.sqrt(0.5 * jnp.pi)
233
+ _sqrt2 = 0.7071067811865476 # jnp.sqrt(0.5)
234
+
235
+ # Erf function evaluations
236
+ ep = erf((1 + 1j) * _sqrt2 * z)
237
+ em = erf((1 - 1j) * _sqrt2 * z)
238
+
239
+ s = sqrtpi_2_4 * (1 + 1j) * (ep - 1j * em)
240
+ c = sqrtpi_2_4 * (1 - 1j) * (ep + 1j * em)
241
+
242
+ return s, c
@@ -0,0 +1,88 @@
1
+ """
2
+ Uniform Theory of Diffraction (UTD) utilities.
3
+
4
+ The foundamentals of UTD are described in :cite:`utd-mcnamara`.
5
+ """
6
+
7
+ import jax
8
+ import jax.numpy as jnp
9
+ from beartype import beartype as typechecker
10
+ from jaxtyping import Array, Complex, Inexact, jaxtyped
11
+
12
+ from .special import erfc
13
+
14
+
15
+ @jax.jit
16
+ @jaxtyped(typechecker=typechecker)
17
+ def F(z: Inexact[Array, " *batch"]) -> Complex[Array, " *batch"]: # noqa: N802
18
+ r"""
19
+ Evaluate the transition function :cite:`utd-mcnamara{p. 184}` at the given points.
20
+
21
+ The transition function is defined as follows:
22
+
23
+ .. math::
24
+ F(z) = 2j \sqrt{z} e^{j z} \int\limits_\sqrt{z}^\infty e^{-j u^2} \text{d}u,
25
+
26
+ where :math:`j^2 = -1`.
27
+
28
+ As detailed in :cite:`utd-mcnamara{p. 164}`, the integral can be expressed in
29
+ terms of Fresnel integrals (:math:`C(z)` and :math:`S(z)`), so that:
30
+
31
+ .. math::
32
+ C(z) - j S(z) = \int\limits_\sqrt{z}^\infty e^{-j u^2} \text{d}u.
33
+
34
+ Because JAX does not provide a XLA implementation of
35
+ :py:data:`scipy.special.fresnel`, this implementation relies on a
36
+ custom complex-valued implementation of the error function and
37
+ the fact that:
38
+
39
+ .. math::
40
+ C(z) - j S(z) = \sqrt{\frac{\pi}{2}}\frac{1-j}{2}\text{erf}\left(\frac{1+j}{\sqrt{2}}z\right).
41
+
42
+ As a result, we can further simplify :math:`F(z)` to:
43
+
44
+ .. math::
45
+ F(z) = \sqrt{\frac{\pi}{2}} \sqrt{z} e^{j z} (1 - j) \text{erfc}\left(\frac{1+j}{\sqrt{2}}z\right),
46
+
47
+ where :math:`\text{erfc}` is the complementary error function.
48
+
49
+ Args:
50
+ z: The array of real or complex points to evaluate.
51
+
52
+ Return:
53
+ The values of the transition function at the given point.
54
+
55
+ Examples:
56
+ .. plot::
57
+
58
+ The following example reproduces the same plot as in
59
+ :cite:`utd-mcnamara{fig. 4.16}`.
60
+
61
+ >>> from differt.em.utd import F
62
+ >>>
63
+ >>> x = jnp.logspace(-3, 1, 100)
64
+ >>> y = F(x)
65
+ >>>
66
+ >>> A = jnp.abs(y) # Amplitude of F(x)
67
+ >>> P = jnp.angle(y, deg=True) # Phase (in deg.) of F(x)
68
+ >>>
69
+ >>> fig, ax1 = plt.subplots()
70
+ >>>
71
+ >>> ax1.semilogx(x, A) # doctest: +SKIP
72
+ >>> ax1.set_xlabel("$x$") # doctest: +SKIP
73
+ >>> ax1.set_ylabel("Magnitude - solid line") # doctest: +SKIP
74
+ >>> ax2 = plt.twinx()
75
+ >>> ax2.semilogx(x, P, "--") # doctest: +SKIP
76
+ >>> ax2.set_ylabel("Phase (°) - dashed line") # doctest: +SKIP
77
+ >>> plt.tight_layout() # doctest: +SKIP
78
+ """
79
+ factor = jnp.sqrt(jnp.pi / 2)
80
+ sqrt_z = jnp.sqrt(z)
81
+
82
+ return (
83
+ (1 + 1j)
84
+ * factor
85
+ * sqrt_z
86
+ * jnp.exp(1j * z)
87
+ * erfc((1 + 1j) * sqrt_z / jnp.sqrt(2))
88
+ )
@@ -0,0 +1,83 @@
1
+ """Utilities for working with EM fields."""
2
+
3
+ from typing import Any, Union
4
+
5
+ import jax
6
+ from beartype import beartype as typechecker
7
+ from jaxtyping import Array, Float, jaxtyped
8
+
9
+ from ..geometry.utils import path_lengths
10
+ from .constants import c
11
+
12
+
13
+ @jax.jit
14
+ @jaxtyped(typechecker=typechecker)
15
+ def lengths_to_delays(
16
+ lengths: Float[Array, " *#batch"],
17
+ speed: Union[float, Float[Array, " *#batch"]] = c,
18
+ ) -> Float[Array, " *#batch"]:
19
+ """
20
+ Compute the delay, in seconds, corresponding to each length.
21
+
22
+ Args:
23
+ lengths: The array of lengths (in meters).
24
+ speed: The speed (in meters per second)
25
+ used to compute the delay. This can be
26
+ an array of speeds. Default is the speed
27
+ of light in vacuum.
28
+
29
+ Return:
30
+ The array of path delays.
31
+
32
+ Examples:
33
+ The following example shows how to compute a delay from a length.
34
+
35
+ >>> from differt.em.constants import c
36
+ >>> from differt.em.utils import (
37
+ ... lengths_to_delays,
38
+ ... )
39
+ >>>
40
+ >>> lengths = jnp.array([1.0, 2.0, 4.0])
41
+ >>> lengths_to_delays(lengths) * c
42
+ Array([1., 2., 4.], dtype=float32)
43
+ >>> lengths_to_delays(lengths, speed=2.0)
44
+ Array([0.5, 1. , 2. ], dtype=float32)
45
+ """
46
+ return lengths / speed
47
+
48
+
49
+ @jax.jit
50
+ @jaxtyped(typechecker=typechecker)
51
+ def path_delays(
52
+ paths: Float[Array, "*batch path_length 3"], **kwargs: Any
53
+ ) -> Float[Array, " *batch"]:
54
+ """
55
+ Compute the path delay, in seconds, of each path.
56
+
57
+ Each path is exactly made of ``path_length`` vertices.
58
+
59
+ Args:
60
+ paths: The array of path vertices.
61
+ kwargs: Keyword arguments passed to
62
+ :func:`lengths_to_delays`.
63
+
64
+ Return:
65
+ The array of path delays.
66
+
67
+ Examples:
68
+ The following example shows how to compute the delay of a very simple path.
69
+
70
+ >>> from differt.em.constants import c
71
+ >>> from differt.em.utils import (
72
+ ... path_delays,
73
+ ... )
74
+ >>>
75
+ >>> path = jnp.array([[1.0, 0.0, 0.0], [1.0, 1.0, 0.0]])
76
+ >>> path_delays(path) * c
77
+ Array(1., dtype=float32)
78
+ >>> path_delays(path, speed=2.0)
79
+ Array(0.5, dtype=float32)
80
+ """
81
+ lengths = path_lengths(paths)
82
+
83
+ return lengths_to_delays(lengths, **kwargs)