File size: 23,814 Bytes
a1a1fbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
"""
Sinusoidal Embeddings
====================

Inputs to deep learning models often represent positions on a spatial, temporal, or
spatio-temporal grid. To enrich these coordinates, positional embeddings can be introduced
to improve a model's capacity to generalize across the domain. In this tutorial, we focus
on sinusoidal positional embeddings.

Sinusoidal embeddings encode inputs as periodic functions (sines and cosines), thereby
lifting low-dimensional coordinates into a richer spectral representation. This spectral
lifting enhances the model's ability to capture fine-scale variations and high-frequency
dynamics.
"""

# %%
# .. raw:: html
#
#    <div style="margin-top: 3em;"></div>
#
# Setup in 1D
# ----------------------
# To build intuition, consider a simple 1D example. Let :math:`x \in \mathbb{R}`
# be a single input, and define the embedding function
#
# .. math::
#    g: \mathbb{R} \rightarrow \mathbb{R}^{2 L}, \quad g(x)=[\sin (x), \  \cos (x), \ \sin (2 x), \ \cos (2 x), \ldots, \ \sin (L x), \ \cos (L x)],
#
# where :math:`L` defines the number of frequencies we wish to use for the embedding. Each
# pair of sine and cosine terms introduces a higher frequency, enriching how positional
# information is represented.
#
# This idea naturally extends to an entire 1D input. Let :math:`\vec{x} \in \mathbb{R}^N`
# denote a discretized domain of :math:`N` points. Then the embedding function becomes
#
# .. math::
#    g: \mathbb{R}^N \rightarrow \mathbb{R}^{N \times 2 L}, \quad g(\vec{x})=\operatorname{concat}(\sin (\vec{x}), \cos (\vec{x}), \sin (2 \vec{x}), \cos (2 \vec{x}), \ldots, \sin (L \vec{x}), \cos (L \vec{x})),
#
# In practice, both the original coordinate and its embedding are passed to the model:
#
# .. math::
#    \operatorname{input}(\vec{x})=\operatorname{concat}(\vec{x}, \ g(\vec{x})) \in \mathbb{R}^{N \times 2 L + 1},
#
# preserving the original input, while augmenting it with a hierarchy of frequency components.
#
# .. raw:: html
#
#    <div style="margin-top: 2em;"></div>
#
# Domain Normalization
# ~~~~~~~~~~~~~~~~~~~~
# When applying sinusoidal embeddings, it is often useful to normalize the input coordinates
# to a periodic interval that aligns with the natural period of the sine and cosine functions.
# For example, a 1D spatial domain :math:`\vec{x} \in[0,1]` of :math:`N` points can be rescaled to
#
# .. math::
#    \vec{x}^{\prime}=2 \pi \vec{x},
#
# so that :math:`\vec{x}^{\prime} \in[0,2 \pi]`.
#
# This mapping preserves the number of sampling points :math:`N` and the overall shape of the domain
# while ensuring that the lowest-frequency sine and cosine components complete exactly one
# full oscillation over the interval.
#
# .. raw:: html
#
#    <div style="margin-top: 2em;"></div>
#
# Choosing :math:`L` to Satisfy the Nyquist-Criterion
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# .. warning::
#    When choosing the number of frequency levels :math:`L`, it is important to ensure that the
#    highest frequency component in the embedding does not exceed the Nyquist limit imposed by
#    the discretisation of the input domain.
#
# For a domain of :math:`N` points, the Nyquist frequency is
#
# .. math::
#    f_{\text{Nyquist}} = \frac{N}{2}.
#
# For the sinusoidal embedding defined above, the Nyquist constraint becomes:
#
# .. math::
#    L < \frac{N}{2}.
#
# The Nyquist frequency represents the maximum frequency that can be correctly captured
# when sampling a signal, equal to half the sampling rate. If frequencies higher than this
# limit are used, they will not be represented as true high frequencies but will instead appear
# as lower ones, producing distortion known as aliasing. This is why we must ensure that
# the highest frequency in our embedding does not exceed the Nyquist limit.
#
# .. raw:: html
#
#    <div style="margin-top: 2em;"></div>
#
# Visualizing the Sinusoidal Embeddings
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Below, we visualize the sinusoidal embeddings for a spatial input domain
# :math:`\vec{x} \in[0,1]` consisting of 1000 equally spaced points, using :math:`L = 3` frequency levels.

# %%

# Import required libraries
import torch
import matplotlib.pyplot as plt
import numpy as np
from neuralop.layers.embeddings import SinusoidalEmbedding

# Set default font sizes for better readability
plt.rcParams.update(
    {
        "font.size": 14,
        "axes.titlesize": 18,
        "axes.labelsize": 16,
        "xtick.labelsize": 14,
        "ytick.labelsize": 14,
        "legend.fontsize": 14,
    }
)

device = "cpu"

# Define a spatial domain and number of frequencies
# Create 1000 equally spaced points in [0, 1]
#  and normalize to [0, 2π] for proper sinusoidal embedding
x = torch.linspace(0, 1, 1000)
x_normalized = torch.linspace(0, 2 * torch.pi, len(x))
# Number of frequency levels for the embedding
L = 3

# Check if the number of frequencies satisfies the Nyquist-Criterion
if L < len(x_normalized) / 2:
    print(f"Nyquist-Shannon sampling theorem is satisfied for the given number of frequencies {L}.")
else:
    print(f"Nyquist-Shannon sampling theorem is violated for the given number of frequencies {L}.")

# Build embedding: [sin(x), cos(x), sin(2x), cos(2x), ...]
# Each frequency level contributes a sine and cosine pair
g = []
for l in range(1, L + 1):
    g.append(torch.sin(l * x_normalized))
    g.append(torch.cos(l * x_normalized))

# Construct input by concatenating the original input and the embedding
# This preserves the original coordinates while adding spectral information
input_arr = np.asarray([x, *g])
input_tensor = torch.tensor(input_arr)

# Plot the embedding components
colors = plt.cm.tab10.colors

plt.figure(figsize=(10, 5))
for freq_idx in range(L):
    color = colors[freq_idx % len(colors)]
    sin_idx = 2 * freq_idx + 1
    cos_idx = 2 * freq_idx + 2

    plt.plot(x, input_tensor[sin_idx], color=color, label=f"Frequency {freq_idx + 1}")
    plt.plot(x, input_tensor[cos_idx], color=color)

plt.xlabel("x", fontsize=16)
plt.ylabel("Embedding value", fontsize=16)
plt.title("Sinusoidal Embedding Components (L = 3)", fontsize=18)
plt.legend(loc="lower left", framealpha=1.0, fontsize=14)
plt.locator_params(axis="y", nbins=5)
plt.tight_layout()
plt.show()

# %%
# .. raw:: html
#
#    <div style="margin-top: 4em;"></div>
#
# Encoding Constant Parameters
# ----------------------------
# A particularly useful extension of sinusoidal embeddings is their ability to encode constant
# parameters. Consider a setting where you have a scalar parameter :math:`m` (such as a material
# property, boundary condition, or physical constant) that you wish to feed into a model.
# Instead of treating :math:`m` as a fixed scalar input, we can represent it using periodic
# functions, either by modulating the amplitude or the frequency of the sinusoidal components.
#
# .. raw:: html
#
#    <div style="margin-top: 2em;"></div>
#
# **1. Amplitude Modulation:** To encode :math:`m` by scaling the amplitudes of the sinusoidal
# functions, we define the embedding as
#
# .. math::
#    m \rightarrow m g(\vec{x}),
#
# where each element of the embedding :math:`g(\vec{x})` is multiplied by :math:`m`.
#
# .. raw:: html
#
#    <div style="margin-top: 2em;"></div>
#
# **2. Frequency Modulation:** Alternatively, to encode :math:`m` by scaling the frequencies,
# we define
#
# .. math::
#    m \rightarrow g(m\vec{x})
#
# where :math:`m` multiplies the input argument of each sinusoidal component.
#
# When encoding constant parameters through frequency modulation, care must be taken to ensure
# that the Nyquist criterion is satisfied. In this case, where the modulation factor :math:`m`
# scales the frequencies, the Nyquist constraint becomes :math:`L < \frac{N}{2m}`.
#
# .. raw:: html
#
#    <div style="margin-top: 2em;"></div>
#
# Below, we demonstrate an example of encoding the parameter :math:`m = 2.5` through both
# amplitude and frequency modulation.

# %%

# Define a spatial domain and number of frequencies
x = torch.linspace(0, 1, 1000)
x_normalized = torch.linspace(0, 2 * torch.pi, len(x))
L = 3

# Define parameter to encode
m = 2.5
m_tensor = torch.tensor([m])

# Check if the number of frequencies and parameter satisfies the Nyquist-Criterion
if L <= len(x_normalized) / (2 * m):
    print(f"Nyquist-Shannon sampling theorem is satisfied for the given parameter {m} and number of frequencies {L}.")
else:
    print(f"Nyquist-Shannon sampling theorem is violated for the given parameter {m} and number of frequencies {L}.")

# Build amplitude-modulated embedding: m * g(x)
g_amplitude = []
for l in range(1, L + 1):
    g_amplitude.append(torch.sin(l * x_normalized) * m_tensor)
    g_amplitude.append(torch.cos(l * x_normalized) * m_tensor)

# Build frequency-modulated embedding: g(m * x)
g_frequency = []
for l in range(1, L + 1):
    g_frequency.append(torch.sin(l * x_normalized * m_tensor))
    g_frequency.append(torch.cos(l * x_normalized * m_tensor))

# Convert to arrays for visualization
input_amplitude = torch.tensor(np.asarray([x, *g_amplitude]))
input_frequency = torch.tensor(np.asarray([x, *g_frequency]))

# Plot both embeddings
colors = plt.cm.tab10.colors
fig, axes = plt.subplots(2, 1, figsize=(10, 9), sharex=True)

## Amplitude modulation
for freq_idx in range(L):
    color = colors[freq_idx % len(colors)]
    sin_idx, cos_idx = 2 * freq_idx + 1, 2 * freq_idx + 2
    axes[0].plot(
        x, input_amplitude[sin_idx], color=color, label=f"Channel {freq_idx + 1}"
    )
    axes[0].plot(x, input_amplitude[cos_idx], color=color)
axes[0].set_title("Amplitude Modulation", fontsize=18, pad=20)
axes[0].set_ylabel("Embedding value", fontsize=16)
axes[0].legend(loc="lower left", framealpha=1.0, fontsize=14)
axes[0].locator_params(axis="y", nbins=5)

## Frequency modulation
for freq_idx in range(L):
    color = colors[freq_idx % len(colors)]
    sin_idx, cos_idx = 2 * freq_idx + 1, 2 * freq_idx + 2
    axes[1].plot(x, input_frequency[sin_idx], color=color, label=f"Channel {freq_idx + 1}")
    axes[1].plot(x, input_frequency[cos_idx], color=color)
axes[1].set_title("Frequency Modulation", fontsize=18, pad=20)
axes[1].set_ylabel("Embedding value", fontsize=16)
axes[1].set_xlabel("x", fontsize=16)
axes[1].locator_params(axis="y", nbins=5)

plt.suptitle(f"Sinusoidal Embeddings with Parameter m = {m}", y=0.98, fontsize=20)
plt.tight_layout()
plt.show()

# %%
# .. raw:: html
#
#    <div style="margin-top: 4em;"></div>
#
# Neural Operator SinusoidalEmbedding Class
# -----------------------------------------
# The ``neuralop`` library provides a unified sinusoidal positional embedding class,
# ``neuralop.layers.embeddings.SinusoidalEmbedding``, with the following embedding techniques:
#
# - ``transformer`` - Vaswani, A. et al (2017), "Attention Is All You Need".
# - ``nerf`` - Mildenhall, B. et al (2020), "NeRF: Representing Scenes as Neural Radiance Fields for View Synthesis".
#
# The `SinusoidalEmbedding` class expects inputs to be of shape
#
#              ``(batch_size, N, input_channels)`` or ``(N, input_channels)``
#
#
# .. raw:: html
#
#    <div style="margin-top: 2em;"></div>
#
# Embedding Variants
# ~~~~~~~~~~~~~~~~~~
# Let :math:`\vec{x} \in \mathbb{R}^N` denote a 1D input domain consisting of
# :math:`N` discretized points. The embedding function
# :math:`g: \mathbb{R}^N \rightarrow \mathbb{R}^{N \times 2L}` maps each input value
# :math:`x_n` to a :math:`2L`-dimensional vector composed of sine and cosine terms evaluated
# at different frequencies. Each embedding type defines these frequencies differently,
# leading to distinct representations.
#
# .. raw:: html
#
#    <div style="margin-top: 2em;"></div>
#
# **1. Transformer-style embedding:** For :math:`0 \leq k < L`:
#
# .. math::
#    g(\vec{x})_{:, 2 k}=\sin \left(\frac{\vec{x}}{\text { max_positions }^{k / L}}\right), \quad g(\vec{x})_{:, 2 k+1}=\cos \left(\frac{\vec{x}}{\text { max_positions }^{k / L}}\right) .
#
# Here, :math:`\text{max_positions}` controls the maximum position for the embedding.
#
# .. raw:: html
#
#    <div style="margin-top: 2em;"></div>
#
# **2. NeRF-style embedding:** For :math:`0 \leq k < L`:
#
# .. math::
#    g(\vec{x})_{:, 2 k}=\sin \left(2^k \pi \vec{x}\right), \quad g(\vec{x})_{:, 2 k+1}=\cos \left(2^k \pi \vec{x}\right) .
#
# In order to ensure that the Nyquist-Criterion is satisfied, for the Transformer-style
# embedding, the embedding frequencies should satisfy: :math:`f_{\max} < f_{\text{Nyquist}}`.
#
# For the NeRF-style embedding:
#
# .. math::
#    2^{L-1} < \frac{N}{2} \ \ \implies \ \  L < 1 + \log_2\left(\frac{N}{2}\right).
#
# .. raw:: html
#
#    <div style="margin-top: 3em;"></div>
#
# Below, we include examples of using the `SinusoidalEmbedding` class with both the
# transformer- and NeRF-style embeddings.

# %%

# Define a spatial domain and the number of frequencies
x = torch.linspace(0, 1, 1000)
x_normalized = torch.linspace(0, 2 * torch.pi, len(x)).reshape(-1, 1)
L = 3

# Check if the number of frequencies satisfies the Nyquist-Criterion
if L <= 1 + torch.log2(torch.tensor(len(x_normalized) / 2)):
    print(f"Nyquist-Shannon sampling theorem is satisfied for the given number of frequencies {L}.")
else:
    print(f"Nyquist-Shannon sampling theorem is violated for the given number of frequencies {L}.")

# Define the transformer embedding
# max_positions controls the frequency scaling in transformer-style embeddings
max_positions = 1000
transformer_embedder = SinusoidalEmbedding(
    in_channels=1,
    num_frequencies=L,
    embedding_type="transformer",
    max_positions=max_positions,
).to(device)

# Apply transformer-style embedding
transformer_embedding = transformer_embedder(x_normalized).permute(1, 0)

# Define the NeRF embedding
nerf_embedder = SinusoidalEmbedding(
    in_channels=1, num_frequencies=L, embedding_type="nerf"
).to(device)

# Apply NeRF-style embedding, with the domain [0, 1]
nerf_embedding = nerf_embedder(x.reshape(-1, 1)).permute(1, 0)

# Plot both embeddings
colors = plt.cm.tab10.colors
fig, axes = plt.subplots(2, 1, figsize=(10, 9), sharex=True)

## Transformer embedding
for freq_idx in range(L):
    color = colors[freq_idx % len(colors)]
    sin_idx, cos_idx = 2 * freq_idx, 2 * freq_idx + 1

    axes[0].plot(x, transformer_embedding[sin_idx], color=color, label=f"Channel {freq_idx + 1}")
    axes[0].plot(x, transformer_embedding[cos_idx], color=color)

axes[0].set_title("Transformer embedding", fontsize=18, pad=20)
axes[0].set_ylabel("Embedding value", fontsize=16)
axes[0].legend(loc="lower left", framealpha=1.0, fontsize=14)
axes[0].locator_params(axis="y", nbins=5)

## NeRF embedding
for freq_idx in range(L):
    color = colors[freq_idx % len(colors)]
    sin_idx, cos_idx = 2 * freq_idx, 2 * freq_idx + 1

    axes[1].plot(x, nerf_embedding[sin_idx], color=color, label=f"Channel {freq_idx + 1}")
    axes[1].plot(x, nerf_embedding[cos_idx], color=color)

axes[1].set_title("NeRF embedding", fontsize=18, pad=20)
axes[1].set_xlabel("x", fontsize=16)
axes[1].set_ylabel("Embedding value", fontsize=16)
axes[1].locator_params(axis="y", nbins=5)

plt.suptitle("Sinusoidal Embeddings using transformer and NeRF embedding types", y=0.98, fontsize=20)
plt.tight_layout()
plt.show()

# %%
# .. raw:: html
#
#    <div style="margin-top: 2em;"></div>
#
# Encoding Constant Parameters with NeuralOp Class
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Similar to the earlier illustrative examples, we can also encode
# a scalar parameter :math:`m` before passing it to a model. Once again, care must be taken to
# ensure that the Nyquist criterion is satisfied.
#
# In the Transformer-style embedding, to avoid aliasing, the embedding frequencies should still
# satisfy
#
# .. math::
#    f_{\max} < f_{\text{Nyquist}}.
#
# For the NeRF-style embedding, the modified constraint becomes:
#
# .. math::
#    2^{L-1}m < \frac{N}{2} \implies L < 1 + \log_2\left(\frac{N}{2m}\right).
#
# .. raw:: html
#
#    <div style="margin-top: 2em;"></div>
#
# Below, we demonstrate an example of encoding the parameter :math:`m = 2.5` through frequency
# modulation of the NeRF-style embedding.

# %%

# Define a spatial domain and the number of frequencies
x = torch.linspace(0, 1, 1000)
L = 3

# Define the parameter to encode
m = 2.5
m_tensor = torch.tensor([m])

# Check if the number of frequencies and parameter satisfies the Nyquist-Criterion
if L <= 1 + torch.log2(torch.tensor(len(x) / (2 * m))):
    print(f"Nyquist-Shannon sampling theorem is satisfied for the given parameter {m} and number of frequencies {L}.")
else:
    print(f"Nyquist-Shannon sampling theorem is violated for the given parameter {m} and number of frequencies {L}.")

# Define the NeRF embedding
nerf_embedder = SinusoidalEmbedding(
    in_channels=1, num_frequencies=L, embedding_type="nerf"
).to(device)

# Apply frequency modulation: multiply input, with the domain [0, 1], by parameter before embedding
# This scales all frequencies by the parameter m
nerf_embedding = nerf_embedder(x.reshape(-1, 1) * m_tensor).permute(1, 0)

# Plot the embedding
colors = plt.cm.tab10.colors

plt.figure(figsize=(10, 5))
for freq_idx in range(L):
    color = colors[freq_idx % len(colors)]
    sin_idx = 2 * freq_idx
    cos_idx = 2 * freq_idx + 1

    plt.plot(x, nerf_embedding[sin_idx], color=color, label=f"Channel {freq_idx}")
    plt.plot(x, nerf_embedding[cos_idx], color=color)

plt.xlabel("x", fontsize=16)
plt.ylabel("Embedding", fontsize=16)
plt.title("NeRF-style embedding with modulated frequency", fontsize=18, pad=20)
plt.legend(loc="lower left", framealpha=1.0, fontsize=14)
plt.locator_params(axis="y", nbins=5)
plt.tight_layout()
plt.show()

# %%
# Similarly, we can encode the parameter :math:`m = 2.5` through amplitude modulation, where we show
# an example using the NeRF-style embedding below.

# %%

# Define a spatial domain and the number of frequencies
x = torch.linspace(0, 1, 1000)
L = 3

# Define the parameter to encode
m = 2.5
m_tensor = torch.tensor([m])

# Check if the number of frequencies and parameter satisfies the Nyquist-Criterion
if L <= 1 + torch.log2(torch.tensor(len(x) / 2)):
    print(f"Nyquist-Shannon sampling theorem is satisfied for the given number of frequencies {L}.")
else:
    print(f"Nyquist-Shannon sampling theorem is violated for the given number of frequencies {L}.")

# Define the embedding
nerf_embedder = SinusoidalEmbedding(
    in_channels=1, num_frequencies=L, embedding_type="nerf"
).to(device)

# Apply amplitude modulation: multiply embedding, with the domain [0, 1], by parameter after computation
# This scales all embedding components by the parameter m
nerf_embedding = nerf_embedder(x.reshape(-1, 1)).permute(1, 0) * m_tensor

# Plot the embedding
colors = plt.cm.tab10.colors

plt.figure(figsize=(10, 5))
for freq_idx in range(L):
    color = colors[freq_idx % len(colors)]
    sin_idx = 2 * freq_idx
    cos_idx = 2 * freq_idx + 1

    plt.plot(x, nerf_embedding[sin_idx], color=color, label=f"Channel {freq_idx}")
    plt.plot(x, nerf_embedding[cos_idx], color=color)

plt.xlabel("x", fontsize=16)
plt.ylabel("Embedding", fontsize=16)
plt.title("NeRF-style embedding with amplitude modulation", fontsize=18, pad=20)
plt.legend(loc="lower left", framealpha=1.0, fontsize=14)
plt.locator_params(axis="y", nbins=5)
plt.tight_layout()
plt.show()

# %%
# .. raw:: html
#
#    <div style="margin-top: 4em;"></div>
#
# Application to Fourier Neural Operators (FNOs)
# ----------------------------------------------
# Fourier Neural Operators (FNOs) learn mappings between functions by operating in the frequency domain.
# They use the Fourier transform to express data as combinations of sine and cosine components,
# enabling them to capture complex, multi-scale interactions across frequencies.
# Given that sinusoidal embeddings also lift low-dimensional data into a richer spectral
# representation, they complement FNOs naturally.
# This synergy makes sinusoidal embeddings particularly effective for neural operator architectures.
#
# In the general setting for neural operators, we strongly recommend choosing the number of frequencies
# :math:`L` such that the Nyquist-Criterion is not violated. This can be done by following the guidelines
# we provided earlier for selecting :math:`L` in both transformer-style and NeRF-style embeddings.
#
# When dealing with FNOs with a specified number of Fourier modes, :math:`\text{n_modes}`, the
# highest embedded frequency should ideally also remain below :math:`\text{n_modes}`,
# as higher frequencies will be zeroed out and not acted upon by the spectral convolution operation.
#
# For the NeRF-style embedding, this condition leads to an explicit upper bound on :math:`L`:
#
# .. math::
#    2^{L-1} < \text{n_modes} \ \ \implies \ \  L < 1 + \log_2\left(\text{n_modes}\right).

# %%
# .. raw:: html
#
#    <div style="margin-top: 4em;"></div>
#
# Setup in Higher Dimensions
# --------------------------
# Let :math:`X \in \mathbb{R}^{d \times N}` denote a :math:`d`-dimensional input domain consisting of
# :math:`N` discretised points, where each row :math:`\vec{x}_{i} \in \mathbb{R}^N` corresponds to
# the sampled coordinates along the :math:`i`-th spatial or temporal dimension. Thus, each column
# of :math:`X` represents a single point :math:`\vec{x}_{:,j} \in \mathbb{R}^d` in the
# :math:`d`-dimensional domain.
#
# Building on the 1D embedding function :math:`g` introduced earlier, we define the
# multi-dimensional embedding
#
# .. math::
#    h: \mathbb{R}^{d \times N} \ \  \rightarrow \ \ \mathbb{R}^{N \times 2 L d}, \quad h(X)=\operatorname{concat}\left(g\left(\vec{x}_1\right), g\left(\vec{x}_2\right), \ldots, g\left(\vec{x}_d\right)\right),
#
# where each :math:`\vec{x}_i` denotes the sampled domain along the :math:`i`-th input dimension.
#
# The multi-dimensional embedding function :math:`h` applies the 1D embedding function :math:`g`
# independently to each coordinate dimension and concatenates the resulting embeddings
# along the feature axis. This approach allows the model to capture
# frequency patterns along each dimension separately while maintaining the overall structure.
#
# .. raw:: html
#
#    <div style="margin-top: 2em;"></div>
#
# Below, we include an example of using the `SinusoidalEmbedding` class to construct both
# transformer- and NeRF-style embeddings for a 3D input.

# %%

# Define a 1D spatial domain and construct 3D input by repeating the 1D domain
dim = 3
x_1d = torch.linspace(0, 1, 1000)
# For transformer: normalize to [0, 2π] and repeat for 3D input, shape (N, 3)
x_normalized_1d = torch.linspace(0, 2 * torch.pi, x_1d.size(0), device=x_1d.device).unsqueeze(1)
x_normalized = x_normalized_1d.repeat(1, dim)
# For NeRF: coordinates in [0, 1] per dimension, shape (N, 3)
x_3d = x_1d.unsqueeze(1).repeat(1, dim)

# Define the number of frequencies
L = 3

# Check if the number of frequencies satisfies the Nyquist-Criterion
# For multi-dimensional inputs, the constraint applies to each dimension independently
if L <= 1 + torch.log2(torch.tensor(len(x_3d) / 2)):
    print(f"Nyquist-Shannon sampling theorem is satisfied for the given number of frequencies {L}.")
else:
    print(f"Nyquist-Shannon sampling theorem is violated for the given number of frequencies {L}.")

# Transformer-style 3D embedding: use normalized coordinates [0, 2π]
max_positions = 1000
transformer_embedder = SinusoidalEmbedding(
    in_channels=dim,
    num_frequencies=L,
    embedding_type="transformer",
    max_positions=max_positions,
).to(device)
transformer_embedding = transformer_embedder(x_normalized).permute(1, 0)

# NeRF-style 3D embedding: use coordinates in [0, 1] per dimension
nerf_embedder = SinusoidalEmbedding(
    in_channels=dim, num_frequencies=L, embedding_type="nerf"
).to(device)
nerf_embedding = nerf_embedder(x_3d).permute(1, 0)