cyqlone develop
Fast, parallel and vectorized solver for linear systems with optimal control structure.
Loading...
Searching...
No Matches
matio.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// Functions for exporting and loading matrices and OCP data to and from .mat files
5/// @ingroup topic-util-matio
6
7#include <cyqlone/config.hpp>
8#include <cyqlone/ocp.hpp>
9#include <cyqlone/sparse.hpp>
10#include <batmat/matrix/view.hpp>
11#include <filesystem>
12#include <memory>
13#include <vector>
14
15// from <matio.h>
16struct _mat_t; // NOLINT(*-reserved-identifier)
17
18namespace cyqlone {
19
20/// @addtogroup topic-util-matio
21/// @{
22
23/// Incomplete matio struct type.
24using mat_t = ::_mat_t;
25/// Owning handle to a matio file. The file will be closed when the handle goes out of scope.
26using MatFilePtr = std::unique_ptr<mat_t, int (*)(mat_t *)>;
27
28enum class MatioOpenMode {
31};
32
33/// Opens a .mat file for reading or writing.
34/// @throw runtime_error if the file cannot be opened.
35MatFilePtr open_mat(const std::filesystem::path &filename,
37/// Create and open a new .mat file for writing.
38/// @throw runtime_error if the file cannot be opened.
39MatFilePtr create_mat(const std::filesystem::path &filename);
40/// Add a value to an open .mat file.
41void add_to_mat(mat_t *mat, const std::string &varname, float value);
42/// Add a value to an open .mat file.
43void add_to_mat(mat_t *mat, const std::string &varname, double value);
44/// Add a value to an open .mat file.
45void add_to_mat(mat_t *mat, const std::string &varname, short value);
46/// Add a value to an open .mat file.
47void add_to_mat(mat_t *mat, const std::string &varname, int value);
48/// Add a value to an open .mat file.
49void add_to_mat(mat_t *mat, const std::string &varname, long value);
50/// Add a value to an open .mat file.
51void add_to_mat(mat_t *mat, const std::string &varname, long long value);
52/// Add a value to an open .mat file.
53void add_to_mat(mat_t *mat, const std::string &varname, unsigned short value);
54/// Add a value to an open .mat file.
55void add_to_mat(mat_t *mat, const std::string &varname, unsigned int value);
56/// Add a value to an open .mat file.
57void add_to_mat(mat_t *mat, const std::string &varname, unsigned long value);
58/// Add a value to an open .mat file.
59void add_to_mat(mat_t *mat, const std::string &varname, unsigned long long value);
60/// Add a matrix to an open .mat file.
61void add_to_mat(mat_t *mat, const std::string &varname,
63/// Add a matrix to an open .mat file.
64void add_to_mat(mat_t *mat, const std::string &varname,
66/// Add a batch of matrices to an open .mat file.
67void add_to_mat(mat_t *mat, const std::string &varname,
69/// Add a batch of matrices to an open .mat file.
70void add_to_mat(mat_t *mat, const std::string &varname,
72/// Add a vector to an open .mat file.
73void add_to_mat(mat_t *mat, const std::string &varname, std::span<const float> data);
74/// Add a vector to an open .mat file.
75void add_to_mat(mat_t *mat, const std::string &varname, std::span<const double> data);
76/// Add a vector to an open .mat file.
77void add_to_mat(mat_t *mat, const std::string &varname, std::span<const unsigned short> data);
78/// Add a vector to an open .mat file.
79void add_to_mat(mat_t *mat, const std::string &varname, std::span<const unsigned int> data);
80/// Add a vector to an open .mat file.
81void add_to_mat(mat_t *mat, const std::string &varname, std::span<const unsigned long> data);
82/// Add a vector to an open .mat file.
83void add_to_mat(mat_t *mat, const std::string &varname, std::span<const unsigned long long> data);
84/// Add a vector to an open .mat file.
85void add_to_mat(mat_t *mat, const std::string &varname, std::span<const short> data);
86/// Add a vector to an open .mat file.
87void add_to_mat(mat_t *mat, const std::string &varname, std::span<const int> data);
88/// Add a vector to an open .mat file.
89void add_to_mat(mat_t *mat, const std::string &varname, std::span<const long> data);
90/// Add a vector to an open .mat file.
91void add_to_mat(mat_t *mat, const std::string &varname, std::span<const long long> data);
92
93/// Add a sparse matrix to an open .mat file.
94/// On the Python side, you can use the following code to load the sparse matrix:
95/// ```python
96/// from scipy.io import loadmat
97/// from scipy.sparse import coo_matrix
98/// mat = loadmat("file.mat")
99/// data = mat["varname"][0, 0]
100/// shape = data["num_rows"].item(), data["num_cols"].item()
101/// rows = data["row_indices"].flatten()
102/// cols = data["col_indices"].flatten()
103/// values = data["values"].flatten()
104/// matrix = coo_matrix((values, (rows, cols)), shape=shape)
105/// ```
106void add_to_mat(mat_t *mat, const std::string &varname, const SparseMatrix &matrix);
107/// Add the data from a LinearOCPStorage to an open .mat file.
108void add_to_mat(mat_t *mat, const std::string &varname, const LinearOCPStorage &ocp);
109/// Load a LinearOCPStorage from a .mat file.
110void read_from_mat(mat_t *mat, const std::string &varname, LinearOCPStorage &ocp);
111/// Load a vector from an open .mat file.
112void read_from_mat(mat_t *mat, const std::string &varname, std::vector<float> &data);
113/// Load a vector from an open .mat file.
114void read_from_mat(mat_t *mat, const std::string &varname, std::vector<double> &data);
115/// Load a vector from an open .mat file.
116void read_from_mat(mat_t *mat, const std::string &varname, std::span<float> data);
117/// Load a vector from an open .mat file.
118void read_from_mat(mat_t *mat, const std::string &varname, std::span<double> data);
119/// Dump the data from a LinearOCPStorage to a new .mat file.
120void ocp_dump_mat(const std::filesystem::path &filename, const LinearOCPStorage &ocp);
121
122/// @}
123
124} // namespace cyqlone
MatFilePtr create_mat(const std::filesystem::path &filename)
Create and open a new .mat file for writing.
Definition matio.cpp:97
MatioOpenMode
Definition matio.hpp:28
void add_to_mat(mat_t *mat, const std::string &varname, float value)
Add a value to an open .mat file.
Definition matio.cpp:122
void read_from_mat(mat_t *mat, const std::string &varname, LinearOCPStorage &ocp)
Load a LinearOCPStorage from a .mat file.
Definition matio.cpp:365
MatFilePtr open_mat(const std::filesystem::path &filename, MatioOpenMode mode=MatioOpenMode::Read)
Opens a .mat file for reading or writing.
Definition matio.cpp:89
::_mat_t mat_t
Incomplete matio struct type.
Definition matio.hpp:24
std::unique_ptr< mat_t, int(*)(mat_t *)> MatFilePtr
Owning handle to a matio file. The file will be closed when the handle goes out of scope.
Definition matio.hpp:26
void ocp_dump_mat(const std::filesystem::path &filename, const LinearOCPStorage &ocp)
Dump the data from a LinearOCPStorage to a new .mat file.
Definition matio.cpp:523
simd_view_types< std::remove_const_t< T >, Abi >::template matrix< T, Order > matrix
Data structure for optimal control problems.
Sparse matrix utilities.