BELFEM 0.9.4
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
fn_BZ_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_BZ_EIGEN_HPP
13#define BELFEM_FN_BZ_EIGEN_HPP
14#include <blaze/util/typetraits/IsComplex.h>
15
16#include "typedefs.hpp"
17#include "assert.hpp"
18#include "fn_is_finite.hpp"
19#include "cl_BZ_Vector.hpp"
20#include "cl_BZ_Matrix.hpp"
21
22namespace belfem
23{
24//------------------------------------------------------------------------------
25
26 inline int_t
27 eigen( const Matrix< real > & aMatrix,
28 Vector< real > & aValues,
29 const bool aAbortOnComplex = true )
30 {
31 // setup-grade call: the backend's own square check is off in release ( armadillo.hpp ),
32 // and a non-finite entry makes the decomposition fail silently on one backend
33 BELFEM_ERROR( aMatrix.n_cols() == aMatrix.n_rows(),
34 "eigen(): matrix must be square ( %lu x %lu )",
35 ( unsigned long ) aMatrix.n_rows(), ( unsigned long ) aMatrix.n_cols() );
36 BELFEM_ERROR( is_finite( aMatrix ),
37 "eigen(): matrix has non-finite entries" );
38
39 blaze::DynamicVector<blaze::complex<real>, blaze::columnVector > tValues ;
40 blaze::eigen( aMatrix.matrix_data(), tValues );
41
42 size_t tN = aMatrix.n_cols() ;
43 aValues.set_size( aMatrix.n_cols() );
44
45 int_t tNumComplex = 0 ;
46
47 for( size_t k=0; k<tN; ++k )
48 {
49 if( std::abs( std::imag( tValues[ k ] ) ) > BELFEM_EPSILON )
50 {
51 BELFEM_ERROR( ! aAbortOnComplex,
52 "eigen(): eigenvalue %u of this matrix is complex ( %g %+g i ), and a real "
53 "Vector cannot hold it. Use eigen_sym() if the matrix is symmetric, or pass "
54 "aAbortOnComplex = false to receive a count and NaN entries instead.",
55 ( unsigned int ) k,
56 ( double ) std::real( tValues[ k ] ),
57 ( double ) std::imag( tValues[ k ] ) );
58
59 aValues( k ) = BELFEM_QUIET_NAN ;
60 ++tNumComplex ;
61 }
62 else
63 {
64 aValues( k ) = std::real( tValues[ k ] );
65 }
66 }
67
68 return tNumComplex ;
69 }
70
71//------------------------------------------------------------------------------
72
73 inline void
74 eigen_sym( const Matrix< real > & aMatrix,
75 Vector< real > & aValues )
76 {
77 // setup-grade call: the backend's own square check is off in release ( armadillo.hpp ),
78 // and a non-finite entry makes the decomposition fail silently on one backend
79 BELFEM_ERROR( aMatrix.n_cols() == aMatrix.n_rows(),
80 "eigen_sym(): matrix must be square ( %lu x %lu )",
81 ( unsigned long ) aMatrix.n_rows(), ( unsigned long ) aMatrix.n_cols() );
82 BELFEM_ERROR( is_finite( aMatrix ),
83 "eigen_sym(): matrix has non-finite entries" );
84
85 aValues.set_size( aMatrix.n_cols() );
86
87 // blaze::syev overwrites the matrix it is given, so it gets a copy
88 Matrix< real > tWork( aMatrix.matrix_data() );
89
90 // 'N' = eigenvalues only. 'U' = read the upper triangle, matching what
91 // arma::eig_sym does, so both backends agree even when the caller passes a
92 // matrix that is not actually symmetric.
93 blaze::syev( tWork.matrix_data(), aValues.vector_data(), 'N', 'U' );
94 }
95
96//------------------------------------------------------------------------------
97}
98#endif //BELFEM_FN_BZ_EIGEN_HPP
#define BELFEM_ERROR(aCheck,...)
Definition assert.hpp:264
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