Txeo v0.1
A Developer-Friendly TensorFlow C++ Wrapper
Loading...
Searching...
No Matches
MatrixIO.h
Go to the documentation of this file.
1#ifndef MATRIXIO_H
2#define MATRIXIO_H
3#pragma once
4
5#include "txeo/Matrix.h"
6
7#include <cstddef>
8#include <filesystem>
9#include <type_traits>
10
11namespace txeo {
12
17class MatrixIO {
18 public:
25 explicit MatrixIO(const std::filesystem::path &path, char separator = ',')
26 : _path(std::move(path)), _separator(separator) {};
27
45 template <typename T>
46 txeo::Matrix<T> read_text_file(bool has_header = false) const;
47
63 template <typename T>
64 void write_text_file(const txeo::Matrix<T> &matrix) const;
65
82 template <typename T>
83 requires(std::is_floating_point_v<T>)
84 void write_text_file(const txeo::Matrix<T> &matrix, size_t precision) const;
85
102 template <typename T>
103 static txeo::Matrix<T> read_textfile(const std::filesystem::path &path, char separator = ',',
104 bool has_header = false) {
105 txeo::MatrixIO io{path, separator};
106 Matrix<T> resp{io.read_text_file<T>(has_header)};
107 return resp;
108 };
109
124 template <typename T>
125 static void write_textfile(const txeo::Matrix<T> &matrix, const std::filesystem::path &path,
126 char separator = ',') {
127 txeo::MatrixIO io{path, separator};
128 io.write_text_file(matrix);
129 }
130
147 template <typename T>
148 requires(std::is_floating_point_v<T>)
149 static void write_textfile(const txeo::Matrix<T> &matrix, size_t precision,
150 const std::filesystem::path &path, char separator = ',') {
151 txeo::MatrixIO io{path, separator};
152 io.write_text_file(matrix, precision);
153 };
154
155 private:
156 std::filesystem::path _path;
157 char _separator;
158};
159
164class MatrixIOError : public std::runtime_error {
165 public:
166 using std::runtime_error::runtime_error;
167};
168
169} // namespace txeo
170
171#endif
A class representing a matrix, derived from Tensor.
Definition Matrix.h:42
Exceptions concerning txeo::MatrixIO.
Definition MatrixIO.h:164
A class to read file data to matrix and to write file data to a matrix.
Definition MatrixIO.h:17
void write_text_file(const txeo::Matrix< T > &matrix, size_t precision) const
Writes a floating-point matrix with specified precision to a text file.
void write_text_file(const txeo::Matrix< T > &matrix) const
Writes a matrix to a text file.
MatrixIO(const std::filesystem::path &path, char separator=',')
Constructs MatrixIO object.
Definition MatrixIO.h:25
static void write_textfile(const txeo::Matrix< T > &matrix, const std::filesystem::path &path, char separator=',')
Writes a matrix to a text file.
Definition MatrixIO.h:125
txeo::Matrix< T > read_text_file(bool has_header=false) const
Returns a matrix with elements read from a text file.
static txeo::Matrix< T > read_textfile(const std::filesystem::path &path, char separator=',', bool has_header=false)
Returns a matrix with elements read from a text file.
Definition MatrixIO.h:103
static void write_textfile(const txeo::Matrix< T > &matrix, size_t precision, const std::filesystem::path &path, char separator=',')
Writes a floating-point matrix with specified precision to a text file.
Definition MatrixIO.h:149
Definition Matrix.h:11