PolyDiM
C++ library for POLYtopal DIscretization Methods
Loading...
Searching...
No Matches
SuiteSparse_Utilities.hpp
Go to the documentation of this file.
1// _LICENSE_HEADER_
2//
3// Copyright (C) 2019 - 2025.
4// Terms register on the GPL-3.0 license.
5//
6// This file can be redistributed and/or modified under the license terms.
7//
8// See top level LICENSE file for more details.
9//
10// This file can be used citing references in CITATION.cff file.
11
12#ifndef __SUITESPARSE_UTILITIES_H
13#define __SUITESPARSE_UTILITIES_H
14
15#include <iostream>
16#include <string>
17
18#include "Eigen/Eigen"
19#include "Eigen_Utilities.hpp"
20#include "Gedim_Macro.hpp"
21
22#if ENABLE_SUITESPARSE == 1
23#include <cholmod.h>
24#include <klu.h>
25#endif
26
27namespace Gedim
28{
29#if ENABLE_SUITESPARSE == 1
30struct SuiteSparse_Utilities
31{
32 template <typename Derived> static double condest(const Eigen::SparseMatrix<Derived> &sparseA)
33 {
34
35 const std::vector<Eigen::Triplet<Derived>> Aentries = Gedim::Eigen_Utilities::SparseMatrixToTriplets<Derived>(sparseA);
36 const unsigned int N = sparseA.rows();
37
39 cc = &CholCommon;
41
43 N,
44 Aentries.size(),
45 0, // stype: both upper and lower are stored
47 cc);
48 for (size_t i = 0; i < Aentries.size(); ++i)
49 {
50 reinterpret_cast<long *>(Gct->i)[i] = Aentries[i].row();
51 reinterpret_cast<long *>(Gct->j)[i] = Aentries[i].col();
52 reinterpret_cast<double *>(Gct->x)[i] = Aentries[i].value();
53 }
54 Gct->nnz = Aentries.size();
55
56 // convert triplet matrix to sparse
58
59 klu_l_common Common;
60 klu_l_defaults(&Common);
61 long *Ap = reinterpret_cast<long *>(G->p);
62 long *Ai = reinterpret_cast<long *>(G->i);
63 double *Ax = reinterpret_cast<double *>(G->x);
66
67 /* Computes a reasonably accurate estimate of the 1-norm condition number, using
68 * Hager’s method, as modified by Higham and Tisseur (same method as used in
69 * MATLAB’s condest */
70 int info = klu_l_condest(Ap, Ax, Symbolic, Numeric, &Common);
71
72 if (!info)
73 {
76 klu_l_free_numeric(&Numeric, &Common);
78 throw std::runtime_error("Suitesparse fails.");
79 }
80 else
81 {
82 double condest = Common.condest;
83
86 klu_l_free_numeric(&Numeric, &Common);
88 return condest;
89 }
90 }
91};
92#endif
93} // namespace Gedim
94#endif // __SUITESPARSE_UTILITIES_H
Eigen_Array()
Definition Eigen_Array.hpp:28
Definition Eigen_Array.cpp:22