OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
TestGSLEigen.cpp
Go to the documentation of this file.
1
41#include <gtest/gtest.h>
42#include <algorithm>
43#include <cmath>
44#include <vector>
46#include "Utilities/GSLEigen.h"
47#include "Utilities/GSLMatrix.h"
48
49class GSLEigenTest : public ::testing::Test {
50protected:
51 void SetUp() override {
52 // Test setup
53 }
54};
55
56TEST_F(GSLEigenTest, EigenNonsymm_Identity) {
60
61 // Identity matrix: eigenvalues should be 1, 1
63
64 int result = gsl_eigen_nonsymm(A, eval, w);
65 EXPECT_EQ(result, 0);
66
67 // Check eigenvalues (should be 1.0)
70
71 EXPECT_NEAR(GSL_REAL(e1), 1.0, 1e-6);
72 EXPECT_NEAR(GSL_IMAG(e1), 0.0, 1e-6);
73 EXPECT_NEAR(GSL_REAL(e2), 1.0, 1e-6);
74 EXPECT_NEAR(GSL_IMAG(e2), 0.0, 1e-6);
75
79}
80
81TEST_F(GSLEigenTest, EigenNonsymm_Diagonal) {
85
86 // Diagonal matrix: [2, 0, 0; 0, 3, 0; 0, 0, 4]
88 gsl_matrix_set(A, 0, 0, 2.0);
89 gsl_matrix_set(A, 1, 1, 3.0);
90 gsl_matrix_set(A, 2, 2, 4.0);
91
92 int result = gsl_eigen_nonsymm(A, eval, w);
93 EXPECT_EQ(result, 0);
94
95 // Eigenvalues should be 2, 3, 4 (in some order)
96 std::vector<double> eigenvalues(3);
97 for (size_t i = 0; i < 3; ++i) {
99 eigenvalues[i] = GSL_REAL(e);
100 }
101
102 std::sort(eigenvalues.begin(), eigenvalues.end());
103 EXPECT_NEAR(eigenvalues[0], 2.0, 1e-6);
104 EXPECT_NEAR(eigenvalues[1], 3.0, 1e-6);
105 EXPECT_NEAR(eigenvalues[2], 4.0, 1e-6);
106
110}
111
112TEST_F(GSLEigenTest, EigenNonsymmv_Identity) {
113 gsl_matrix* A = gsl_matrix_alloc(2, 2);
117
119
120 int result = gsl_eigen_nonsymmv(A, eval, evec, w);
121 EXPECT_EQ(result, 0);
122
123 // Check eigenvalues
126
127 EXPECT_NEAR(GSL_REAL(e1), 1.0, 1e-6);
128 EXPECT_NEAR(GSL_IMAG(e1), 0.0, 1e-6);
129 EXPECT_NEAR(GSL_REAL(e2), 1.0, 1e-6);
130 EXPECT_NEAR(GSL_IMAG(e2), 0.0, 1e-6);
131
136}
137
138TEST_F(GSLEigenTest, EigenNonsymmv_Diagonal) {
139 gsl_matrix* A = gsl_matrix_alloc(2, 2);
143
144 // Diagonal matrix: [2, 0; 0, 3]
146 gsl_matrix_set(A, 0, 0, 2.0);
147 gsl_matrix_set(A, 1, 1, 3.0);
148
149 int result = gsl_eigen_nonsymmv(A, eval, evec, w);
150 EXPECT_EQ(result, 0);
151
152 // Check eigenvalues
153 std::vector<double> eigenvalues(2);
154 for (size_t i = 0; i < 2; ++i) {
156 eigenvalues[i] = GSL_REAL(e);
157 }
158
159 std::sort(eigenvalues.begin(), eigenvalues.end());
160 EXPECT_NEAR(eigenvalues[0], 2.0, 1e-6);
161 EXPECT_NEAR(eigenvalues[1], 3.0, 1e-6);
162
167}
168
169TEST_F(GSLEigenTest, EigenNonsymm_ErrorHandling) {
170 gsl_matrix* A = gsl_matrix_alloc(2, 3); // Not square
173
174 int result = gsl_eigen_nonsymm(A, eval, w);
175 EXPECT_NE(result, 0); // Should return error
176
180}
181
182TEST_F(GSLEigenTest, WorkspaceAllocation) {
184 EXPECT_NE(w1, nullptr);
185 EXPECT_EQ(w1->n, 5);
187
189 EXPECT_NE(w2, nullptr);
190 EXPECT_EQ(w2->n, 5);
192}
#define GSL_REAL(z)
Definition GSLCompat.h:117
#define GSL_IMAG(z)
Definition GSLCompat.h:118
void gsl_eigen_nonsymmv_free(gsl_eigen_nonsymmv_workspace *w)
Free a workspace allocated by gsl_eigen_nonsymmv_alloc.
Definition GSLEigen.h:85
int gsl_eigen_nonsymm(gsl_matrix *A, gsl_vector_complex *eval, gsl_eigen_nonsymm_workspace *)
Compute eigenvalues of a real nonsymmetric matrix.
Definition GSLEigen.h:94
int gsl_eigen_nonsymmv(gsl_matrix *A, gsl_vector_complex *eval, gsl_matrix_complex *evec, gsl_eigen_nonsymmv_workspace *w)
Compute eigenvalues and a simplified set of eigenvectors.
Definition GSLEigen.h:194
gsl_eigen_nonsymm_workspace * gsl_eigen_nonsymm_alloc(size_t n)
Allocate a nonsymmetric eigenvalue workspace for matrices.
Definition GSLEigen.h:49
void gsl_eigen_nonsymm_free(gsl_eigen_nonsymm_workspace *w)
Free a workspace allocated by gsl_eigen_nonsymm_alloc.
Definition GSLEigen.h:69
gsl_eigen_nonsymmv_workspace * gsl_eigen_nonsymmv_alloc(size_t n)
Allocate a nonsymmetric eigenvalue/vector workspace for matrices.
Definition GSLEigen.h:75
gsl_matrix * gsl_matrix_alloc(size_t n1, size_t n2)
Allocate a zero-initialized real matrix of size .
Definition GSLMatrix.h:94
void gsl_matrix_set(gsl_matrix *m, size_t i, size_t j, double x)
Set .
Definition GSLMatrix.h:453
void gsl_matrix_free(gsl_matrix *m)
Free a matrix allocated by gsl_matrix_alloc.
Definition GSLMatrix.h:108
void gsl_matrix_complex_free(gsl_matrix_complex *m)
Free a matrix allocated by gsl_matrix_complex_alloc.
Definition GSLMatrix.h:136
gsl_complex gsl_vector_complex_get(const gsl_vector_complex *v, size_t i)
Get complex entry .
Definition GSLMatrix.h:566
gsl_vector_complex * gsl_vector_complex_alloc(size_t n)
Allocate a zero-initialized complex vector of length .
Definition GSLMatrix.h:171
void gsl_matrix_set_identity(gsl_matrix *m)
Set to identity on the main diagonal.
Definition GSLMatrix.h:486
void gsl_vector_complex_free(gsl_vector_complex *v)
Free a vector allocated by gsl_vector_complex_alloc.
Definition GSLMatrix.h:186
gsl_matrix_complex * gsl_matrix_complex_alloc(size_t n1, size_t n2)
Allocate a zero-initialized complex matrix of size .
Definition GSLMatrix.h:120
void gsl_matrix_set_zero(gsl_matrix *m)
Set all entries to zero.
Definition GSLMatrix.h:480
TEST_F(GSLEigenTest, EigenNonsymm_Identity)
void SetUp() override
Complex number stored as .
Definition GSLComplex.h:27
Workspace for nonsymmetric eigenvalue computation.
Definition GSLEigen.h:30
Workspace for nonsymmetric eigenvalues/eigenvectors.
Definition GSLEigen.h:38
Dense complex matrix in row-major storage.
Definition GSLMatrix.h:45
Dense real matrix in row-major storage.
Definition GSLMatrix.h:29
Dense complex vector with stride.
Definition GSLMatrix.h:76