CLA3P v0.3.1
Compact Linear Algebra Parallel Portable Package
Loading...
Searching...
No Matches
ex07e_solving_sparse_linear_systems_auto.cpp
#include <iostream>
#include <cla3p/dense.hpp>
#include <cla3p/sparse.hpp>
#include <cla3p/linsol.hpp>
#include <cla3p/algebra.hpp>
/*-----------------------------------------------------*/
static cla3p::csr::RdMatrix DefaultSparseMatrix()
{
static cla3p::int_t rowptr[] = {0, 3, 5, 8, 11, 13};
static cla3p::int_t colidx[] = {0, 1, 3, 0, 1, 2, 3, 4, 0, 2, 3, 1, 4};
static cla3p::real_t values[] = {1, -1, -3, -2, 5, 4, 6, 4, -4, 2, 7, 8, -5};
return cla3p::csr::RdMatrix(5, 5, rowptr, colidx, values, false);
}
/*-----------------------------------------------------*/
static cla3p::csr::RdMatrix DefaultSymmetricSparseMatrix()
{
static cla3p::int_t rowptr[] = {0, 3, 5, 7, 8, 9};
static cla3p::int_t colidx[] = {0, 1, 3, 1, 4, 2, 3, 3, 4};
static cla3p::real_t values[] = {1, -2, -4, 5, 8, 4, 2, 7, -5};
return cla3p::csr::RdMatrix(5, 5, rowptr, colidx, values, false, cla3p::Property::SymmetricUpper());
}
/*-----------------------------------------------------*/
template <typename T_Rhs>
static void solve_linear_system(const cla3p::csr::RdMatrix& A, const T_Rhs& B)
{
/*
* Perform analysis & symbolic decomposition on A.
*/
autoSolver.analysis(A);
/*
* Decompose A into a product depending on property.
*/
autoSolver.decompose(A);
T_Rhs X;
/*
* Fill X with the solution (A^{-1} * B).
*/
autoSolver.solve(B, X);
std::string type_name = "Unknown";
if(std::is_same<T_Rhs, cla3p::dns::RdVector>::value) type_name = "Vector";
if(std::is_same<T_Rhs, cla3p::dns::RdMatrix>::value) type_name = "Matrix";
std::cout << " " << type_name << " rhs::";
std::cout << "Absolute error: " << (B - A * X).evaluate().normOne() << std::endl;
}
/*-----------------------------------------------------*/
int main()
{
/*
* Create a random general matrix.
*/
const cla3p::csr::RdMatrix Age = DefaultSparseMatrix();
/*
* Create a random symmetric matrix.
*/
const cla3p::csr::RdMatrix Asy = DefaultSymmetricSparseMatrix();
/*
* Create random right hand sides.
*/
std::cout << "General lhs\n";
solve_linear_system(Age, b);
solve_linear_system(Age, B);
std::cout << "\nSymmetric lhs\n";
solve_linear_system(Asy, b);
solve_linear_system(Asy, B);
return 0;
}
/*-----------------------------------------------------*/
The definite Cholesky (LL') linear solver for sparse matrices.
Definition pardiso_auto.hpp:35
void decompose(const T_Matrix &mat)
Performs matrix decomposition.
void analysis(const T_Matrix &mat)
Performs matrix analysis & symbolic decomposition.
void solve(const dns::XxMatrix< T_Scalar > &rhs, dns::XxMatrix< T_Scalar > &sol)
Performs matrix solution.
static Property SymmetricUpper()
Factory method for upper-triangular symmetric property.
static XxMatrix< real_t > random(int_t nr, int_t nc, const Property &pr=Property::General(), T_RScalar lo=T_RScalar(0), T_RScalar hi=T_RScalar(1))
static XxVector< real_t > random(int_t n, T_RScalar lo=T_RScalar(0), T_RScalar hi=T_RScalar(1))
double real_t
Double precision real.
Definition scalar.hpp:44
XxMatrix< real_t > RdMatrix
Double precision real matrix.
Definition dense.hpp:55
XxMatrix< int_t, real_t > RdMatrix
Double precision real CSR (Compressed Sparse Row) matrix.
Definition sparse.hpp:35
XxVector< real_t > RdVector
Double precision real vector.
Definition dense.hpp:31