File size: 735 Bytes
01e81ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import numpy as np


def true_derivative(states: np.ndarray) -> np.ndarray:
    angle = states[..., 0]
    momentum = states[..., 1]
    return np.stack([momentum, -np.sin(angle)], axis=-1).astype(np.float32)


def true_energy(states: np.ndarray) -> np.ndarray:
    angle = states[..., 0]
    momentum = states[..., 1]
    return 0.5 * momentum**2 + 1 - np.cos(angle)


def generate_states(samples: int, seed: int) -> tuple[np.ndarray, np.ndarray]:
    rng = np.random.default_rng(seed)
    states = np.column_stack(
        [
            rng.uniform(-np.pi, np.pi, samples),
            rng.uniform(-2.0, 2.0, samples),
        ]
    ).astype(np.float32)
    return states, true_derivative(states)