MTL5 -- Matrix Template Library C++ API
mtl:: -- C++20 header-only linear algebra for mixed-precision algorithm design
Loading...
Searching...
No Matches
mtl::io Namespace Reference

Namespaces

namespace  detail

Classes

struct  spy_options
 Rendering options shared by the spy variants. More...

Functions

template<typename Value = double, typename Parameters = mat::parameters<>>
mat::compressed2D< Value, Parameters > mm_read (const std::string &filename)
 Read a Matrix Market file into a compressed2D, from either format.
template<typename Value = double>
mat::dense2D< Value > mm_read_dense (const std::string &filename)
 Read a Matrix Market file into a dense2D, from either format, with the same support and checks as mm_read.
template<typename Matrix>
void mm_write (const std::string &filename, const Matrix &A, const std::string &comment="")
 Write a matrix in Matrix Market array format (column-major), with the field taken from the value type: real, integer or complex.
template<typename Value, typename Parameters>
void mm_write_sparse (const std::string &filename, const mat::compressed2D< Value, Parameters > &A, const std::string &comment="")
 Write a sparse matrix in Matrix Market coordinate format, with the field taken from the value type: real, integer or complex.
void write_png_gray (const std::filesystem::path &path, const std::uint8_t *pixels, std::size_t w, std::size_t h)
 Write an 8-bit grayscale PNG. pixels is w*h bytes, row-major, top row first.
void write_png_rgb (const std::filesystem::path &path, const std::uint8_t *rgb, std::size_t w, std::size_t h)
 Write an 8-bit RGB PNG. rgb is w*h*3 bytes (R,G,B per pixel), row-major.
template<typename Value = double>
mat::dense2D< Value > read_dense (const std::string &filename, char delimiter=',')
 Read a dense matrix from a CSV or whitespace-delimited file.
template<typename Value = double>
mat::compressed2D< Value > read_sparse (const std::string &filename, std::size_t nrows, std::size_t ncols)
 Read a sparse matrix from a triplet file (row col value per line, 0-based).
template<typename M, typename F>
void for_each_nonzero (const M &A, F &&f)
 Generic fallback: scan (i,j) and report entries that differ from zero.
template<typename Value, typename Parameters, typename F>
void for_each_nonzero (const mat::compressed2D< Value, Parameters > &A, F &&f)
 CSR: iterate the three arrays directly (O(nnz)), skipping stored zeros.
template<typename Value, typename Parameters, typename F>
void for_each_nonzero (const mat::coordinate2D< Value, Parameters > &A, F &&f)
 COO: iterate the stored triplets (O(nnz)), skipping stored zeros.
template<typename Value, typename Parameters, typename F>
void for_each_nonzero (const mat::ell_matrix< Value, Parameters > &A, F &&f)
 ELLPACK: iterate the packed rows up to the invalid sentinel (O(nnz)), skipping stored zeros.
template<typename M>
void spy (const M &A, const std::filesystem::path &path, const spy_options &opt={})
 Binary non-zero pattern: black mark on white where the matrix has a non-zero (MATLAB spy).
template<typename M>
void spy_magnitude (const M &A, const std::filesystem::path &path, const spy_options &opt={})
 Non-zero pattern colored by per-cell max |a_ij| (analog of imagesc(abs(A))).
template<typename M>
void spy_density (const M &A, const std::filesystem::path &path, const spy_options &opt={})
 Non-zero pattern colored by per-cell non-zero count – reveals clustering once a large matrix is down-sampled.
template<typename Matrix>
void write_dense (const std::string &filename, const Matrix &A, char delimiter=',')
 Write a dense matrix to a CSV or whitespace-delimited file.
template<typename Value, typename Parameters>
void write_sparse (const std::string &filename, const mat::compressed2D< Value, Parameters > &A)
 Write a sparse matrix to a triplet file (0-based indices).

Function Documentation

◆ for_each_nonzero() [1/4]

template<typename M, typename F>
void mtl::io::for_each_nonzero ( const M & A,
F && f )

Generic fallback: scan (i,j) and report entries that differ from zero.

Used for dense2D and any Matrix type without a specialized overload.

◆ for_each_nonzero() [2/4]

template<typename Value, typename Parameters, typename F>
void mtl::io::for_each_nonzero ( const mat::compressed2D< Value, Parameters > & A,
F && f )

CSR: iterate the three arrays directly (O(nnz)), skipping stored zeros.

◆ for_each_nonzero() [3/4]

template<typename Value, typename Parameters, typename F>
void mtl::io::for_each_nonzero ( const mat::coordinate2D< Value, Parameters > & A,
F && f )

COO: iterate the stored triplets (O(nnz)), skipping stored zeros.

◆ for_each_nonzero() [4/4]

template<typename Value, typename Parameters, typename F>
void mtl::io::for_each_nonzero ( const mat::ell_matrix< Value, Parameters > & A,
F && f )

ELLPACK: iterate the packed rows up to the invalid sentinel (O(nnz)), skipping stored zeros.

◆ mm_read()

template<typename Value = double, typename Parameters = mat::parameters<>>
mat::compressed2D< Value, Parameters > mtl::io::mm_read ( const std::string & filename)

Read a Matrix Market file into a compressed2D, from either format.

Supported: coordinate and array; real, integer and complex fields (complex needs a std::complex Value); general, symmetric, skew-symmetric and hermitian (complex only), mirrored into the full matrix. Not supported: pattern. Malformed input throws std::runtime_error: a bad banner, a negative or overflowing size, a malformed or out-of-range entry, too few or too many entries (#843). Duplicate coordinates are summed, so a coordinate file's nnz may exceed m*n (#855).

◆ mm_read_dense()

template<typename Value = double>
mat::dense2D< Value > mtl::io::mm_read_dense ( const std::string & filename)

Read a Matrix Market file into a dense2D, from either format, with the same support and checks as mm_read.

Reads ".gz" transparently (zlib).

◆ mm_write()

template<typename Matrix>
void mtl::io::mm_write ( const std::string & filename,
const Matrix & A,
const std::string & comment = "" )

Write a matrix in Matrix Market array format (column-major), with the field taken from the value type: real, integer or complex.

◆ mm_write_sparse()

template<typename Value, typename Parameters>
void mtl::io::mm_write_sparse ( const std::string & filename,
const mat::compressed2D< Value, Parameters > & A,
const std::string & comment = "" )

Write a sparse matrix in Matrix Market coordinate format, with the field taken from the value type: real, integer or complex.

◆ read_dense()

template<typename Value = double>
mat::dense2D< Value > mtl::io::read_dense ( const std::string & filename,
char delimiter = ',' )

Read a dense matrix from a CSV or whitespace-delimited file.

One row per line, values separated by delimiter; blank lines are skipped. Dimensions are taken from the file: every row must have the same number of values. Throws std::runtime_error on an empty field, a value that does not parse, a row of a different length, or a file with no rows.

◆ read_sparse()

template<typename Value = double>
mat::compressed2D< Value > mtl::io::read_sparse ( const std::string & filename,
std::size_t nrows,
std::size_t ncols )

Read a sparse matrix from a triplet file (row col value per line, 0-based).

Requires explicit dimensions since the file may not contain all rows/cols. Blank lines are skipped. Throws std::runtime_error on a line that is not exactly "row col value", and std::out_of_range on an index outside the given dimensions. Duplicates are summed, as in coordinate2D::compress().

◆ spy()

template<typename M>
void mtl::io::spy ( const M & A,
const std::filesystem::path & path,
const spy_options & opt = {} )

Binary non-zero pattern: black mark on white where the matrix has a non-zero (MATLAB spy).

Grayscale PNG.

◆ spy_density()

template<typename M>
void mtl::io::spy_density ( const M & A,
const std::filesystem::path & path,
const spy_options & opt = {} )

Non-zero pattern colored by per-cell non-zero count – reveals clustering once a large matrix is down-sampled.

Linear by default, log10 with opt.log_scale. RGB PNG; empty cells are white.

◆ spy_magnitude()

template<typename M>
void mtl::io::spy_magnitude ( const M & A,
const std::filesystem::path & path,
const spy_options & opt = {} )

Non-zero pattern colored by per-cell max |a_ij| (analog of imagesc(abs(A))).

Linear by default, log10 with opt.log_scale. RGB PNG; empty cells are white.

◆ write_dense()

template<typename Matrix>
void mtl::io::write_dense ( const std::string & filename,
const Matrix & A,
char delimiter = ',' )

Write a dense matrix to a CSV or whitespace-delimited file.

One row per line, values separated by delimiter.

◆ write_png_gray()

void mtl::io::write_png_gray ( const std::filesystem::path & path,
const std::uint8_t * pixels,
std::size_t w,
std::size_t h )
inline

Write an 8-bit grayscale PNG. pixels is w*h bytes, row-major, top row first.

◆ write_png_rgb()

void mtl::io::write_png_rgb ( const std::filesystem::path & path,
const std::uint8_t * rgb,
std::size_t w,
std::size_t h )
inline

Write an 8-bit RGB PNG. rgb is w*h*3 bytes (R,G,B per pixel), row-major.

◆ write_sparse()

template<typename Value, typename Parameters>
void mtl::io::write_sparse ( const std::string & filename,
const mat::compressed2D< Value, Parameters > & A )

Write a sparse matrix to a triplet file (0-based indices).

Format: row col value, one triplet per line.