batmat 0.0.14
Batched linear algebra routines
Loading...
Searching...
No Matches
example.cpp
Basic example demonstrating how to use batmat to perform Cholesky factorization of a batch of matrices.
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <random>
using batmat::index_t;
using batmat::real_t;
namespace la = batmat::linalg;
int main() {
using batch_size = std::integral_constant<index_t, 4>;
constexpr auto storage_order = batmat::matrix::StorageOrder::ColMajor;
// Class representing a batch of four matrices.
// Allocate some batches of matrices (initialized to zero).
index_t n = 3, m = n + 5;
Mat C{{.rows = n, .cols = n}}, A{{.rows = n, .cols = m}};
// Fill A with random values.
std::mt19937 rng{12345};
std::uniform_real_distribution<real_t> uni{-1.0, 1.0};
std::ranges::generate(A, [&] { return uni(rng); });
// Compute C = AAᵀ to make it symmetric positive definite (lower triangular part only).
// Allocate L for the Cholesky factors.
Mat L{{.rows = n, .cols = n}, batmat::matrix::uninitialized};
// Compute the Cholesky factors L of C (lower triangular).
// Print the results.
for (index_t l = 0; l < C.depth(); ++l) {
guanaqo::print_python(std::cout << "C[" << l << "] =\n", C(l));
guanaqo::print_python(std::cout << "L[" << l << "] =\n", L(l));
}
// Compute LLᵀ (in-place).
// Check that LLᵀ == C.
int errors = 0;
const auto eps = std::numeric_limits<real_t>::epsilon();
for (index_t l = 0; l < C.depth(); ++l)
for (index_t c = 0; c < C.cols(); ++c)
for (index_t r = c; r < C.rows(); ++r)
errors += std::abs(C(l, r, c) - L(l, r, c)) < 10 * eps ? 0 : 1;
return errors;
}
int main()
Definition example.cpp:16
std::ostream & print_python(std::ostream &os, std::span< T, E > x, std::string_view end="\n", bool squeeze=true)
void syrk(Structured< VA, SA > A, Structured< VD, SD > D, Opts... opts)
D = A Aᵀ with D symmetric.
Definition gemm.hpp:310
void potrf(Structured< VC, SC > C, Structured< VD, SC > D, simdified_value_t< VC > regularization=0)
D = chol(C) with C symmetric, D triangular.
Definition potrf.hpp:98
void fill(simdified_value_t< VB > a, VB &&B)
B = a.
Definition copy.hpp:204
constexpr auto triu(M &&m)
Upper-triangular view.
constexpr auto tril(M &&m)
Lower-triangular view.
struct batmat::matrix::uninitialized_t uninitialized
Tag type to indicate that memory should not be initialized.
Class for a batch of matrices that owns its storage.
Class for a batch of matrices that owns its storage.
Definition matrix.hpp:52