BELFEM 0.9.4
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
fn_AR_eigen.hpp
Go to the documentation of this file.
1/*
2 * BELFEM -- The Berkeley Lab Finite Element Framework
3 * Copyright (c) 2026, The Regents of the University of California,
4 * through Lawrence Berkeley National Laboratory (subject to receipt of any required
5 * approvals from the U.S. Dept. of Energy). All rights reserved.
6 *
7 * Developers: Christian Messe, Gregory Giard
8 *
9 * See the top-level LICENSE file for the complete license and disclaimer.
10 */
11
12#ifndef BELFEM_FN_AR_EIGEN_HPP
13#define BELFEM_FN_AR_EIGEN_HPP
14#include "typedefs.hpp"
15#include "assert.hpp"
16#include "fn_is_finite.hpp"
17#include "cl_AR_Vector.hpp"
18#include "cl_AR_Matrix.hpp"
19
20namespace belfem
21{
22//------------------------------------------------------------------------------
23
24 inline int_t
25 eigen( const Matrix< real > & aMatrix,
26 Vector< real > & aValues,
27 const bool aAbortOnComplex = true )
28 {
29 // setup-grade call: the backend's own square check is off in release ( armadillo.hpp ),
30 // and a non-finite entry makes the decomposition fail silently on one backend
31 BELFEM_ERROR( aMatrix.n_cols() == aMatrix.n_rows(),
32 "eigen(): matrix must be square ( %lu x %lu )",
33 ( unsigned long ) aMatrix.n_rows(), ( unsigned long ) aMatrix.n_cols() );
34 BELFEM_ERROR( is_finite( aMatrix ),
35 "eigen(): matrix has non-finite entries" );
36
37 arma::cx_vec tValues ;
38
39 // arma::eig_gen reports failure ( non-finite input, no convergence ) by emptying
40 // tValues; indexing it afterwards would read out of bounds
41 BELFEM_ERROR( arma::eig_gen( tValues, aMatrix.matrix_data() ),
42 "eigen(): the eigenvalue decomposition failed ( non-finite entries or no convergence )" );
43
44 size_t tN = aMatrix.n_cols() ;
45 aValues.set_size( aMatrix.n_cols() );
46
47 int_t tNumComplex = 0 ;
48
49 for( size_t k=0; k<tN; ++k )
50 {
51 if( std::abs( std::imag( tValues( k ) ) ) > BELFEM_EPSILON )
52 {
53 BELFEM_ERROR( ! aAbortOnComplex,
54 "eigen(): eigenvalue %u of this matrix is complex ( %g %+g i ), and a real "
55 "Vector cannot hold it. Use eigen_sym() if the matrix is symmetric, or pass "
56 "aAbortOnComplex = false to receive a count and NaN entries instead.",
57 ( unsigned int ) k,
58 ( double ) std::real( tValues( k ) ),
59 ( double ) std::imag( tValues( k ) ) );
60
61 aValues( k ) = BELFEM_QUIET_NAN ;
62 ++tNumComplex ;
63 }
64 else
65 {
66 aValues( k ) = std::real( tValues( k ) );
67 }
68 }
69
70 return tNumComplex ;
71 }
72
73//------------------------------------------------------------------------------
74
75 inline void
76 eigen_sym( const Matrix< real > & aMatrix,
77 Vector< real > & aValues )
78 {
79 // setup-grade call: the backend's own square check is off in release ( armadillo.hpp ),
80 // and a non-finite entry makes the decomposition fail silently on one backend
81 BELFEM_ERROR( aMatrix.n_cols() == aMatrix.n_rows(),
82 "eigen_sym(): matrix must be square ( %lu x %lu )",
83 ( unsigned long ) aMatrix.n_rows(), ( unsigned long ) aMatrix.n_cols() );
84 BELFEM_ERROR( is_finite( aMatrix ),
85 "eigen_sym(): matrix has non-finite entries" );
86
87 // arma::eig_sym writes into an arma::Col, but Vector< T > is backed by an
88 // arma::Mat, so the eigenvalues are collected in a local and copied out
89 arma::Col< real > tValues ;
90
91 BELFEM_ERROR( arma::eig_sym( tValues, aMatrix.matrix_data() ),
92 "eigen_sym(): the symmetric eigenvalue decomposition did not converge" );
93
94 size_t tN = aMatrix.n_cols() ;
95 aValues.set_size( tN );
96
97 for( size_t k=0; k<tN; ++k )
98 {
99 aValues( k ) = tValues( k );
100 }
101 }
102
103//------------------------------------------------------------------------------
104}
105
106#endif //BELFEM_FN_AR_EIGEN_HPP
#define BELFEM_ERROR(aCheck,...)
Definition assert.hpp:264
MatrixType & matrix_data()
Definition cl_AR_Matrix.hpp:139
size_t n_rows() const
Definition cl_AR_Matrix.hpp:187
size_t n_cols() const
Definition cl_AR_Matrix.hpp:195
void set_size(const size_t aNumRows)
change the size of the vector
Definition cl_AR_Vector.hpp:313
Finiteness test for a whole matrix or vector, backend-free.
int_t eigen(const Matrix< real > &aMatrix, Vector< real > &aValues, const bool aAbortOnComplex=true)
Eigenvalues of a general (not necessarily symmetric) square matrix.
Definition fn_AR_eigen.hpp:25
void eigen_sym(const Matrix< real > &aMatrix, Vector< real > &aValues)
Eigenvalues of a symmetric matrix.
Definition fn_AR_eigen.hpp:76
USER GUIDES:
Definition cl_Capacitor.cpp:16
constexpr real BELFEM_EPSILON
Definition typedefs.hpp:89
bool is_finite(const Matrix< T > &aA)
true if every entry of aA is finite ( no NaN, no infinity ).
Definition fn_is_finite.hpp:37
int32_t int_t
Definition typedefs.hpp:51
#define BELFEM_QUIET_NAN
Definition typedefs.hpp:86