|
| 1 | +.. _sec-trajectories-sampledtraj: |
| 2 | + |
| 3 | +Sampled trajectories |
| 4 | +==================== |
| 5 | + |
| 6 | +In Python, the ``SampledTraj`` name is a convenience wrapper that mimics the |
| 7 | +C++ template deduction style. Depending on the provided samples, it builds and |
| 8 | +returns one of the following objects: |
| 9 | + |
| 10 | +- ``SampledTraj_Scalar`` |
| 11 | +- ``SampledTraj_Vector`` |
| 12 | +- ``SampledTraj_Matrix`` |
| 13 | + |
| 14 | +This keeps the user-facing syntax short and consistent with ``AnalyticTraj``. |
| 15 | + |
| 16 | + |
| 17 | +Loading a sampled trajectory from a ``.npz`` file |
| 18 | +------------------------------------------------- |
| 19 | + |
| 20 | +A sampled trajectory cannot be constructed directly from a file path. |
| 21 | +The recommended workflow is: |
| 22 | + |
| 23 | +1. load the ``.npz`` file with ``numpy.load``; |
| 24 | +2. extract the arrays containing the sampling times and sampled values; |
| 25 | +3. call ``SampledTraj(t, x)``. |
| 26 | + |
| 27 | +The wrapper then deduces the appropriate trajectory type from ``x``. |
| 28 | + |
| 29 | +.. tabs:: |
| 30 | + |
| 31 | + .. group-tab:: Python |
| 32 | + |
| 33 | + .. code-block:: python |
| 34 | +
|
| 35 | + import numpy as np |
| 36 | + from codac import * |
| 37 | +
|
| 38 | + data = np.load("traj_vec.npz") |
| 39 | + traj = SampledTraj(data["t"], data["x"]) |
| 40 | +
|
| 41 | + print(type(traj)) |
| 42 | +
|
| 43 | +In the above example, if ``x`` is a 2D array of shape ``(N,n)``, then |
| 44 | +``traj`` is a ``SampledTraj_Vector``. |
| 45 | + |
| 46 | +Another example: |
| 47 | + |
| 48 | +.. tabs:: |
| 49 | + |
| 50 | + .. group-tab:: Python |
| 51 | + |
| 52 | + .. code-block:: python |
| 53 | +
|
| 54 | + import numpy as np |
| 55 | + from codac import * |
| 56 | +
|
| 57 | + t = np.array([0.0, 0.5, 1.0, 1.5]) |
| 58 | + x = np.array([ |
| 59 | + [0.0, 1.0], |
| 60 | + [0.5, 1.5], |
| 61 | + [1.0, 2.0], |
| 62 | + [1.5, 2.5], |
| 63 | + ]) |
| 64 | +
|
| 65 | + traj = SampledTraj(t, x) |
| 66 | + print(traj) # outputs: SampledTraj. [0, 1.5]↦[[0, 1.5][1, 2.5]], 4 pts |
0 commit comments